MCPcopy Create free account
hub / github.com/dailydotdev/apps / DirtyFormModal

Function DirtyFormModal

packages/shared/src/components/modals/DirtyFormModal.tsx:18–116  ·  view source on GitHub ↗
({
  isOpen,
  onRequestClose,
  onDiscard,
  onSave,
}: DirtyFormModalProps)

Source from the content-addressed store, hash-verified

16}
17
18export default function DirtyFormModal({
19 isOpen,
20 onRequestClose,
21 onDiscard,
22 onSave,
23}: DirtyFormModalProps): ReactElement {
24 const { closeModal } = useLazyModal();
25 const [isSaving, setIsSaving] = useState(false);
26
27 const handleSave = async () => {
28 if (isSaving) {
29 return;
30 }
31
32 let result: void | Promise<void>;
33 try {
34 result = onSave();
35 } catch {
36 closeModal();
37 return;
38 }
39
40 // Callers that save synchronously keep the original fire-and-forget close.
41 if (!result || typeof result.then !== 'function') {
42 closeModal();
43 return;
44 }
45
46 setIsSaving(true);
47
48 try {
49 await result;
50 } catch {
51 // The caller owns surfacing the failure; the modal closes either way so
52 // the user lands back on their still-unsaved form.
53 } finally {
54 setIsSaving(false);
55 closeModal();
56 }
57 };
58
59 const handleDiscard = () => {
60 onDiscard();
61 closeModal();
62 };
63
64 return (
65 <Modal
66 isOpen={isOpen}
67 // Dismissing mid-save would close whichever modal is current by the time
68 // the save settles, so the overlay and Escape are inert while it runs.
69 onRequestClose={isSaving ? undefined : onRequestClose}
70 kind={Modal.Kind.FlexibleCenter}
71 size={Modal.Size.Small}
72 shouldCloseOnOverlayClick={!isSaving}
73 isDrawerOnMobile
74 drawerProps={{ displayCloseButton: false }}
75 >

Callers

nothing calls this directly

Calls 1

useLazyModalFunction · 0.90

Tested by

no test coverage detected