import { PromptNavigationIcon, } from "@/components/icons/product-icons"; import { useMemo, useRef, useState } from "react"; import { Search } from "lucide-react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { SearchInput } from "@/components/ui/input"; import { Sheet, SheetContent, SheetTitle, } from "@/components/ui/sheet"; import { type PromptAnchor, userPromptAnchors, } from "@/components/thread/promptNavigation"; import { fmtDateTime } from "@/lib/format-time"; import type { UIMessage } from "@/lib/types"; import { cn } from "@/lib/utils"; interface PromptNavigatorProps { messages: UIMessage[]; prompts?: PromptAnchor[]; onJumpToPrompt: (promptId: string) => void; } const ROW_HEIGHT = 88; export function PromptNavigator({ messages, prompts: suppliedPrompts, onJumpToPrompt, }: PromptNavigatorProps) { const { i18n, t } = useTranslation(); const prompts = useMemo(() => suppliedPrompts ?? userPromptAnchors(messages), [suppliedPrompts, messages]); const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const listRef = useRef(null); const [listTop, setListTop] = useState(0); 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]); const firstRow = Math.max(0, Math.floor(listTop / ROW_HEIGHT) - 2); const lastRow = firstRow + Math.ceil(window.innerHeight / ROW_HEIGHT) + 5; if (prompts.length === 0) return null; const jump = (promptId: string) => { setOpen(false); onJumpToPrompt(promptId); }; return ( <>
{t("thread.promptNavigator.title")}
{ setQuery(event.target.value); setListTop(0); if (listRef.current) listRef.current.scrollTop = 0; }} aria-label={t("thread.promptNavigator.search")} placeholder={t("thread.promptNavigator.search")} className="h-10 rounded-full border-border bg-background pl-9 pr-3 text-sm transition-colors" />
setListTop(event.currentTarget.scrollTop)} onKeyDown={(event) => { const button = (event.target as HTMLElement).closest("[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(`[data-prompt-row="${next}"]`)?.focus()); }}> {filteredPrompts.length > 0 ? (
{filteredPrompts.slice(firstRow, lastRow).map((prompt, offset) => ( ))}
) : (
{t("thread.promptNavigator.noResults")}
)}
); } interface PromptNavigatorRowProps { row: number; locale: string; onJump: (promptId: string) => void; prompt: PromptAnchor; } function PromptNavigatorRow({ row, locale, onJump, prompt, }: PromptNavigatorRowProps) { const { t } = useTranslation(); const timestamp = fmtDateTime(prompt.createdAt, locale); return ( ); }