Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

23.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import { useCallback, useEffect, useId, useRef, useState } from "react";
import {
  CornerDownRightIcon,
  ListEndIcon,
} from "lucide-react";
import { TrashIcon } from "@/lib/hugeicons-derived";
import {
  DragDropVerticalIcon,
  Edit03Icon,
  MoreHorizontalIcon,
} from "@hugeicons/core-free-icons";
import { Copy01Icon } from "@/lib/copy-icon";
import { HugeiconsIcon } from "@hugeicons/react";
import { Button } from "@/components/ui/button";
import {
  DropdownMenuItem,
  DropdownMenuSeparator,
} from "@/components/ui/dropdown-menu";
import { NonModalDropdownMenu } from "@/components/ui/non-modal-dropdown-menu";
import { COMPOSER_INPUT_SELECTOR } from "@/features/settings";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { usePromptQueueReorder } from "./use-prompt-queue-reorder";
import { TooltipIconButton } from "@/components/assistant-ui/tooltip-icon-button";
import { QueueResumeIcon } from "@/components/assistant-ui/queue-resume-icon";
import {
  composerSubmitIntent,
  type PromptQueueUIEntry,
  type PromptQueueUIItem,
  useChatPreferencesStore,
} from "@/features/chat";
import { copyToClipboard } from "@/lib/copy-to-clipboard";
import { useT } from "@/i18n";
import { cn } from "@/lib/utils";

// Matches the composer's IME watchdog.
const IME_STUCK_TIMEOUT_MS = 2500;

type PromptQueueListProps = {
  entry: PromptQueueUIEntry;
  items: PromptQueueUIItem[];
  onEdit: (id: string, prompt: string) => boolean;
  onRemove: (id: string) => boolean;
  onMove: (id: string, targetId: string) => boolean;
  onSteer: (id: string) => boolean;
  onResume: () => void;
};

const MENU_OR_TRIGGER = "[data-slot='dropdown-menu-content'], [data-queue-menu]";
// A focused row button shows its label below it, over the next 32px row; it must not take that row's clicks.
const ROW_TOOLTIP_CLASS = "prompt-queue-option-tooltip pointer-events-none";

export function PromptQueueList({
  entry,
  items,
  onEdit,
  onRemove,
  onMove,
  onSteer,
  onResume,
}: PromptQueueListProps) {
  const t = useT();
  const followUpBehavior = useChatPreferencesStore((s) => s.followUpBehavior);
  const setFollowUpBehavior = useChatPreferencesStore((s) => s.setFollowUpBehavior);
  const sendShortcut = useChatPreferencesStore((s) => s.sendShortcut);
  const [editingId, setEditingId] = useState<string | null>(null);
  const [draft, setDraft] = useState("");
  const [announcement, setAnnouncement] = useState("");
  const inputRef = useRef<HTMLTextAreaElement>(null);
  const composingRef = useRef(false);
  const composingTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  const editFromMenuRef = useRef(false);
  const pointerDownRef = useRef<{ target: EventTarget | null; at: number }>({
    target: null,
    at: 0,
  });
  useEffect(() => {
    const note = (event: PointerEvent) => {
      pointerDownRef.current = { target: event.target, at: event.timeStamp };
    };
    document.addEventListener("pointerdown", note, true);
    return () => document.removeEventListener("pointerdown", note, true);
  }, []);
  const instructionsId = useId();
  const editingItem = items.find(
    (item) => item.id === editingId && item.canEdit,
  );
  const activeEditingId = editingItem?.id;
  const movableItems = items.filter((item) => item.canEdit && item.canRemove);
  const {
    listRef,
    draggingId,
    move,
    cancelDrag,
    onPointerDown,
    onPointerMove,
    onPointerUp,
  } = usePromptQueueReorder(
    items,
    Boolean(activeEditingId),
    onMove,
    setAnnouncement,
  );

  useEffect(() => {
    if (!activeEditingId) return;
    inputRef.current?.focus();
    inputRef.current?.select();
  }, [activeEditingId]);

  // Some IMEs never send compositionend; drop the flag after a quiet spell.
  const setComposing = useCallback((next: boolean) => {
    composingRef.current = next;
    if (composingTimerRef.current) clearTimeout(composingTimerRef.current);
    composingTimerRef.current = next
      ? setTimeout(() => {
          composingTimerRef.current = null;
          composingRef.current = false;
        }, IME_STUCK_TIMEOUT_MS)
      : null;
  }, []);

  useEffect(
    () => () => {
      if (composingTimerRef.current) clearTimeout(composingTimerRef.current);
    },
    [],
  );

  function startEditing(item: PromptQueueUIItem) {
    setComposing(false);
    setDraft(item.prompt);
    setEditingId(item.id);
  }

  function finishEditing() {
    const id = editingId;
    setEditingId(null);
    setDraft("");
    // restore focus after the editor's row controls return.
    requestAnimationFrame(() => {
      const row = Array.from(
        listRef.current?.querySelectorAll<HTMLElement>(
          "[data-queue-item-id]",
        ) ?? [],
      ).find((element) => element.dataset.queueItemId === id);
      row?.querySelector<HTMLButtonElement>("[data-queue-menu]")?.focus();
    });
  }

  function saveEditing() {
    if (!editingItem || (!draft.trim() && !editingItem.attachmentNames?.length)) return;
    if (onEdit(editingItem.id, draft)) {
      setAnnouncement(t("promptQueue.announceUpdated"));
      finishEditing();
    } else {
      setAnnouncement(t("promptQueue.announceEditFailed"));
    }
  }

  return (
    // browsers paint scrollbars outside scroller radii, so this frame owns rounding and clipping.
    <div
      data-queue-frame=""
      className="relative z-0 mx-3 mb-[calc(-8px*var(--ui-space-scale,1))] overflow-hidden rounded-t-xl border border-border/60 bg-background sm:mx-5 dark:bg-[color-mix(in_srgb,var(--card)_50%,var(--background))]"
    >
      <div
        ref={listRef}
        className="max-h-[28dvh] overflow-y-auto px-1.5 pt-1 pb-3 text-muted-foreground sm:px-2 [&_button]:border-0 [&_button]:shadow-none [&_.aui-button-icon:focus-visible]:bg-accent"
        aria-label={t("promptQueue.regionLabel", {
          current: entry.current,
          total: entry.total,
        })}
      >
        <p id={instructionsId} className="sr-only">
          {t("promptQueue.reorderInstructions")}
        </p>
        <div role="status" aria-live="polite" className="sr-only">
          {announcement}
        </div>
        <div role="list" aria-label={t("promptQueue.listLabel")}>
          {items.map((item, index) => {
            const isEditing = editingItem?.id === item.id;
            const position = index + 1;
            const moveIndex = movableItems.findIndex(
              (candidate) => candidate.id === item.id,
            );
            const canMove =
              !activeEditingId && moveIndex >= 0 && movableItems.length > 1;
            const previous = movableItems[moveIndex - 1];
            const next = movableItems[moveIndex + 1];
            return (
              <div
                key={item.id}
                role="listitem"
                data-queue-item-id={item.id}
                data-queue-dragging={draggingId === item.id || undefined}
                aria-label={t("promptQueue.itemLabel", {
                  position,
                  total: items.length,
                  prompt: [item.prompt, ...(item.attachmentNames ?? [])].filter(Boolean).join(" · "),
                })}
                className={cn(
                  "group relative rounded-lg transition-colors",
                  draggingId && "will-change-transform",
                  draggingId === item.id && "z-10 opacity-50",
                )}
              >
                {isEditing ? (
                  <div className="group/queue-editor has-[textarea:focus-visible]:[--queue-hint-visibility:visible] flex flex-wrap items-center justify-end gap-2 px-1 py-2">
                    <div className="w-full overflow-hidden rounded-lg border-0 bg-muted/40 p-3 has-[:focus-visible]:bg-muted dark:bg-card dark:has-[:focus-visible]:bg-accent">
                      <textarea
                        ref={inputRef}
                        value={draft}
                        rows={2}
                        onChange={(event) =>
                          setDraft(event.currentTarget.value)
                        }
                        onCompositionStart={() => setComposing(true)}
                        onCompositionUpdate={() => setComposing(true)}
                        onCompositionEnd={() => setComposing(false)}
                        // Blur commits or cancels any composition first, so this reset is safe.
                        onBlur={() => setComposing(false)}
                        onKeyDown={(event) => {
                          // The candidate window owns this key; cancelling here would discard the draft.
                          if (
                            event.nativeEvent.isComposing ||
                            event.nativeEvent.keyCode === 229
                          ) {
                            setComposing(true);
                            return;
                          }
                          if (composingRef.current) {
                            // Candidate-confirming Enter can arrive as non-composing; keep it gated.
                            if (event.key === "Enter") {
                              if (!event.shiftKey) event.preventDefault();
                              return;
                            }
                            setComposing(false);
                          }
                          if (event.key === "Escape") {
                            event.preventDefault();
                            event.stopPropagation();
                            finishEditing();
                            return;
                          }
                          if (
                            composerSubmitIntent(
                              {
                                key: event.key,
                                metaKey: event.metaKey,
                                ctrlKey: event.ctrlKey,
                                shiftKey: event.shiftKey,
                                altKey: event.altKey,
                                repeat: event.repeat,
                                isComposing: event.nativeEvent.isComposing,
                                keyCode: event.nativeEvent.keyCode,
                              },
                              sendShortcut,
                              event.currentTarget.value,
                            )
                          ) {
                            event.preventDefault();
                            event.stopPropagation();
                            saveEditing();
                          }
                        }}
                        // Negative margins move the resize grip to the corner; padding keeps the text.
                        className="-mr-1.5 -mb-1.5 block max-h-36 min-h-12 w-[calc(100%+0.375rem)] resize-y border-0 bg-transparent pt-0 pr-1.5 pb-1.5 pl-1 text-sm text-foreground outline-none"
                        aria-label={t("promptQueue.editLabel", { position })}
                      />
                    </div>
                    <span
                      aria-hidden="true"
                      className="mr-auto text-xs font-medium text-foreground [visibility:var(--queue-hint-visibility,hidden)]"
                    >
                      {t("promptQueue.editingHint")}
                    </span>
                    <Button
                      type="button"
                      variant="ghost"
                      size="sm"
                      className="focus-visible:bg-accent"
                      onClick={finishEditing}
                    >
                      {t("promptQueue.cancel")}
                    </Button>
                    <Button
                      type="button"
                      size="sm"
                      className="focus-visible:bg-primary/80"
                      disabled={!draft.trim() && !item.attachmentNames?.length}
                      onClick={saveEditing}
                    >
                      {t("promptQueue.save")}
                    </Button>
                  </div>
                ) : (
                  // Compact for a pointer; touch keeps 44px targets, so its rows stay taller.
                  <div className="flex min-h-8 items-center gap-1 pointer-coarse:min-h-11">
                    <TooltipIconButton
                      type="button"
                      tooltip={t("promptQueue.dragTooltip")}
                      tooltipClassName={ROW_TOOLTIP_CLASS}
                      aria-label={t("promptQueue.reorderLabel", {
                        position,
                        total: items.length,
                      })}
                      aria-describedby={instructionsId}
                      className="h-7 w-4 shrink-0 touch-none cursor-grab text-muted-foreground/50 hover:bg-transparent hover:text-muted-foreground/50 active:cursor-grabbing pointer-coarse:h-11 pointer-coarse:w-8 dark:hover:bg-transparent"
                      disabled={!canMove}
                      onPointerDown={(event) => onPointerDown(event, item.id)}
                      onPointerMove={onPointerMove}
                      onPointerUp={onPointerUp}
                      onPointerCancel={cancelDrag}
                      onLostPointerCapture={cancelDrag}
                      onKeyDown={(event) => {
                        if (event.key === "Escape" && cancelDrag()) {
                          event.preventDefault();
                          event.stopPropagation();
                          return;
                        }
                        const targets: Record<string, string | undefined> = {
                          ArrowUp: previous?.id,
                          ArrowDown: next?.id,
                          Home: movableItems[0]?.id,
                          End: movableItems.at(-1)?.id,
                        };
                        if (!(event.key in targets)) return;
                        event.preventDefault();
                        event.stopPropagation();
                        move(item.id, targets[event.key]);
                      }}
                    >
                      <HugeiconsIcon icon={DragDropVerticalIcon} strokeWidth={1.75} className="size-4" />
                    </TooltipIconButton>
                    <ListEndIcon
                      className="hidden size-4 shrink-0 text-muted-foreground/60 sm:block"
                      aria-hidden="true"
                    />
                    <span className="min-w-0 flex-1 truncate px-1.5 text-sm text-foreground/80">
                      {item.prompt}
                      {item.attachmentNames?.length ? (
                        <span title={item.attachmentNames.join(", ")}>
                          {item.prompt ? " · " : ""}
                          {item.attachmentNames.join(", ")}
                        </span>
                      ) : null}
                    </span>
                    {index === 0 && entry.paused && (
                      <span className="hidden shrink-0 px-1 text-xs text-muted-foreground sm:inline">
                        {t("promptQueue.paused")}
                      </span>
                    )}
                    <TooltipIconButton
                      type="button"
                      tooltip={t("promptQueue.steerTooltip")}
                      tooltipClassName={ROW_TOOLTIP_CLASS}
                      aria-label={t("promptQueue.steerLabel", { position })}
                      disabled={!item.canEdit || !item.canRemove}
                      className="h-7 w-auto shrink-0 gap-1.5 px-2 font-normal text-muted-foreground hover:text-foreground pointer-coarse:h-11"
                      onClick={() => {
                        setAnnouncement(
                          t(
                            onSteer(item.id)
                              ? "promptQueue.announceSteered"
                              : "promptQueue.announceSteerFailed",
                          ),
                        );
                      }}
                    >
                      <CornerDownRightIcon className="size-4" />
                      <span>{t("promptQueue.steer")}</span>
                    </TooltipIconButton>
                    <TooltipIconButton
                      type="button"
                      tooltip={t("promptQueue.removeTooltip")}
                      tooltipClassName={ROW_TOOLTIP_CLASS}
                      aria-label={t("promptQueue.removeLabel", { position })}
                      disabled={!item.canRemove}
                      className="size-7 shrink-0 text-muted-foreground hover:text-destructive pointer-coarse:size-11"
                      onClick={() => {
                        if (onRemove(item.id))
                          setAnnouncement(t("promptQueue.announceRemoved"));
                      }}
                    >
                      <HugeiconsIcon icon={TrashIcon} strokeWidth={1.8} />
                    </TooltipIconButton>
                    <NonModalDropdownMenu
                      trigger={(triggerRef) => (
                        <TooltipIconButton
                          ref={triggerRef}
                          tooltip={t("promptQueue.moreTooltip")}
                          tooltipClassName={ROW_TOOLTIP_CLASS}
                          data-queue-menu
                          aria-label={t("promptQueue.moreLabel", { position })}
                          className="size-7 shrink-0 text-muted-foreground hover:text-foreground data-[state=open]:bg-accent data-[state=open]:text-foreground pointer-coarse:size-11"
                        >
                          <HugeiconsIcon icon={MoreHorizontalIcon} className="size-4" />
                        </TooltipIconButton>
                      )}
                      align="end"
                      side="bottom"
                      sideOffset={6}
                      className="w-56 rounded-lg border border-border/60 p-1 shadow-lg"
                      // Opening the menu must not select an item on pointer release.
                      onPointerUpCapture={(event) => event.preventDefault()}
                      // A queued send focuses the composer, so stay open; other focus moves dismiss.
                      onFocusOutside={(event) => {
                        const { target, relatedTarget, timeStamp } =
                          event.detail.originalEvent;
                        const down = pointerDownRef.current;
                        const byPointer =
                          timeStamp - down.at < 1000 &&
                          down.target instanceof Element &&
                          !down.target.closest(MENU_OR_TRIGGER);
                        if (
                          byPointer ||
                          !(target instanceof Element) ||
                          !target.matches(COMPOSER_INPUT_SELECTOR)
                        )
                          return;
                        event.preventDefault();
                        if (
                          relatedTarget instanceof HTMLElement &&
                          relatedTarget.closest(MENU_OR_TRIGGER)
                        )
                          relatedTarget.focus({ preventScroll: true });
                      }}
                      onCloseAutoFocus={(event) => {
                        if (!editFromMenuRef.current) return;
                        event.preventDefault();
                        editFromMenuRef.current = false;
                        inputRef.current?.focus();
                      }}
                    >
                      <DropdownMenuItem
                        disabled={!item.canEdit}
                        onSelect={() => {
                          editFromMenuRef.current = true;
                          startEditing(item);
                        }}
                      >
                        <HugeiconsIcon icon={Edit03Icon} strokeWidth={1.8} />
                        {t("promptQueue.editItem")}
                      </DropdownMenuItem>
                      <DropdownMenuItem
                        onSelect={() => {
                          void copyToClipboard(item.prompt).then((copied) =>
                            setAnnouncement(
                              t(
                                copied
                                  ? "promptQueue.announceCopied"
                                  : "promptQueue.announceCopyFailed",
                              ),
                            ),
                          );
                        }}
                      >
                        <HugeiconsIcon icon={Copy01Icon} strokeWidth={1.8} />
                        {t("promptQueue.copyItem")}
                      </DropdownMenuItem>
                      <DropdownMenuSeparator />
                      <Tooltip
                        delayDuration={300}
                        disableHoverableContent={true}
                      >
                        <TooltipTrigger
                          asChild={true}
                          disableClickToggle={true}
                        >
                          <DropdownMenuItem
                            onSelect={() => {
                              const behavior =
                                followUpBehavior === "queue"
                                  ? "steer"
                                  : "queue";
                              setFollowUpBehavior(behavior);
                              setAnnouncement(
                                t(
                                  behavior === "queue"
                                    ? "promptQueue.announceQueueingOn"
                                    : "promptQueue.announceQueueingOff",
                                ),
                              );
                            }}
                          >
                            <ListEndIcon />
                            {t(
                              followUpBehavior === "queue"
                                ? "promptQueue.turnOffQueueing"
                                : "promptQueue.turnOnQueueing",
                            )}
                          </DropdownMenuItem>
                        </TooltipTrigger>
                        <TooltipContent
                          side="top"
                          align="end"
                          sideOffset={8}
                          collisionPadding={8}
                          className="prompt-queue-option-tooltip pointer-events-none max-w-[min(20rem,calc(100vw-1rem))]"
                        >
                          {t(
                            followUpBehavior === "queue"
                              ? "promptQueue.queueingOffHint"
                              : "promptQueue.queueingOnHint",
                          )}{" "}
                          {t("promptQueue.queueingHintShared")}
                        </TooltipContent>
                      </Tooltip>
                      {entry.paused && (
                        <DropdownMenuItem onSelect={onResume}>
                          <QueueResumeIcon className="size-3.5" />
                          {t("promptQueue.resume")}
                        </DropdownMenuItem>
                      )}
                    </NonModalDropdownMenu>
                  </div>
                )}
              </div>
            );
          })}
        </div>
      </div>
    </div>
  );
}