diff options
author | crupest <crupest@outlook.com> | 2023-09-21 15:35:27 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-09-21 15:35:27 +0800 |
commit | c7d2545ec7bbcdba30b775453d53df5a359410bd (patch) | |
tree | 768668b9d1b2cd835212c3a3567dd8f54700a7fa /FrontEnd/src/components/dialog/index.tsx | |
parent | f836d77e73f3ea0af45c5f71dae7268143d6d86f (diff) | |
download | timeline-c7d2545ec7bbcdba30b775453d53df5a359410bd.tar.gz timeline-c7d2545ec7bbcdba30b775453d53df5a359410bd.tar.bz2 timeline-c7d2545ec7bbcdba30b775453d53df5a359410bd.zip |
Revert dialog.
Diffstat (limited to 'FrontEnd/src/components/dialog/index.tsx')
-rw-r--r-- | FrontEnd/src/components/dialog/index.tsx | 65 |
1 files changed, 59 insertions, 6 deletions
diff --git a/FrontEnd/src/components/dialog/index.tsx b/FrontEnd/src/components/dialog/index.tsx index 9ca06de2..17db8fd0 100644 --- a/FrontEnd/src/components/dialog/index.tsx +++ b/FrontEnd/src/components/dialog/index.tsx @@ -1,12 +1,65 @@ +import { useState } from "react"; + export { default as Dialog } from "./Dialog"; export { default as FullPageDialog } from "./FullPageDialog"; export { default as OperationDialog } from "./OperationDialog"; export { default as ConfirmDialog } from "./ConfirmDialog"; export { default as DialogContainer } from "./DialogContainer"; -export { - useDialog, - useDialogController, - useCloseDialog, - DialogProvider, -} from "./DialogProvider"; +type DialogMap<D extends string, V> = { + [K in D]: V; +}; + +type DialogKeyMap<D extends string> = DialogMap<D, number>; + +type DialogPropsMap<D extends string> = DialogMap< + D, + { key: number | string; open: boolean; onClose: () => void } +>; + +export function useDialog<D extends string>( + dialogs: D[], + options?: { + initDialog?: D | null; + onClose?: { + [K in D]?: () => void; + }; + }, +): { + dialog: D | null; + switchDialog: (newDialog: D | null) => void; + dialogPropsMap: DialogPropsMap<D>; + createDialogSwitch: (newDialog: D | null) => () => void; +} { + const [dialog, setDialog] = useState<D | null>(options?.initDialog ?? null); + + const [dialogKeys, setDialogKeys] = useState<DialogKeyMap<D>>( + () => Object.fromEntries(dialogs.map((d) => [d, 0])) as DialogKeyMap<D>, + ); + + const switchDialog = (newDialog: D | null) => { + if (dialog !== null) { + setDialogKeys({ ...dialogKeys, [dialog]: dialogKeys[dialog] + 1 }); + } + setDialog(newDialog); + }; + + return { + dialog, + switchDialog, + dialogPropsMap: Object.fromEntries( + dialogs.map((d) => [ + d, + { + key: `${d}-${dialogKeys[d]}`, + open: dialog === d, + onClose: () => { + switchDialog(null); + options?.onClose?.[d]?.(); + }, + }, + ]), + ) as DialogPropsMap<D>, + createDialogSwitch: (newDialog: D | null) => () => switchDialog(newDialog), + }; +} |