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