Open History to see the last commit
1import * as Haptics from "expo-haptics";
2import { type AppSymbolName, SymbolView } from "../../components/AppSymbol";
3import { LayoutAnimation, Pressable, ScrollView, 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 { useThemeColor } from "../../lib/useThemeColor";
11import Animated, { FadeIn } from "react-native-reanimated";
12
13const WORK_LOG_LAYOUT_ANIMATION = {
14 duration: 180,
15 create: {
16 type: LayoutAnimation.Types.easeInEaseOut,
17 property: LayoutAnimation.Properties.opacity,
18 },
19 update: { type: LayoutAnimation.Types.easeInEaseOut },
20 delete: {
21 type: LayoutAnimation.Types.easeInEaseOut,
22 property: LayoutAnimation.Properties.opacity,
23 },
24} as const;
25
26function triggerDisclosureFeedback() {
27 LayoutAnimation.configureNext(WORK_LOG_LAYOUT_ANIMATION);
28 void Haptics.selectionAsync();
29}
30
31function stripShellWrapper(value: string): string {
32 const trimmed = value.trim();
33 const match = trimmed.match(/^\/bin\/zsh -lc ['"]?([\s\S]*?)['"]?$/);
34 return (match?.[1] ?? trimmed).trim();
35}
36
37function compactActivityDetail(detail: string | null): string | null {
38 if (!detail) {
39 return null;
40 }
41
42 const cleaned = stripShellWrapper(detail).replace(/\s+/g, " ").trim();
43 return cleaned.length > 0 ? cleaned : null;
44}
45
46function workRowSymbolName(icon: ThreadFeedActivity["icon"]): AppSymbolName {
47 switch (icon) {
48 case "agent":
49 return { ios: "sparkles", android: "auto_awesome" };
50 case "alert":
51 return { ios: "exclamationmark.triangle", android: "error" };
52 case "check":
53 return { ios: "checkmark", android: "check" };
54 case "command":
55 return { ios: "terminal", android: "terminal" };
56 case "edit":
57 return { ios: "square.and.pencil", android: "edit" };
58 case "eye":
59 return { ios: "eye", android: "visibility" };
60 case "globe":
61 return { ios: "globe", android: "public" };
62 case "hammer":
63 return { ios: "hammer", android: "construction" };
64 case "message":
65 return { ios: "bubble.left", android: "chat_bubble" };
66 case "warning":
67 return { ios: "xmark", android: "close" };
68 case "wrench":
69 return { ios: "wrench", android: "build" };
70 case "zap":
71 return { ios: "bolt", android: "bolt" };
72 }
73}
74
75// Entering fades only for rows created moments ago: rows remount whenever the
76// list scrolls them back into view, and old rows must not replay an entrance.
77const FRESH_ROW_WINDOW_MS = 3_000;
78function isFreshRow(createdAt: string): boolean {
79 const timestamp = Date.parse(createdAt);
80 return Number.isFinite(timestamp) && Date.now() - timestamp < FRESH_ROW_WINDOW_MS;
81}
82
83// Tool-like activities with a neutral status carry no signal worth a row.
84export function visibleWorkLogActivities(
85 activities: ReadonlyArray<ThreadFeedActivity>,
86): ReadonlyArray<ThreadFeedActivity> {
87 return activities.filter((activity) => !(activity.toolLike && activity.status === "neutral"));
88}
89
90// Pre-measurement heights for the feed's getFixedItemSize. Collapsed work-log
91// rows are single-line (numberOfLines={1}) inside a min-height that stays
92// taller than the text at every supported base font size (text-xs reaches
93// 23px at the 22pt maximum, under the 32px min-h-8), so row height is
94// deterministic. The "work log" label has no such clamp — its height follows
95// the scaled text-2xs line height. Values mirror the classNames below — keep
96// them in sync; a mismatch only costs a one-time correction on measure.
97const WORK_ROW_HEIGHT = 32; // min-h-8
98const WORK_ROW_GAP = 1; // gap-px
99const WORK_LOG_HEADER_PADDING = 2; // pb-0.5 under the "work log" label
100const WORK_LOG_BOTTOM_MARGIN = 4; // mb-1
101
102export const WORK_GROUP_TOGGLE_HEIGHT = 36; // min-h-8 (32) + mb-1 (4)
103
104export function collapsedWorkLogHeight(
105 activities: ReadonlyArray<ThreadFeedActivity>,
106 baseFontSize: number,
107): number {
108 const rows = visibleWorkLogActivities(activities);
109 if (rows.length === 0) {
110 return 0;
111 }
112 const onlyToolRows = rows.every((row) => row.toolLike);
113 const headerHeight =
114 scaledTypographyLineHeight(MOBILE_TYPOGRAPHY.caption, baseFontSize) + WORK_LOG_HEADER_PADDING;
115 return (
116 WORK_LOG_BOTTOM_MARGIN +
117 (onlyToolRows ? 0 : headerHeight) +
118 rows.length * WORK_ROW_HEIGHT +
119 (rows.length - 1) * WORK_ROW_GAP
120 );
121}
122
123export function ThreadWorkLog(props: {
124 readonly activities: ReadonlyArray<ThreadFeedActivity>;
125 readonly copiedRowId: string | null;
126 readonly expandedRows: Readonly<Record<string, boolean>>;
127 readonly iconSubtleColor: import("react-native").ColorValue;
128 readonly onCopyRow: (rowId: string, value: string) => void;
129 readonly onToggleRow: (rowId: string) => void;
130}) {
131 const pressedBackground = useThemeColor("--color-subtle");
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 pressedBackground = useThemeColor("--color-subtle");
285 const noun = props.onlyToolActivities
286 ? props.hiddenCount === 1
287 ? "tool call"
288 : "tool calls"
289 : props.hiddenCount === 1
290 ? "log entry"
291 : "log entries";
292 const collapsedLabel = `Show ${props.hiddenCount} previous ${noun}`;
293 const expandedLabel = props.onlyToolActivities
294 ? "Show fewer tool calls"
295 : "Show fewer log entries";
296
297 return (
298 <View className="-mx-1 mb-1 px-1 py-0">
299 <Pressable
300 accessibilityRole="button"
301 accessibilityState={{ expanded: props.expanded }}
302 accessibilityLabel={props.expanded ? expandedLabel : collapsedLabel}
303 hitSlop={4}
304 onPress={() => {
305 void Haptics.selectionAsync();
306 props.onToggle();
307 }}
308 style={({ pressed }) => ({
309 backgroundColor: pressed ? pressedBackground : "transparent",
310 })}
311 className="min-h-8 flex-row items-center gap-1.5 rounded-md px-0.5 py-0"
312 >
313 <View className="h-[18px] w-5 items-center justify-center">
314 <SymbolView
315 name={
316 props.expanded
317 ? { ios: "chevron.up", android: "keyboard_arrow_up" }
318 : { ios: "chevron.down", android: "keyboard_arrow_down" }
319 }
320 size={12}
321 tintColor={props.iconSubtleColor}
322 type="monochrome"
323 />
324 </View>
325 <Text className="font-t3-medium text-xs text-foreground opacity-80">
326 {props.expanded ? expandedLabel : `+${props.hiddenCount} previous ${noun}`}
327 </Text>
328 </Pressable>
329 </View>
330 );
331}