Repository navigation
Expand file tree
/
Copy pathThreadQueueControl.tsx
More file actions
632 lines (614 loc) · 24.5 KB
/
Copy pathThreadQueueControl.tsx
File metadata and controls
632 lines (614 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import { type StaticScreenProps, useNavigation } from "@react-navigation/native";
import { useAtomValue } from "@effect/atom-react";
import type { ChatAttachment, EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
import { Image } from "expo-image";
import * as Haptics from "expo-haptics";
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { Animated, Platform, Pressable, ScrollView, View } from "react-native";
import { Gesture, GestureDetector, GestureHandlerRootView } from "react-native-gesture-handler";
import ReanimatedSwipeable, {
type SwipeableMethods,
} from "react-native-gesture-handler/ReanimatedSwipeable";
import { Screen, ScreenStack, ScreenStackHeaderConfig } from "react-native-screens";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated";
import { MaterialButton } from "../../components/MaterialButton";
import { AndroidSheetHeader } from "../../components/AndroidScreenHeader";
import { AppText as Text } from "../../components/AppText";
import { SymbolView } from "../../components/AppSymbol";
import { ControlPillMenu } from "../../components/ControlPill";
import { scopedThreadKey } from "../../lib/scopedEntities";
import { useUniwindTheme } from "../../lib/useUniwindTheme";
import { nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";
import { useAssetUrl } from "../../state/assets";
import { beginQueuedRunEdit, useQueuedRunEdit } from "../../state/queued-run-edit";
import { environmentThreadDetails, threadEnvironment } from "../../state/threads";
import { useAtomCommand } from "../../state/use-atom-command";
import {
REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
buildCancelQueuedRunCommand,
resolveQueueDragBeforeRunId,
resolveQueueDropBeforeRunId,
resolveThreadQueueRowControls,
} from "./threadQueueControlPresentation";
import { threadDragGapOffset } from "./threadDragGap";
const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
const REMOVE_ACTION_WIDTH = 76;
const THUMBNAIL_LIMIT = 3;
type QueueTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
type QueueAction = "steer" | "edit" | "up" | "down" | "remove";
type QueueRowLayout = { readonly id: RunId; readonly y?: number; readonly height?: number };
export function useThreadQueueWorkflow(target: QueueTarget) {
return useAtomValue(environmentThreadDetails.queueWorkflowAtom(target));
}
export function useThreadQueuedCount(target: QueueTarget) {
return useAtomValue(environmentThreadDetails.queuedCountAtom(target));
}
export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {
const target = route.params;
const navigation = useNavigation();
const insets = useSafeAreaInsets();
const theme = useUniwindTheme();
const workflow = useThreadQueueWorkflow(target);
const threadKey = scopedThreadKey(target.environmentId, target.threadId);
const editing = useQueuedRunEdit(threadKey);
const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun, "reorder queued message");
const promote = useAtomCommand(threadEnvironment.promoteQueuedRun, "promote queued message");
const cancel = useAtomCommand(threadEnvironment.cancelQueuedRun, "remove queued message");
const resume = useAtomCommand(threadEnvironment.resumeThreadQueue, "resume queue");
const [resuming, setResuming] = useState(false);
const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
const busyRef = useRef(false);
const [draggedRunId, setDraggedRunId] = useState<RunId | null>(null);
const [previewBeforeRunId, setPreviewBeforeRunId] = useState<RunId | null | undefined>();
const [dragRows, setDragRows] = useState<ReadonlyArray<QueueRowLayout> | null>(null);
const rowLayouts = useRef(new Map<RunId, { y: number; height: number }>());
const drag = useRef<{
runId: RunId;
order: string;
beforeRunId: RunId | null | undefined;
rows: ReadonlyArray<QueueRowLayout>;
} | null>(null);
const [translation] = useState(() => new Animated.Value(0));
const queuedRuns = workflow?.queuedRuns ?? [];
const order = queuedRuns.map(({ run }) => run.id).join(",");
useEffect(() => {
if (drag.current && drag.current.order !== order) {
drag.current = null;
setDraggedRunId(null);
setPreviewBeforeRunId(undefined);
setDragRows(null);
translation.setValue(0);
}
}, [order, translation]);
// Nothing left to manage: the sheet closes rather than sitting on an empty
// list the user has to dismiss themselves.
const hadQueuedRuns = useRef(queuedRuns.length > 0);
useEffect(() => {
if (queuedRuns.length > 0) {
hadQueuedRuns.current = true;
return;
}
if (hadQueuedRuns.current) navigation.goBack();
}, [navigation, queuedRuns.length]);
const move = async (runId: RunId, beforeRunId: RunId | null) => {
if (busyRef.current || !workflow?.canReorder) return;
busyRef.current = true;
setBusyRunId(runId);
void Haptics.selectionAsync();
try {
await reorder({ ...target, input: { threadId: target.threadId, runId, beforeRunId } });
} finally {
busyRef.current = false;
setBusyRunId(null);
}
};
const act = async (runId: RunId, action: QueueAction) => {
if (busyRef.current) return;
const index = queuedRuns.findIndex(({ run }) => run.id === runId);
if (index < 0) return;
if (action === "up" && index > 0) {
await move(runId, queuedRuns[index - 1]!.run.id);
return;
}
if (action === "down" && index < queuedRuns.length - 1) {
await move(runId, queuedRuns[index + 2]?.run.id ?? null);
return;
}
if (action === "edit") {
const entry = queuedRuns[index]!;
void Haptics.selectionAsync();
beginQueuedRunEdit(threadKey, {
runId,
messageId: entry.messageId,
originalText: entry.text,
existingAttachments: entry.attachments,
...(entry.context ? { context: entry.context } : {}),
});
navigation.goBack();
return;
}
if (action !== "steer" && action !== "remove") return;
busyRef.current = true;
setBusyRunId(runId);
void Haptics.selectionAsync();
try {
if (action === "remove") {
await cancel(buildCancelQueuedRunCommand({ ...target, runId }));
} else if (workflow?.activeRun && workflow.canPromoteToSteer) {
await promote({
...target,
input: {
threadId: target.threadId,
queuedRunId: runId,
targetRunId: workflow.activeRun.id,
},
});
}
} finally {
busyRef.current = false;
setBusyRunId(null);
}
};
const canReorder = workflow?.canReorder === true && queuedRuns.length > 1;
const sourceLayout = dragRows?.find((row) => row.id === draggedRunId);
const lastLayout = dragRows?.at(-1);
const insertionOffset =
previewBeforeRunId === undefined
? undefined
: previewBeforeRunId === null
? lastLayout?.y === undefined || lastLayout.height === undefined
? undefined
: lastLayout.y + lastLayout.height
: dragRows?.find((row) => row.id === previewBeforeRunId)?.y;
const queueRows = () =>
queuedRuns.map(({ run }) => ({ id: run.id, ...rowLayouts.current.get(run.id) }));
const content = (
<ScrollView
className="flex-1"
scrollEnabled={draggedRunId === null}
// The iOS header is translucent and floats over this view; UIKit has to
// inset the content or the first row sits underneath the title.
contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
contentContainerClassName="px-5 pb-6"
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }}
>
{workflow?.isHeld && queuedRuns.length > 0 ? (
<View className="gap-2 py-3">
<Text className="text-sm text-foreground-muted">Queue held after restart</Text>
<MaterialButton
label="Resume queue"
disabled={resuming || busyRunId !== null}
onPress={async () => {
if (busyRef.current) return;
busyRef.current = true;
setResuming(true);
try {
await resume({ ...target, input: { threadId: target.threadId } });
} finally {
busyRef.current = false;
setResuming(false);
}
}}
/>
</View>
) : null}
{queuedRuns.length === 0 ? (
<Text className="pt-6 text-center text-sm text-foreground-muted">
No messages waiting in this queue.
</Text>
) : null}
{queuedRuns.map(({ run, text, attachments }, index) => {
const layout = dragRows?.find((row) => row.id === run.id);
const offset =
sourceLayout?.y !== undefined &&
sourceLayout.height !== undefined &&
layout?.y !== undefined &&
insertionOffset !== undefined
? threadDragGapOffset(layout.y, sourceLayout.y, sourceLayout.height, insertionOffset)
: 0;
const controls = resolveThreadQueueRowControls({
busy: busyRunId !== null || draggedRunId !== null,
canPromoteToSteer: workflow?.canPromoteToSteer ?? false,
canReorder: workflow?.canReorder ?? false,
index,
isEditing: editing?.runId === run.id,
queuedCount: queuedRuns.length,
text,
});
const title =
controls.displayText || (attachments.length > 0 ? "Attachments" : "Queued message");
return (
<QueueShiftedRow
key={run.id}
offset={offset}
dragging={draggedRunId !== null}
lifted={draggedRunId === run.id}
onLayout={({ nativeEvent }) => rowLayouts.current.set(run.id, nativeEvent.layout)}
>
<Animated.View
className="flex-row items-center border-b border-border bg-sheet"
style={
draggedRunId === run.id
? { transform: [{ translateY: translation }], zIndex: 1, opacity: 0.85 }
: undefined
}
>
{canReorder ? (
// Outside the swipeable: two pans on one row would race, and
// the handle owns vertical movement while the row owns sideways.
<QueueDragHandle
disabled={busyRunId !== null}
title={title}
canMoveUp={controls.canMoveUp}
canMoveDown={controls.canMoveDown}
onStep={(action) => void act(run.id, action)}
onStart={() => {
const rows = queueRows();
const beforeRunId = resolveQueueDragBeforeRunId(rows, run.id, 0);
drag.current = { runId: run.id, order, beforeRunId, rows };
translation.setValue(0);
setDragRows(rows);
setPreviewBeforeRunId(beforeRunId);
setDraggedRunId(run.id);
void Haptics.selectionAsync();
}}
onMove={(y) => {
const current = drag.current;
if (current?.runId !== run.id || current.order !== order) return;
translation.setValue(y);
const before = resolveQueueDragBeforeRunId(current.rows, run.id, y);
if (current.beforeRunId !== before) {
current.beforeRunId = before;
setPreviewBeforeRunId(before);
}
}}
onEnd={(y, success) => {
const started = drag.current;
const stop = () => {
if (drag.current !== started) return;
drag.current = null;
setDraggedRunId(null);
setPreviewBeforeRunId(undefined);
setDragRows(null);
translation.setValue(0);
};
// A remote reorder or a newly started run invalidates this drag.
if (!success || started?.order !== order || started.runId !== run.id) {
stop();
return;
}
const before = resolveQueueDropBeforeRunId(started.rows, run.id, y);
if (before === undefined) {
stop();
return;
}
const source = started.rows.find((row) => row.id === run.id);
const tail = started.rows.at(-1);
const insertion =
before === null
? tail?.y !== undefined && tail.height !== undefined
? tail.y + tail.height
: undefined
: started.rows.find((row) => row.id === before)?.y;
if (
source?.y !== undefined &&
source.height !== undefined &&
insertion !== undefined
) {
Animated.timing(translation, {
toValue: insertion - source.y - (insertion > source.y ? source.height : 0),
duration: 160,
useNativeDriver: true,
}).start();
}
setPreviewBeforeRunId(before);
void move(run.id, before).finally(stop);
}}
/>
) : null}
<QueueRowSwipeable
enabled={draggedRunId === null && busyRunId === null && controls.canDismiss}
background={theme["--color-sheet"]}
onRemove={() => void act(run.id, "remove")}
>
<ControlPillMenu
accessibilityLabel={`Actions for queued message ${index + 1}`}
shouldOpenOnLongPress
actions={[
...(workflow?.canPromoteToSteer
? [
{
id: "steer",
title: "Steer now",
attributes: { disabled: !controls.canSteer },
image: Platform.OS === "ios" ? "arrow.turn.left.up" : "arrow_upward",
},
]
: []),
{
id: "edit",
title: "Edit",
attributes: { disabled: !controls.canEdit },
image: Platform.OS === "ios" ? "pencil" : "edit",
},
{ id: "up", title: "Move up", attributes: { disabled: !controls.canMoveUp } },
{
id: "down",
title: "Move down",
attributes: { disabled: !controls.canMoveDown },
},
{
id: "remove",
title: "Remove",
attributes: { disabled: !controls.canDismiss, destructive: true },
},
]}
onPressAction={({ nativeEvent }) =>
void act(run.id, nativeEvent.event as QueueAction)
}
>
<Pressable
accessibilityRole="button"
accessibilityLabel={title}
accessibilityHint="Opens this message in the composer for editing"
disabled={!controls.canEdit}
onPress={() => void act(run.id, "edit")}
className="min-h-14 flex-row items-center gap-2.5 py-2.5 active:opacity-70"
>
<QueueAttachmentThumbnails
environmentId={target.environmentId}
attachments={attachments}
/>
<Text
className={
controls.isEditing
? "min-w-0 flex-1 text-sm text-foreground-muted"
: "min-w-0 flex-1 text-sm text-foreground"
}
numberOfLines={1}
>
{title}
</Text>
{controls.isEditing ? (
<Text className="shrink-0 text-2xs uppercase tracking-wide text-primary">
Editing
</Text>
) : null}
{workflow?.canPromoteToSteer ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={`Steer with message ${index + 1} now`}
disabled={!controls.canSteer}
onPress={() => void act(run.id, "steer")}
className="h-8 shrink-0 justify-center rounded-full bg-primary px-3 active:opacity-70 disabled:opacity-40"
>
<Text className="font-t3-medium text-xs text-primary-foreground">
Steer
</Text>
</Pressable>
) : null}
</Pressable>
</ControlPillMenu>
</QueueRowSwipeable>
</Animated.View>
</QueueShiftedRow>
);
})}
</ScrollView>
);
if (Platform.OS === "ios") {
// A plain formSheet screen never renders a stack header, so it comes from
// a nested native stack inside the sheet (same shape as the git sheet).
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<View collapsable={false} className="flex-1 bg-sheet">
<ScreenStack style={{ flex: 1 }}>
<Screen
activityState={2}
enabled
isNativeStack
screenId="thread-queue-sheet-native"
scrollEdgeEffects={HEADER_SCROLL_EDGE_EFFECTS}
style={{ backgroundColor: theme["--color-sheet"], flex: 1 }}
>
{content}
<ScreenStackHeaderConfig
backgroundColor="rgba(0,0,0,0)"
color={theme["--color-foreground"]}
hideBackButton
hideShadow={false}
title="Queued"
titleColor={theme["--color-foreground"]}
titleFontSize={18}
titleFontWeight="800"
translucent
/>
</Screen>
</ScreenStack>
</View>
</GestureHandlerRootView>
);
}
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<View collapsable={false} className="flex-1 bg-sheet">
<AndroidSheetHeader title="Queued" onBack={() => navigation.goBack()} />
{content}
</View>
</GestureHandlerRootView>
);
}
function QueueShiftedRow(props: {
readonly offset: number;
readonly dragging: boolean;
readonly lifted: boolean;
readonly onLayout: React.ComponentProps<typeof View>["onLayout"];
readonly children: React.ReactNode;
}) {
const { dragging, offset } = props;
const style = useAnimatedStyle(() => ({
transform: [
{
translateY: dragging
? withTiming(offset, { duration: 160, reduceMotion: ReduceMotion.System })
: offset,
},
],
}));
return (
<Reanimated.View onLayout={props.onLayout} style={[style, { zIndex: props.lifted ? 1 : 0 }]}>
{props.children}
</Reanimated.View>
);
}
/** Swipe left to remove, the one destructive action that needs no menu. */
function QueueRowSwipeable(props: {
readonly enabled: boolean;
readonly background: string;
readonly onRemove: () => void;
readonly children: React.ReactNode;
}) {
const swipeableRef = useRef<SwipeableMethods | null>(null);
return (
<ReanimatedSwipeable
ref={swipeableRef}
enabled={props.enabled}
friction={2}
rightThreshold={40}
overshootRight={false}
// The sheet's own vertical scroll and the drag handle both move up and
// down; a swipe has to be clearly sideways before it takes the gesture.
failOffsetY={[-12, 12]}
containerStyle={{ backgroundColor: props.background, flex: 1 }}
childrenContainerStyle={{ backgroundColor: props.background }}
onSwipeableOpen={(direction) => {
if (direction !== "right") return;
swipeableRef.current?.close();
props.onRemove();
}}
renderRightActions={() => (
<Pressable
accessibilityRole="button"
accessibilityLabel={REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL}
disabled={!props.enabled}
onPress={() => {
swipeableRef.current?.close();
props.onRemove();
}}
className="items-center justify-center bg-danger active:opacity-70 disabled:opacity-40"
style={{ width: REMOVE_ACTION_WIDTH }}
>
<SymbolView name="trash" size={16} tintColorClassName="accent-danger-foreground" />
<Text className="pt-1 text-2xs font-t3-medium text-danger-foreground">Remove</Text>
</Pressable>
)}
>
{props.children}
</ReanimatedSwipeable>
);
}
function QueueAttachmentThumbnails(props: {
readonly environmentId: EnvironmentId;
readonly attachments: ReadonlyArray<ChatAttachment>;
}) {
const images = props.attachments.filter((attachment) => attachment.mimeType.startsWith("image/"));
const shown = images.slice(0, THUMBNAIL_LIMIT);
const overflow = props.attachments.length - shown.length;
if (props.attachments.length === 0) return null;
return (
<View className="shrink-0 flex-row items-center gap-1">
{shown.map((attachment) => (
<QueueAttachmentThumbnail
key={attachment.id}
environmentId={props.environmentId}
attachment={attachment}
/>
))}
{overflow > 0 ? (
<View className="h-6 min-w-6 items-center justify-center rounded bg-subtle px-1">
<Text className="text-2xs tabular-nums text-foreground-muted">
{shown.length === 0 ? `${overflow}` : `+${overflow}`}
</Text>
</View>
) : null}
</View>
);
}
function QueueAttachmentThumbnail(props: {
readonly environmentId: EnvironmentId;
readonly attachment: ChatAttachment;
}) {
const url = useAssetUrl(props.environmentId, {
_tag: "attachment",
attachmentId: props.attachment.id,
fileName: props.attachment.name,
mimeType: props.attachment.mimeType,
disposition: "inline",
});
if (url === null) {
return <View className="h-6 w-6 rounded bg-subtle" />;
}
return (
<Image
source={{ uri: url }}
contentFit="cover"
style={{ width: 24, height: 24, borderRadius: 4 }}
accessibilityIgnoresInvertColors
/>
);
}
function QueueDragHandle(props: {
disabled: boolean;
title: string;
canMoveUp: boolean;
canMoveDown: boolean;
onStep: (action: "up" | "down") => void;
onStart: () => void;
onMove: (y: number) => void;
onEnd: (y: number, success: boolean) => void;
}) {
const latest = useRef(props);
useLayoutEffect(() => {
latest.current = props;
});
const gesture = useMemo(
() =>
Gesture.Pan()
.enabled(!props.disabled)
.minDistance(0)
.shouldCancelWhenOutside(false)
.runOnJS(true)
.onStart(() => latest.current.onStart())
.onUpdate((event) => latest.current.onMove(event.translationY))
.onFinalize((event, success) => latest.current.onEnd(event.translationY, success)),
[props.disabled],
);
return (
<GestureDetector gesture={gesture}>
<View
collapsable={false}
accessible
accessibilityRole="adjustable"
accessibilityLabel={`Reorder ${props.title}`}
accessibilityState={{ disabled: props.disabled }}
accessibilityActions={[
...(props.canMoveUp ? [{ name: "decrement", label: "Move up" }] : []),
...(props.canMoveDown ? [{ name: "increment", label: "Move down" }] : []),
]}
onAccessibilityAction={({ nativeEvent }) => {
if (props.disabled) return;
if (nativeEvent.actionName === "decrement" && props.canMoveUp) props.onStep("up");
if (nativeEvent.actionName === "increment" && props.canMoveDown) props.onStep("down");
}}
className="h-12 w-8 items-center justify-center"
>
<SymbolView
name="line.3.horizontal"
size={16}
tintColorClassName="accent-foreground-muted"
/>
</View>
</GestureDetector>
);
}