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

McpAppFullscreenScreen.tsx

159 lines5.8 KB

1import { useIsFocused, useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { EnvironmentId, ThreadId, TurnItemId } from "@t3tools/contracts";
3import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4import { turnItemDetailRevision } from "@t3tools/client-runtime/work-log/item-detail";
5import { mcpAppFromToolItem } from "@t3tools/shared/toolOutput";
6import { useEffect, useMemo, useRef, useState } from "react";
7import { Pressable, View } from "react-native";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9​
10import { AppText as Text } from "../../components/ui/AppText";
11import { SymbolView } from "../../components/ui/AppSymbol";
12import { orchestrationEnvironment } from "../../state/orchestration";
13import { scopeThreadRef } from "@t3tools/client-runtime/environment";
14import { useThreadShell, useThreadShells } from "../../state/entities";
15import { useEnvironmentQuery } from "../../state/query";
16import { ThreadMcpApp } from "./McpAppWebView";
17​
18type McpAppFullscreenScreenProps = StaticScreenProps<{
19 readonly environmentId: string;
20 readonly threadId: string;
21 readonly conversationThreadId?: string;
22 readonly itemId: string;
23 readonly revision?: string;
24}>;
25​
26function useConversationThreadId(
27 environmentId: EnvironmentId,
28 threadId: ThreadId,
29 requested: string | undefined,
30): ThreadId {
31 const shells = useThreadShells();
32 return useMemo(
33 () =>
34 requested !== undefined && descendsFrom(shells, environmentId, requested, threadId)
35 ? ThreadId.make(requested)
36 : threadId,
37 [shells, environmentId, threadId, requested],
38 );
40​
41function descendsFrom(
42 shells: ReadonlyArray<EnvironmentThreadShell>,
43 environmentId: EnvironmentId,
44 threadId: string,
45 ancestorId: ThreadId,
46): boolean {
47 const parents = new Map(
48 shells
49 .filter((shell) => shell.environmentId === environmentId)
50 .filter((shell) => shell.lineage.relationshipToParent === "fork")
51 .map((shell) => [shell.id as string, shell.lineage.parentThreadId]),
52 );
53 let current: string | null | undefined = threadId;
54 for (let depth = 0; depth < 64 && current != null; depth += 1) {
55 if (current === ancestorId) return true;
56 current = parents.get(current);
57 }
58 return false;
60​
61/** Resolve apps from stored tool calls so links cannot supply apps the call never produced. */
62export function McpAppFullscreenScreen({ route }: McpAppFullscreenScreenProps) {
63 const navigation = useNavigation();
64 const insets = useSafeAreaInsets();
65 // Wait for focus before goBack; an exit under another screen must not pop that screen.
66 const focused = useIsFocused();
67 const exitPending = useRef(false);
68 const onClose = () => {
69 if (navigation.isFocused()) {
70 navigation.goBack();
71 return;
72 }
73 if (exitPending.current) return;
74 exitPending.current = true;
75 const unsubscribe = navigation.addListener("focus", () => {
76 unsubscribe();
77 navigation.goBack();
78 });
79 };
80 const [size, setSize] = useState({ width: 0, height: 0 });
81 const params = route.params;
82 const environmentId = EnvironmentId.make(params.environmentId);
83 const threadId = ThreadId.make(params.threadId);
84 const itemId = TurnItemId.make(params.itemId);
85 const conversationThreadId = useConversationThreadId(
86 environmentId,
87 threadId,
88 params.conversationThreadId,
89 );
90 const detail = useEnvironmentQuery(
91 orchestrationEnvironment.turnItem({
92 environmentId,
93 input: {
94 threadId,
95 itemId,
96 ...(params.revision === undefined ? {} : { revision: params.revision }),
97 },
98 }),
99 );
100 const item = detail.data?.item;
101 // Keep one reference per app; refetches must not rebuild the host.
102 const derived = item?.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
103 const [app, setApp] = useState(derived);
104 if (derived?.attachmentId !== app?.attachmentId) setApp(derived);
105 const conversation = useThreadShell(scopeThreadRef(environmentId, conversationThreadId));
106 const awaitingUser =
107 conversation?.hasPendingApprovals === true || conversation?.hasPendingUserInput === true;
108 // A request for the user is server state with no local handler to react in.
109 useEffect(() => {
110 if (awaitingUser && focused && !exitPending.current) navigation.goBack();
111 }, [awaitingUser, focused, navigation]);
112​
113 return (
114 <View className="flex-1 bg-screen" style={{ paddingTop: insets.top }}>
115 <View className="h-11 flex-row items-center justify-between px-3">
116 <Text className="text-base font-semibold text-foreground" numberOfLines={1}>
117 {app?.server ?? "App"}
118 </Text>
119 <Pressable
120 accessibilityRole="button"
121 accessibilityLabel="Exit full screen"
122 hitSlop={8}
123 onPress={onClose}
124 >
125 <SymbolView name="xmark" size={18} tintColor="gray" />
126 </Pressable>
127 </View>
128 <View
129 className="flex-1"
130 style={{ paddingBottom: insets.bottom }}
131 onLayout={(event) =>
132 setSize({
133 width: Math.round(event.nativeEvent.layout.width),
134 height: Math.round(event.nativeEvent.layout.height - insets.bottom),
135 })
136 }
137 >
138 {app === undefined ? (
139 detail.data === undefined ? null : (
140 <Text className="m-6 text-sm text-foreground-muted">This app cannot be shown.</Text>
141 )
142 ) : size.width > 0 ? (
143 <ThreadMcpApp
144 environmentId={environmentId}
145 threadId={threadId}
146 conversationThreadId={conversationThreadId}
147 itemId={itemId}
148 revision={params.revision ?? (item == null ? "" : turnItemDetailRevision(item))}
149 app={app}
150 width={size.width}
151 height={size.height}
152 displayMode="fullscreen"
153 onExitFullscreen={onClose}
154 />
155 ) : null}
156 </View>
157 </View>
158 );