1378a6099S猫头猫import {BottomSheetMethods} from '@gorhom/bottom-sheet/lib/typescript/types'; 2378a6099S猫头猫import {atom, useAtom} from 'jotai'; 3*c9af9657S猫头猫import {MutableRefObject, useCallback, useEffect, useRef} from 'react'; 4378a6099S猫头猫import {BackHandler, NativeEventSubscription} from 'react-native'; 5378a6099S猫头猫import panels from './types'; 6378a6099S猫头猫 7378a6099S猫头猫type IPanel = typeof panels; 8378a6099S猫头猫type IPanelkeys = keyof IPanel; 9378a6099S猫头猫 10378a6099S猫头猫const panelNameAtom = atom<IPanelkeys | null>(null); 11378a6099S猫头猫const payloadAtom = atom<any>(null); 12378a6099S猫头猫 134060c00aS猫头猫export function _usePanel( 144060c00aS猫头猫 ref?: MutableRefObject<BottomSheetMethods | undefined | null>, 154060c00aS猫头猫) { 16378a6099S猫头猫 const [panelName, setPanelName] = useAtom(panelNameAtom); 17378a6099S猫头猫 const [payload, setPayload] = useAtom(payloadAtom); 18378a6099S猫头猫 const backHandlerRef = useRef<NativeEventSubscription>(); 19378a6099S猫头猫 20378a6099S猫头猫 function showPanel<T extends IPanelkeys>( 21378a6099S猫头猫 name: T, 22378a6099S猫头猫 payload?: Parameters<IPanel[T]>[0], 23378a6099S猫头猫 ) { 24378a6099S猫头猫 setPanelName(name); 25378a6099S猫头猫 setPayload(payload); 26378a6099S猫头猫 } 27378a6099S猫头猫 28378a6099S猫头猫 useEffect(() => { 29378a6099S猫头猫 if (backHandlerRef.current) { 30378a6099S猫头猫 backHandlerRef.current?.remove(); 31378a6099S猫头猫 backHandlerRef.current = undefined; 32378a6099S猫头猫 } 33378a6099S猫头猫 if (ref) { 34378a6099S猫头猫 backHandlerRef.current = BackHandler.addEventListener( 35378a6099S猫头猫 'hardwareBackPress', 36378a6099S猫头猫 () => { 37378a6099S猫头猫 ref.current?.close(); 38378a6099S猫头猫 return true; 39378a6099S猫头猫 }, 40378a6099S猫头猫 ); 41378a6099S猫头猫 } 42378a6099S猫头猫 return () => { 43378a6099S猫头猫 if (backHandlerRef.current) { 44378a6099S猫头猫 backHandlerRef.current?.remove(); 45378a6099S猫头猫 backHandlerRef.current = undefined; 46378a6099S猫头猫 } 47378a6099S猫头猫 }; 48378a6099S猫头猫 }, []); 49378a6099S猫头猫 50*c9af9657S猫头猫 const unmountPanel = useCallback(() => { 51378a6099S猫头猫 setPanelName(null); 52378a6099S猫头猫 setPayload(null); 53*c9af9657S猫头猫 }, []); 54378a6099S猫头猫 55378a6099S猫头猫 return {payload, panelName, showPanel, unmountPanel}; 56378a6099S猫头猫} 57378a6099S猫头猫 58378a6099S猫头猫export default function usePanel() { 59378a6099S猫头猫 const {showPanel, unmountPanel} = _usePanel(); 60378a6099S猫头猫 return {showPanel, unmountPanel}; 61378a6099S猫头猫} 62