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

McpAppFullscreenScreen.tsx

150 lines5.6 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 { useCallback, useEffect, useMemo, 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 [exitRequested, setExitRequested] = useState(false);
68 const onClose = useCallback(() => {
69 if (navigation.isFocused()) navigation.goBack();
70 else setExitRequested(true);
71 }, [navigation]);
72 const [size, setSize] = useState({ width: 0, height: 0 });
73 const params = route.params;
74 const environmentId = EnvironmentId.make(params.environmentId);
75 const threadId = ThreadId.make(params.threadId);
76 const itemId = TurnItemId.make(params.itemId);
77 const conversationThreadId = useConversationThreadId(
78 environmentId,
79 threadId,
80 params.conversationThreadId,
81 );
82 const detail = useEnvironmentQuery(
83 orchestrationEnvironment.turnItem({
84 environmentId,
85 input: {
86 threadId,
87 itemId,
88 ...(params.revision === undefined ? {} : { revision: params.revision }),
89 },
90 }),
91 );
92 const item = detail.data?.item;
93 // Keep one reference per app; refetches must not rebuild the host.
94 const derived = item?.type === "dynamic_tool" ? mcpAppFromToolItem(item) : undefined;
95 const [app, setApp] = useState(derived);
96 if (derived?.attachmentId !== app?.attachmentId) setApp(derived);
97 const conversation = useThreadShell(scopeThreadRef(environmentId, conversationThreadId));
98 const awaitingUser =
99 conversation?.hasPendingApprovals === true || conversation?.hasPendingUserInput === true;
100 useEffect(() => {
101 if ((awaitingUser || exitRequested) && focused) navigation.goBack();
102 }, [awaitingUser, exitRequested, focused, navigation]);
103​
104 return (
105 <View className="flex-1 bg-screen" style={{ paddingTop: insets.top }}>
106 <View className="h-11 flex-row items-center justify-between px-3">
107 <Text className="text-base font-semibold text-foreground" numberOfLines={1}>
108 {app?.server ?? "App"}
109 </Text>
110 <Pressable
111 accessibilityRole="button"
112 accessibilityLabel="Exit full screen"
113 hitSlop={8}
114 onPress={onClose}
115 >
116 <SymbolView name="xmark" size={18} tintColor="gray" />
117 </Pressable>
118 </View>
119 <View
120 className="flex-1"
121 style={{ paddingBottom: insets.bottom }}
122 onLayout={(event) =>
123 setSize({
124 width: Math.round(event.nativeEvent.layout.width),
125 height: Math.round(event.nativeEvent.layout.height - insets.bottom),
126 })
127 }
128 >
129 {app === undefined ? (
130 detail.data === undefined ? null : (
131 <Text className="m-6 text-sm text-foreground-muted">This app cannot be shown.</Text>
132 )
133 ) : size.width > 0 ? (
134 <ThreadMcpApp
135 environmentId={environmentId}
136 threadId={threadId}
137 conversationThreadId={conversationThreadId}
138 itemId={itemId}
139 revision={params.revision ?? (item == null ? "" : turnItemDetailRevision(item))}
140 app={app}
141 width={size.width}
142 height={size.height}
143 displayMode="fullscreen"
144 onExitFullscreen={onClose}
145 />
146 ) : null}
147 </View>
148 </View>
149 );