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.

Allow escape to close modal + instantly save draft when closing

Tim Wilson4c8ecc3c4917
diff --git a/apps/web/ui/modals/link-builder/draft-controls.tsx b/apps/web/ui/modals/link-builder/draft-controls.tsx
index 1370da49a6..9f670c7e4d 100644
--- a/apps/web/ui/modals/link-builder/draft-controls.tsx
+++ b/apps/web/ui/modals/link-builder/draft-controls.tsx
@@ -24,6 +24,7 @@ import { LinkDraft, useLinkDrafts } from "./use-link-drafts";
export type DraftControlsHandle = {
onSubmitSuccessful: () => void;
+ onClose: () => void;
};
type DraftControlsProps = {
@@ -88,9 +89,16 @@ export const DraftControls = forwardRef<
// Remove the current draft when it's submitted
removeDraft(sessionId);
},
+ onClose() {
+ // Save draft instantly when the link builder is closed
+ const [url, key] = getValues(["url", "key"]);
+ if ((url || key) && isDirty) {
+ saveDraft(sessionId, getValues());
+ }
+ },
};
},
- [sessionId],
+ [sessionId, isDirty],
);
return (isDirty && hasSaved) || drafts.length > 0 ? (
diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsx
index 7e08d9fd30..7b9c189cf1 100644
--- a/apps/web/ui/modals/link-builder/index.tsx
+++ b/apps/web/ui/modals/link-builder/index.tsx
@@ -220,12 +220,12 @@ function LinkBuilderInner({
showModal={showLinkBuilder}
setShowModal={setShowLinkBuilder}
className="max-w-screen-lg"
- preventDefaultClose={homepageDemo ? false : true}
onClose={() => {
if (searchParams.has("newLink"))
queryParams({
del: ["newLink", "newLinkDomain"],
});
+ draftControlsRef.current?.onClose();
}}
>
<LinkModalContext.Provider
@@ -349,6 +349,7 @@ function LinkBuilderInner({
del: ["newLink"],
});
}
+ draftControlsRef.current?.onClose();
}}
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"
>
diff --git a/apps/web/ui/modals/link-builder/use-link-drafts.ts b/apps/web/ui/modals/link-builder/use-link-drafts.ts
index 55d9711f22..e4b37db20c 100644
--- a/apps/web/ui/modals/link-builder/use-link-drafts.ts
+++ b/apps/web/ui/modals/link-builder/use-link-drafts.ts
@@ -48,12 +48,13 @@ export function useLinkDrafts({
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]);
+ const filteredDrafts = useMemo(
+ () =>
+ linkId
+ ? drafts.filter((draft) => draft.link.id === linkId)
+ : drafts.filter((draft) => !draft.link.id),
+ [drafts, linkId],
+ );
return { drafts: filteredDrafts, saveDraft, removeDraft };
}