mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import * as React from 'react'
2import {View} from 'react-native'
3
4// Based on @react-navigation/native-stack/src/createNativeStackNavigator.ts
5// MIT License
6// Copyright (c) 2017 React Navigation Contributors
7
8import {
9 createNavigatorFactory,
10 EventArg,
11 ParamListBase,
12 StackActionHelpers,
13 StackActions,
14 StackNavigationState,
15 StackRouter,
16 StackRouterOptions,
17 useNavigationBuilder,
18} from '@react-navigation/native'
19import type {
20 NativeStackNavigationEventMap,
21 NativeStackNavigationOptions,
22} from '@react-navigation/native-stack'
23import type {NativeStackNavigatorProps} from '@react-navigation/native-stack/src/types'
24import {NativeStackView} from '@react-navigation/native-stack'
25
26import {BottomBarWeb} from './bottom-bar/BottomBarWeb'
27import {DesktopLeftNav} from './desktop/LeftNav'
28import {DesktopRightNav} from './desktop/RightNav'
29import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
30import {useOnboardingState} from '#/state/shell'
31import {
32 useLoggedOutView,
33 useLoggedOutViewControls,
34} from '#/state/shell/logged-out'
35import {useSession} from '#/state/session'
36import {isWeb} from 'platform/detection'
37import {LoggedOut} from '../com/auth/LoggedOut'
38import {Onboarding} from '../com/auth/Onboarding'
39
40type NativeStackNavigationOptionsWithAuth = NativeStackNavigationOptions & {
41 requireAuth?: boolean
42}
43
44function NativeStackNavigator({
45 id,
46 initialRouteName,
47 children,
48 screenListeners,
49 screenOptions,
50 ...rest
51}: NativeStackNavigatorProps) {
52 // --- this is copy and pasted from the original native stack navigator ---
53 const {state, descriptors, navigation, NavigationContent} =
54 useNavigationBuilder<
55 StackNavigationState<ParamListBase>,
56 StackRouterOptions,
57 StackActionHelpers<ParamListBase>,
58 NativeStackNavigationOptionsWithAuth,
59 NativeStackNavigationEventMap
60 >(StackRouter, {
61 id,
62 initialRouteName,
63 children,
64 screenListeners,
65 screenOptions,
66 })
67 React.useEffect(
68 () =>
69 // @ts-expect-error: there may not be a tab navigator in parent
70 navigation?.addListener?.('tabPress', (e: any) => {
71 const isFocused = navigation.isFocused()
72
73 // Run the operation in the next frame so we're sure all listeners have been run
74 // This is necessary to know if preventDefault() has been called
75 requestAnimationFrame(() => {
76 if (
77 state.index > 0 &&
78 isFocused &&
79 !(e as EventArg<'tabPress', true>).defaultPrevented
80 ) {
81 // When user taps on already focused tab and we're inside the tab,
82 // reset the stack to replicate native behaviour
83 navigation.dispatch({
84 ...StackActions.popToTop(),
85 target: state.key,
86 })
87 }
88 })
89 }),
90 [navigation, state.index, state.key],
91 )
92
93 // --- our custom logic starts here ---
94 const {hasSession} = useSession()
95 const activeRoute = state.routes[state.index]
96 const activeDescriptor = descriptors[activeRoute.key]
97 const activeRouteRequiresAuth = activeDescriptor.options.requireAuth ?? false
98 const onboardingState = useOnboardingState()
99 const {showLoggedOut} = useLoggedOutView()
100 const {setShowLoggedOut} = useLoggedOutViewControls()
101 const {isMobile} = useWebMediaQueries()
102 if (activeRouteRequiresAuth && !hasSession) {
103 return <LoggedOut />
104 }
105 if (showLoggedOut) {
106 return <LoggedOut onDismiss={() => setShowLoggedOut(false)} />
107 }
108 if (onboardingState.isActive) {
109 return <Onboarding />
110 }
111 const newDescriptors: typeof descriptors = {}
112 for (let key in descriptors) {
113 const descriptor = descriptors[key]
114 const requireAuth = descriptor.options.requireAuth ?? false
115 newDescriptors[key] = {
116 ...descriptor,
117 render() {
118 if (requireAuth && !hasSession) {
119 return <View />
120 } else {
121 return descriptor.render()
122 }
123 },
124 }
125 }
126 return (
127 <NavigationContent>
128 <NativeStackView
129 {...rest}
130 state={state}
131 navigation={navigation}
132 descriptors={newDescriptors}
133 />
134 {isWeb && isMobile && <BottomBarWeb />}
135 {isWeb && !isMobile && (
136 <>
137 <DesktopLeftNav />
138 <DesktopRightNav />
139 </>
140 )}
141 </NavigationContent>
142 )
143}
144
145export const createNativeStackNavigatorWithAuth = createNavigatorFactory<
146 StackNavigationState<ParamListBase>,
147 NativeStackNavigationOptionsWithAuth,
148 NativeStackNavigationEventMap,
149 typeof NativeStackNavigator
150>(NativeStackNavigator)