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