The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
split into components
Kiran K0bfdde994f18
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/merge-partner-accounts-modal.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/merge-partner-accounts-modal.tsxindex e59c2f94e6..b2e4ed9ecc 100644--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/merge-partner-accounts-modal.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/settings/merge-partner-accounts-modal.tsx@@ -1,93 +1,22 @@"use client";-import { mergePartnerAccountsAction } from "@/lib/actions/partners/merge-partner-accounts";-import usePartnerProfile from "@/lib/swr/use-partner-profile";-import { Button, Modal } from "@dub/ui";-import { cn } from "@dub/utils";-import { OTPInput } from "input-otp";-import { useAction } from "next-safe-action/hooks";+import { MergePartnerAccountsFormProvider } from "@/ui/partners/merge-accounts/form-context";+import { SendVerificationCodeForm } from "@/ui/partners/merge-accounts/send-verification-code-form";+import { VerifyCodeForm } from "@/ui/partners/merge-accounts/verify-code-form";+import { Modal } from "@dub/ui";import {- createContext,Dispatch,SetStateAction,useCallback,- useContext,- useEffect,useMemo,useState,} from "react";-import { useForm } from "react-hook-form";-import { toast } from "sonner";interface MergePartnerAccountsModalProps {showMergePartnerAccountsModal: boolean;setShowMergePartnerAccountsModal: Dispatch<SetStateAction<boolean>>;}-interface EmailAccounts {- sourceEmail: string;- targetEmail: string;-}--interface Account {- name?: string;- email: string;- avatarUrl?: string;-}--export interface FormData {- sourceAccount: {- account: Account;- };- targetAccount: {- account: Account;- };-}--export interface FormContextType {- sourceAccount: Account;- targetAccount: Account;- setSourceAccount: Dispatch<SetStateAction<Account>>;- setTargetAccount: Dispatch<SetStateAction<Account>>;-}--const FormContext = createContext<FormContextType | undefined>(undefined);--export const useAccountForm = () => {- const context = useContext(FormContext);-- if (!context) {- throw new Error("useAccountForm must be used within a FormProvider");- }-- return context;-};--export const FormProvider: React.FC<{ children: React.ReactNode }> = ({- children,-}) => {- const [sourceAccount, setSourceAccount] = useState<Account>({- email: "",- });-- const [targetAccount, setTargetAccount] = useState<Account>({- email: "",- });-- return (- <FormContext.Provider- value={{- sourceAccount,- targetAccount,- setSourceAccount,- setTargetAccount,- }}- >- {children}- </FormContext.Provider>- );-};-function MergePartnerAccountsModal(props: MergePartnerAccountsModalProps) {const { showMergePartnerAccountsModal, setShowMergePartnerAccountsModal } =props;@@ -114,314 +43,22 @@ function MergePartnerAccountsModalInner({</div><div className="flex flex-col gap-2 bg-neutral-50 p-4 sm:p-6">- <FormProvider>- {step === 1 && <SendVerificationCode setStep={() => setStep(2)} />}- {step === 2 && <VerifyCode setStep={() => setStep(3)} />}- {step === 3 && <MergeAccounts />}- </FormProvider>- </div>- </div>- );-}--// Step 1: Send verification code to both accounts-function SendVerificationCode({ setStep }: { setStep: () => void }) {- const { partner } = usePartnerProfile();- const { setSourceAccount, setTargetAccount } = useAccountForm();-- const {- watch,- register,- handleSubmit,- formState: { isSubmitting },- } = useForm({- defaultValues: {- sourceEmail: partner?.email || "",- targetEmail: "",- },- });-- const [sourceEmail, targetEmail] = watch(["sourceEmail", "targetEmail"]);-- const { executeAsync, isPending } = useAction(mergePartnerAccountsAction, {- onSuccess: async () => {- setStep();- },- onError({ error }) {- toast.error(error.serverError);- },- });+ <MergePartnerAccountsFormProvider>+ {step === 1 && (+ <SendVerificationCodeForm setStep={() => setStep(2)} />+ )}- useEffect(() => {- setSourceAccount((prev) => ({ ...prev, email: sourceEmail }));- }, [sourceEmail, setSourceAccount]);+ {step === 2 && <VerifyCodeForm setStep={() => setStep(3)} />}- useEffect(() => {- setTargetAccount((prev) => ({ ...prev, email: sourceEmail }));- }, [targetEmail, setTargetAccount]);-- const onSubmit = async () => {- await executeAsync({- step: "send-tokens",- sourceEmail,- targetEmail,- });- };-- return (- <form onSubmit={handleSubmit(onSubmit)} className="space-y-6">- <p className="text-sm font-medium text-neutral-700">- Enter the email of the account you'd like to merge. We'll send a- verification code to both emails.- </p>-- <div className="flex flex-col gap-6">- <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">- <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">- Source account- </h3>- <div className="rounded-lg border border-solid border-neutral-200 bg-white p-3">- <label className="text-sm font-medium leading-5 text-neutral-900">- </label>- <div className="relative mt-2 rounded-md shadow-sm">- <input- type="email"- required- autoFocus- placeholder="Enter source account email"- className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"- {...register("sourceEmail")}- />- </div>- </div>- </div>-- <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">- <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">- Target account- </h3>- <div className="rounded-lg border border-solid border-neutral-200 bg-white p-3">- <label className="text-sm font-medium leading-5 text-neutral-900">- </label>- <div className="relative mt-2 rounded-md shadow-sm">- <input- type="email"- required- placeholder="Enter target account email"- className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"- {...register("targetEmail")}- />- </div>- </div>- </div>- </div>-- <div className="flex items-center justify-end gap-2">- <Button- variant="secondary"- text="Cancel"- className="h-8 w-fit px-3"- disabled={isPending || isSubmitting}- />- <Button- text="Send codes"- className="h-8 w-fit px-3"- type="submit"- disabled={!sourceEmail || !targetEmail}- loading={isPending || isSubmitting}- />- </div>- </form>- );-}--// Step 2: Verify code-function VerifyCode({ setStep }: { setStep: () => void }) {- const { sourceAccount, targetAccount } = useAccountForm();-- const {- watch,- handleSubmit,- setValue,- formState: { isSubmitting },- } = useForm({- defaultValues: {- sourceCode: "",- targetCode: "",- },- });-- const [sourceCode, targetCode] = watch(["sourceCode", "targetCode"]);-- const { executeAsync, isPending } = useAction(mergePartnerAccountsAction, {- onSuccess: async () => {- setStep();- },- onError({ error }) {- toast.error(error.serverError);- },- });-- const onSubmit = async () => {- await executeAsync({- step: "verify-tokens",- sourceEmail: sourceAccount.email,- targetEmail: targetAccount.email,- sourceCode,- targetCode,- });- };-- return (- <form onSubmit={handleSubmit(onSubmit)} className="space-y-6">- <p className="text-sm font-medium text-neutral-700">- We sent a 6-digit code to both emails. Didn’t receive them? Resend- codes.- </p>-- <div className="flex flex-col gap-6">- <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">- <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">- Source account- </h3>-- <div className="flex flex-col gap-4 rounded-lg border border-solid border-neutral-200 bg-white p-3">- <div>- <label className="text-sm font-medium leading-5 text-neutral-900">- </label>- <div className="relative mt-2 rounded-md shadow-sm">- <input- disabled- className="block w-full rounded-md border-neutral-300 text-neutral-900 disabled:bg-neutral-100 sm:text-sm"- defaultValue={sourceAccount.email}- />- </div>- </div>-- <div>- <label className="text-sm font-medium leading-5 text-neutral-900">- Verification code- </label>- <div className="relative mt-2">- <OTPInput- maxLength={6}- value={sourceCode}- onChange={(code) => {- setValue("sourceCode", code);- }}- render={({ slots }) => (- <div className="flex w-full items-center justify-between">- {slots.map(({ char, isActive, hasFakeCaret }, idx) => (- <div- key={idx}- className={cn(- "relative flex h-14 w-12 items-center justify-center text-xl",- "rounded-lg border border-neutral-200 bg-white ring-0 transition-all",- isActive &&- "z-10 border border-neutral-800 ring-2 ring-neutral-200",- // isInvalidCode && "border-red-500 ring-red-200",- )}- >- {char}- {hasFakeCaret && (- <div className="animate-caret-blink pointer-events-none absolute inset-0 flex items-center justify-center">- <div className="h-5 w-px bg-black" />- </div>- )}- </div>- ))}- </div>- )}- />- </div>- </div>- </div>- </div>-- <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">- <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">- Target account- </h3>-- <div className="flex flex-col gap-4 rounded-lg border border-solid border-neutral-200 bg-white p-3">- <div>- <label className="text-sm font-medium leading-5 text-neutral-900">- </label>- <div className="relative mt-2 rounded-md shadow-sm">- <input- disabled- className="block w-full rounded-md border-neutral-300 text-neutral-900 disabled:bg-neutral-100 sm:text-sm"- defaultValue={targetAccount.email}- />- </div>- </div>-- <div>- <label className="text-sm font-medium leading-5 text-neutral-900">- Verification code- </label>- <div className="relative mt-2">- <OTPInput- maxLength={6}- value={targetCode}- onChange={(code) => {- setValue("targetCode", code);- }}- render={({ slots }) => (- <div className="flex w-full items-center justify-between">- {slots.map(({ char, isActive, hasFakeCaret }, idx) => (- <div- key={idx}- className={cn(- "relative flex h-14 w-12 items-center justify-center text-xl",- "rounded-lg border border-neutral-200 bg-white ring-0 transition-all",- isActive &&- "z-10 border border-neutral-800 ring-2 ring-neutral-200",- // isInvalidCode && "border-red-500 ring-red-200",- )}- >- {char}- {hasFakeCaret && (- <div className="animate-caret-blink pointer-events-none absolute inset-0 flex items-center justify-center">- <div className="h-5 w-px bg-black" />- </div>- )}- </div>- ))}- </div>- )}- />- </div>- </div>- </div>- </div>+ {step === 3 && <MergeAccountsForm />}+ </MergePartnerAccountsFormProvider></div>-- <div className="flex items-center justify-end gap-2">- <Button- variant="secondary"- text="Cancel"- className="h-8 w-fit px-3"- disabled={isPending || isSubmitting}- />- <Button- text="Verify accounts"- className="h-8 w-fit px-3"- type="submit"- disabled={!sourceCode || !targetCode}- loading={isPending || isSubmitting}- />- </div>- </form>+ </div>);}// Step 3: Merge accounts-function MergeAccounts() {+function MergeAccountsForm() {return <div>MergeAccounts</div>;}diff --git a/apps/web/ui/partners/merge-accounts/form-context.tsx b/apps/web/ui/partners/merge-accounts/form-context.tsxnew file mode 100644index 0000000000..41cb3305a9--- /dev/null+++ b/apps/web/ui/partners/merge-accounts/form-context.tsx@@ -0,0 +1,59 @@+"use client";++import {+ createContext,+ Dispatch,+ SetStateAction,+ useContext,+ useState,+} from "react";++interface Account {+ name?: string;+ email: string;+ avatarUrl?: string;+}++export interface FormContextType {+ sourceAccount: Account;+ targetAccount: Account;+ setSourceAccount: Dispatch<SetStateAction<Account>>;+ setTargetAccount: Dispatch<SetStateAction<Account>>;+}++const FormContext = createContext<FormContextType | undefined>(undefined);++export const MergePartnerAccountsFormProvider: React.FC<{+ children: React.ReactNode;+}> = ({ children }) => {+ const [sourceAccount, setSourceAccount] = useState<Account>({+ email: "",+ });++ const [targetAccount, setTargetAccount] = useState<Account>({+ email: "",+ });++ return (+ <FormContext.Provider+ value={{+ sourceAccount,+ targetAccount,+ setSourceAccount,+ setTargetAccount,+ }}+ >+ {children}+ </FormContext.Provider>+ );+};++export const useMergePartnerAccountsForm = () => {+ const context = useContext(FormContext);++ if (!context) {+ throw new Error("Must be used within a Provider.");+ }++ return context;+};diff --git a/apps/web/ui/partners/merge-accounts/merge-account-form.tsx b/apps/web/ui/partners/merge-accounts/merge-account-form.tsxnew file mode 100644index 0000000000..e69de29bb2diff --git a/apps/web/ui/partners/merge-accounts/send-verification-code-form.tsx b/apps/web/ui/partners/merge-accounts/send-verification-code-form.tsxnew file mode 100644index 0000000000..7db9b3fa1f--- /dev/null+++ b/apps/web/ui/partners/merge-accounts/send-verification-code-form.tsx@@ -0,0 +1,122 @@+"use client";++import { mergePartnerAccountsAction } from "@/lib/actions/partners/merge-partner-accounts";+import usePartnerProfile from "@/lib/swr/use-partner-profile";+import { useMergePartnerAccountsForm } from "@/ui/partners/merge-accounts/form-context";+import { Button } from "@dub/ui";+import { useAction } from "next-safe-action/hooks";+import { useEffect } from "react";+import { useForm } from "react-hook-form";+import { toast } from "sonner";++export function SendVerificationCodeForm({ setStep }: { setStep: () => void }) {+ const { partner } = usePartnerProfile();+ const { setSourceAccount, setTargetAccount } = useMergePartnerAccountsForm();++ const {+ watch,+ register,+ handleSubmit,+ formState: { isSubmitting },+ } = useForm({+ defaultValues: {+ sourceEmail: partner?.email || "",+ targetEmail: "",+ },+ });++ const [sourceEmail, targetEmail] = watch(["sourceEmail", "targetEmail"]);++ const { executeAsync, isPending } = useAction(mergePartnerAccountsAction, {+ onSuccess: async () => {+ setStep();+ },+ onError({ error }) {+ toast.error(error.serverError);+ },+ });++ useEffect(() => {+ setSourceAccount((prev) => ({ ...prev, email: sourceEmail }));+ }, [sourceEmail, setSourceAccount]);++ useEffect(() => {+ setTargetAccount((prev) => ({ ...prev, email: sourceEmail }));+ }, [targetEmail, setTargetAccount]);++ const onSubmit = async () => {+ await executeAsync({+ step: "send-tokens",+ sourceEmail,+ targetEmail,+ });+ };++ return (+ <form onSubmit={handleSubmit(onSubmit)} className="space-y-6">+ <p className="text-sm font-medium text-neutral-700">+ Enter the email of the account you'd like to merge. We'll send a+ verification code to both emails.+ </p>++ <div className="flex flex-col gap-6">+ <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">+ <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">+ Source account+ </h3>+ <div className="rounded-lg border border-solid border-neutral-200 bg-white p-3">+ <label className="text-sm font-medium leading-5 text-neutral-900">+ </label>+ <div className="relative mt-2 rounded-md shadow-sm">+ <input+ type="email"+ required+ autoFocus+ placeholder="Enter source account email"+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"+ {...register("sourceEmail")}+ />+ </div>+ </div>+ </div>++ <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">+ <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">+ Target account+ </h3>+ <div className="rounded-lg border border-solid border-neutral-200 bg-white p-3">+ <label className="text-sm font-medium leading-5 text-neutral-900">+ </label>+ <div className="relative mt-2 rounded-md shadow-sm">+ <input+ type="email"+ required+ placeholder="Enter target account email"+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"+ {...register("targetEmail")}+ />+ </div>+ </div>+ </div>+ </div>++ <div className="flex items-center justify-end gap-2">+ <Button+ variant="secondary"+ text="Cancel"+ className="h-8 w-fit px-3"+ disabled={isPending || isSubmitting}+ />+ <Button+ text="Send codes"+ className="h-8 w-fit px-3"+ type="submit"+ disabled={!sourceEmail || !targetEmail}+ loading={isPending || isSubmitting}+ />+ </div>+ </form>+ );+}diff --git a/apps/web/ui/partners/merge-accounts/verify-code-form.tsx b/apps/web/ui/partners/merge-accounts/verify-code-form.tsxnew file mode 100644index 0000000000..9e7f04f59e--- /dev/null+++ b/apps/web/ui/partners/merge-accounts/verify-code-form.tsx@@ -0,0 +1,192 @@+"use client";++import { mergePartnerAccountsAction } from "@/lib/actions/partners/merge-partner-accounts";+import { useMergePartnerAccountsForm } from "@/ui/partners/merge-accounts/form-context";+import { Button } from "@dub/ui";+import { cn } from "@dub/utils";+import { OTPInput } from "input-otp";+import { useAction } from "next-safe-action/hooks";+import { useForm } from "react-hook-form";+import { toast } from "sonner";++export function VerifyCodeForm({ setStep }: { setStep: () => void }) {+ const { sourceAccount, targetAccount } = useMergePartnerAccountsForm();++ const {+ watch,+ handleSubmit,+ setValue,+ formState: { isSubmitting },+ } = useForm({+ defaultValues: {+ sourceCode: "",+ targetCode: "",+ },+ });++ const [sourceCode, targetCode] = watch(["sourceCode", "targetCode"]);++ const { executeAsync, isPending } = useAction(mergePartnerAccountsAction, {+ onSuccess: async () => {+ setStep();+ },+ onError({ error }) {+ toast.error(error.serverError);+ },+ });++ const onSubmit = async () => {+ await executeAsync({+ step: "verify-tokens",+ sourceEmail: sourceAccount.email,+ targetEmail: targetAccount.email,+ sourceCode,+ targetCode,+ });+ };++ return (+ <form onSubmit={handleSubmit(onSubmit)} className="space-y-6">+ <p className="text-sm font-medium text-neutral-700">+ We sent a 6-digit code to both emails. Didn’t receive them? Resend+ codes.+ </p>++ <div className="flex flex-col gap-6">+ <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">+ <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">+ Source account+ </h3>++ <div className="flex flex-col gap-4 rounded-lg border border-solid border-neutral-200 bg-white p-3">+ <div>+ <label className="text-sm font-medium leading-5 text-neutral-900">+ </label>+ <div className="relative mt-2 rounded-md shadow-sm">+ <input+ disabled+ className="block w-full rounded-md border-neutral-300 text-neutral-900 disabled:bg-neutral-100 sm:text-sm"+ defaultValue={sourceAccount.email}+ />+ </div>+ </div>++ <div>+ <label className="text-sm font-medium leading-5 text-neutral-900">+ Verification code+ </label>+ <div className="relative mt-2">+ <OTPInput+ maxLength={6}+ value={sourceCode}+ onChange={(code) => {+ setValue("sourceCode", code);+ }}+ render={({ slots }) => (+ <div className="flex w-full items-center justify-between">+ {slots.map(({ char, isActive, hasFakeCaret }, idx) => (+ <div+ key={idx}+ className={cn(+ "relative flex h-14 w-12 items-center justify-center text-xl",+ "rounded-lg border border-neutral-200 bg-white ring-0 transition-all",+ isActive &&+ "z-10 border border-neutral-800 ring-2 ring-neutral-200",+ // isInvalidCode && "border-red-500 ring-red-200",+ )}+ >+ {char}+ {hasFakeCaret && (+ <div className="animate-caret-blink pointer-events-none absolute inset-0 flex items-center justify-center">+ <div className="h-5 w-px bg-black" />+ </div>+ )}+ </div>+ ))}+ </div>+ )}+ />+ </div>+ </div>+ </div>+ </div>++ <div className="rounded-xl border border-solid border-neutral-200 bg-neutral-100 p-1 pt-0">+ <h3 className="px-1.5 py-2 text-xs font-medium leading-4 text-neutral-500">+ Target account+ </h3>++ <div className="flex flex-col gap-4 rounded-lg border border-solid border-neutral-200 bg-white p-3">+ <div>+ <label className="text-sm font-medium leading-5 text-neutral-900">+ </label>+ <div className="relative mt-2 rounded-md shadow-sm">+ <input+ disabled+ className="block w-full rounded-md border-neutral-300 text-neutral-900 disabled:bg-neutral-100 sm:text-sm"+ defaultValue={targetAccount.email}+ />+ </div>+ </div>++ <div>+ <label className="text-sm font-medium leading-5 text-neutral-900">+ Verification code+ </label>+ <div className="relative mt-2">+ <OTPInput+ maxLength={6}+ value={targetCode}+ onChange={(code) => {+ setValue("targetCode", code);+ }}+ render={({ slots }) => (+ <div className="flex w-full items-center justify-between">+ {slots.map(({ char, isActive, hasFakeCaret }, idx) => (+ <div+ key={idx}+ className={cn(+ "relative flex h-14 w-12 items-center justify-center text-xl",+ "rounded-lg border border-neutral-200 bg-white ring-0 transition-all",+ isActive &&+ "z-10 border border-neutral-800 ring-2 ring-neutral-200",+ // isInvalidCode && "border-red-500 ring-red-200",+ )}+ >+ {char}+ {hasFakeCaret && (+ <div className="animate-caret-blink pointer-events-none absolute inset-0 flex items-center justify-center">+ <div className="h-5 w-px bg-black" />+ </div>+ )}+ </div>+ ))}+ </div>+ )}+ />+ </div>+ </div>+ </div>+ </div>+ </div>++ <div className="flex items-center justify-end gap-2">+ <Button+ variant="secondary"+ text="Cancel"+ className="h-8 w-fit px-3"+ disabled={isPending || isSubmitting}+ />+ <Button+ text="Verify accounts"+ className="h-8 w-fit px-3"+ type="submit"+ disabled={sourceCode.length !== 6 || targetCode.length !== 6}+ loading={isPending || isSubmitting}+ />+ </div>+ </form>+ );+}