Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update verified states in form

Tim Wilson784698566cec
diff --git a/apps/web/ui/partners/online-presence-form.tsx b/apps/web/ui/partners/online-presence-form.tsx
index 8928206c28..cb84b23f60 100644
--- a/apps/web/ui/partners/online-presence-form.tsx
+++ b/apps/web/ui/partners/online-presence-form.tsx
@@ -11,6 +11,8 @@ import usePartnerProfile from "@/lib/swr/use-partner-profile";
import { parseUrlSchemaAllowEmpty } from "@/lib/zod/schemas/utils";
import { DomainVerificationModal } from "@/ui/modals/domain-verification-modal";
import {
+ AnimatedSizeContainer,
+ BadgeCheck2Fill,
Button,
CircleCheckFill,
Globe,
@@ -18,15 +20,21 @@ import {
Instagram,
LinkedIn,
TikTok,
+ Trash,
Twitter,
YouTube,
} from "@dub/ui";
-import { getPrettyUrl } from "@dub/utils";
+import { getPrettyUrl, nFormatter } from "@dub/utils";
import { cn } from "@dub/utils/src/functions";
import { useAction } from "next-safe-action/hooks";
import { useRouter } from "next/navigation";
-import { forwardRef, ReactNode, useCallback, useState } from "react";
-import { FormProvider, useForm, useFormContext } from "react-hook-form";
+import { forwardRef, ReactNode, useCallback, useMemo, useState } from "react";
+import {
+ FormProvider,
+ useForm,
+ useFormContext,
+ useWatch,
+} from "react-hook-form";
import { toast } from "sonner";
import { mutate } from "swr";
import { z } from "zod";
@@ -173,8 +181,37 @@ export const OnlinePresenceForm = forwardRef<
>
<div className={cn("flex w-full flex-col gap-6 text-left")}>
<FormRow
- variant={variant}
label="Website"
+ property="website"
+ verifiedAtField="websiteVerifiedAt"
+ icon={Globe}
+ onVerifyClick={async () => {
+ try {
+ const result =
+ await updateOnlinePresenceAction(getValues());
+
+ if (
+ !result?.data?.website ||
+ !result?.data?.websiteTxtRecord
+ ) {
+ throw new Error(
+ "Missing website or TXT record in update result",
+ );
+ }
+
+ setDomainVerificationData({
+ domain: new URL(result.data.website).hostname,
+ txtRecord: result.data.websiteTxtRecord,
+ });
+
+ mutate("/api/partner-profile");
+ } catch (e) {
+ toast.error("Failed to start website verification");
+ console.error("Failed to start website verification", e);
+ }
+
+ return false;
+ }}
input={
<input
type="text"
@@ -189,48 +226,17 @@ export const OnlinePresenceForm = forwardRef<
{...register("website")}
/>
}
- button={
- <VerifyButton
- property="website"
- verifiedAtField="websiteVerifiedAt"
- icon={Globe}
- onClick={async () => {
- try {
- const result =
- await updateOnlinePresenceAction(getValues());
-
- if (
- !result?.data?.website ||
- !result?.data?.websiteTxtRecord
- ) {
- throw new Error(
- "Missing website or TXT record in update result",
- );
- }
-
- setDomainVerificationData({
- domain: new URL(result.data.website).hostname,
- txtRecord: result.data.websiteTxtRecord,
- });
-
- mutate("/api/partner-profile");
- } catch (e) {
- toast.error("Failed to start website verification");
- console.error(
- "Failed to start website verification",
- e,
- );
- }
-
- return false;
- }}
- />
- }
/>
<FormRow
- variant={variant}
label="YouTube"
+ property="youtube"
+ prefix="@"
+ verifiedAtField="youtubeVerifiedAt"
+ icon={YouTube}
+ onVerifyClick={() =>
+ startVerification("youtube", getValues("youtube"))
+ }
input={
<div className="flex rounded-md">
<span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm">
@@ -253,21 +259,17 @@ export const OnlinePresenceForm = forwardRef<
/>
</div>
}
- button={
- <VerifyButton
- property="youtube"
- verifiedAtField="youtubeVerifiedAt"
- icon={YouTube}
- onClick={() =>
- startVerification("youtube", getValues("youtube"))
- }
- />
- }
/>
<FormRow
- variant={variant}
label="X/Twitter"
+ property="twitter"
+ prefix="@"
+ verifiedAtField="twitterVerifiedAt"
+ icon={Twitter}
+ onVerifyClick={() =>
+ startVerification("twitter", getValues("twitter"))
+ }
input={
<div className="flex rounded-md">
<span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm">
@@ -287,21 +289,18 @@ export const OnlinePresenceForm = forwardRef<
/>
</div>
}
- button={
- <VerifyButton
- property="twitter"
- verifiedAtField="twitterVerifiedAt"
- icon={Twitter}
- onClick={() =>
- startVerification("twitter", getValues("twitter"))
- }
- />
- }
/>
<FormRow
- variant={variant}
label="LinkedIn"
+ property="linkedin"
+ prefix="in/"
+ verifiedAtField="linkedinVerifiedAt"
+ icon={LinkedIn}
+ onVerifyClick={() =>
+ startVerification("linkedin", getValues("linkedin"))
+ }
+ verifyDisabledTooltip="LinkedIn verification is coming soon."
input={
<div className="flex rounded-md">
<span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm">
@@ -321,22 +320,18 @@ export const OnlinePresenceForm = forwardRef<
/>
</div>
}
- button={
- <VerifyButton
- property="linkedin"
- verifiedAtField="linkedinVerifiedAt"
- icon={LinkedIn}
- onClick={() =>
- startVerification("linkedin", getValues("linkedin"))
- }
- disabledTooltip="LinkedIn verification is coming soon."
- />
- }
/>
<FormRow
- variant={variant}
label="Instagram"
+ property="instagram"
+ prefix="@"
+ verifiedAtField="instagramVerifiedAt"
+ icon={Instagram}
+ onVerifyClick={() =>
+ startVerification("instagram", getValues("instagram"))
+ }
+ verifyDisabledTooltip="Instagram verification is coming soon."
input={
<div className="flex rounded-md">
<span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm">
@@ -356,22 +351,17 @@ export const OnlinePresenceForm = forwardRef<
/>
</div>
}
- button={
- <VerifyButton
- property="instagram"
- verifiedAtField="instagramVerifiedAt"
- icon={Instagram}
- onClick={() =>
- startVerification("instagram", getValues("instagram"))
- }
- disabledTooltip="Instagram verification is coming soon."
- />
- }
/>
<FormRow
- variant={variant}
label="TikTok"
+ property="tiktok"
+ prefix="@"
+ verifiedAtField="tiktokVerifiedAt"
+ icon={TikTok}
+ onVerifyClick={() =>
+ startVerification("tiktok", getValues("tiktok"))
+ }
input={
<div className="flex rounded-md">
<span className="inline-flex items-center rounded-l-md border border-r-0 border-neutral-300 bg-neutral-50 px-3 text-neutral-500 sm:text-sm">
@@ -394,16 +384,6 @@ export const OnlinePresenceForm = forwardRef<
/>
</div>
}
- button={
- <VerifyButton
- property="tiktok"
- verifiedAtField="tiktokVerifiedAt"
- icon={TikTok}
- onClick={() =>
- startVerification("tiktok", getValues("tiktok"))
- }
- />
- }
/>
</div>
@@ -454,18 +434,12 @@ function useOAuthVerification(source: "onboarding" | "settings") {
);
}
-function VerifyButton({
+function useVerifiedState({
property,
verifiedAtField,
- icon: Icon,
- onClick,
- disabledTooltip,
}: {
property: keyof OnlinePresenceFormData;
verifiedAtField: string;
- icon: Icon;
- onClick: () => Promise<boolean>;
- disabledTooltip?: string;
}) {
const { partner: partnerProfile } = usePartnerProfile();
@@ -483,6 +457,33 @@ function VerifyButton({
const isVerified = noChange && Boolean(partnerProfile?.[verifiedAtField]);
+ return {
+ isVerified,
+ loading,
+ };
+}
+
+function VerifyButton({
+ property,
+ verifiedAtField,
+ icon: Icon,
+ onClick,
+ disabledTooltip,
+}: {
+ property: keyof OnlinePresenceFormData;
+ verifiedAtField: string;
+ icon: Icon;
+ onClick: () => Promise<boolean>;
+ disabledTooltip?: string;
+}) {
+ const { control, getFieldState } = useFormContext<OnlinePresenceFormData>();
+ const value = useWatch({ control, name: property });
+
+ const { isVerified, loading } = useVerifiedState({
+ property,
+ verifiedAtField,
+ });
+
const [isSaving, setIsSaving] = useState(false);
return (
@@ -516,35 +517,109 @@ function VerifyButton({
}
function FormRow({
- variant,
label,
input,
- button,
+ property,
+ prefix,
+ verifiedAtField,
+ icon: Icon,
+ onVerifyClick,
+ verifyDisabledTooltip,
}: {
- variant: "onboarding" | "settings";
label: string;
input: ReactNode;
- button: ReactNode;
+
+ property: keyof OnlinePresenceFormData;
+ prefix?: string;
+ verifiedAtField: string;
+ icon: Icon;
+ onVerifyClick: () => Promise<boolean>;
+ verifyDisabledTooltip?: string;
}) {
+ const { partner } = usePartnerProfile();
+ const { control, setValue } = useFormContext<OnlinePresenceFormData>();
+ const value = useWatch({ control, name: property });
+
+ const { isVerified } = useVerifiedState({ property, verifiedAtField });
+
+ const info = useMemo(() => {
+ if (partner && property === "youtube" && isVerified) {
+ return [
+ partner.youtubeSubscriberCount > 0
+ ? `${nFormatter(partner.youtubeSubscriberCount)} subscribers`
+ : null,
+ partner.youtubeViewCount > 0
+ ? `${nFormatter(partner.youtubeViewCount)} views`
+ : null,
+ ].filter(Boolean);
+ }
+ return null;
+ }, [partner, property, isVerified]);
+
return (
- <div>
- <label className={cn("flex flex-col gap-1.5")}>
- <span className="text-content-emphasis text-sm font-medium">
- {label}
- </span>
- <div className={cn("relative")}>
- {input}
- {button}
+ <div className="-m-0.5">
+ <AnimatedSizeContainer
+ height
+ initial={false}
+ transition={{ duration: 0.2, ease: "easeInOut" }}
+ >
+ <div className="p-0.5">
+ {isVerified ? (
+ <div className="flex flex-col gap-1.5">
+ <span className="text-content-emphasis text-sm font-medium">
+ {label}
+ </span>
+ <div className="border-subtle flex items-center justify-between rounded-lg border bg-white p-3">
+ <div className="flex items-center gap-3">
+ <div className="border-subtle flex size-8 items-center justify-center rounded-full border">
+ <Icon className="size-4" />
+ </div>
+ <div className="flex flex-col text-xs">
+ <div className="flex items-center gap-1">
+ <span className="text-content-emphasis block font-semibold">
+ {prefix}
+ {value}
+ </span>
+ <BadgeCheck2Fill className="size-3.5 text-green-600" />
+ </div>
+ {info && info.length > 0 && (
+ <div className="text-content-subtle font-medium">
+ {info.join(" • ")}
+ </div>
+ )}
+ </div>
+ </div>
+ <div className="flex items-center gap-2">
+ <Button
+ variant="outline"
+ icon={<Trash className="size-4" />}
+ className="text-content-subtle hover:text-content-default size-8 p-0"
+ onClick={() => {
+ setValue(property, "", { shouldDirty: true });
+ }}
+ />
+ </div>
+ </div>
+ </div>
+ ) : (
+ <label className={cn("flex flex-col gap-1.5")}>
+ <span className="text-content-emphasis text-sm font-medium">
+ {label}
+ </span>
+ <div className={cn("relative")}>
+ {input}
+ <VerifyButton
+ property={property}
+ verifiedAtField={verifiedAtField}
+ icon={Icon}
+ onClick={onVerifyClick}
+ disabledTooltip={verifyDisabledTooltip}
+ />
+ </div>
+ </label>
+ )}
</div>
- </label>
+ </AnimatedSizeContainer>
</div>
);
}
-
-const onPasteSocial = (e: React.ClipboardEvent<HTMLInputElement>) => {
- e.preventDefault();
-
- // Extract the final portion of any URL
- const text = e.clipboardData.getData("text/plain");
- e.currentTarget.value = (text.split("/").at(-1) ?? text).replace(/^@/, "");
-};