The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useIsFocused, useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 2 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 3 | import { type ReactNode } from "react"; |
| 4 | import { View } from "react-native"; |
| 5 | |
| 6 | import { LocalPreviewScreen } from "../browser/LocalPreviewRouteScreen"; |
| 7 | import { DevicePreviewScreen } from "../devices/DevicePreviewRouteScreen"; |
| 8 | import { |
| 9 | WorkspaceContentWidthContext, |
| 10 | WorkspaceSettledContentWidthContext, |
| 11 | } from "../layout/workspace-content-width"; |
| 12 | import { |
| 13 | ThreadInspectorNavigationContext, |
| 14 | type ThreadInspectorTarget, |
| 15 | } from "./thread-inspector-navigation"; |
| 16 | import { linkedThreadRouteParams } from "./linkedThreadRoute"; |
| 17 | import { LinkedThreadView } from "./LinkedThreadSheet"; |
| 18 | import type { ThreadInspectorPresentation } from "./thread-inspector-header"; |
| 19 | import { ThreadPreviewRoute } from "./thread-preview-route"; |
| 20 | |
| 21 | /** A thread shown beside or over the workspace does not take the workspace's content width. */ |
| 22 | export function InspectorContentScope({ children }: { readonly children: ReactNode }) { |
| 23 | return ( |
| 24 | <WorkspaceContentWidthContext value={null}> |
| 25 | <WorkspaceSettledContentWidthContext value={null}> |
| 26 | {children} |
| 27 | </WorkspaceSettledContentWidthContext> |
| 28 | </WorkspaceContentWidthContext> |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | export function ThreadInspectorPreview(props: { |
| 33 | readonly target: ThreadInspectorTarget; |
| 34 | readonly onClose: () => void; |
| 35 | readonly onOpen: (target: ThreadInspectorTarget) => void; |
| 36 | readonly renderThread: ( |
| 37 | props: StaticScreenProps<{ readonly environmentId: string; readonly threadId: string }> & { |
| 38 | readonly inspectorPresentation: ThreadInspectorPresentation; |
| 39 | }, |
| 40 | ) => ReactNode; |
| 41 | }) { |
| 42 | const navigation = useNavigation(); |
| 43 | const focused = useIsFocused(); |
| 44 | const { target } = props; |
| 45 | const maximizeDevice = (deviceKey: string | null) => { |
| 46 | props.onOpen({ ...target, deviceKey: deviceKey ?? undefined }); |
| 47 | navigation.navigate("ThreadDevicePreview", { |
| 48 | environmentId: target.environmentId, |
| 49 | threadId: target.threadId, |
| 50 | deviceKey: deviceKey ?? undefined, |
| 51 | }); |
| 52 | }; |
| 53 | if (!focused) return null; |
| 54 | return ( |
| 55 | <ThreadInspectorNavigationContext value={props.onOpen}> |
| 56 | <InspectorContentScope> |
| 57 | {target.type === "localPreview" && target.url ? ( |
| 58 | <LocalPreviewScreen |
| 59 | key={`${target.environmentId}:${target.url}`} |
| 60 | environmentId={EnvironmentId.make(target.environmentId)} |
| 61 | url={target.url} |
| 62 | onClose={props.onClose} |
| 63 | onMaximize={(url) => { |
| 64 | props.onOpen({ ...target, url }); |
| 65 | navigation.navigate("ThreadLocalPreview", { |
| 66 | environmentId: target.environmentId, |
| 67 | url, |
| 68 | }); |
| 69 | }} |
| 70 | /> |
| 71 | ) : target.type === "linkedThread" && target.linked ? ( |
| 72 | <LinkedThread |
| 73 | key={`${target.linked.viaEnvironmentId}:${target.environmentId}:${target.threadId}`} |
| 74 | thread={target} |
| 75 | linked={target.linked} |
| 76 | /> |
| 77 | ) : target.type === "device" ? ( |
| 78 | <DevicePreviewScreen |
| 79 | key={`${target.environmentId}:${target.threadId}`} |
| 80 | environmentId={EnvironmentId.make(target.environmentId)} |
| 81 | threadId={ThreadId.make(target.threadId)} |
| 82 | initialDeviceKey={target.deviceKey} |
| 83 | onClose={props.onClose} |
| 84 | onMaximize={maximizeDevice} |
| 85 | /> |
| 86 | ) : ( |
| 87 | <View className="flex-1 bg-screen"> |
| 88 | <ThreadPreviewRoute environmentId={target.environmentId} threadId={target.threadId}> |
| 89 | {(route) => |
| 90 | props.renderThread({ |
| 91 | route, |
| 92 | inspectorPresentation: { |
| 93 | onMaximize: () => |
| 94 | navigation.navigate("ThreadInspectorFullscreen", { |
| 95 | environmentId: route.params.environmentId, |
| 96 | threadId: route.params.threadId, |
| 97 | }), |
| 98 | }, |
| 99 | }) |
| 100 | } |
| 101 | </ThreadPreviewRoute> |
| 102 | </View> |
| 103 | )} |
| 104 | </InspectorContentScope> |
| 105 | </ThreadInspectorNavigationContext> |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** A linked environment's read-only thread in the inspector; Maximize gives it the whole screen. */ |
| 110 | function LinkedThread(props: { |
| 111 | readonly thread: Pick<ThreadInspectorTarget, "environmentId" | "threadId">; |
| 112 | readonly linked: NonNullable<ThreadInspectorTarget["linked"]>; |
| 113 | }) { |
| 114 | const navigation = useNavigation(); |
| 115 | const params = linkedThreadRouteParams( |
| 116 | { |
| 117 | environmentId: EnvironmentId.make(props.thread.environmentId), |
| 118 | threadId: ThreadId.make(props.thread.threadId), |
| 119 | }, |
| 120 | { |
| 121 | viaEnvironmentId: EnvironmentId.make(props.linked.viaEnvironmentId), |
| 122 | label: props.linked.label, |
| 123 | }, |
| 124 | props.linked.title ?? null, |
| 125 | ); |
| 126 | return ( |
| 127 | <LinkedThreadView |
| 128 | target={{ |
| 129 | viaEnvironmentId: EnvironmentId.make(params.viaEnvironmentId), |
| 130 | environmentId: EnvironmentId.make(params.environmentId), |
| 131 | threadId: ThreadId.make(params.threadId), |
| 132 | label: params.label, |
| 133 | title: params.title ?? null, |
| 134 | }} |
| 135 | presentation={{ onMaximize: () => navigation.navigate("LinkedThreadFullscreen", params) }} |
| 136 | /> |
| 137 | ); |
| 138 | } |