dialogs
63 linesSource
Use when app needs an alert, a confirmation before a destructive or irreversible action, an error message to acknowledge, or a custom MUI Dialog (create/edit dialog, picker, scanner) opened from code with useDialogs and awaited for its result.
Rules
- useDialogs() from wcz-layout/hooks returns { alert, confirm, open, close }; LayoutProvider already mounts the provider.
- confirm(message) resolves true/false; ask it before every delete and other irreversible action. Delete prompts use t("DeleteConfirmation", { count }).
- alert(message) for anything the user must acknowledge, first of all a failed action: if (error instanceof Error) await alert(error.message).
- open(Component, payload) renders a custom dialog and resolves with what it passes to onClose(result). Type it DialogProps<TPayload, TResult>; leave TResult off only for dialogs that return nothing. Never keep dialog open-state in the caller.
- close(dialogPromise, result) closes a dialog from outside, with the promise open() returned; inside, call the injected onClose.
- Save inside the dialog: await persistence, then onClose(success). The optional open(..., { onClose }) callback is not a veto: the provider closes even if it rejects. Do not put a fallible save there and expect the form to stay open.
- A feature's dialogs live in routes/<entities>/-components, shared ones in src/components.
Examples
tsx
const { confirm, alert, open } = useDialogs();
if (await confirm(t("DeleteConfirmation", { count: ids.length }))) {
try {
await handleOnDelete(ids).when("settled");
} catch (error) {
if (error instanceof Error) await alert(error.message);
}
}
const saved = await open(EditFeatureDialog, { id });tsx
interface Payload {
id: string;
}
export const EditFeatureDialog = ({ payload, open, onClose }: DialogProps<Payload, boolean>) => {
const { t } = useTranslation();
return (
<Dialog fullWidth open={open} onClose={() => onClose(false)}>
<DialogTitle>{t("Feature.Confirm")}</DialogTitle>
<DialogContent>{payload.id}</DialogContent>
<DialogActions>
<Button onClick={() => onClose(false)}>{t("Cancel")}</Button>
<Button onClick={() => onClose(true)}>{t("Confirm")}</Button>
</DialogActions>
</Dialog>
);
};