30 lines
2.9 KiB
TypeScript
30 lines
2.9 KiB
TypeScript
import { useMemo, useRef, useState } from 'react';
|
|
import type { ComponentType } from 'react';
|
|
import type { LucideProps } from 'lucide-react';
|
|
import { ArrowUpRight, Search } from 'lucide-react';
|
|
|
|
export type WorkbenchAction = { id: string; label: string; description: string; group: string; icon: ComponentType<LucideProps>; run: () => void; disabled?: boolean; reason?: string };
|
|
|
|
export function WorkbenchTools({ actions, onClose }: { actions: WorkbenchAction[]; onClose: () => void }) {
|
|
const [query, setQuery] = useState('');
|
|
const list = useRef<HTMLDivElement>(null);
|
|
const filtered = useMemo(() => actions.filter(action => (action.label + action.description + action.group).toLocaleLowerCase().includes(query.trim().toLocaleLowerCase())), [actions, query]);
|
|
return <div className="workbench-tools">
|
|
<div className="tools-search"><Search size={18} /><input autoFocus data-autofocus aria-label="搜索工作台工具" value={query} onChange={event => setQuery(event.target.value)} placeholder="搜索工具、文件或任务操作…" onKeyDown={event => {
|
|
if (event.key === 'ArrowDown') { event.preventDefault(); list.current?.querySelector<HTMLButtonElement>('button:not(:disabled)')?.focus(); }
|
|
if (event.key === 'Enter') { const first = filtered.find(action => !action.disabled); if (first) { onClose(); first.run(); } }
|
|
}} /><kbd>Esc</kbd></div>
|
|
<div className="tools-results" ref={list} onKeyDown={event => {
|
|
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
|
|
const buttons = Array.from(list.current?.querySelectorAll<HTMLButtonElement>('button:not(:disabled)') ?? []);
|
|
if (!buttons.length) return;
|
|
event.preventDefault(); const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
|
|
buttons[event.key === 'Home' ? 0 : event.key === 'End' ? buttons.length - 1 : (index + (event.key === 'ArrowDown' ? 1 : -1) + buttons.length) % buttons.length]?.focus();
|
|
}}>
|
|
{[...new Set(filtered.map(action => action.group))].map(group => <section key={group}><h3>{group}</h3>{filtered.filter(action => action.group === group).map(action => <button key={action.id} aria-label={action.label} disabled={action.disabled} title={action.disabled ? action.reason : action.description} onClick={() => { onClose(); action.run(); }}><span className="tools-action-icon"><action.icon size={18} /></span><span><strong>{action.label}</strong><small>{action.disabled ? action.reason || '当前不可用' : action.description}</small></span><ArrowUpRight size={15} /></button>)}</section>)}
|
|
{!filtered.length && <div className="tools-empty"><Search size={23} /><strong>没有找到“{query}”</strong><p>试试“模型”“文件”或“研究”。</p><button type="button" className="text-button" onClick={() => setQuery('')}>清除搜索</button></div>}
|
|
</div>
|
|
<div className="tools-footnote"><span>↑ ↓ 选择工具 · Enter 打开</span><span>Ctrl K 随时唤起</span></div>
|
|
</div>;
|
|
}
|