Open History to see the last commit
1import * as Haptics from "expo-haptics";
2import { SymbolView } from "expo-symbols";
3import { deriveThreadQueueWorkflowState } from "@t3tools/client-runtime/state/thread-workflows";
4import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
5import { useMemo, useState } from "react";
6import { Pressable, ScrollView, View } from "react-native";
7
8import { AppText as Text } from "../../components/AppText";
9import { useUniwindTheme } from "../../lib/useUniwindTheme";
10import { threadEnvironment } from "../../state/threads";
11import { useAtomCommand } from "../../state/use-atom-command";
12import { useThreadProjection } from "../../state/use-thread-detail";
13import {
14 buildCancelQueuedRunCommand,
15 resolveThreadQueueRowControls,
16} from "./threadQueueControlPresentation";
17
18export function ThreadQueueControl(props: {
19 readonly environmentId: EnvironmentId;
20 readonly threadId: ThreadId;
21}) {
22 const scoped = useThreadProjection(props);
23 const workflow = useMemo(
24 () => (scoped ? deriveThreadQueueWorkflowState(scoped.projection) : null),
25 [scoped],
26 );
27 const reorder = useAtomCommand(threadEnvironment.reorderQueuedRun, "reorder queued message");
28 const promote = useAtomCommand(threadEnvironment.promoteQueuedRun, "promote queued message");
29 const cancel = useAtomCommand(threadEnvironment.cancelQueuedRun, "cancel queued message");
30 const [busyRunId, setBusyRunId] = useState<RunId | null>(null);
31 const theme = useUniwindTheme();
32 const iconColor = theme["--color-icon-subtle"];
33
34 if (!workflow || workflow.queuedRuns.length === 0) return null;
35
36 const move = async (runId: RunId, beforeRunId: RunId | null) => {
37 setBusyRunId(runId);
38 void Haptics.selectionAsync();
39 await reorder({
40 environmentId: props.environmentId,
41 input: { threadId: props.threadId, runId, beforeRunId },
42 });
43 setBusyRunId(null);
44 };
45
46 const steer = async (queuedRunId: RunId) => {
47 if (!workflow.activeRun || !workflow.canPromoteToSteer) return;
48 setBusyRunId(queuedRunId);
49 void Haptics.selectionAsync();
50 await promote({
51 environmentId: props.environmentId,
52 input: {
53 threadId: props.threadId,
54 queuedRunId,
55 targetRunId: workflow.activeRun.id,
56 },
57 });
58 setBusyRunId(null);
59 };
60
61 const dismiss = async (runId: RunId) => {
62 setBusyRunId(runId);
63 void Haptics.selectionAsync();
64 await cancel(
65 buildCancelQueuedRunCommand({
66 environmentId: props.environmentId,
67 runId,
68 threadId: props.threadId,
69 }),
70 );
71 setBusyRunId(null);
72 };
73
74 return (
75 <View className="mx-4 mb-3 overflow-hidden rounded-2xl border border-adaptive-neutral-300-a60-white-a12 bg-card">
76 <View className="flex-row items-center gap-2 border-b border-adaptive-neutral-300-a60-white-a12 px-3 py-2">
77 <SymbolView name="list.number" size={13} tintColor={iconColor} type="monochrome" />
78 <Text className="font-t3-medium text-xs text-foreground">Queue</Text>
79 <Text className="ml-auto text-2xs tabular-nums text-foreground-muted">
80 {workflow.queuedRuns.length}
81 </Text>
82 </View>
83 <ScrollView style={{ maxHeight: 156 }} contentContainerStyle={{ paddingVertical: 4 }}>
84 {workflow.queuedRuns.map(({ run, text }, index) => {
85 const controls = resolveThreadQueueRowControls({
86 busy: busyRunId !== null,
87 canPromoteToSteer: workflow.canPromoteToSteer,
88 canReorder: workflow.canReorder,
89 index,
90 queuedCount: workflow.queuedRuns.length,
91 text,
92 });
93
94 return (
95 <View key={run.id} className="min-h-11 flex-row items-center gap-1.5 px-2">
96 <Text className="w-5 text-right text-3xs tabular-nums text-foreground-muted">
97 {index + 1}
98 </Text>
99 <Text className="min-w-0 flex-1 text-xs text-foreground" numberOfLines={1}>
100 {controls.displayText}
101 </Text>
102 <Pressable
103 accessibilityRole="button"
104 accessibilityLabel="Move queued message up"
105 disabled={!controls.canMoveUp}
106 onPress={() => void move(run.id, workflow.queuedRuns[index - 1]?.run.id ?? null)}
107 className="h-9 w-9 items-center justify-center disabled:opacity-30"
108 >
109 <SymbolView name="arrow.up" size={13} tintColor={iconColor} type="monochrome" />
110 </Pressable>
111 <Pressable
112 accessibilityRole="button"
113 accessibilityLabel="Move queued message down"
114 disabled={!controls.canMoveDown}
115 onPress={() => void move(run.id, workflow.queuedRuns[index + 2]?.run.id ?? null)}
116 className="h-9 w-9 items-center justify-center disabled:opacity-30"
117 >
118 <SymbolView name="arrow.down" size={13} tintColor={iconColor} type="monochrome" />
119 </Pressable>
120 <Pressable
121 accessibilityRole="button"
122 accessibilityLabel="Promote queued message to steer"
123 disabled={!controls.canSteer}
124 onPress={() => void steer(run.id)}
125 className="min-h-8 flex-row items-center gap-1 rounded-lg border border-adaptive-neutral-300-a60-white-a12 px-2 disabled:opacity-30"
126 >
127 <SymbolView
128 name="arrow.turn.left.up"
129 size={12}
130 tintColor={iconColor}
131 type="monochrome"
132 />
133 <Text className="font-t3-medium text-2xs text-foreground">Steer</Text>
134 </Pressable>
135 <Pressable
136 accessibilityRole="button"
137 accessibilityLabel={controls.dismissAccessibilityLabel}
138 disabled={!controls.canDismiss}
139 onPress={() => void dismiss(run.id)}
140 className="h-9 w-9 items-center justify-center disabled:opacity-30"
141 >
142 <SymbolView name="xmark" size={13} tintColor={iconColor} type="monochrome" />
143 </Pressable>
144 </View>
145 );
146 })}
147 </ScrollView>
148 </View>
149 );
150}