Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AndroidWorkspaceTabsProvider.tsx

44 lines1.9 KB

1import type { NavigationState } from "@react-navigation/native";
2import { createContext, use, type ReactNode } from "react";
3import { Platform } from "react-native";
4import { androidWorkspaceSettingsOpen } from "./android-workspace-tabs";
5import { WorkspaceTabsProvider } from "./WorkspaceTabsContext";
6​
7interface AndroidWorkspaceRoot {
8 /**
9 * The rail's `useNavigation()` dispatches to the focused navigator, which may be nested Settings.
10 * Target the root stack's key (and read `rootNavigationRef`'s state) so a tab press resets the root.
11 */
12 readonly key: string;
13 /** Settings is derived from navigation rather than kept in the tab reducer. */
14 readonly settingsOpen: boolean;
15 readonly settingsRoute: NavigationState["routes"][number] | undefined;
16 readonly settingsPage: string | null;
18​
19const AndroidWorkspaceRootContext = createContext<AndroidWorkspaceRoot | null>(null);
20​
21export function useAndroidWorkspaceRoot() {
22 return use(AndroidWorkspaceRootContext);
24​
25/** Android keeps one tab controller above the root navigator and split sidebar. */
26export function AndroidWorkspaceTabsProvider(props: {
27 readonly state: NavigationState;
28 readonly children: ReactNode;
29}) {
30 const key = props.state.key;
31 const settingsOpen = Platform.OS === "android" && androidWorkspaceSettingsOpen(props.state);
32 const settingsIndex = props.state.routes.findIndex((route) => route.name === "Settings");
33 const settingsRoute = settingsIndex < 0 ? undefined : props.state.routes[settingsIndex];
34 const settingsPage =
35 settingsIndex < 0 ? null : (props.state.routes[settingsIndex + 1]?.name ?? null);
36 const root = { key, settingsOpen, settingsRoute, settingsPage };
37 return Platform.OS === "android" ? (
38 <WorkspaceTabsProvider>
39 <AndroidWorkspaceRootContext value={root}>{props.children}</AndroidWorkspaceRootContext>
40 </WorkspaceTabsProvider>
41 ) : (
42 props.children
43 );