2026-10-06 00:46:38 +08:00
|
|
|
import {
|
|
|
|
|
PromptNavigationIcon,
|
|
|
|
|
} from "@/components/icons/product-icons";
|
2026-11-05 17:09:20 +08:00
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
2026-10-06 00:46:38 +08:00
|
|
|
import { Search } from "lucide-react";
|
2026-06-06 19:49:33 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
2026-11-05 17:09:20 +08:00
|
|
|
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
2026-06-06 19:49:33 +08:00
|
|
|
import {
|
|
|
|
|
type PromptAnchor,
|
|
|
|
|
userPromptAnchors,
|
|
|
|
|
} from "@/components/thread/promptNavigation";
|
2026-10-13 13:56:28 +08:00
|
|
|
import { fmtDateTime } from "@/lib/format-time";
|
2026-06-06 19:49:33 +08:00
|
|
|
import type { UIMessage } from "@/lib/types";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
interface PromptNavigatorProps {
|
|
|
|
|
messages: UIMessage[];
|
2026-10-18 14:57:23 +08:00
|
|
|
prompts?: PromptAnchor[];
|
2026-06-06 19:49:33 +08:00
|
|
|
onJumpToPrompt: (promptId: string) => void;
|
2026-11-05 17:09:20 +08:00
|
|
|
shortcutEnabled?: boolean;
|
2026-06-06 19:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-05 17:09:20 +08:00
|
|
|
const ROW_HEIGHT = 64;
|
2026-10-18 14:57:23 +08:00
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
export function PromptNavigator({
|
|
|
|
|
messages,
|
2026-10-18 14:57:23 +08:00
|
|
|
prompts: suppliedPrompts,
|
2026-06-06 19:49:33 +08:00
|
|
|
onJumpToPrompt,
|
2026-11-05 17:09:20 +08:00
|
|
|
shortcutEnabled = true,
|
2026-06-06 19:49:33 +08:00
|
|
|
}: PromptNavigatorProps) {
|
|
|
|
|
const { i18n, t } = useTranslation();
|
2026-10-18 14:57:23 +08:00
|
|
|
const prompts = useMemo(() => suppliedPrompts ?? userPromptAnchors(messages), [suppliedPrompts, messages]);
|
2026-06-06 19:49:33 +08:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
const [query, setQuery] = useState("");
|
2026-10-18 14:57:23 +08:00
|
|
|
const listRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const [listTop, setListTop] = useState(0);
|
2026-11-05 17:09:20 +08:00
|
|
|
const [highlighted, setHighlighted] = useState(0);
|
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
const triggerRef = useRef<HTMLButtonElement>(null);
|
2026-11-13 19:07:59 +08:00
|
|
|
const openerRef = useRef<HTMLElement | null>(null);
|
2026-11-05 17:09:20 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const keyDown = (event: KeyboardEvent) => {
|
|
|
|
|
if (!shortcutEnabled || !prompts.length || event.defaultPrevented || event.repeat || event.isComposing
|
|
|
|
|
|| event.altKey || event.shiftKey || !(event.ctrlKey || event.metaKey) || event.key.toLowerCase() !== "f") return;
|
|
|
|
|
if (!open && (triggerRef.current?.closest("[hidden], [aria-hidden=true]") || document.querySelector("[role=dialog][data-state=open]"))) return;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
setListTop(0); setOpen(true);
|
|
|
|
|
inputRef.current?.focus(); inputRef.current?.select();
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener("keydown", keyDown);
|
|
|
|
|
return () => window.removeEventListener("keydown", keyDown);
|
|
|
|
|
}, [shortcutEnabled, prompts.length, open]);
|
|
|
|
|
useEffect(() => { if (open) { setQuery(""); setHighlighted(0); setListTop(0); } }, [open]);
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
const filteredPrompts = useMemo(() => {
|
|
|
|
|
const needle = query.trim().toLocaleLowerCase();
|
|
|
|
|
if (!needle) return prompts;
|
|
|
|
|
return prompts.filter((prompt) =>
|
|
|
|
|
`${prompt.label}\n${prompt.preview}`.toLocaleLowerCase().includes(needle),
|
|
|
|
|
);
|
|
|
|
|
}, [prompts, query]);
|
2026-10-18 14:57:23 +08:00
|
|
|
const firstRow = Math.max(0, Math.floor(listTop / ROW_HEIGHT) - 2);
|
|
|
|
|
const lastRow = firstRow + Math.ceil(window.innerHeight / ROW_HEIGHT) + 5;
|
2026-06-06 19:49:33 +08:00
|
|
|
|
|
|
|
|
if (prompts.length === 0) return null;
|
|
|
|
|
|
|
|
|
|
const jump = (promptId: string) => {
|
|
|
|
|
setOpen(false);
|
|
|
|
|
onJumpToPrompt(promptId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Button
|
2026-11-05 17:09:20 +08:00
|
|
|
ref={triggerRef}
|
2026-06-06 19:49:33 +08:00
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className={cn(
|
2026-10-11 04:30:43 +08:00
|
|
|
"host-no-drag h-8 w-8 rounded-control text-muted-foreground/80",
|
2026-10-06 00:46:38 +08:00
|
|
|
"hover:text-foreground",
|
2026-06-06 19:49:33 +08:00
|
|
|
)}
|
|
|
|
|
aria-label={t("thread.promptNavigator.open")}
|
2026-11-05 17:09:20 +08:00
|
|
|
aria-keyshortcuts="Control+F Meta+F"
|
2026-10-18 14:57:23 +08:00
|
|
|
onClick={() => { setListTop(0); setOpen(true); }}
|
2026-06-06 19:49:33 +08:00
|
|
|
>
|
2026-10-06 00:46:38 +08:00
|
|
|
<PromptNavigationIcon className="h-4 w-4" />
|
2026-06-06 19:49:33 +08:00
|
|
|
</Button>
|
|
|
|
|
|
2026-11-05 17:09:20 +08:00
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
|
<DialogContent
|
|
|
|
|
showCloseButton={false}
|
2026-06-06 19:49:33 +08:00
|
|
|
aria-describedby={undefined}
|
2026-11-13 19:07:59 +08:00
|
|
|
onOpenAutoFocus={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
openerRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
|
|
|
inputRef.current?.focus();
|
|
|
|
|
}}
|
|
|
|
|
onCloseAutoFocus={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const target = openerRef.current?.isConnected ? openerRef.current : triggerRef.current;
|
|
|
|
|
target?.focus({ preventScroll: true });
|
|
|
|
|
}}
|
2026-11-05 17:09:20 +08:00
|
|
|
className="session-search-dialog flex max-h-[min(40rem,100%)] w-[calc(100vw-2rem)] max-w-[42rem] flex-col gap-0 overflow-hidden p-0"
|
2026-06-06 19:49:33 +08:00
|
|
|
>
|
2026-11-05 17:09:20 +08:00
|
|
|
<DialogTitle className="sr-only">
|
2026-06-06 19:49:33 +08:00
|
|
|
{t("thread.promptNavigator.title")}
|
2026-11-05 17:09:20 +08:00
|
|
|
</DialogTitle>
|
|
|
|
|
<div className="session-search-input flex h-[62px] shrink-0 items-center gap-3 border-b border-border px-[18px]">
|
2026-06-06 19:49:33 +08:00
|
|
|
<Search
|
|
|
|
|
aria-hidden
|
2026-11-05 17:09:20 +08:00
|
|
|
className="h-[18px] w-[18px] shrink-0 text-muted-foreground"
|
2026-06-06 19:49:33 +08:00
|
|
|
/>
|
2026-11-05 17:09:20 +08:00
|
|
|
<input
|
|
|
|
|
ref={inputRef}
|
2026-06-06 19:49:33 +08:00
|
|
|
value={query}
|
2026-10-18 14:57:23 +08:00
|
|
|
onChange={(event) => {
|
|
|
|
|
setQuery(event.target.value);
|
|
|
|
|
setListTop(0);
|
2026-11-05 17:09:20 +08:00
|
|
|
setHighlighted(0);
|
2026-10-18 14:57:23 +08:00
|
|
|
if (listRef.current) listRef.current.scrollTop = 0;
|
|
|
|
|
}}
|
2026-06-06 19:49:33 +08:00
|
|
|
aria-label={t("thread.promptNavigator.search")}
|
|
|
|
|
placeholder={t("thread.promptNavigator.search")}
|
2026-11-05 17:09:20 +08:00
|
|
|
onKeyDown={(event) => {
|
|
|
|
|
if (event.key === "Enter" && filteredPrompts[highlighted]) { event.preventDefault(); jump(filteredPrompts[highlighted].id); }
|
|
|
|
|
if ((event.key === "ArrowDown" || event.key === "ArrowUp") && filteredPrompts.length) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const next = (highlighted + (event.key === "ArrowDown" ? 1 : -1) + filteredPrompts.length) % filteredPrompts.length;
|
|
|
|
|
setHighlighted(next);
|
|
|
|
|
if (listRef.current) { listRef.current.scrollTop = next * ROW_HEIGHT; setListTop(next * ROW_HEIGHT); }
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
className="h-full min-w-0 flex-1 bg-transparent text-[19px] font-normal leading-none text-foreground outline-none placeholder:text-muted-foreground"
|
2026-06-06 19:49:33 +08:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-11-05 17:09:20 +08:00
|
|
|
<div ref={listRef} className="min-h-0 flex-1 overflow-y-auto overscroll-contain p-2.5 scrollbar-thin scrollbar-track-transparent"
|
2026-10-18 14:57:23 +08:00
|
|
|
onScroll={(event) => setListTop(event.currentTarget.scrollTop)}
|
|
|
|
|
onKeyDown={(event) => {
|
|
|
|
|
const button = (event.target as HTMLElement).closest<HTMLButtonElement>("[data-prompt-row]");
|
|
|
|
|
if (!button || !listRef.current) return;
|
|
|
|
|
const index = Number(button.dataset.promptRow);
|
|
|
|
|
let next = { ArrowDown: index + 1, ArrowUp: index - 1, Home: 0, End: filteredPrompts.length - 1 }[event.key];
|
|
|
|
|
if (event.key === "Tab") {
|
|
|
|
|
if (!event.shiftKey && index === lastRow - 1) next = index + 1;
|
|
|
|
|
if (event.shiftKey && index === firstRow && firstRow > 0) next = index - 1;
|
|
|
|
|
}
|
|
|
|
|
if (next === undefined || next < 0 || next >= filteredPrompts.length) return;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
listRef.current.scrollTop = next * ROW_HEIGHT;
|
|
|
|
|
setListTop(listRef.current.scrollTop);
|
|
|
|
|
requestAnimationFrame(() => listRef.current?.querySelector<HTMLButtonElement>(`[data-prompt-row="${next}"]`)?.focus());
|
|
|
|
|
}}>
|
2026-06-06 19:49:33 +08:00
|
|
|
{filteredPrompts.length > 0 ? (
|
2026-10-18 14:57:23 +08:00
|
|
|
<div style={{ paddingTop: firstRow * ROW_HEIGHT, paddingBottom: Math.max(0, filteredPrompts.length - lastRow) * ROW_HEIGHT }}>
|
|
|
|
|
{filteredPrompts.slice(firstRow, lastRow).map((prompt, offset) => (
|
2026-06-06 19:49:33 +08:00
|
|
|
<PromptNavigatorRow
|
|
|
|
|
key={prompt.id}
|
2026-10-18 14:57:23 +08:00
|
|
|
row={firstRow + offset}
|
2026-11-05 17:09:20 +08:00
|
|
|
highlighted={highlighted === firstRow + offset}
|
|
|
|
|
onHighlight={() => setHighlighted(firstRow + offset)}
|
2026-06-06 19:49:33 +08:00
|
|
|
locale={i18n.resolvedLanguage || i18n.language}
|
|
|
|
|
prompt={prompt}
|
|
|
|
|
onJump={jump}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="px-3 py-10 text-center text-sm text-muted-foreground">
|
|
|
|
|
{t("thread.promptNavigator.noResults")}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-11-05 17:09:20 +08:00
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
2026-06-06 19:49:33 +08:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface PromptNavigatorRowProps {
|
2026-10-18 14:57:23 +08:00
|
|
|
row: number;
|
2026-06-06 19:49:33 +08:00
|
|
|
locale: string;
|
|
|
|
|
onJump: (promptId: string) => void;
|
|
|
|
|
prompt: PromptAnchor;
|
2026-11-05 17:09:20 +08:00
|
|
|
highlighted: boolean;
|
|
|
|
|
onHighlight: () => void;
|
2026-06-06 19:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function PromptNavigatorRow({
|
2026-10-18 14:57:23 +08:00
|
|
|
row,
|
2026-06-06 19:49:33 +08:00
|
|
|
locale,
|
|
|
|
|
onJump,
|
|
|
|
|
prompt,
|
2026-11-05 17:09:20 +08:00
|
|
|
highlighted,
|
|
|
|
|
onHighlight,
|
2026-06-06 19:49:33 +08:00
|
|
|
}: PromptNavigatorRowProps) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const timestamp = fmtDateTime(prompt.createdAt, locale);
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-10-18 14:57:23 +08:00
|
|
|
data-prompt-row={row}
|
2026-06-06 19:49:33 +08:00
|
|
|
className={cn(
|
2026-11-05 17:09:20 +08:00
|
|
|
"h-16 w-full overflow-hidden rounded-control px-3 py-2 text-left transition-colors",
|
|
|
|
|
"hover:bg-muted focus-visible:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/30",
|
|
|
|
|
highlighted && "bg-muted",
|
2026-06-06 19:49:33 +08:00
|
|
|
)}
|
|
|
|
|
aria-label={t("thread.promptNavigator.jumpTo", { label: prompt.label })}
|
|
|
|
|
onClick={() => onJump(prompt.id)}
|
2026-11-05 17:09:20 +08:00
|
|
|
onMouseEnter={onHighlight}
|
2026-06-06 19:49:33 +08:00
|
|
|
>
|
2026-11-05 17:09:20 +08:00
|
|
|
<div className="truncate text-[14px] font-medium leading-5 text-foreground">
|
2026-06-06 19:49:33 +08:00
|
|
|
{prompt.preview}
|
|
|
|
|
</div>
|
|
|
|
|
{timestamp ? (
|
2026-11-05 17:09:20 +08:00
|
|
|
<div className="mt-1 text-[12px] leading-4 text-muted-foreground">
|
2026-06-06 19:49:33 +08:00
|
|
|
{timestamp}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|