@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 566 lines 21.3 KB
Raw
1 import type { SubagentPillSegment } from "@t3tools/client-runtime/state/thread-subagents";
2 import { GlassContainer, GlassView } from "expo-glass-effect";
3 import { useGT } from "gt-react-native";
4 import { Fragment, type ReactNode, useEffect, useRef, useState } from "react";
5 import { type LayoutChangeEvent, Pressable, useWindowDimensions, View } from "react-native";
6 import Animated, {
7 Easing,
8 FadeIn,
9 FadeOut,
10 ReduceMotion,
11 type SharedValue,
12 useAnimatedStyle,
13 useReducedMotion,
14 useSharedValue,
15 withTiming,
16 } from "react-native-reanimated";
17 import { withUniwind } from "uniwind";
18
19 import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
20 import { AppText as Text } from "../../components/ui/AppText";
21 import { SymbolView } from "../../components/ui/AppSymbol";
22 import { ControlPill } from "../../components/ui/ControlPill";
23 import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
24 import { BrowserPreviewButton } from "../browser/browser-preview-button";
25 import { DevicePreviewButton } from "../devices/device-preview-button";
26 import type { FloatingStatusAction, FloatingWorkingStatus } from "./floating-working-status";
27 import { ShimmeringWorkContent } from "./thread-work-log";
28
29 const CONTROL_HEIGHT = 38.5;
30 const CONTROL_GAP = 8;
31 const COMPOSER_CAPSULE_INSET = 6;
32 const GLASS_MERGE_SPACING = 12;
33 const CONTROL_ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
34 const CONTROL_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
35 const CONTROL_TIMING = {
36 duration: 240,
37 easing: Easing.out(Easing.cubic),
38 reduceMotion: ReduceMotion.System,
39 } as const;
40 const CONTROL_ROW_GAP = 14;
41 const CONTROL_SEPARATION = (CONTROL_ROW_GAP + CONTROL_HEIGHT) / 2;
42 const LABEL_ENTERING = FadeIn.duration(160).delay(80).reduceMotion(ReduceMotion.System);
43 const LABEL_EXITING = FadeOut.duration(100).reduceMotion(ReduceMotion.System);
44
45 // Expo reapplies glass after layout or reattachment, restoring material UIKit can lose behind visible labels.
46 const UniwindGlassView = withUniwind(GlassView, {
47 style: { fromClassName: "className" },
48 });
49 const UniwindGlassContainer = withUniwind(GlassContainer, {
50 style: { fromClassName: "className" },
51 });
52 const AnimatedGlassView = Animated.createAnimatedComponent(UniwindGlassView);
53
54 // Segment widths feed back into labelWidth; ignore sub-point changes to avoid rounding-driven ellipsis loops.
55 function settleWidth(next: number) {
56 return (current: number) => (Math.abs(current - next) < 1 ? current : next);
57 }
58
59 const CONTROL_OVERLAY_OFFSET = CONTROL_HEIGHT + CONTROL_GAP - COMPOSER_CAPSULE_INSET;
60 export const FLOATING_WORKING_CONTROL_COVERAGE = CONTROL_OVERLAY_OFFSET + CONTROL_GAP;
61
62 export function FloatingWorkingControl(props: {
63 readonly colorScheme: "light" | "dark";
64 readonly status: FloatingWorkingStatus | null;
65 readonly extensionStatus?: { readonly label: string; readonly onPress: () => void } | null;
66 readonly devicePreview: { readonly count: number; readonly onPress: () => void } | null;
67 readonly browserPreview: { readonly count: number; readonly onPress: () => void } | null;
68 readonly showScrollToEnd: boolean;
69 readonly onScrollToEnd: () => void;
70 readonly agents: SubagentPillSegment | null;
71 readonly onOpenAgents: () => void;
72 readonly queuedCount: number;
73 readonly onOpenQueue: () => void;
74 readonly lift?: SharedValue<number>;
75 }) {
76 const gt = useGT();
77 const { width: windowWidth } = useWindowDimensions();
78 const reducedMotion = useReducedMotion();
79 const [overlayWidth, setOverlayWidth] = useState(windowWidth);
80 const [queueWidth, setQueueWidth] = useState(0);
81 const [actionWidth, setActionWidth] = useState(0);
82 const [extensionWidth, setExtensionWidth] = useState(0);
83 const extensionStatus = props.extensionStatus ?? null;
84 const extensionOnly = props.status?.kind === "extension";
85 const useGlass = NATIVE_LIQUID_GLASS_SUPPORTED && !extensionOnly;
86 const actions: ReadonlyArray<FloatingStatusAction> =
87 props.status?.kind === "goal"
88 ? props.status.actions
89 : props.status?.kind === "stop" && props.status.action !== null
90 ? [props.status.action]
91 : [];
92 const [agentsWidth, setAgentsWidth] = useState(0);
93 const hasQueue = props.queuedCount > 0;
94 const hasPreview = props.devicePreview !== null || props.browserPreview !== null;
95 const [previewWidth, setPreviewWidth] = useState(0);
96 const agents = props.agents;
97 const hasAgents = agents !== null;
98 // Only reserve the scroll button's slot while it is visible.
99 const reservedWidth = props.showScrollToEnd ? CONTROL_HEIGHT + 32 : 16;
100 const availableLabelWidth = Math.max(
101 0,
102 Math.min(overlayWidth, windowWidth) -
103 reservedWidth -
104 (actions.length > 0 ? actionWidth : 0) -
105 (hasQueue ? queueWidth : 0) -
106 (hasAgents ? agentsWidth : 0) -
107 (hasPreview ? previewWidth : 0),
108 );
109 const labelWidth = Math.max(
110 0,
111 availableLabelWidth - (extensionStatus === null ? 0 : extensionWidth),
112 );
113 const separationProgress = useSharedValue(props.showScrollToEnd ? 1 : 0);
114
115 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- animates the Reanimated separation value when scroll-to-end visibility changes
116 useEffect(() => {
117 separationProgress.set(withTiming(props.showScrollToEnd ? 1 : 0, CONTROL_TIMING));
118 }, [props.showScrollToEnd, separationProgress]);
119
120 const lift = props.lift;
121 const liftStyle = useAnimatedStyle(() => ({
122 transform: [{ translateY: -(lift?.value ?? 0) }],
123 }));
124 const arrowTransformStyle = useAnimatedStyle(() => ({
125 transform: [{ translateX: -CONTROL_SEPARATION * (1 - separationProgress.value) }],
126 }));
127 const arrowContentStyle = useAnimatedStyle(() => ({
128 opacity: separationProgress.value,
129 }));
130
131 // Animate an in-flow sizer for native glass layout. Measure labels at fixed width to avoid repeated animation retargeting.
132 const capsuleWidth = useSharedValue<number | null>(null);
133 const measuredWidthRef = useRef<number | null>(null);
134 const handleLabelLayout = (event: LayoutChangeEvent) => {
135 const width = event.nativeEvent.layout.width;
136 if (width === measuredWidthRef.current) {
137 return;
138 }
139 const first = measuredWidthRef.current === null;
140 measuredWidthRef.current = width;
141 capsuleWidth.set(first ? width : withTiming(width, CONTROL_TIMING));
142 };
143 const hasStatus = props.status !== null;
144 const hasCapsule = hasStatus || hasQueue || hasAgents || hasPreview;
145 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the Reanimated capsule width value and measured width when the status goes away
146 useEffect(() => {
147 if (!hasStatus) {
148 measuredWidthRef.current = null;
149 capsuleWidth.set(null);
150 }
151 }, [capsuleWidth, hasStatus]);
152 const capsuleStyle = useAnimatedStyle(() => ({
153 transform: [{ translateX: CONTROL_SEPARATION * (1 - separationProgress.value) }],
154 }));
155 const capsuleSizerStyle = useAnimatedStyle(() => ({
156 width: capsuleWidth.value ?? 0,
157 }));
158
159 if (!hasCapsule && !props.showScrollToEnd) {
160 return null;
161 }
162
163 const previewCompact =
164 hasStatus ||
165 hasAgents ||
166 hasQueue ||
167 (props.devicePreview !== null && props.browserPreview !== null);
168 const statusInteractive =
169 props.status?.kind === "connection" ||
170 props.status?.kind === "extension" ||
171 props.status?.kind === "child-input";
172 const capsuleInteractive =
173 statusInteractive ||
174 extensionStatus !== null ||
175 actions.length > 0 ||
176 hasQueue ||
177 hasAgents ||
178 hasPreview;
179 const statusContent =
180 props.status !== null ? (
181 <View
182 pointerEvents={statusInteractive ? "box-none" : "none"}
183 className="h-11 items-center justify-center overflow-hidden"
184 >
185 <Animated.View className="h-11" style={capsuleSizerStyle} />
186 <View
187 pointerEvents={statusInteractive ? "box-none" : "none"}
188 className="absolute h-11 items-center justify-center"
189 style={{ width: labelWidth }}
190 >
191 <FloatingStatusLabel
192 key={
193 props.status.kind === "extension"
194 ? props.status.kind
195 : `${props.status.kind}:${props.status.label}`
196 }
197 status={props.status}
198 onLayout={handleLabelLayout}
199 />
200 </View>
201 </View>
202 ) : null;
203
204 const capsuleContent = (
205 <View className="flex-row items-center">
206 {statusContent}
207 {extensionStatus !== null ? (
208 <Pressable
209 accessibilityRole="button"
210 accessibilityLabel={extensionStatus.label}
211 accessibilityHint={gt("Shows the full Pi status")}
212 onPress={extensionStatus.onPress}
213 onLayout={(event) => setExtensionWidth(settleWidth(event.nativeEvent.layout.width))}
214 style={{ maxWidth: availableLabelWidth * 0.5 }}
215 className="h-11 flex-row items-center px-3 active:opacity-70"
216 >
217 {hasStatus ? <View className="absolute left-0 h-4 w-px bg-border" /> : null}
218 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
219 {extensionStatus.label}
220 </Text>
221 </Pressable>
222 ) : null}
223 {actions.length > 0 ? (
224 <View
225 onLayout={(event) => setActionWidth(settleWidth(event.nativeEvent.layout.width))}
226 className="h-11 flex-row items-center"
227 >
228 {actions.map((action) => (
229 <Fragment key={action.label}>
230 <View className="h-4 w-px bg-border" />
231 <Pressable
232 accessibilityRole="button"
233 accessibilityLabel={action.accessibilityLabel}
234 accessibilityState={{ disabled: action.disabled }}
235 disabled={action.disabled}
236 onPress={action.onPress}
237 className={`h-11 items-center justify-center px-3 active:opacity-70 ${action.disabled ? "opacity-50" : ""}`}
238 >
239 <Text className="font-t3-medium text-xs text-foreground" numberOfLines={1}>
240 {action.label}
241 </Text>
242 </Pressable>
243 </Fragment>
244 ))}
245 </View>
246 ) : null}
247 {hasPreview ? (
248 <View
249 className="h-11 flex-row items-center"
250 onLayout={(event) => setPreviewWidth(settleWidth(event.nativeEvent.layout.width))}
251 >
252 {hasStatus ? <View className="h-4 w-px bg-border" /> : null}
253 {props.devicePreview !== null ? (
254 <DevicePreviewButton {...props.devicePreview} compact={previewCompact} />
255 ) : null}
256 {props.browserPreview !== null ? (
257 <BrowserPreviewButton {...props.browserPreview} compact={previewCompact} />
258 ) : null}
259 </View>
260 ) : null}
261 {agents !== null ? (
262 <Pressable
263 accessibilityRole="button"
264 accessibilityLabel={gt("Open agents, {agents}", { agents: agents.accessibilityLabel })}
265 accessibilityHint={gt("Opens this turn's subagents")}
266 onPress={props.onOpenAgents}
267 onLayout={(event) => setAgentsWidth(settleWidth(event.nativeEvent.layout.width))}
268 className="h-11 flex-row items-center gap-1.5 px-3 active:opacity-70"
269 >
270 {hasStatus || hasPreview ? <View className="absolute left-0 h-4 w-px bg-border" /> : null}
271 <SymbolView name="person.2" size={13} tintColorClassName="accent-foreground" />
272 <Text className="font-t3-medium text-xs tabular-nums" numberOfLines={1}>
273 {agents.label}
274 </Text>
275 </Pressable>
276 ) : null}
277 {hasQueue ? (
278 <Pressable
279 accessibilityRole="button"
280 accessibilityLabel={gt("{count, plural, other {Open queue, {count} messages}}", {
281 count: props.queuedCount,
282 })}
283 accessibilityHint={gt("Opens queued messages to send now, edit, or remove")}
284 onPress={props.onOpenQueue}
285 onLayout={(event) => setQueueWidth(settleWidth(event.nativeEvent.layout.width))}
286 style={{ maxWidth: Math.min(overlayWidth, windowWidth) * 0.45 }}
287 className="h-11 flex-row items-center gap-2 px-3 active:opacity-70"
288 >
289 {hasStatus || hasPreview || hasAgents ? (
290 <View className="absolute left-0 h-4 w-px bg-border" />
291 ) : null}
292 <SymbolView name="list.number" size={13} tintColorClassName="accent-foreground" />
293 <Text className="shrink font-t3-medium text-xs tabular-nums" numberOfLines={1}>
294 {gt("{count, plural, other {{count} queued}}", { count: props.queuedCount })}
295 </Text>
296 </Pressable>
297 ) : null}
298 </View>
299 );
300
301 return (
302 <Animated.View
303 pointerEvents="box-none"
304 className="absolute left-0 right-0 z-20 items-center"
305 style={[{ top: -CONTROL_OVERLAY_OFFSET }, liftStyle]}
306 onLayout={(event) => setOverlayWidth(event.nativeEvent.layout.width)}
307 entering={useGlass || extensionOnly || reducedMotion ? undefined : CONTROL_ENTERING}
308 exiting={useGlass || extensionOnly || reducedMotion ? undefined : CONTROL_EXITING}
309 >
310 {hasCapsule && useGlass ? (
311 <UniwindGlassContainer
312 spacing={GLASS_MERGE_SPACING}
313 pointerEvents="box-none"
314 className="flex-row items-center gap-4"
315 >
316 <AnimatedGlassView
317 colorScheme={props.colorScheme}
318 glassEffectStyle="regular"
319 isInteractive={capsuleInteractive}
320 pointerEvents={capsuleInteractive ? "box-none" : "none"}
321 className="h-11 items-center justify-center overflow-hidden rounded-full"
322 style={capsuleStyle}
323 >
324 {capsuleContent}
325 </AnimatedGlassView>
326
327 <AnimatedGlassView
328 colorScheme={props.colorScheme}
329 glassEffectStyle={{ style: props.showScrollToEnd ? "regular" : "none", animate: true }}
330 isInteractive
331 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
332 accessibilityElementsHidden={!props.showScrollToEnd}
333 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
334 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
335 style={arrowTransformStyle}
336 >
337 <Animated.View style={arrowContentStyle}>
338 <ScrollToEndButton disabled={!props.showScrollToEnd} onPress={props.onScrollToEnd} />
339 </Animated.View>
340 </AnimatedGlassView>
341 </UniwindGlassContainer>
342 ) : hasCapsule ? (
343 <View pointerEvents="box-none" className="flex-row items-center gap-4">
344 <Animated.View
345 pointerEvents={capsuleInteractive ? "box-none" : "none"}
346 className="h-11 items-center justify-center overflow-hidden rounded-full border border-border bg-glass-fallback shadow-md shadow-black/10"
347 style={capsuleStyle}
348 >
349 {capsuleContent}
350 </Animated.View>
351
352 <Animated.View
353 pointerEvents={props.showScrollToEnd ? "auto" : "none"}
354 accessibilityElementsHidden={!props.showScrollToEnd}
355 importantForAccessibility={props.showScrollToEnd ? "auto" : "no-hide-descendants"}
356 style={[arrowTransformStyle, arrowContentStyle]}
357 >
358 <ControlPill
359 accessibilityLabel={gt("Scroll to end")}
360 activateOnPressIn
361 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
362 disabled={!props.showScrollToEnd}
363 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
364 onPress={props.onScrollToEnd}
365 />
366 </Animated.View>
367 </View>
368 ) : NATIVE_LIQUID_GLASS_SUPPORTED ? (
369 <UniwindGlassView
370 colorScheme={props.colorScheme}
371 glassEffectStyle="regular"
372 isInteractive
373 className="h-11 w-11 items-center justify-center overflow-hidden rounded-full"
374 >
375 <ScrollToEndButton onPress={props.onScrollToEnd} />
376 </UniwindGlassView>
377 ) : (
378 <ControlPill
379 accessibilityLabel={gt("Scroll to end")}
380 activateOnPressIn
381 className="h-11 w-11 border border-border bg-glass-fallback shadow-md shadow-black/10"
382 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
383 onPress={props.onScrollToEnd}
384 />
385 )}
386 </Animated.View>
387 );
388 }
389
390 function FloatingStatusLabel(props: {
391 readonly status: FloatingWorkingStatus;
392 readonly onLayout: (event: LayoutChangeEvent) => void;
393 }) {
394 if (props.status.kind === "extension") {
395 return (
396 <StatusLabelRow
397 accessibilityLabel={props.status.label}
398 accessibilityRole="button"
399 onPress={props.status.onPress}
400 onLayout={props.onLayout}
401 className="gap-1.5"
402 animated={false}
403 >
404 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
405 {props.status.label}
406 </Text>
407 <SymbolView name="chevron.down" size={10} tintColorClassName="accent-foreground" />
408 </StatusLabelRow>
409 );
410 }
411 if (props.status.kind === "child-input") {
412 return (
413 <StatusLabelRow
414 key="child-input"
415 accessibilityLabel={props.status.accessibilityLabel}
416 accessibilityRole="button"
417 className="gap-2"
418 onLayout={props.onLayout}
419 onPress={props.status.onPress}
420 >
421 <SymbolView name="text.bubble" size={13} tintColorClassName="accent-foreground" />
422 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
423 {props.status.label}
424 </Text>
425 </StatusLabelRow>
426 );
427 }
428 if (props.status.kind === "connection") {
429 return (
430 <StatusLabelRow
431 key="connection"
432 accessibilityLabel={props.status.label}
433 accessibilityRole="button"
434 className="gap-2"
435 onLayout={props.onLayout}
436 onPress={props.status.onPress}
437 >
438 <View className="h-2 w-2 rounded-full bg-red-500" />
439 <Text
440 className="max-w-[260px] shrink font-t3-medium text-xs text-foreground"
441 numberOfLines={1}
442 >
443 {props.status.label}
444 </Text>
445 </StatusLabelRow>
446 );
447 }
448 if (props.status.kind === "stop") {
449 return (
450 <StatusLabelRow
451 key="stop"
452 accessibilityLabel={props.status.label}
453 className="gap-2"
454 onLayout={props.onLayout}
455 >
456 {props.status.unresponsive ? (
457 <SymbolView
458 name="exclamationmark.triangle"
459 size={13}
460 tintColorClassName="accent-warning-foreground"
461 type="monochrome"
462 />
463 ) : (
464 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
465 )}
466 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
467 {props.status.label}
468 </Text>
469 </StatusLabelRow>
470 );
471 }
472 if (props.status.kind === "goal") {
473 return (
474 <StatusLabelRow
475 key="goal"
476 accessibilityLabel={props.status.accessibilityLabel}
477 className="gap-2"
478 onLayout={props.onLayout}
479 >
480 <SymbolView
481 name="target"
482 size={13}
483 tintColorClassName="accent-foreground"
484 type="monochrome"
485 />
486 <Text className="shrink font-t3-medium text-xs text-foreground" numberOfLines={1}>
487 {props.status.label}
488 </Text>
489 </StatusLabelRow>
490 );
491 }
492 return (
493 <StatusLabelRow
494 key="preparing"
495 accessibilityLabel={props.status.label}
496 className="gap-1.5"
497 onLayout={props.onLayout}
498 >
499 <SymbolView
500 name="arrow.triangle.branch"
501 size={13}
502 tintColorClassName="accent-foreground"
503 type="monochrome"
504 />
505 <ShimmeringWorkContent
506 className="flex-none"
507 textClassName="font-t3-medium"
508 compact
509 icon="arrow.triangle.branch"
510 iconSubtleColor="transparent"
511 label={props.status.label}
512 showIcon={false}
513 />
514 </StatusLabelRow>
515 );
516 }
517
518 function StatusLabelRow(props: {
519 readonly animated?: boolean;
520 readonly accessibilityLabel?: string;
521 readonly accessibilityRole?: "button";
522 readonly className?: string;
523 readonly children: ReactNode;
524 readonly onLayout: (event: LayoutChangeEvent) => void;
525 readonly onPress?: () => void;
526 }) {
527 const reducedMotion = useReducedMotion();
528 const rowClassName = `h-11 flex-row items-center px-4 ${props.className ?? ""}`;
529 return (
530 <Animated.View
531 className="absolute max-w-full"
532 entering={props.animated === false || reducedMotion ? undefined : LABEL_ENTERING}
533 exiting={props.animated === false || reducedMotion ? undefined : LABEL_EXITING}
534 onLayout={props.onLayout}
535 >
536 {props.onPress ? (
537 <Pressable
538 accessibilityLabel={props.accessibilityLabel}
539 accessibilityRole={props.accessibilityRole}
540 className={`${rowClassName} active:opacity-70`}
541 onPress={props.onPress}
542 >
543 {props.children}
544 </Pressable>
545 ) : (
546 <View accessible accessibilityLabel={props.accessibilityLabel} className={rowClassName}>
547 {props.children}
548 </View>
549 )}
550 </Animated.View>
551 );
552 }
553
554 function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) {
555 const gt = useGT();
556 return (
557 <ControlPill
558 accessibilityLabel={gt("Scroll to end")}
559 activateOnPressIn
560 className="h-11 w-11 bg-transparent"
561 disabled={props.disabled}
562 icon={{ ios: "chevron.down", android: "keyboard_arrow_down" }}
563 onPress={props.onPress}
564 />
565 );
566 }