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

threadQueueControlPresentation.ts

84 lines2.8 KB

1import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
2​
3export const REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL = "Remove queued message";
4​
5export interface ThreadQueueRowControls {
6 readonly canDismiss: boolean;
7 readonly canEdit: boolean;
8 readonly canMoveDown: boolean;
9 readonly canMoveUp: boolean;
10 readonly canSteer: boolean;
11 readonly dismissAccessibilityLabel: string;
12 readonly displayText: string;
13 readonly isEditing: boolean;
15​
16export function resolveThreadQueueRowControls(input: {
17 readonly busy: boolean;
18 readonly canPromoteToSteer: boolean;
19 readonly canReorder: boolean;
20 readonly index: number;
21 readonly isEditing?: boolean;
22 readonly queuedCount: number;
23 readonly text: string;
24}): ThreadQueueRowControls {
25 const mutationEnabled = !input.busy;
26 const isEditing = input.isEditing === true;
27​
28 return {
29 canDismiss: !input.busy,
30 // Re-opening the row already in the composer would reload it and throw
31 // away whatever has been typed since.
32 canEdit: mutationEnabled && !isEditing,
33 canMoveDown: mutationEnabled && input.canReorder && input.index < input.queuedCount - 1,
34 canMoveUp: mutationEnabled && input.canReorder && input.index > 0,
35 canSteer: mutationEnabled && input.canPromoteToSteer && !isEditing,
36 dismissAccessibilityLabel: REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL,
37 displayText: input.text,
38 isEditing,
39 };
41​
42export function buildCancelQueuedRunCommand(input: {
43 readonly environmentId: EnvironmentId;
44 readonly runId: RunId;
45 readonly threadId: ThreadId;
46}): {
47 readonly environmentId: EnvironmentId;
48 readonly input: {
49 readonly runId: RunId;
50 readonly threadId: ThreadId;
51 };
52} {
53 return {
54 environmentId: input.environmentId,
55 input: {
56 runId: input.runId,
57 threadId: input.threadId,
58 },
59 };
61​
62export function resolveQueueDragBeforeRunId(
63 rows: ReadonlyArray<{ id: RunId; y?: number; height?: number }>,
64 runId: RunId,
65 translationY: number,
66): RunId | null | undefined {
67 const sourceIndex = rows.findIndex((row) => row.id === runId);
68 const source = rows[sourceIndex];
69 if (!source || rows.some((row) => row.y === undefined || row.height === undefined)) return;
70 const center = source.y! + source.height! / 2 + translationY;
71 const remaining = rows.filter((row) => row.id !== runId);
72 return remaining.find((row) => center < row.y! + row.height! / 2)?.id ?? null;
74​
75export function resolveQueueDropBeforeRunId(
76 rows: ReadonlyArray<{ id: RunId; y?: number; height?: number }>,
77 runId: RunId,
78 translationY: number,
79): RunId | null | undefined {
80 const before = resolveQueueDragBeforeRunId(rows, runId, translationY);
81 if (before === undefined) return;
82 const sourceIndex = rows.findIndex((row) => row.id === runId);
83 return before === (rows[sourceIndex + 1]?.id ?? null) ? undefined : before;