Cleanup pass 2
This commit is contained in:
@@ -31,7 +31,7 @@ export function useCreateFlowExit({
|
||||
user: { id: string; email: string } | null;
|
||||
/** When save fails, surface the server message in the create shell banner (no leave confirm). */
|
||||
setDraftSaveBannerMessage?: (_message: string | null) => void;
|
||||
/** When exit would discard unsaved work, return true to proceed. Defaults to `window.confirm`. */
|
||||
/** When exit would discard unsaved work, return true to proceed. Defaults to denying leave. */
|
||||
confirmLeave?: () => Promise<boolean>;
|
||||
}): (_options?: { saveDraft?: boolean }) => Promise<void> {
|
||||
return useCallback(
|
||||
@@ -41,12 +41,7 @@ export function useCreateFlowExit({
|
||||
const saveDraft = options?.saveDraft ?? false;
|
||||
|
||||
if (!saveDraft) {
|
||||
const confirmFn =
|
||||
confirmLeave ??
|
||||
(async () => {
|
||||
if (typeof window === "undefined") return true;
|
||||
return window.confirm(messages.create.topNav.leaveConfirmLoss);
|
||||
});
|
||||
const confirmFn = confirmLeave ?? (async () => false);
|
||||
const confirmed = await confirmFn();
|
||||
if (!confirmed) return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback } from "react";
|
||||
import messages from "../../../../messages/en/index";
|
||||
import { useAsyncConfirm } from "../../../hooks/useAsyncConfirm";
|
||||
import type { CustomMethodCardFieldBlock } from "../../../../lib/create/customMethodCardFieldBlocks";
|
||||
import {
|
||||
confirmDiscardMethodCardCustomizeSession,
|
||||
isMethodCardCustomizeSessionDirty,
|
||||
type MethodCardCustomizeSnapshot,
|
||||
type MethodCardHeaderDraft,
|
||||
} from "../../../../lib/create/methodCardCustomizeSession";
|
||||
|
||||
const copy = messages.create.customRule.modalKebabMenu;
|
||||
|
||||
const confirmOptions = {
|
||||
title: copy.discardUnsavedCustomizeChangesTitle,
|
||||
description: copy.discardUnsavedCustomizeChangesDescription,
|
||||
proceedText: copy.discardUnsavedCustomizeChangesProceed,
|
||||
cancelText: copy.discardUnsavedCustomizeChangesCancel,
|
||||
};
|
||||
|
||||
/**
|
||||
* Create-flow confirm for exiting customize mode with unsaved edits.
|
||||
*
|
||||
* @returns Async helpers plus `confirmDialog` to render once in the screen JSX.
|
||||
*/
|
||||
export function useDiscardCustomizeConfirm() {
|
||||
const { requestConfirm, confirmDialog } = useAsyncConfirm();
|
||||
|
||||
const runConfirm = useCallback(
|
||||
() => requestConfirm(confirmOptions),
|
||||
[requestConfirm],
|
||||
);
|
||||
|
||||
const confirmDiscard = useCallback(
|
||||
async <TDraft,>(
|
||||
modalEditUnlocked: boolean,
|
||||
snapshot: MethodCardCustomizeSnapshot<TDraft> | null,
|
||||
pendingDraft: TDraft | null,
|
||||
draftFieldBlocks: CustomMethodCardFieldBlock[] | null,
|
||||
headerDraft: MethodCardHeaderDraft | null,
|
||||
) =>
|
||||
confirmDiscardMethodCardCustomizeSession(
|
||||
modalEditUnlocked,
|
||||
snapshot,
|
||||
pendingDraft,
|
||||
draftFieldBlocks,
|
||||
headerDraft,
|
||||
runConfirm,
|
||||
),
|
||||
[runConfirm],
|
||||
);
|
||||
|
||||
const confirmDirtyCustomizeCancel = useCallback(
|
||||
async <TDraft,>(
|
||||
snapshot: MethodCardCustomizeSnapshot<TDraft>,
|
||||
pendingDraft: TDraft | null,
|
||||
draftFieldBlocks: CustomMethodCardFieldBlock[] | null,
|
||||
headerDraft: MethodCardHeaderDraft | null,
|
||||
) => {
|
||||
if (
|
||||
!isMethodCardCustomizeSessionDirty(
|
||||
snapshot,
|
||||
pendingDraft,
|
||||
draftFieldBlocks,
|
||||
headerDraft,
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return runConfirm();
|
||||
},
|
||||
[runConfirm],
|
||||
);
|
||||
|
||||
return { confirmDiscard, confirmDirtyCustomizeCancel, confirmDialog };
|
||||
}
|
||||
Reference in New Issue
Block a user