-
-
Notifications
You must be signed in to change notification settings - Fork 843
Expand file tree
/
Copy pathApp.tsx
More file actions
293 lines (272 loc) · 11 KB
/
Copy pathApp.tsx
File metadata and controls
293 lines (272 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import * as React from 'react';
import {Appearance, Dimensions, StyleSheet, View} from 'react-native';
import {observer} from 'mobx-react';
import {isHydrated} from 'mobx-persist-store';
import {NavigationContainer} from '@react-navigation/native';
import {Provider as PaperProvider} from 'react-native-paper';
import {BottomSheetModalProvider} from '@gorhom/bottom-sheet';
import {createDrawerNavigator} from '@react-navigation/drawer';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {KeyboardProvider} from 'react-native-keyboard-controller';
import {
gestureHandlerRootHOC,
GestureHandlerRootView,
} from 'react-native-gesture-handler';
import {ttsStore, uiStore} from './src/store';
import {useTheme} from './src/hooks';
import {useDeepLinking} from './src/hooks/useDeepLinking';
import {Theme} from './src/utils/types';
import {l10n, initLocale} from './src/locales';
import {L10nContext} from './src/utils';
import {ROUTES} from './src/utils/navigationConstants';
import {
SidebarContent,
ModelsHeaderRight,
PalHeaderRight,
HeaderLeft,
AppWithMigration,
TTSSetupSheet,
DownloadOverlay,
HubRunSheetHost,
} from './src/components';
import {MarkdownProvider} from './src/components/MarkdownView';
import {AutomationBridge, BenchmarkRunnerScreen} from './src/__automation__';
import {
ChatScreen,
ModelsScreen,
SettingsScreen,
BenchmarkScreen,
AboutScreen,
// Dev tools screen. Only available in debug mode.
DevToolsScreen,
} from './src/screens';
import PalsScreen from './src/screens/PalsScreen';
import {OnboardingStack} from './src/screens/OnboardingScreens';
// Check if app is in debug mode
const isDebugMode = __DEV__;
const Drawer = createDrawerNavigator();
const screenWidth = Dimensions.get('window').width;
// Component that handles deep linking - must be inside NavigationContainer
const DeepLinkHandler = () => {
useDeepLinking();
return null;
};
// Branches between the OnboardingStack (first-launch flow) and the main
// Drawer.Navigator. Both children mount under the same provider tree —
// switching does NOT remount providers above this point.
//
// The hydration check is belt-and-suspenders. AppWithMigrationWrapper
// already gates render on `isHydrated(uiStore)`, but reading the same
// observable here keeps the contract local and survives refactors of the
// outer gate.
type SwitchPointProps = {drawer: React.ReactNode};
const SwitchPoint: React.FC<SwitchPointProps> = observer(({drawer}) => {
if (!isHydrated(uiStore)) {
return null;
}
if (!uiStore.hasCompletedOnboarding) {
return <OnboardingStack />;
}
return <>{drawer}</>;
});
const App = observer(() => {
const theme = useTheme();
const styles = createStyles(theme);
const currentL10n = l10n[uiStore.language];
// Initialize locale with the current language
React.useEffect(() => {
initLocale(uiStore.language);
}, []);
// Initialize TTS store (memory gate + AppState/session listeners).
// Fire-and-forget: `init()` is idempotent and swallows its own errors.
React.useEffect(() => {
ttsStore.init().catch(() => {
// init() swallows its own errors; catch to satisfy no-floating-promises.
});
}, []);
return (
<GestureHandlerRootView style={styles.root}>
{__E2E__ ? <AutomationBridge /> : null}
<SafeAreaProvider>
<KeyboardProvider statusBarTranslucent navigationBarTranslucent>
<PaperProvider theme={theme}>
<L10nContext.Provider value={currentL10n}>
<MarkdownProvider>
<NavigationContainer>
<DeepLinkHandler />
<BottomSheetModalProvider>
<SwitchPoint
drawer={
<Drawer.Navigator
screenOptions={{
headerLeft: () => <HeaderLeft />,
drawerStyle: {
width:
screenWidth > 400 ? 320 : screenWidth * 0.8,
},
headerStyle: {
backgroundColor: theme.colors.background,
},
headerTintColor: theme.colors.onBackground,
headerTitleStyle: styles.headerTitle,
}}
drawerContent={props => (
<SidebarContent {...props} />
)}>
<Drawer.Screen
name={ROUTES.CHAT}
component={gestureHandlerRootHOC(ChatScreen)}
options={{
headerShown: false,
}}
/>
<Drawer.Screen
name={ROUTES.PALS}
component={gestureHandlerRootHOC(PalsScreen)}
options={{
headerRight: () => <PalHeaderRight />,
headerStyle: styles.headerWithoutDivider,
title: currentL10n.screenTitles.pals,
}}
/>
<Drawer.Screen
name={ROUTES.MODELS}
component={gestureHandlerRootHOC(ModelsScreen)}
options={{
headerRight: () => <ModelsHeaderRight />,
headerStyle: styles.headerWithoutDivider,
title: currentL10n.screenTitles.models,
}}
/>
<Drawer.Screen
name={ROUTES.BENCHMARK}
component={gestureHandlerRootHOC(BenchmarkScreen)}
options={{
headerStyle: styles.headerWithoutDivider,
title: currentL10n.screenTitles.benchmark,
}}
/>
<Drawer.Screen
name={ROUTES.SETTINGS}
component={gestureHandlerRootHOC(SettingsScreen)}
options={{
headerStyle: styles.headerWithoutDivider,
title: currentL10n.screenTitles.settings,
}}
/>
<Drawer.Screen
name={ROUTES.APP_INFO}
component={gestureHandlerRootHOC(AboutScreen)}
options={{
headerStyle: styles.headerWithoutDivider,
title: currentL10n.screenTitles.appInfo,
}}
/>
{/* Only show Dev Tools screen in debug mode */}
{isDebugMode && (
<Drawer.Screen
name={ROUTES.DEV_TOOLS}
component={gestureHandlerRootHOC(DevToolsScreen)}
options={{
headerStyle: styles.headerWithoutDivider,
title: 'Dev Tools',
}}
/>
)}
{/*
E2E-only deep-link-driven benchmark matrix runner.
Hidden from the drawer sidebar via
drawerItemStyle:{display:'none'}; reachable only by
the deep link pocketpal://e2e/benchmark in the e2e
flavor build (see useDeepLinking cold-launch effect
and android/app/src/e2e/AndroidManifest.xml).
*/}
{__E2E__ && (
<Drawer.Screen
name={ROUTES.BENCHMARK_RUNNER}
component={gestureHandlerRootHOC(
BenchmarkRunnerScreen,
)}
options={{
headerStyle: styles.headerWithoutDivider,
title: 'Benchmark Runner',
drawerItemStyle: {display: 'none'},
}}
/>
)}
</Drawer.Navigator>
}
/>
<TTSSetupSheet />
<DownloadOverlay />
<HubRunSheetHost />
</BottomSheetModalProvider>
</NavigationContainer>
</MarkdownProvider>
</L10nContext.Provider>
</PaperProvider>
</KeyboardProvider>
</SafeAreaProvider>
</GestureHandlerRootView>
);
});
const createStyles = (theme: Theme) =>
StyleSheet.create({
root: {
flex: 1,
},
headerWithoutDivider: {
elevation: 0,
shadowOpacity: 0,
borderBottomWidth: 0,
backgroundColor: theme.colors.background,
},
headerWithDivider: {
backgroundColor: theme.colors.background,
},
headerTitle: {
...theme.fonts.titleSmall,
},
});
// Neutral background-only hold, rendered until mobx-persist-store has
// loaded UIStore from AsyncStorage. It is a single full-screen View whose
// only meaningful property is backgroundColor, resolved from the system
// color scheme. Deliberately carries NO branding, NO Text, NO
// SafeAreaProvider, NO insets, and NO spinner: a flat colored View has
// nothing to match against either native launch surface (iOS has a branded
// storyboard, Android has no native launch screen), so it cannot diverge
// from native on any axis and reads simply as "app launching".
const splashStyles = StyleSheet.create({
light: {flex: 1, backgroundColor: '#ffffff'},
dark: {flex: 1, backgroundColor: '#000000'},
});
const HydrationHold = () => (
<View
testID="hydration-splash"
style={
Appearance.getColorScheme() === 'dark'
? splashStyles.dark
: splashStyles.light
}
/>
);
// Wrap the App component with AppWithMigration to show migration UI when
// needed. Gates the first render of any theme-consuming subtree on
// mobx-persist-store hydration so persisted `language` and `colorScheme`
// are observed on first paint.
//
// The gate must wrap App itself (App calls useTheme() BEFORE <PaperProvider>
// mounts), so AppWithMigrationWrapper — which sits above App and has no
// theme dependency — is the chosen host. While unhydrated it renders the
// neutral background-only hold above.
const AppWithMigrationWrapper = observer(() => {
if (!isHydrated(uiStore)) {
return <HydrationHold />;
}
return (
<AppWithMigration>
<App />
</AppWithMigration>
);
});
export default AppWithMigrationWrapper;