The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add drafts
Tim Wilson950e128bcf21
diff --git a/apps/web/ui/modals/link-builder/draft-controls.tsx b/apps/web/ui/modals/link-builder/draft-controls.tsxnew file mode 100644index 0000000000..727cfecdd3--- /dev/null+++ b/apps/web/ui/modals/link-builder/draft-controls.tsx@@ -0,0 +1,267 @@+import { LinkWithTagsProps } from "@/lib/types";+import { AnimatedSizeContainer, Popover, useMediaQuery } from "@dub/ui";+import {+ Check2,+ CircleCheck,+ CircleInfo,+ LoadingCircle,+ Xmark,+} from "@dub/ui/src/icons";+import { cn, nanoid, timeAgo, truncate } from "@dub/utils";+import { ChevronDown } from "lucide-react";+import {+ forwardRef,+ SVGProps,+ useEffect,+ useImperativeHandle,+ useMemo,+ useState,+} from "react";+import { useFormContext } from "react-hook-form";+import { toast } from "sonner";+import { useDebouncedCallback } from "use-debounce";+import { LinkFormData } from "./";+import { LinkDraft, useLinkDrafts } from "./use-link-drafts";++export type DraftControlsHandle = {+ onSubmitSuccessful: () => void;+};++type DraftControlsProps = {+ props?: LinkWithTagsProps;+};++export const DraftControls = forwardRef<+ DraftControlsHandle,+ DraftControlsProps+>(({ props }: DraftControlsProps, ref) => {+ const { isMobile } = useMediaQuery();++ const {+ watch,+ getValues,+ setValue,+ formState: { isDirty },+ } = useFormContext<LinkFormData>();++ const [sessionId, setSessionId] = useState(() => nanoid());+ const [isSavePending, setIsSavePending] = useState(false);+ const [hasSaved, setHasSaved] = useState(false);+ const [openPopover, setOpenPopover] = useState(false);++ const {+ drafts: allDrafts,+ saveDraft,+ removeDraft,+ } = useLinkDrafts({+ linkId: props?.id,+ });++ const drafts = useMemo(() => {+ return allDrafts.filter((draft) => draft.id !== sessionId);+ }, [allDrafts, sessionId]);++ const saveDraftDebounced = useDebouncedCallback(() => {+ saveDraft(sessionId, getValues());+ setIsSavePending(false);+ setHasSaved(true);+ }, 1000);++ // Watch for form changes and save draft+ useEffect(() => {+ const { unsubscribe } = watch(() => {+ const [url, key] = getValues(["url", "key"]);+ if ((url || key) && isDirty) {+ setIsSavePending(true);+ saveDraftDebounced();+ }+ });+ return () => unsubscribe();+ }, [watch, isDirty]);++ useImperativeHandle(+ ref,+ () => {+ return {+ onSubmitSuccessful() {+ // Remove the current draft when it's submitted+ removeDraft(sessionId);+ },+ };+ },+ [sessionId],+ );++ return (isDirty && hasSaved) || drafts.length > 0 ? (+ <Popover+ content={+ <div className="w-full min-w-36 px-1 py-1 sm:w-auto">+ {drafts.length > 0 ? (+ <span className="block pb-2 pl-2.5 pt-2 text-xs font-medium text-gray-500">+ Restore drafts+ </span>+ ) : (+ <span className="block flex gap-1 px-2.5 pb-2 pt-2 text-xs text-gray-500">+ <CircleInfo className="size-3.5" />+ Your drafts will appear here+ </span>+ )}+ {drafts.length > 0 && (+ <AnimatedSizeContainer width={!isMobile} height>+ <ul className="scrollbar-hide grid max-h-40 overflow-y-auto">+ {drafts.map((draft) => (+ <DraftOption+ key={draft.id}+ draft={draft}+ onSelect={() => {+ setSessionId(draft.id);+ setOpenPopover(false);+ Object.entries(draft.link).forEach(([key, value]) => {+ setValue(key as any, value, { shouldDirty: true });+ });+ toast.success("Draft restored!");+ }}+ onDelete={() => removeDraft(draft.id)}+ />+ ))}+ </ul>+ </AnimatedSizeContainer>+ )}+ </div>+ }+ align="end"+ onWheel={(e) => {+ // Allows scrolling to work when the popover's in a modal+ e.stopPropagation();+ }}+ openPopover={openPopover}+ setOpenPopover={setOpenPopover}+ >+ <button+ type="button"+ className={cn(+ "animate-fade-in group text-sm transition-colors",+ isDirty && hasSaved+ ? "text-gray-400 hover:text-gray-600"+ : "text-gray-500 hover:text-gray-700",+ )}+ >+ {isDirty && hasSaved ? (+ <div className="flex items-center justify-end gap-2">+ {isSavePending ? (+ <LoadingCircle className="size-3.5" />+ ) : (+ <CircleCheck className="size-3.5" />+ )}+ {isSavePending ? "Saving..." : "Draft saved"}+ </div>+ ) : drafts.length > 0 ? (+ <div className="flex items-center justify-end gap-1">+ Drafts+ <ChevronDown className="size-3.5" />+ </div>+ ) : null}+ </button>+ </Popover>+ ) : null;+});++function DraftOption({+ draft,+ onSelect,+ onDelete,+}: {+ draft: LinkDraft;+ onSelect: () => void;+ onDelete: () => void;+}) {+ const [confirmDelete, setConfirmDelete] = useState(false);++ // Memoize time so it doesn't change on rerender+ const time = useMemo(+ () => timeAgo(new Date(draft.timestamp), { withAgo: true }),+ [draft.timestamp],+ );++ return (+ <li+ key={draft.id}+ role="button"+ className="group flex items-center justify-between gap-1 rounded py-1.5 pl-2 pr-1.5 text-sm transition-colors hover:bg-gray-100"+ onClick={() => {+ onSelect();+ }}+ >+ <div className="flex grow items-center justify-between gap-4 sm:gap-8">+ <div className="flex items-center gap-2.5">+ <RestoreDraftIcon className="size-3.5 text-gray-400" />+ {draft.link.key ? (+ <span className="text-gray-800">+ /{truncate(draft.link.key, 20)}+ </span>+ ) : (+ <span className="text-gray-500">(draft)</span>+ )}+ </div>+ <span className="text-xs text-gray-500">{time}</span>+ </div>+ <button+ type="button"+ onClick={(e) => {+ e.stopPropagation();+ if (confirmDelete) onDelete();+ else setConfirmDelete(true);+ }}+ className="p-1 text-gray-400 transition-colors hover:text-gray-500"+ title="Delete draft"+ >+ {confirmDelete ? (+ <Check2 className="size-3.5" />+ ) : (+ <Xmark className="size-3.5" />+ )}+ </button>+ </li>+ );+}++function RestoreDraftIcon(props: SVGProps<SVGSVGElement>) {+ return (+ <svg+ height="18"+ width="18"+ viewBox="0 0 18 18"+ xmlns="http://www.w3.org/2000/svg"+ {...props}+ >+ <g fill="currentColor">+ <polyline+ fill="none"+ points="9 4.75 9 9 12.25 11.25"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="2"+ />+ <g className="origin-center group-hover:rotate-[360deg] group-hover:transition-transform group-hover:duration-500">+ <polyline+ fill="none"+ points="1.88 14.695 2.288 11.75 5.232 12.157"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="2"+ />+ <path+ d="M1.75,9C1.75,4.996,4.996,1.75,9,1.75s7.25,3.246,7.25,7.25-3.246,7.25-7.25,7.25c-3.031,0-5.627-1.86-6.71-4.5"+ fill="none"+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth="2"+ />+ </g>+ </g>+ </svg>+ );+}diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsxindex 8aac460647..82c51166fc 100644--- a/apps/web/ui/modals/link-builder/index.tsx+++ b/apps/web/ui/modals/link-builder/index.tsx@@ -46,6 +46,7 @@ import TextareaAutosize from "react-textarea-autosize";import { toast } from "sonner";import { mutate } from "swr";import { useDebounce } from "use-debounce";+import { DraftControls, DraftControlsHandle } from "./draft-controls";import { useExpirationModal } from "./expiration-modal";import { LinkPreview } from "./link-preview";import { MoreDropdown } from "./more-dropdown";@@ -120,8 +121,14 @@ function LinkBuilderInner({formState: { isDirty, isSubmitting, isSubmitSuccessful, errors },} = useFormContext<LinkFormData>();- const data = watch();- const { url, domain, key, proxy } = data;+ const [url, domain, key, proxy, title, description] = watch([+ "url",+ "domain",+ "key",+ "proxy",+ "title",+ "description",+ ]);const [debouncedUrl] = useDebounce(getUrlWithoutUTMParams(url), 500);const endpoint = useMemo(@@ -160,7 +167,14 @@ function LinkBuilderInner({errors.url ||(props && !isDirty),);- }, [showLinkBuilder, isSubmitting, isSubmitSuccessful, errors, props, data]);+ }, [+ showLinkBuilder,+ isSubmitting,+ isSubmitSuccessful,+ errors,+ props,+ isDirty,+ ]);const keyRef = useRef<HTMLInputElement>(null);useEffect(() => {@@ -184,13 +198,15 @@ function LinkBuilderInner({const shortLink = useMemo(() =>linkConstructor({- key: data.key,- domain: data.domain,+ key,+ domain,pretty: true,}),- [data.key, data.domain],+ [key, domain],);+ const draftControlsRef = useRef<DraftControlsHandle>(null);+const { PasswordModal, PasswordButton } = usePasswordModal();const { TargetingModal, TargetingButton } = useTargetingModal();const { ExpirationModal, ExpirationButton } = useExpirationModal();@@ -270,6 +286,7 @@ function LinkBuilderInner({}} else toast.success("Successfully updated shortlink!");+ draftControlsRef.current?.onSubmitSuccessful();setShowLinkBuilder(false);} else {const { error } = await res.json();@@ -315,20 +332,23 @@ function LinkBuilderInner({</h3></div>{!homepageDemo && (- <button- type="button"- onClick={() => {- setShowLinkBuilder(false);- if (searchParams.has("newLink")) {- queryParams({- del: ["newLink"],- });- }- }}- className="group hidden rounded-full p-2 text-gray-500 transition-all duration-75 hover:bg-gray-100 focus:outline-none active:bg-gray-200 md:block"- >- <X className="h-5 w-5" />- </button>+ <div className="flex items-center gap-4">+ <DraftControls ref={draftControlsRef} props={props} />+ <button+ type="button"+ onClick={() => {+ setShowLinkBuilder(false);+ if (searchParams.has("newLink")) {+ queryParams({+ del: ["newLink"],+ });+ }+ }}+ className="group hidden rounded-full p-2 text-gray-500 transition-all duration-75 hover:bg-gray-100 focus:outline-none active:bg-gray-200 md:block"+ >+ <X className="h-5 w-5" />+ </button>+ </div>)}</div>@@ -375,7 +395,7 @@ function LinkBuilderInner({if (d.key !== undefined)setValue("key", d.key, { shouldDirty: true });}}- data={data}+ data={{ url, title, description }}saving={isSubmitting || isSubmitSuccessful}loading={loading}domains={domains}diff --git a/apps/web/ui/modals/link-builder/use-link-drafts.ts b/apps/web/ui/modals/link-builder/use-link-drafts.tsnew file mode 100644index 0000000000..03d49fe71d--- /dev/null+++ b/apps/web/ui/modals/link-builder/use-link-drafts.ts@@ -0,0 +1,50 @@+import { useLocalStorage } from "@dub/ui";+import { subDays } from "date-fns";+import { useCallback, useLayoutEffect, useMemo } from "react";+import { LinkFormData } from ".";++export type LinkDraft = {+ timestamp: number;+ id: string;+ link: Partial<LinkFormData>;+};++export function useLinkDrafts({ linkId }: { linkId?: string } = {}) {+ const [drafts, setDrafts] = useLocalStorage<LinkDraft[]>("link-drafts", []);++ // Removes drafts older than 1 week, limiting to 10 drafts+ const removeOldDrafts = useCallback(() => {+ setDrafts(+ drafts+ .filter((draft) => draft.timestamp > subDays(new Date(), 7).getTime())+ .sort((a, b) => b.timestamp - a.timestamp)+ .slice(0, 10),+ );+ }, [drafts]);++ // Initialize / clean up drafts+ useLayoutEffect(() => {+ if (!Array.isArray(drafts)) setDrafts([]);+ else removeOldDrafts();+ }, []);++ const saveDraft = (id: string, link: Partial<LinkFormData>) => {+ setDrafts([+ ...drafts.filter((d) => d.id !== id),+ { id, link, timestamp: new Date().getTime() },+ ]);+ };++ const removeDraft = (id: string) => {+ setDrafts(drafts.filter((draft) => draft.id !== id));+ };++ const filteredDrafts = useMemo(() => {+ console.log(linkId, drafts);+ return linkId+ ? drafts.filter((draft) => draft.link.id === linkId)+ : drafts.filter((draft) => !draft.link.id);+ }, [drafts, linkId]);++ return { drafts: filteredDrafts, saveDraft, removeDraft };+}diff --git a/packages/ui/src/button.tsx b/packages/ui/src/button.tsxindex 22cfcaa42f..e98e3f299b 100644--- a/packages/ui/src/button.tsx+++ b/packages/ui/src/button.tsx@@ -102,7 +102,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(className={cn("group flex h-10 w-full items-center justify-center gap-2 whitespace-nowrap rounded-md border px-4 text-sm",props.disabled || loading- ? "cursor-not-allowed border-gray-200 bg-gray-100 text-gray-400"+ ? "cursor-not-allowed border-gray-200 bg-gray-100 text-gray-400 outline-none": buttonVariants({ variant }),className,)}