xref: /MusicFree/src/components/panels/usePanel.ts (revision 2aa881935ca35b8fb1abc4206e0dc35149231456)
1import {GlobalState} from '@/utils/stateMapper';
2import {useCallback} from 'react';
3import panels from './types';
4
5type IPanel = typeof panels;
6type IPanelkeys = keyof IPanel;
7
8interface IPanelInfo {
9    name: IPanelkeys | null;
10    payload: any;
11}
12
13/** 浮层信息 */
14export const panelInfoStore = new GlobalState<IPanelInfo>({
15    name: null,
16    payload: null,
17});
18
19/** 使用浮层的hook */
20export default function usePanel() {
21    const showPanel = useCallback(function <T extends IPanelkeys>(
22        name: T,
23        payload?: Parameters<IPanel[T]>[0],
24    ) {
25        panelInfoStore.setValue({
26            name,
27            payload,
28        });
29    },
30    []);
31
32    const unmountPanel = useCallback(() => {
33        panelInfoStore.setValue({
34            name: null,
35            payload: null,
36        });
37    }, []);
38
39    return {showPanel, unmountPanel};
40}
41