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 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 rows = visibleWorkLogActivities(props.activities).map((activity) => ({
131 ...activity,
132 detail: compactActivityDetail(activity.detail),
133 }));
134
135 if (rows.length === 0) {
136 return null;
137 }
138
139 const onlyToolRows = rows.every((row) => row.toolLike);
140
141 return (
142 <View className="-mx-1 mb-1 px-1 py-0">
143 {!onlyToolRows ? (
144 <Text className="px-0.5 pb-0.5 font-t3-medium text-2xs text-foreground-muted opacity-60">
145 work log
146 </Text>
147 ) : null}
148
149 <View className="gap-px">
150 {rows.map((row) => {
151 const expanded = props.expandedRows[row.id] ?? false;
152 const canExpand = row.canExpand;
153 const fullDetail = expanded ? row.getFullDetail() : null;
154 const displayText = row.detail ? `${row.summary} ${row.detail}` : row.summary;
155 const iconIsDestructive = row.icon === "alert" || row.icon === "warning";
156
157 return (
158 <Animated.View
159 key={row.id}
160 {...(isFreshRow(row.createdAt) ? { entering: FadeIn.duration(200) } : {})}
161 >
162 <Pressable
163 accessibilityRole={canExpand ? "button" : undefined}
164 accessibilityLabel={displayText}
165 accessibilityHint={
166 canExpand
167 ? "Double tap to show full details. Long press to copy."
168 : "Long press to copy."
169 }
170 accessibilityState={canExpand ? { expanded } : undefined}
171 hitSlop={4}
172 onPress={() => {
173 if (canExpand) {
174 triggerDisclosureFeedback();
175 props.onToggleRow(row.id);
176 }
177 }}
178 onLongPress={() => props.onCopyRow(row.id, row.getCopyText())}
179 className="rounded-md px-0.5 py-0 active:bg-subtle"
180 >
181 <View className="min-h-8 flex-row items-center gap-1.5">
182 <View className="h-[18px] w-5 shrink-0 items-center justify-center">
183 <SymbolView
184 name={workRowSymbolName(row.icon)}
185 size={13}
186 weight="medium"
187 tintColor={iconIsDestructive ? "#e11d48" : props.iconSubtleColor}
188 type="monochrome"
189 />
190 </View>
191
192 <Text className="min-w-0 flex-1 text-xs text-foreground" numberOfLines={1}>
193 <Text
194 className={cn(
195 "font-t3-medium text-foreground",
196 iconIsDestructive && "text-adaptive-rose-600-400",
197 )}
198 >
199 {row.summary}
200 </Text>
201 {row.detail ? (
202 <Text className="text-foreground-muted opacity-60"> {row.detail}</Text>
203 ) : null}
204 </Text>
205
206 <View className="shrink-0 flex-row items-center gap-px">
207 {props.copiedRowId === row.id ? (
208 <Text className="pr-1 font-t3-medium text-3xs text-adaptive-emerald-600-400">
209 Copied
210 </Text>
211 ) : null}
212 <View className="h-4 w-4 items-center justify-center">
213 {canExpand ? (
214 <SymbolView
215 name={
216 expanded
217 ? { ios: "chevron.up", android: "keyboard_arrow_up" }
218 : { ios: "chevron.down", android: "keyboard_arrow_down" }
219 }
220 size={11}
221 tintColor={props.iconSubtleColor}
222 type="monochrome"
223 />
224 ) : null}
225 </View>
226 <View className="h-4 w-4 items-center justify-center">
227 {row.status ? (
228 <SymbolView
229 name={
230 row.status === "failure"
231 ? { ios: "xmark", android: "close" }
232 : row.status === "success"
233 ? { ios: "checkmark", android: "check" }
234 : { ios: "minus", android: "remove" }
235 }
236 size={11}
237 tintColor={props.iconSubtleColor}
238 type="monochrome"
239 />
240 ) : null}
241 </View>
242 </View>
243 </View>
244 </Pressable>
245
246 {fullDetail ? (
247 <View className="ml-7 border-l border-adaptive-neutral-300-a60-white-a12 pb-1 pl-3 pt-0.5">
248 <ScrollView
249 nestedScrollEnabled
250 directionalLockEnabled
251 showsVerticalScrollIndicator
252 className="max-h-60"
253 contentContainerStyle={{ paddingRight: 8 }}
254 >
255 <Text
256 selectable
257 className="font-mono text-2xs leading-normal text-foreground-muted"
258 >
259 {fullDetail}
260 </Text>
261 </ScrollView>
262 </View>
263 ) : null}
264 </Animated.View>
265 );
266 })}
267 </View>
268 </View>
269 );
270}
271
272export function ThreadWorkGroupToggle(props: {
273 readonly expanded: boolean;
274 readonly hiddenCount: number;
275 readonly iconSubtleColor: import("react-native").ColorValue;
276 readonly onlyToolActivities: boolean;
277 readonly onToggle: () => void;
278}) {
279 const noun = props.onlyToolActivities
280 ? props.hiddenCount === 1
281 ? "tool call"
282 : "tool calls"
283 : props.hiddenCount === 1
284 ? "log entry"
285 : "log entries";
286 const collapsedLabel = `Show ${props.hiddenCount} previous ${noun}`;
287 const expandedLabel = props.onlyToolActivities
288 ? "Show fewer tool calls"
289 : "Show fewer log entries";
290
291 return (
292 <View className="-mx-1 mb-1 px-1 py-0">
293 <Pressable
294 accessibilityRole="button"
295 accessibilityState={{ expanded: props.expanded }}
296 accessibilityLabel={props.expanded ? expandedLabel : collapsedLabel}
297 hitSlop={4}
298 onPress={() => {
299 void Haptics.selectionAsync();
300 props.onToggle();
301 }}
302 className="min-h-8 flex-row items-center gap-1.5 rounded-md px-0.5 py-0 active:bg-subtle"
303 >
304 <View className="h-[18px] w-5 items-center justify-center">
305 <SymbolView
306 name={
307 props.expanded
308 ? { ios: "chevron.up", android: "keyboard_arrow_up" }
309 : { ios: "chevron.down", android: "keyboard_arrow_down" }
310 }
311 size={12}
312 tintColor={props.iconSubtleColor}
313 type="monochrome"
314 />
315 </View>
316 <Text className="font-t3-medium text-xs text-foreground opacity-80">
317 {props.expanded ? expandedLabel : `+${props.hiddenCount} previous ${noun}`}
318 </Text>
319 </Pressable>
320 </View>
321 );
322}