"use client"; import * as React from "react"; import { AtSignIcon, ChevronRightIcon, Loader2Icon, XIcon } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import { InputGroupTextarea } from "@/components/ui/input-group"; import { Popover, PopoverAnchor, PopoverContent } from "@/components/ui/popover"; import { Textarea } from "@/components/ui/textarea"; import { api } from "@/lib/api"; import { activeMention, type ChatMention, mentionKinds, mentionSearch, mentionToken, selectedMentions, } from "@/lib/chat-mentions"; import { cn } from "@/lib/utils"; type Props = Omit, "value" | "onChange" | "ref"> & { value: string; onValueChange: (value: string) => void; inputGroup?: boolean; }; export function MentionTextarea({ value, onValueChange, onKeyDown, className, inputGroup, disabled, ...props }: Props) { const textarea = React.useRef(null); const composing = React.useRef(false); const pendingKeyboardIndex = React.useRef(null); const [cursor, setCursor] = React.useState(null); const [dismissed, setDismissed] = React.useState(false); const [activeIndex, setActiveIndex] = React.useState(0); const [result, setResult] = React.useState<{ key: string; items: ChatMention[]; next_cursor?: string; error?: string; loadingMore?: boolean; pageError?: string; } | null>(null); const request = React.useRef<{ key: string; controller: AbortController; loadingMore: boolean } | null>(null); const listId = React.useId(); const active = cursor === null ? null : activeMention(value, cursor); const search = mentionSearch(active?.query ?? ""); const open = !!active && !dismissed && !disabled; const categories = search.categories; const requestKey = open && !categories.length ? `${search.kind}:${search.query}` : ""; const loading = !!requestKey && result?.key !== requestKey; const items = result?.key === requestKey ? result.items : []; const error = result?.key === requestKey ? result.error : undefined; const nextCursor = result?.key === requestKey ? result.next_cursor : undefined; const loadingMore = result?.key === requestKey && result.loadingMore; const pageError = result?.key === requestKey ? result.pageError : undefined; const count = categories.length || items.length; const selected = selectedMentions(value); React.useEffect(() => { if (!requestKey) return; setResult(null); const controller = new AbortController(); request.current = { key: requestKey, controller, loadingMore: false }; const timer = setTimeout(() => { api.chatMentions(search.kind, search.query, controller.signal).then( (page) => { if (!controller.signal.aborted) setResult({ key: requestKey, ...page }); }, (error: Error) => { if (!controller.signal.aborted) setResult({ key: requestKey, items: [], error: error.message }); }, ); }, 180); return () => { clearTimeout(timer); controller.abort(); }; }, [requestKey, search.kind, search.query]); function loadMore() { const session = request.current; if ( !nextCursor || !session || session.key !== requestKey || session.controller.signal.aborted || session.loadingMore ) return; session.loadingMore = true; setResult((current) => current?.key === requestKey ? { ...current, loadingMore: true, pageError: undefined } : current, ); api .chatMentions(search.kind, search.query, session.controller.signal, nextCursor) .then( (page) => { if (session.controller.signal.aborted) return; setResult((current) => { if (current?.key !== session.key) return current; const seen = new Set(current.items.map((item) => `${item.kind}:${item.id}`)); return { ...current, items: [...current.items, ...page.items.filter((item) => !seen.has(`${item.kind}:${item.id}`))], next_cursor: page.next_cursor, loadingMore: false, }; }); }, (error: Error) => { if (!session.controller.signal.aborted) setResult((current) => current?.key === session.key ? { ...current, loadingMore: false, pageError: error.message } : current, ); }, ) .finally(() => { session.loadingMore = false; }); } React.useEffect(() => { if (open) document.getElementById(`${listId}-${activeIndex}`)?.scrollIntoView({ block: "nearest" }); }, [activeIndex, listId, open]); React.useEffect(() => { if (open && pendingKeyboardIndex.current !== null && pendingKeyboardIndex.current < count) { document.getElementById(`${listId}-${pendingKeyboardIndex.current}`)?.scrollIntoView({ block: "nearest" }); pendingKeyboardIndex.current = null; } }, [count, listId, open]); function replaceActive(text: string, close: boolean) { if (!active) return; const next = value.slice(0, active.start) + text + value.slice(active.end); const caret = active.start + text.length; onValueChange(next); setCursor(caret); setDismissed(close); setActiveIndex(0); requestAnimationFrame(() => { textarea.current?.focus(); textarea.current?.setSelectionRange(caret, caret); }); } function choose(index: number) { if (categories[index]) replaceActive(`@${categories[index].label} `, false); else if (items[index]) replaceActive(`${mentionToken(items[index])} `, true); } function keyDown(event: React.KeyboardEvent) { if (composing.current || event.nativeEvent.isComposing || event.keyCode === 229) return; if (open) { if (event.key === "Escape") { event.preventDefault(); setDismissed(true); return; } if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault(); if (event.key === "ArrowDown" && count && activeIndex >= count - 1 && nextCursor) { pendingKeyboardIndex.current = count; setActiveIndex(count); loadMore(); return; } if (count) setActiveIndex((index) => (index + (event.key === "ArrowDown" ? 1 : count - 1)) % count); return; } if ((event.key === "Enter" || event.key === "Tab") && !event.shiftKey && !event.ctrlKey && !event.metaKey) { if (event.key === "Tab" && (loading || !count)) { setDismissed(true); return; } event.preventDefault(); if (!loading && count) choose(Math.min(activeIndex, count - 1)); return; } } onKeyDown?.(event); } const Control = inputGroup ? InputGroupTextarea : Textarea; return (
{ if (!next) setDismissed(true); }} >
{ pendingKeyboardIndex.current = null; onValueChange(event.target.value); setCursor(event.target.selectionStart); setDismissed(false); setActiveIndex(0); }} onSelect={(event) => setCursor(event.currentTarget.selectionStart)} onCompositionStart={() => { composing.current = true; }} onCompositionEnd={() => { composing.current = false; }} onKeyDown={keyDown} />
event.preventDefault()} onCloseAutoFocus={(event) => event.preventDefault()} onInteractOutside={(event) => { if (event.target === textarea.current) event.preventDefault(); }} aria-label="选择引用记录" >
{categories.length ? "选择引用类型" : `搜索${mentionKinds.find((kind) => kind.kind === search.kind)?.label ?? "全部记录"}`} ↑↓ 选择 · Enter 确认 · Esc 关闭
{ const list = event.currentTarget; if (list.scrollHeight - list.scrollTop - list.clientHeight < 64) loadMore(); }} > {categories.map((kind, index) => ( ))} {!categories.length && loading && (
搜索中…
)} {!categories.length && !loading && error && (
搜索失败:{error}。请重新输入重试。
)} {!categories.length && !loading && !error && !items.length && (
没有匹配记录,请更换名称、地址或 ID
)} {items.map((item, index) => ( ))}
{!categories.length && (

{loadingMore && 正在加载更多…} {!loadingMore && nextCursor && ( )} {!loadingMore && !nextCursor && !loading && !error && items.length > 0 && `已显示全部 ${items.length} 条`} {!loadingMore && !nextCursor && (loading || !!error || items.length === 0) && "输入名称、地址或 ID 搜索记录"}

)}
{selected.length > 0 && (
{selected.map((item) => ( {item.label} ))} 发送时读取最新详情 · 最多 10 条
)}
); }