mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {
3 StyleProp,
4 StyleSheet,
5 TouchableOpacity,
6 View,
7 ViewStyle,
8} from 'react-native'
9import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
10import {useNavigation} from '@react-navigation/native'
11import {CenteredView} from './Views'
12import {usePalette} from 'lib/hooks/usePalette'
13import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
14import {useAnalytics} from 'lib/analytics/analytics'
15import {NavigationProp} from 'lib/routes/types'
16import {useSetDrawerOpen} from '#/state/shell'
17
18const BACK_HITSLOP = {left: 20, top: 20, right: 50, bottom: 20}
19
20export function SimpleViewHeader({
21 showBackButton = true,
22 style,
23 children,
24}: React.PropsWithChildren<{
25 showBackButton?: boolean
26 style?: StyleProp<ViewStyle>
27}>) {
28 const pal = usePalette('default')
29 const setDrawerOpen = useSetDrawerOpen()
30 const navigation = useNavigation<NavigationProp>()
31 const {track} = useAnalytics()
32 const {isMobile} = useWebMediaQueries()
33 const canGoBack = navigation.canGoBack()
34
35 const onPressBack = React.useCallback(() => {
36 if (navigation.canGoBack()) {
37 navigation.goBack()
38 } else {
39 navigation.navigate('Home')
40 }
41 }, [navigation])
42
43 const onPressMenu = React.useCallback(() => {
44 track('ViewHeader:MenuButtonClicked')
45 setDrawerOpen(true)
46 }, [track, setDrawerOpen])
47
48 const Container = isMobile ? View : CenteredView
49 return (
50 <Container style={[styles.header, isMobile && styles.headerMobile, style]}>
51 {showBackButton ? (
52 <TouchableOpacity
53 testID="viewHeaderDrawerBtn"
54 onPress={canGoBack ? onPressBack : onPressMenu}
55 hitSlop={BACK_HITSLOP}
56 style={canGoBack ? styles.backBtn : styles.backBtnWide}
57 accessibilityRole="button"
58 accessibilityLabel={canGoBack ? 'Back' : 'Menu'}
59 accessibilityHint="">
60 {canGoBack ? (
61 <FontAwesomeIcon
62 size={18}
63 icon="angle-left"
64 style={[styles.backIcon, pal.text]}
65 />
66 ) : (
67 <FontAwesomeIcon
68 size={18}
69 icon="bars"
70 style={[styles.backIcon, pal.textLight]}
71 />
72 )}
73 </TouchableOpacity>
74 ) : null}
75 {children}
76 </Container>
77 )
78}
79
80const styles = StyleSheet.create({
81 header: {
82 flexDirection: 'row',
83 alignItems: 'center',
84 paddingHorizontal: 18,
85 paddingVertical: 12,
86 width: '100%',
87 },
88 headerMobile: {
89 paddingHorizontal: 12,
90 paddingVertical: 10,
91 },
92 backBtn: {
93 width: 30,
94 height: 30,
95 },
96 backBtnWide: {
97 width: 30,
98 height: 30,
99 paddingHorizontal: 6,
100 },
101 backIcon: {
102 marginTop: 6,
103 },
104})