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

floating-working-control.tsx

606 lines21.6 KB

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