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

thread-inspector-preview.tsx

142 lines5.1 KB

1import {
2 NavigationContainer,
3 NavigationIndependentTree,
4 useIsFocused,
5 useNavigation,
6 type StaticScreenProps,
7} from "@react-navigation/native";
8import { EnvironmentId, ThreadId } from "@t3tools/contracts";
9import { type ReactNode } from "react";
10import { View } from "react-native";
11​
12import { IconButton } from "../../components/ui/IconButton";
13import { SheetHeader } from "../../components/ui/SheetHeader";
14import { NativeInspectorContent } from "../../native/NativeInspectorContent";
15import { createV5StackNavigator } from "../../native/createV5StackNavigator";
16import { NativeStackScreenOptions } from "../../native/StackHeader";
17import { WorkspaceSlotOptionsContext } from "../../native/v5-workspace-context";
18import { useMobileNavigationTheme } from "../../lib/useMobileNavigationTheme";
19import { DevicePreviewScreen } from "../devices/DevicePreviewRouteScreen";
20import {
21 WorkspaceContentWidthContext,
22 WorkspaceSettledContentWidthContext,
23} from "../layout/workspace-content-width";
24import {
25 ThreadInspectorNavigationContext,
26 type ThreadInspectorTarget,
27} from "./thread-inspector-navigation";
28​
29export interface ThreadInspectorPresentation {
30 readonly onClose: () => void;
31 readonly onMaximize?: () => void;
33​
34export function ThreadInspectorHeader(
35 props: ThreadInspectorPresentation & { readonly title: string },
36) {
37 return (
38 <>
39 <NativeStackScreenOptions options={{ headerShown: true, headerTransparent: true }} />
40 <SheetHeader
41 title={props.title}
42 onClose={props.onClose}
43 accessibilityLabel="Close thread preview"
44 trailing={
45 props.onMaximize ? (
46 <IconButton
47 icon="arrow.up.left.and.arrow.down.right"
48 accessibilityLabel="Maximize thread"
49 onPress={props.onMaximize}
50 />
51 ) : undefined
52 }
53 />
54 </>
55 );
57​
58const PreviewStack = createV5StackNavigator<{
59 Thread: { readonly environmentId: string; readonly threadId: string };
60}>();
61​
62export function InspectorContentScope({ children }: { readonly children: ReactNode }) {
63 return (
64 <WorkspaceSlotOptionsContext value={null}>
65 <WorkspaceContentWidthContext value={null}>
66 <WorkspaceSettledContentWidthContext value={null}>
67 {children}
68 </WorkspaceSettledContentWidthContext>
69 </WorkspaceContentWidthContext>
70 </WorkspaceSlotOptionsContext>
71 );
73​
74export function ThreadInspectorPreview(props: {
75 readonly target: ThreadInspectorTarget;
76 readonly onClose: () => void;
77 readonly onOpen: (target: ThreadInspectorTarget) => void;
78 readonly renderThread: (
79 props: StaticScreenProps<{ readonly environmentId: string; readonly threadId: string }> & {
80 readonly inspectorPresentation: ThreadInspectorPresentation;
81 },
82 ) => ReactNode;
83}) {
84 const navigation = useNavigation();
85 const focused = useIsFocused();
86 const theme = useMobileNavigationTheme();
87 const { target } = props;
88 const maximizeDevice = (deviceKey: string | null) => {
89 props.onOpen({ ...target, deviceKey: deviceKey ?? undefined });
90 navigation.navigate("ThreadDevicePreview", {
91 environmentId: target.environmentId,
92 threadId: target.threadId,
93 deviceKey: deviceKey ?? undefined,
94 });
95 };
96 if (!focused) return null;
97 return (
98 <ThreadInspectorNavigationContext value={props.onOpen}>
99 <InspectorContentScope>
100 <NativeInspectorContent>
101 {target.type === "device" ? (
102 <DevicePreviewScreen
103 key={`${target.environmentId}:${target.threadId}`}
104 environmentId={EnvironmentId.make(target.environmentId)}
105 threadId={ThreadId.make(target.threadId)}
106 initialDeviceKey={target.deviceKey}
107 onClose={props.onClose}
108 onMaximize={maximizeDevice}
109 />
110 ) : (
111 <View className="flex-1 bg-screen">
112 <NavigationIndependentTree>
113 <NavigationContainer theme={theme} onUnhandledAction={navigation.dispatch}>
114 <PreviewStack.Navigator
115 key={`${target.environmentId}:${target.threadId}`}
116 screenOptions={{ headerBackVisible: false }}
117 >
118 <PreviewStack.Screen name="Thread" initialParams={target}>
119 {(screen) =>
120 props.renderThread({
121 route: screen.route,
122 inspectorPresentation: {
123 onClose: props.onClose,
124 onMaximize: () =>
125 navigation.navigate("ThreadInspectorFullscreen", {
126 environmentId: screen.route.params.environmentId,
127 threadId: screen.route.params.threadId,
128 }),
129 },
130 })
131 }
132 </PreviewStack.Screen>
133 </PreviewStack.Navigator>
134 </NavigationContainer>
135 </NavigationIndependentTree>
136 </View>
137 )}
138 </NativeInspectorContent>
139 </InspectorContentScope>
140 </ThreadInspectorNavigationContext>
141 );