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

ThreadActivityInspector.tsx

199 lines7.5 KB

1import * as Haptics from "expo-haptics";
2import { SymbolView } from "expo-symbols";
3import type { EnvironmentId } from "@t3tools/contracts";
4import { useRouter } from "expo-router";
5import { useMemo, useState } from "react";
6import { Linking, Pressable, ScrollView, type ColorValue, View } from "react-native";
7​
8import { AppText as Text } from "../../components/AppText";
9import type { ThreadFeedActivity } from "../../lib/threadActivity";
10import { buildThreadActivityInspector } from "../../lib/threadActivityInspector";
11import { buildThreadFilesNavigation } from "../../lib/routes";
12import { resolveWorkspaceRelativeFilePath } from "../files/filePath";
13import { threadEnvironment } from "../../state/threads";
14import { useAtomCommand } from "../../state/use-atom-command";
15import { useV2ItemSupport } from "../../state/v2-item-support";
16​
17export function ThreadActivityInspector(props: {
18 readonly activity: ThreadFeedActivity;
19 readonly environmentId: EnvironmentId;
20 readonly iconColor: ColorValue;
21 readonly workspaceRoot?: string | null;
22}) {
23 const router = useRouter();
24 const row = props.activity.projectedItem;
25 const support = useV2ItemSupport({
26 environmentId: props.environmentId,
27 sourceThreadId: row.sourceThreadId,
28 sourceItemId: row.sourceItemId,
29 });
30 const model = useMemo(
31 () => buildThreadActivityInspector(props.activity, support),
32 [props.activity, support],
33 );
34 const revertCheckpoint = useAtomCommand(threadEnvironment.revertCheckpoint, {
35 label: "checkpoint rollback",
36 reportFailure: true,
37 });
38 const [rollingBack, setRollingBack] = useState(false);
39​
40 return (
41 <View className="gap-3">
42 <View className="flex-row flex-wrap gap-x-4 gap-y-2 rounded-lg border border-neutral-300/50 bg-black/[0.025] p-2.5 dark:border-white/[0.1] dark:bg-white/[0.025]">
43 {model.fields.map((field) => (
44 <View key={`${field.label}:${field.value}`} className="min-w-[42%] flex-1 gap-0.5">
45 <Text className="font-t3-medium text-3xs uppercase tracking-wide text-foreground-muted opacity-60">
46 {field.label}
47 </Text>
48 <Text selectable className="text-2xs leading-4 text-foreground">
49 {field.value}
50 </Text>
51 </View>
52 ))}
53 </View>
54​
55 {model.blocks.map((block) => (
56 <View key={`${block.label}:${block.value}`} className="gap-1">
57 <Text className="font-t3-medium text-3xs uppercase tracking-wide text-foreground-muted opacity-60">
58 {block.label}
59 </Text>
60 <ScrollView
61 nestedScrollEnabled
62 directionalLockEnabled
63 showsVerticalScrollIndicator
64 style={{ maxHeight: 240 }}
65 contentContainerStyle={{ paddingRight: 8 }}
66 >
67 <Text
68 selectable
69 className="text-2xs leading-[17px] text-foreground-muted"
70 style={block.monospaced ? { fontFamily: "ui-monospace" } : undefined}
71 >
72 {block.value}
73 </Text>
74 </ScrollView>
75 </View>
76 ))}
77​
78 {model.fileLinks.length > 0 ? (
79 <View className="gap-1">
80 <Text className="font-t3-medium text-3xs uppercase tracking-wide text-foreground-muted opacity-60">
81 Files
82 </Text>
83 {model.fileLinks.map((link) => {
84 const relativePath =
85 resolveWorkspaceRelativeFilePath(props.workspaceRoot, link.path) ??
86 (link.path.startsWith("/") ? null : link.path);
87 return (
88 <Pressable
89 key={`${link.path}:${link.line ?? 0}:${link.label}`}
90 accessibilityRole={relativePath ? "link" : undefined}
91 disabled={relativePath === null}
92 onPress={() => {
93 if (!relativePath) return;
94 void Haptics.selectionAsync();
95 router.push(
96 buildThreadFilesNavigation(
97 { environmentId: props.environmentId, threadId: row.sourceThreadId },
98 relativePath,
99 link.line,
100 ),
101 );
102 }}
103 className="min-h-9 flex-row items-center gap-2 rounded-md border border-neutral-300/50 px-2.5 py-1.5 dark:border-white/[0.1]"
104 >
105 <SymbolView
106 name="doc.text"
107 size={13}
108 tintColor={props.iconColor}
109 type="monochrome"
110 />
111 <Text className="min-w-0 flex-1 text-2xs leading-4 text-foreground">
112 {link.label}
113 </Text>
114 </Pressable>
115 );
116 })}
117 </View>
118 ) : null}
119​
120 {model.webLinks.length > 0 ? (
121 <View className="gap-1">
122 <Text className="font-t3-medium text-3xs uppercase tracking-wide text-foreground-muted opacity-60">
123 Sources
124 </Text>
125 {model.webLinks.map((link) => (
126 <Pressable
127 key={link.url}
128 accessibilityRole="link"
129 onPress={() => void Linking.openURL(link.url)}
130 className="min-h-9 flex-row items-center gap-2 rounded-md border border-neutral-300/50 px-2.5 py-1.5 dark:border-white/[0.1]"
131 >
132 <SymbolView
133 name="arrow.up.right.square"
134 size={13}
135 tintColor={props.iconColor}
136 type="monochrome"
137 />
138 <Text className="min-w-0 flex-1 text-2xs leading-4 text-foreground">
139 {link.label}
140 </Text>
141 </Pressable>
142 ))}
143 </View>
144 ) : null}
145​
146 {model.rollbackTarget ? (
147 <Pressable
148 accessibilityRole="button"
149 accessibilityLabel="Roll back to this checkpoint"
150 disabled={rollingBack}
151 onPress={() => {
152 setRollingBack(true);
153 void Haptics.selectionAsync();
154 void revertCheckpoint({
155 environmentId: props.environmentId,
156 input: {
157 threadId: model.rollbackTarget!.threadId,
158 checkpointId: model.rollbackTarget!.checkpointId,
159 scopeId: model.rollbackTarget!.scopeId,
160 },
161 }).finally(() => setRollingBack(false));
162 }}
163 className="min-h-10 flex-row items-center justify-center gap-2 rounded-lg border border-neutral-300/60 px-3 py-2 dark:border-white/[0.12]"
164 >
165 <SymbolView
166 name={rollingBack ? "hourglass" : "arrow.counterclockwise"}
167 size={14}
168 tintColor={props.iconColor}
169 type="monochrome"
170 />
171 <Text className="font-t3-medium text-xs text-foreground">
172 {rollingBack ? "Rolling back…" : "Roll back to checkpoint"}
173 </Text>
174 </Pressable>
175 ) : null}
176​
177 <View className="gap-1 border-t border-neutral-300/50 pt-2 dark:border-white/[0.1]">
178 <Text className="font-t3-medium text-3xs uppercase tracking-wide text-foreground-muted opacity-60">
179 Structured details
180 </Text>
181 <ScrollView
182 nestedScrollEnabled
183 directionalLockEnabled
184 showsVerticalScrollIndicator
185 style={{ maxHeight: 280 }}
186 contentContainerStyle={{ paddingRight: 8 }}
187 >
188 <Text
189 selectable
190 className="text-2xs leading-[17px] text-foreground-muted"
191 style={{ fontFamily: "ui-monospace" }}
192 >
193 {model.structuredDetails}
194 </Text>
195 </ScrollView>
196 </View>
197 </View>
198 );