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

thread-work-log.tsx

288 lines10.7 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.fullDetail !== null;
112 const displayText = row.detail ? `${row.summary} ${row.detail}` : row.summary;
113 const iconIsDestructive = row.icon === "alert" || row.icon === "warning";
114​
115 return (
116 <Animated.View
117 key={row.id}
118 {...(isFreshRow(row.createdAt) ? { entering: FadeIn.duration(200) } : {})}
119 >
120 <Pressable
121 accessibilityRole={canExpand ? "button" : undefined}
122 accessibilityLabel={displayText}
123 accessibilityHint={
124 canExpand
125 ? "Double tap to show full details. Long press to copy."
126 : "Long press to copy."
127 }
128 accessibilityState={canExpand ? { expanded } : undefined}
129 hitSlop={4}
130 onPress={() => {
131 if (canExpand) {
132 triggerDisclosureFeedback();
133 props.onToggleRow(row.id);
134 }
135 }}
136 onLongPress={() => props.onCopyRow(row.id, row.copyText)}
137 style={({ pressed }) => ({
138 backgroundColor: pressed ? pressedBackground : "transparent",
139 })}
140 className="rounded-md px-0.5 py-0"
141 >
142 <View className="min-h-8 flex-row items-center gap-1.5">
143 <View className="h-[18px] w-5 shrink-0 items-center justify-center">
144 <SymbolView
145 name={workRowSymbolName(row.icon)}
146 size={13}
147 weight="medium"
148 tintColor={iconIsDestructive ? "#e11d48" : props.iconSubtleColor}
149 type="monochrome"
150 />
151 </View>
152​
153 <Text className="min-w-0 flex-1 text-xs text-foreground" numberOfLines={1}>
154 <Text
155 className={cn(
156 "font-t3-medium text-foreground",
157 iconIsDestructive && "text-rose-600 dark:text-rose-400",
158 )}
159 >
160 {row.summary}
161 </Text>
162 {row.detail ? (
163 <Text className="text-foreground-muted opacity-60"> {row.detail}</Text>
164 ) : null}
165 </Text>
166​
167 <View className="shrink-0 flex-row items-center gap-px">
168 {props.copiedRowId === row.id ? (
169 <Text className="pr-1 font-t3-medium text-3xs text-emerald-600 dark:text-emerald-400">
170 Copied
171 </Text>
172 ) : null}
173 <View className="h-4 w-4 items-center justify-center">
174 {canExpand ? (
175 <SymbolView
176 name={
177 expanded
178 ? { ios: "chevron.up", android: "keyboard_arrow_up" }
179 : { ios: "chevron.down", android: "keyboard_arrow_down" }
180 }
181 size={11}
182 tintColor={props.iconSubtleColor}
183 type="monochrome"
184 />
185 ) : null}
186 </View>
187 <View className="h-4 w-4 items-center justify-center">
188 {row.status ? (
189 <SymbolView
190 name={
191 row.status === "failure"
192 ? { ios: "xmark", android: "close" }
193 : row.status === "success"
194 ? { ios: "checkmark", android: "check" }
195 : { ios: "minus", android: "remove" }
196 }
197 size={11}
198 tintColor={row.status === "failure" ? "#e11d48" : props.iconSubtleColor}
199 type="monochrome"
200 />
201 ) : null}
202 </View>
203 </View>
204 </View>
205 </Pressable>
206​
207 {expanded && row.fullDetail ? (
208 <View className="ml-7 border-l border-neutral-300/60 pb-1 pl-3 pt-0.5 dark:border-white/[0.12]">
209 <ScrollView
210 nestedScrollEnabled
211 directionalLockEnabled
212 showsVerticalScrollIndicator
213 className="max-h-60"
214 contentContainerStyle={{ paddingRight: 8 }}
215 >
216 <Text
217 selectable
218 className="font-mono text-2xs leading-normal text-foreground-muted"
219 >
220 {row.fullDetail}
221 </Text>
222 </ScrollView>
223 </View>
224 ) : null}
225 </Animated.View>
226 );
227 })}
228 </View>
229 </View>
230 );
232​
233export function ThreadWorkGroupToggle(props: {
234 readonly expanded: boolean;
235 readonly hiddenCount: number;
236 readonly iconSubtleColor: import("react-native").ColorValue;
237 readonly onlyToolActivities: boolean;
238 readonly onToggle: () => void;
239}) {
240 const colorScheme = useColorScheme();
241 const pressedBackground = colorScheme === "dark" ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.035)";
242 const noun = props.onlyToolActivities
243 ? props.hiddenCount === 1
244 ? "tool call"
245 : "tool calls"
246 : props.hiddenCount === 1
247 ? "log entry"
248 : "log entries";
249 const collapsedLabel = `Show ${props.hiddenCount} previous ${noun}`;
250 const expandedLabel = props.onlyToolActivities
251 ? "Show fewer tool calls"
252 : "Show fewer log entries";
253​
254 return (
255 <View className="-mx-1 mb-1 px-1 py-0">
256 <Pressable
257 accessibilityRole="button"
258 accessibilityState={{ expanded: props.expanded }}
259 accessibilityLabel={props.expanded ? expandedLabel : collapsedLabel}
260 hitSlop={4}
261 onPress={() => {
262 void Haptics.selectionAsync();
263 props.onToggle();
264 }}
265 style={({ pressed }) => ({
266 backgroundColor: pressed ? pressedBackground : "transparent",
267 })}
268 className="min-h-8 flex-row items-center gap-1.5 rounded-md px-0.5 py-0"
269 >
270 <View className="h-[18px] w-5 items-center justify-center">
271 <SymbolView
272 name={
273 props.expanded
274 ? { ios: "chevron.up", android: "keyboard_arrow_up" }
275 : { ios: "chevron.down", android: "keyboard_arrow_down" }
276 }
277 size={12}
278 tintColor={props.iconSubtleColor}
279 type="monochrome"
280 />
281 </View>
282 <Text className="font-t3-medium text-xs text-foreground opacity-80">
283 {props.expanded ? expandedLabel : `+${props.hiddenCount} previous ${noun}`}
284 </Text>
285 </Pressable>
286 </View>
287 );