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

thread-work-log.tsx

289 lines10.8 KB

1import * as Haptics from "expo-haptics";
2import { type AppSymbolName, SymbolView } from "../../components/AppSymbol";
3import { LayoutAnimation, Pressable, ScrollView, useColorScheme, View } from "react-native";
4​
5import { AppText as Text } from "../../components/AppText";
6import { cn } from "../../lib/cn";
7import type { ThreadFeedActivity } from "../../lib/threadActivity";
8import Animated, { FadeIn } from "react-native-reanimated";
9​
10const WORK_LOG_LAYOUT_ANIMATION = {
11 duration: 180,
12 create: {
13 type: LayoutAnimation.Types.easeInEaseOut,
14 property: LayoutAnimation.Properties.opacity,
15 },
16 update: { type: LayoutAnimation.Types.easeInEaseOut },
17 delete: {
18 type: LayoutAnimation.Types.easeInEaseOut,
19 property: LayoutAnimation.Properties.opacity,
20 },
21} as const;
22​
23function triggerDisclosureFeedback() {
24 LayoutAnimation.configureNext(WORK_LOG_LAYOUT_ANIMATION);
25 void Haptics.selectionAsync();
27​
28function stripShellWrapper(value: string): string {
29 const trimmed = value.trim();
30 const match = trimmed.match(/^\/bin\/zsh -lc ['"]?([\s\S]*?)['"]?$/);
31 return (match?.[1] ?? trimmed).trim();
33​
34function compactActivityDetail(detail: string | null): string | null {
35 if (!detail) {
36 return null;
37 }
38​
39 const cleaned = stripShellWrapper(detail).replace(/\s+/g, " ").trim();
40 return cleaned.length > 0 ? cleaned : null;
42​
43function workRowSymbolName(icon: ThreadFeedActivity["icon"]): AppSymbolName {
44 switch (icon) {
45 case "agent":
46 return { ios: "sparkles", android: "auto_awesome" };
47 case "alert":
48 return { ios: "exclamationmark.triangle", android: "error" };
49 case "check":
50 return { ios: "checkmark", android: "check" };
51 case "command":
52 return { ios: "terminal", android: "terminal" };
53 case "edit":
54 return { ios: "square.and.pencil", android: "edit" };
55 case "eye":
56 return { ios: "eye", android: "visibility" };
57 case "globe":
58 return { ios: "globe", android: "public" };
59 case "hammer":
60 return { ios: "hammer", android: "construction" };
61 case "message":
62 return { ios: "bubble.left", android: "chat_bubble" };
63 case "warning":
64 return { ios: "xmark", android: "close" };
65 case "wrench":
66 return { ios: "wrench", android: "build" };
67 case "zap":
68 return { ios: "bolt", android: "bolt" };
69 }
71​
72// Entering fades only for rows created moments ago: rows remount whenever the
73// list scrolls them back into view, and old rows must not replay an entrance.
74const FRESH_ROW_WINDOW_MS = 3_000;
75function isFreshRow(createdAt: string): boolean {
76 const timestamp = Date.parse(createdAt);
77 return Number.isFinite(timestamp) && Date.now() - timestamp < FRESH_ROW_WINDOW_MS;
79​
80export function ThreadWorkLog(props: {
81 readonly activities: ReadonlyArray<ThreadFeedActivity>;
82 readonly copiedRowId: string | null;
83 readonly expandedRows: Readonly<Record<string, boolean>>;
84 readonly iconSubtleColor: import("react-native").ColorValue;
85 readonly onCopyRow: (rowId: string, value: string) => void;
86 readonly onToggleRow: (rowId: string) => void;
87}) {
88 const colorScheme = useColorScheme();
89 const pressedBackground = colorScheme === "dark" ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.035)";
90 const rows = props.activities
91 .filter((activity) => !(activity.toolLike && activity.status === "neutral"))
92 .map((activity) => ({ ...activity, detail: compactActivityDetail(activity.detail) }));
93​
94 if (rows.length === 0) {
95 return null;
96 }
97​
98 const onlyToolRows = rows.every((row) => row.toolLike);
99​
100 return (
101 <View className="-mx-1 mb-1 px-1 py-0">
102 {!onlyToolRows ? (
103 <Text className="px-0.5 pb-0.5 font-t3-medium text-2xs text-foreground-muted opacity-60">
104 work log
105 </Text>
106 ) : null}
107​
108 <View className="gap-px">
109 {rows.map((row) => {
110 const expanded = props.expandedRows[row.id] ?? false;
111 const canExpand = row.canExpand;
112 const fullDetail = expanded ? row.getFullDetail() : null;
113 const displayText = row.detail ? `${row.summary} ${row.detail}` : row.summary;
114 const iconIsDestructive = row.icon === "alert" || row.icon === "warning";
115​
116 return (
117 <Animated.View
118 key={row.id}
119 {...(isFreshRow(row.createdAt) ? { entering: FadeIn.duration(200) } : {})}
120 >
121 <Pressable
122 accessibilityRole={canExpand ? "button" : undefined}
123 accessibilityLabel={displayText}
124 accessibilityHint={
125 canExpand
126 ? "Double tap to show full details. Long press to copy."
127 : "Long press to copy."
128 }
129 accessibilityState={canExpand ? { expanded } : undefined}
130 hitSlop={4}
131 onPress={() => {
132 if (canExpand) {
133 triggerDisclosureFeedback();
134 props.onToggleRow(row.id);
135 }
136 }}
137 onLongPress={() => props.onCopyRow(row.id, row.getCopyText())}
138 style={({ pressed }) => ({
139 backgroundColor: pressed ? pressedBackground : "transparent",
140 })}
141 className="rounded-md px-0.5 py-0"
142 >
143 <View className="min-h-8 flex-row items-center gap-1.5">
144 <View className="h-[18px] w-5 shrink-0 items-center justify-center">
145 <SymbolView
146 name={workRowSymbolName(row.icon)}
147 size={13}
148 weight="medium"
149 tintColor={iconIsDestructive ? "#e11d48" : props.iconSubtleColor}
150 type="monochrome"
151 />
152 </View>
153​
154 <Text className="min-w-0 flex-1 text-xs text-foreground" numberOfLines={1}>
155 <Text
156 className={cn(
157 "font-t3-medium text-foreground",
158 iconIsDestructive && "text-rose-600 dark:text-rose-400",
159 )}
160 >
161 {row.summary}
162 </Text>
163 {row.detail ? (
164 <Text className="text-foreground-muted opacity-60"> {row.detail}</Text>
165 ) : null}
166 </Text>
167​
168 <View className="shrink-0 flex-row items-center gap-px">
169 {props.copiedRowId === row.id ? (
170 <Text className="pr-1 font-t3-medium text-3xs text-emerald-600 dark:text-emerald-400">
171 Copied
172 </Text>
173 ) : null}
174 <View className="h-4 w-4 items-center justify-center">
175 {canExpand ? (
176 <SymbolView
177 name={
178 expanded
179 ? { ios: "chevron.up", android: "keyboard_arrow_up" }
180 : { ios: "chevron.down", android: "keyboard_arrow_down" }
181 }
182 size={11}
183 tintColor={props.iconSubtleColor}
184 type="monochrome"
185 />
186 ) : null}
187 </View>
188 <View className="h-4 w-4 items-center justify-center">
189 {row.status ? (
190 <SymbolView
191 name={
192 row.status === "failure"
193 ? { ios: "xmark", android: "close" }
194 : row.status === "success"
195 ? { ios: "checkmark", android: "check" }
196 : { ios: "minus", android: "remove" }
197 }
198 size={11}
199 tintColor={row.status === "failure" ? "#e11d48" : props.iconSubtleColor}
200 type="monochrome"
201 />
202 ) : null}
203 </View>
204 </View>
205 </View>
206 </Pressable>
207​
208 {fullDetail ? (
209 <View className="ml-7 border-l border-neutral-300/60 pb-1 pl-3 pt-0.5 dark:border-white/[0.12]">
210 <ScrollView
211 nestedScrollEnabled
212 directionalLockEnabled
213 showsVerticalScrollIndicator
214 className="max-h-60"
215 contentContainerStyle={{ paddingRight: 8 }}
216 >
217 <Text
218 selectable
219 className="font-mono text-2xs leading-normal text-foreground-muted"
220 >
221 {fullDetail}
222 </Text>
223 </ScrollView>
224 </View>
225 ) : null}
226 </Animated.View>
227 );
228 })}
229 </View>
230 </View>
231 );
233​
234export function ThreadWorkGroupToggle(props: {
235 readonly expanded: boolean;
236 readonly hiddenCount: number;
237 readonly iconSubtleColor: import("react-native").ColorValue;
238 readonly onlyToolActivities: boolean;
239 readonly onToggle: () => void;
240}) {
241 const colorScheme = useColorScheme();
242 const pressedBackground = colorScheme === "dark" ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.035)";
243 const noun = props.onlyToolActivities
244 ? props.hiddenCount === 1
245 ? "tool call"
246 : "tool calls"
247 : props.hiddenCount === 1
248 ? "log entry"
249 : "log entries";
250 const collapsedLabel = `Show ${props.hiddenCount} previous ${noun}`;
251 const expandedLabel = props.onlyToolActivities
252 ? "Show fewer tool calls"
253 : "Show fewer log entries";
254​
255 return (
256 <View className="-mx-1 mb-1 px-1 py-0">
257 <Pressable
258 accessibilityRole="button"
259 accessibilityState={{ expanded: props.expanded }}
260 accessibilityLabel={props.expanded ? expandedLabel : collapsedLabel}
261 hitSlop={4}
262 onPress={() => {
263 void Haptics.selectionAsync();
264 props.onToggle();
265 }}
266 style={({ pressed }) => ({
267 backgroundColor: pressed ? pressedBackground : "transparent",
268 })}
269 className="min-h-8 flex-row items-center gap-1.5 rounded-md px-0.5 py-0"
270 >
271 <View className="h-[18px] w-5 items-center justify-center">
272 <SymbolView
273 name={
274 props.expanded
275 ? { ios: "chevron.up", android: "keyboard_arrow_up" }
276 : { ios: "chevron.down", android: "keyboard_arrow_down" }
277 }
278 size={12}
279 tintColor={props.iconSubtleColor}
280 type="monochrome"
281 />
282 </View>
283 <Text className="font-t3-medium text-xs text-foreground opacity-80">
284 {props.expanded ? expandedLabel : `+${props.hiddenCount} previous ${noun}`}
285 </Text>
286 </Pressable>
287 </View>
288 );