xref: /MusicFree/src/components/dialogs/useDialog.ts (revision 5589cdf32b2bb0f641e5ac7bf1f6152cd6b9b70e)
1import {GlobalState} from '@/utils/stateMapper';
2import {useCallback} from 'react';
3import {IDialogKey, IDialogType} from './components';
4
5interface IDialogInfo {
6    name: IDialogKey | null;
7    payload: any;
8}
9
10export const dialogInfoStore = new GlobalState<IDialogInfo>({
11    name: null,
12    payload: null,
13});
14
15export function showDialog<T extends keyof IDialogType>(
16    name: T,
17    payload?: Parameters<IDialogType[T]>[0],
18) {
19    dialogInfoStore.setValue({
20        name,
21        payload,
22    });
23}
24
25export function hideDialog() {
26    dialogInfoStore.setValue({
27        name: null,
28        payload: null,
29    });
30}
31
32export default function useDialog() {
33    const showDialog = useCallback(
34        <T extends keyof IDialogType>(
35            name: T,
36            payload?: Parameters<IDialogType[T]>[0],
37        ) => {
38            dialogInfoStore.setValue({
39                name,
40                payload,
41            });
42        },
43        [],
44    );
45
46    const hideDialog = useCallback(() => {
47        dialogInfoStore.setValue({
48            name: null,
49            payload: null,
50        });
51    }, []);
52
53    return {showDialog, hideDialog};
54}
55
56export function getCurrentDialog() {
57    return dialogInfoStore.getValue();
58}
59