Import apps/android/src/screens/chat/use-chat-back-handler.ts
This commit is contained in:
parent
298a318bcf
commit
ac04a62411
|
|
@ -4,11 +4,17 @@ import { BackHandler, Keyboard, type TextInput } from 'react-native'
|
||||||
type BackPanel = { open: boolean; close: () => void }
|
type BackPanel = { open: boolean; close: () => void }
|
||||||
|
|
||||||
/** Ordered panels keep the existing back priority; the listener always reads current state. */
|
/** Ordered panels keep the existing back priority; the listener always reads current state. */
|
||||||
export function useChatBackHandler(input: RefObject<TextInput | null>, onBack: () => void, panels: BackPanel[]) {
|
export function useChatBackHandler(
|
||||||
const latest = useRef({ onBack, panels })
|
input: RefObject<TextInput | null>,
|
||||||
latest.current = { onBack, panels }
|
onBack: () => boolean | void,
|
||||||
|
panels: BackPanel[],
|
||||||
|
active = true,
|
||||||
|
) {
|
||||||
|
const latest = useRef({ onBack, panels, active })
|
||||||
|
latest.current = { onBack, panels, active }
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const subscription = BackHandler.addEventListener('hardwareBackPress', () => {
|
const subscription = BackHandler.addEventListener('hardwareBackPress', () => {
|
||||||
|
if (!latest.current.active) return false
|
||||||
const panel = latest.current.panels.find(panel => panel.open)
|
const panel = latest.current.panels.find(panel => panel.open)
|
||||||
if (panel !== undefined) {
|
if (panel !== undefined) {
|
||||||
panel.close()
|
panel.close()
|
||||||
|
|
@ -20,8 +26,7 @@ export function useChatBackHandler(input: RefObject<TextInput | null>, onBack: (
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
Keyboard.dismiss()
|
Keyboard.dismiss()
|
||||||
latest.current.onBack()
|
return latest.current.onBack() !== false
|
||||||
return true
|
|
||||||
})
|
})
|
||||||
return () => subscription.remove()
|
return () => subscription.remove()
|
||||||
}, [input])
|
}, [input])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue