|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import useLink from "@/lib/swr/use-link"; |
| 4 | +import useWorkspace from "@/lib/swr/use-workspace"; |
| 5 | +import { ExpandedLinkProps } from "@/lib/types"; |
| 6 | +import { LinkAnalyticsBadge } from "@/ui/links/link-analytics-badge"; |
| 7 | +import { LinkBuilderDestinationUrlInput } from "@/ui/links/link-builder/controls/link-builder-destination-url-input"; |
| 8 | +import { LinkBuilderShortLinkInput } from "@/ui/links/link-builder/controls/link-builder-short-link-input"; |
| 9 | +import { LinkCommentsInput } from "@/ui/links/link-builder/controls/link-comments-input"; |
| 10 | +import { ConversionTrackingToggle } from "@/ui/links/link-builder/conversion-tracking-toggle"; |
| 11 | +import { |
| 12 | + DraftControls, |
| 13 | + DraftControlsHandle, |
| 14 | +} from "@/ui/links/link-builder/draft-controls"; |
| 15 | +import { LinkActionBar } from "@/ui/links/link-builder/link-action-bar"; |
| 16 | +import { LinkBuilderHeader } from "@/ui/links/link-builder/link-builder-header"; |
| 17 | +import { |
| 18 | + LinkBuilderProvider, |
| 19 | + LinkFormData, |
| 20 | +} from "@/ui/links/link-builder/link-builder-provider"; |
| 21 | +import { LinkFeatureButtons } from "@/ui/links/link-builder/link-feature-buttons"; |
| 22 | +import { LinkPreview } from "@/ui/links/link-builder/link-preview"; |
| 23 | +import { OptionsList } from "@/ui/links/link-builder/options-list"; |
| 24 | +import { QRCodePreview } from "@/ui/links/link-builder/qr-code-preview"; |
| 25 | +import { TagSelect } from "@/ui/links/link-builder/tag-select"; |
| 26 | +import { useLinkBuilderSubmit } from "@/ui/links/link-builder/use-link-builder-submit"; |
| 27 | +import { useMetatags } from "@/ui/links/link-builder/use-metatags"; |
| 28 | +import { useKeyboardShortcut, useMediaQuery } from "@dub/ui"; |
| 29 | +import { cn } from "@dub/utils"; |
| 30 | +import { notFound, useParams, useRouter } from "next/navigation"; |
| 31 | +import { useEffect, useRef, useState } from "react"; |
| 32 | +import { useFormContext, useFormState } from "react-hook-form"; |
| 33 | + |
| 34 | +export function LinkPageClient() { |
| 35 | + const params = useParams<{ link: string | string[] }>(); |
| 36 | + |
| 37 | + const linkParts = Array.isArray(params.link) ? params.link : null; |
| 38 | + if (!linkParts) notFound(); |
| 39 | + |
| 40 | + const domain = linkParts[0]; |
| 41 | + const slug = linkParts.length > 1 ? linkParts.slice(1).join("/") : "_root"; |
| 42 | + |
| 43 | + const router = useRouter(); |
| 44 | + const workspace = useWorkspace(); |
| 45 | + |
| 46 | + const { link } = useLink( |
| 47 | + { |
| 48 | + domain, |
| 49 | + slug, |
| 50 | + }, |
| 51 | + { |
| 52 | + keepPreviousData: true, |
| 53 | + // doing onErrorRetry to avoid race condiition for when a link's domain / key is updated |
| 54 | + onErrorRetry: (error, _key, _config, revalidate, { retryCount }) => { |
| 55 | + if (error.status === 401 || error.status === 404) { |
| 56 | + if (retryCount > 1) { |
| 57 | + router.push(`/${workspace.slug}/links`); |
| 58 | + return; |
| 59 | + } |
| 60 | + } |
| 61 | + // Default retry behavior for other errors |
| 62 | + setTimeout(() => revalidate({ retryCount }), 5000); |
| 63 | + }, |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + return link ? ( |
| 68 | + <LinkBuilderProvider props={link} workspace={workspace}> |
| 69 | + <LinkBuilder link={link} /> |
| 70 | + </LinkBuilderProvider> |
| 71 | + ) : ( |
| 72 | + <LoadingSkeleton /> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +function LinkBuilder({ link }: { link: ExpandedLinkProps }) { |
| 77 | + const router = useRouter(); |
| 78 | + const workspace = useWorkspace(); |
| 79 | + |
| 80 | + const { isDesktop } = useMediaQuery(); |
| 81 | + |
| 82 | + const { control, handleSubmit, reset, getValues } = |
| 83 | + useFormContext<LinkFormData>(); |
| 84 | + const { isSubmitSuccessful, isDirty } = useFormState({ control }); |
| 85 | + |
| 86 | + const draftControlsRef = useRef<DraftControlsHandle>(null); |
| 87 | + |
| 88 | + const onSubmitSuccess = (data: LinkFormData) => { |
| 89 | + draftControlsRef.current?.onSubmitSuccessful(); |
| 90 | + |
| 91 | + router.replace(`/${workspace.slug}/links/${data.domain}/${data.key}`, { |
| 92 | + scroll: false, |
| 93 | + }); |
| 94 | + }; |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + if (isSubmitSuccessful) |
| 98 | + reset(getValues(), { keepValues: true, keepDirty: false }); |
| 99 | + }, [isSubmitSuccessful, reset, getValues]); |
| 100 | + |
| 101 | + const onSubmit = useLinkBuilderSubmit({ |
| 102 | + onSuccess: onSubmitSuccess, |
| 103 | + }); |
| 104 | + |
| 105 | + useMetatags(); |
| 106 | + |
| 107 | + // Go back to `/links` when ESC is pressed |
| 108 | + useKeyboardShortcut("Escape", () => router.push(`/${workspace.slug}/links`), { |
| 109 | + enabled: !isDirty, |
| 110 | + }); |
| 111 | + |
| 112 | + const [isChangingLink, setIsChangingLink] = useState(false); |
| 113 | + |
| 114 | + return ( |
| 115 | + <div className="flex min-h-[calc(100vh-8px)] flex-col rounded-t-[inherit] bg-white"> |
| 116 | + <div className="py-2 pl-4 pr-5"> |
| 117 | + <LinkBuilderHeader |
| 118 | + onSelectLink={(selectedLink) => { |
| 119 | + if (selectedLink.id === link.id) return; |
| 120 | + |
| 121 | + if (isDirty) { |
| 122 | + if ( |
| 123 | + !confirm( |
| 124 | + "You have unsaved changes. Are you sure you want to continue?", |
| 125 | + ) |
| 126 | + ) |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + setIsChangingLink(true); |
| 131 | + router.push( |
| 132 | + `/${workspace.slug}/links/${selectedLink.domain}/${selectedLink.key}`, |
| 133 | + ); |
| 134 | + }} |
| 135 | + className="p-0" |
| 136 | + foldersEnabled={!!workspace.flags?.linkFolders} |
| 137 | + > |
| 138 | + <div |
| 139 | + className={cn( |
| 140 | + "flex min-w-0 items-center gap-2 transition-opacity", |
| 141 | + isChangingLink && "opacity-50", |
| 142 | + )} |
| 143 | + > |
| 144 | + <DraftControls |
| 145 | + ref={draftControlsRef} |
| 146 | + props={link} |
| 147 | + workspaceId={workspace.id!} |
| 148 | + /> |
| 149 | + <div className="shrink-0"> |
| 150 | + <LinkAnalyticsBadge link={link} /> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </LinkBuilderHeader> |
| 154 | + </div> |
| 155 | + <form |
| 156 | + className={cn( |
| 157 | + "grid grow grid-cols-1 transition-opacity lg:grid-cols-[minmax(0,1fr)_300px]", |
| 158 | + "divide-neutral-200 border-t border-neutral-200 lg:divide-x lg:divide-y", |
| 159 | + isChangingLink && "opacity-50", |
| 160 | + )} |
| 161 | + onSubmit={handleSubmit(onSubmit)} |
| 162 | + > |
| 163 | + <div className="relative flex min-h-full flex-col px-4 md:px-6"> |
| 164 | + <div className="relative mx-auto flex w-full max-w-xl flex-col gap-7 pb-4 pt-10 lg:pb-10"> |
| 165 | + <LinkBuilderDestinationUrlInput /> |
| 166 | + |
| 167 | + <LinkBuilderShortLinkInput /> |
| 168 | + |
| 169 | + <TagSelect /> |
| 170 | + |
| 171 | + <LinkCommentsInput /> |
| 172 | + |
| 173 | + <ConversionTrackingToggle /> |
| 174 | + |
| 175 | + {isDesktop && ( |
| 176 | + <LinkFeatureButtons className="mt-1 flex-wrap" variant="page" /> |
| 177 | + )} |
| 178 | + |
| 179 | + <OptionsList /> |
| 180 | + </div> |
| 181 | + |
| 182 | + {isDesktop && ( |
| 183 | + <> |
| 184 | + <div className="grow" /> |
| 185 | + <LinkActionBar /> |
| 186 | + </> |
| 187 | + )} |
| 188 | + </div> |
| 189 | + <div className="px-4 md:px-6 lg:bg-neutral-50 lg:px-0"> |
| 190 | + <div className="mx-auto max-w-xl divide-neutral-200 lg:divide-y"> |
| 191 | + <div className="py-4 lg:px-4 lg:py-6"> |
| 192 | + <QRCodePreview /> |
| 193 | + </div> |
| 194 | + <div className="py-4 lg:px-4 lg:py-6"> |
| 195 | + <LinkPreview /> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + {!isDesktop && ( |
| 200 | + <LinkActionBar> |
| 201 | + <LinkFeatureButtons variant="page" /> |
| 202 | + </LinkActionBar> |
| 203 | + )} |
| 204 | + </form> |
| 205 | + </div> |
| 206 | + ); |
| 207 | +} |
| 208 | + |
| 209 | +function LoadingSkeleton() { |
| 210 | + return ( |
| 211 | + <div className="flex min-h-[calc(100vh-8px)] flex-col rounded-t-[inherit] bg-white"> |
| 212 | + <div className="flex items-center justify-between gap-4 py-3 pl-4 pr-5"> |
| 213 | + <div className="h-8 w-64 max-w-full animate-pulse rounded-md bg-neutral-100" /> |
| 214 | + <div className="h-7 w-32 max-w-full animate-pulse rounded-md bg-neutral-100" /> |
| 215 | + </div> |
| 216 | + <div |
| 217 | + className={cn( |
| 218 | + "grid grow grid-cols-1 lg:grid-cols-[minmax(0,1fr)_300px]", |
| 219 | + "divide-neutral-200 border-t border-neutral-200 lg:divide-x lg:divide-y lg:divide-y-0", |
| 220 | + )} |
| 221 | + > |
| 222 | + <div className="relative flex min-h-full flex-col px-4 md:px-6"> |
| 223 | + <div className="relative mx-auto flex w-full max-w-xl flex-col gap-7 pb-4 pt-10 lg:pb-10"> |
| 224 | + {["h-[66px]", "h-[66px]", "h-[64px]", "h-[104px]"].map( |
| 225 | + (className, idx) => ( |
| 226 | + <div key={idx} className={cn("flex flex-col gap-2", className)}> |
| 227 | + <div className="h-5 w-24 animate-pulse rounded-md bg-neutral-100" /> |
| 228 | + <div className="grow animate-pulse rounded-md bg-neutral-100" /> |
| 229 | + </div> |
| 230 | + ), |
| 231 | + )} |
| 232 | + </div> |
| 233 | + </div> |
| 234 | + <div className="px-4 md:px-6 lg:bg-neutral-50 lg:px-0"> |
| 235 | + <div className="mx-auto max-w-xl divide-neutral-200 lg:divide-y"></div> |
| 236 | + </div> |
| 237 | + </div> |
| 238 | + </div> |
| 239 | + ); |
| 240 | +} |
0 commit comments