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) { 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(null); const busyRef = useRef(false); const [draggedRunId, setDraggedRunId] = useState(null); const [previewBeforeRunId, setPreviewBeforeRunId] = useState(); const [dragRows, setDragRows] = useState | null>(null); const rowLayouts = useRef(new Map()); const drag = useRef<{ runId: RunId; order: string; beforeRunId: RunId | null | undefined; rows: ReadonlyArray; } | 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 = ( {workflow?.isHeld && queuedRuns.length > 0 ? ( Queue held after restart { if (busyRef.current) return; busyRef.current = true; setResuming(true); try { await resume({ ...target, input: { threadId: target.threadId } }); } finally { busyRef.current = false; setResuming(false); } }} /> ) : null} {queuedRuns.length === 0 ? ( No messages waiting in this queue. ) : 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 ( rowLayouts.current.set(run.id, nativeEvent.layout)} > {canReorder ? ( // Outside the swipeable: two pans on one row would race, and // the handle owns vertical movement while the row owns sideways. 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} void act(run.id, "remove")} > void act(run.id, nativeEvent.event as QueueAction) } > void act(run.id, "edit")} className="min-h-14 flex-row items-center gap-2.5 py-2.5 active:opacity-70" > {title} {controls.isEditing ? ( Editing ) : null} {workflow?.canPromoteToSteer ? ( void act(run.id, "steer")} className="h-8 shrink-0 justify-center rounded-full bg-primary px-3 active:opacity-70 disabled:opacity-40" > Steer ) : null} ); })} ); 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 ( {content} ); } return ( navigation.goBack()} /> {content} ); } function QueueShiftedRow(props: { readonly offset: number; readonly dragging: boolean; readonly lifted: boolean; readonly onLayout: React.ComponentProps["onLayout"]; readonly children: React.ReactNode; }) { const { dragging, offset } = props; const style = useAnimatedStyle(() => ({ transform: [ { translateY: dragging ? withTiming(offset, { duration: 160, reduceMotion: ReduceMotion.System }) : offset, }, ], })); return ( {props.children} ); } /** 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(null); return ( { if (direction !== "right") return; swipeableRef.current?.close(); props.onRemove(); }} renderRightActions={() => ( { swipeableRef.current?.close(); props.onRemove(); }} className="items-center justify-center bg-danger active:opacity-70 disabled:opacity-40" style={{ width: REMOVE_ACTION_WIDTH }} > Remove )} > {props.children} ); } function QueueAttachmentThumbnails(props: { readonly environmentId: EnvironmentId; readonly attachments: ReadonlyArray; }) { 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 ( {shown.map((attachment) => ( ))} {overflow > 0 ? ( {shown.length === 0 ? `${overflow}` : `+${overflow}`} ) : null} ); } 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 ; } return ( ); } 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 ( { 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" > ); }