forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import * as React from 'react'
2import {View} from 'react-native'
3// Based on @react-navigation/native-stack/src/navigators/createNativeStackNavigator.ts
4// MIT License
5// Copyright (c) 2017 React Navigation Contributors
6import {
7 createNavigatorFactory,
8 type EventArg,
9 type NavigatorTypeBagBase,
10 type ParamListBase,
11 type StackActionHelpers,
12 StackActions,
13 type StackNavigationState,
14 StackRouter,
15 type StackRouterOptions,
16 type StaticConfig,
17 type TypedNavigator,
18 useNavigationBuilder,
19} from '@react-navigation/native'
20import {NativeStackView} from '@react-navigation/native-stack'
21import {
22 type NativeStackNavigationEventMap,
23 type NativeStackNavigationOptions,
24 type NativeStackNavigationProp,
25 type NativeStackNavigatorProps,
26} from '@react-navigation/native-stack'
27
28import {PWI_ENABLED} from '#/lib/build-flags'
29import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
30import {isNative, isWeb} from '#/platform/detection'
31import {useSession} from '#/state/session'
32import {useOnboardingState} from '#/state/shell'
33import {
34 useLoggedOutView,
35 useLoggedOutViewControls,
36} from '#/state/shell/logged-out'
37import {LoggedOut} from '#/view/com/auth/LoggedOut'
38import {Onboarding} from '#/screens/Onboarding'
39import {SignupQueued} from '#/screens/SignupQueued'
40import {atoms as a, useLayoutBreakpoints} from '#/alf'
41import {PolicyUpdateOverlay} from '#/components/PolicyUpdateOverlay'
42import {BottomBarWeb} from './bottom-bar/BottomBarWeb'
43import {DesktopLeftNav} from './desktop/LeftNav'
44import {DesktopRightNav} from './desktop/RightNav'
45
46type NativeStackNavigationOptionsWithAuth = NativeStackNavigationOptions & {
47 requireAuth?: boolean
48}
49
50function NativeStackNavigator({
51 id,
52 initialRouteName,
53 children,
54 layout,
55 screenListeners,
56 screenOptions,
57 screenLayout,
58 ...rest
59}: NativeStackNavigatorProps) {
60 // --- this is copy and pasted from the original native stack navigator ---
61 const {state, describe, descriptors, navigation, NavigationContent} =
62 useNavigationBuilder<
63 StackNavigationState<ParamListBase>,
64 StackRouterOptions,
65 StackActionHelpers<ParamListBase>,
66 NativeStackNavigationOptionsWithAuth,
67 NativeStackNavigationEventMap
68 >(StackRouter, {
69 id,
70 initialRouteName,
71 children,
72 layout,
73 screenListeners,
74 screenOptions,
75 screenLayout,
76 })
77
78 React.useEffect(
79 () =>
80 // @ts-expect-error: there may not be a tab navigator in parent
81 navigation?.addListener?.('tabPress', (e: any) => {
82 const isFocused = navigation.isFocused()
83
84 // Run the operation in the next frame so we're sure all listeners have been run
85 // This is necessary to know if preventDefault() has been called
86 requestAnimationFrame(() => {
87 if (
88 state.index > 0 &&
89 isFocused &&
90 !(e as EventArg<'tabPress', true>).defaultPrevented
91 ) {
92 // When user taps on already focused tab and we're inside the tab,
93 // reset the stack to replicate native behaviour
94 navigation.dispatch({
95 ...StackActions.popToTop(),
96 target: state.key,
97 })
98 }
99 })
100 }),
101 [navigation, state.index, state.key],
102 )
103
104 // --- our custom logic starts here ---
105 const {hasSession, currentAccount} = useSession()
106 const activeRoute = state.routes[state.index]
107 const activeDescriptor = descriptors[activeRoute.key]
108 const activeRouteRequiresAuth = activeDescriptor.options.requireAuth ?? false
109 const onboardingState = useOnboardingState()
110 const {showLoggedOut} = useLoggedOutView()
111 const {setShowLoggedOut} = useLoggedOutViewControls()
112 const {isMobile} = useWebMediaQueries()
113 const {leftNavMinimal} = useLayoutBreakpoints()
114 if (!hasSession && (!PWI_ENABLED || activeRouteRequiresAuth || isNative)) {
115 return <LoggedOut />
116 }
117 if (hasSession && currentAccount?.signupQueued) {
118 return <SignupQueued />
119 }
120 if (showLoggedOut) {
121 return <LoggedOut onDismiss={() => setShowLoggedOut(false)} />
122 }
123 if (onboardingState.isActive) {
124 return <Onboarding />
125 }
126 const newDescriptors: typeof descriptors = {}
127 for (let key in descriptors) {
128 const descriptor = descriptors[key]
129 const requireAuth = descriptor.options.requireAuth ?? false
130 newDescriptors[key] = {
131 ...descriptor,
132 render() {
133 if (requireAuth && !hasSession) {
134 return <View />
135 } else {
136 return descriptor.render()
137 }
138 },
139 }
140 }
141
142 // Show the bottom bar if we have a session only on mobile web. If we don't have a session, we want to show it
143 // on both tablet and mobile web so that we see the create account CTA.
144 const showBottomBar = hasSession ? isMobile : leftNavMinimal
145
146 return (
147 <NavigationContent>
148 <View role="main" style={a.flex_1}>
149 <NativeStackView
150 {...rest}
151 state={state}
152 navigation={navigation}
153 descriptors={descriptors}
154 describe={describe}
155 />
156 </View>
157 {isWeb && (
158 <>
159 {showBottomBar ? <BottomBarWeb /> : <DesktopLeftNav />}
160 {!isMobile && <DesktopRightNav routeName={activeRoute.name} />}
161 </>
162 )}
163
164 {/* Only shown after logged in and onboaring etc are complete */}
165 {hasSession && <PolicyUpdateOverlay />}
166 </NavigationContent>
167 )
168}
169
170export function createNativeStackNavigatorWithAuth<
171 const ParamList extends ParamListBase,
172 const NavigatorID extends string | undefined = undefined,
173 const TypeBag extends NavigatorTypeBagBase = {
174 ParamList: ParamList
175 NavigatorID: NavigatorID
176 State: StackNavigationState<ParamList>
177 ScreenOptions: NativeStackNavigationOptionsWithAuth
178 EventMap: NativeStackNavigationEventMap
179 NavigationList: {
180 [RouteName in keyof ParamList]: NativeStackNavigationProp<
181 ParamList,
182 RouteName,
183 NavigatorID
184 >
185 }
186 Navigator: typeof NativeStackNavigator
187 },
188 const Config extends StaticConfig<TypeBag> = StaticConfig<TypeBag>,
189>(config?: Config): TypedNavigator<TypeBag, Config> {
190 return createNavigatorFactory(NativeStackNavigator)(config)
191}