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 { useThemeColor } from "../../lib/useThemeColor";
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 iconColor = useThemeColor("--color-icon-subtle");
32
33 if (!workflow || workflow.queuedRuns.length === 0) return null;
34
35 const move = async (runId: RunId, beforeRunId: RunId | null) => {
36 setBusyRunId(runId);
37 void Haptics.selectionAsync();
38 await reorder({
39 environmentId: props.environmentId,
40 input: { threadId: props.threadId, runId, beforeRunId },
41 });
42 setBusyRunId(null);
43 };
44
45 const steer = async (queuedRunId: RunId) => {
46 if (!workflow.activeRun || !workflow.canPromoteToSteer) return;
47 setBusyRunId(queuedRunId);
48 void Haptics.selectionAsync();
49 await promote({
50 environmentId: props.environmentId,
51 input: {
52 threadId: props.threadId,
53 queuedRunId,
54 targetRunId: workflow.activeRun.id,
55 },
56 });
57 setBusyRunId(null);
58 };
59
60 const dismiss = async (runId: RunId) => {
61 setBusyRunId(runId);
62 void Haptics.selectionAsync();
63 await cancel(
64 buildCancelQueuedRunCommand({
65 environmentId: props.environmentId,
66 runId,
67 threadId: props.threadId,
68 }),
69 );
70 setBusyRunId(null);
71 };
72
73 return (
74 <View className="mx-4 mb-3 overflow-hidden rounded-2xl border border-neutral-300/60 bg-card dark:border-white/[0.1]">
75 <View className="flex-row items-center gap-2 border-b border-neutral-300/50 px-3 py-2 dark:border-white/[0.08]">
76 <SymbolView name="list.number" size={13} tintColor={iconColor} type="monochrome" />
77 <Text className="font-t3-medium text-xs text-foreground">Queue</Text>
78 <Text className="ml-auto text-2xs tabular-nums text-foreground-muted">
79 {workflow.queuedRuns.length}
80 </Text>
81 </View>
82 <ScrollView style={{ maxHeight: 156 }} contentContainerStyle={{ paddingVertical: 4 }}>
83 {workflow.queuedRuns.map(({ run, text }, index) => {
84 const controls = resolveThreadQueueRowControls({
85 busy: busyRunId !== null,
86 canPromoteToSteer: workflow.canPromoteToSteer,
87 canReorder: workflow.canReorder,
88 index,
89 queuedCount: workflow.queuedRuns.length,
90 text,
91 });
92
93 return (
94 <View key={run.id} className="min-h-11 flex-row items-center gap-1.5 px-2">
95 <Text className="w-5 text-right text-3xs tabular-nums text-foreground-muted">
96 {index + 1}
97 </Text>
98 <Text className="min-w-0 flex-1 text-xs text-foreground" numberOfLines={1}>
99 {controls.displayText}
100 </Text>
101 <Pressable
102 accessibilityRole="button"
103 accessibilityLabel="Move queued message up"
104 disabled={!controls.canMoveUp}
105 onPress={() => void move(run.id, workflow.queuedRuns[index - 1]?.run.id ?? null)}
106 className="h-9 w-9 items-center justify-center disabled:opacity-30"
107 >
108 <SymbolView name="arrow.up" size={13} tintColor={iconColor} type="monochrome" />
109 </Pressable>
110 <Pressable
111 accessibilityRole="button"
112 accessibilityLabel="Move queued message down"
113 disabled={!controls.canMoveDown}
114 onPress={() => void move(run.id, workflow.queuedRuns[index + 2]?.run.id ?? null)}
115 className="h-9 w-9 items-center justify-center disabled:opacity-30"
116 >
117 <SymbolView name="arrow.down" size={13} tintColor={iconColor} type="monochrome" />
118 </Pressable>
119 <Pressable
120 accessibilityRole="button"
121 accessibilityLabel="Promote queued message to steer"
122 disabled={!controls.canSteer}
123 onPress={() => void steer(run.id)}
124 className="min-h-8 flex-row items-center gap-1 rounded-lg border border-neutral-300/60 px-2 disabled:opacity-30 dark:border-white/[0.1]"
125 >
126 <SymbolView
127 name="arrow.turn.left.up"
128 size={12}
129 tintColor={iconColor}
130 type="monochrome"
131 />
132 <Text className="font-t3-medium text-2xs text-foreground">Steer</Text>
133 </Pressable>
134 <Pressable
135 accessibilityRole="button"
136 accessibilityLabel={controls.dismissAccessibilityLabel}
137 disabled={!controls.canDismiss}
138 onPress={() => void dismiss(run.id)}
139 className="h-9 w-9 items-center justify-center disabled:opacity-30"
140 >
141 <SymbolView name="xmark" size={13} tintColor={iconColor} type="monochrome" />
142 </Pressable>
143 </View>
144 );
145 })}
146 </ScrollView>
147 </View>
148 );
149}