import { useEffect, useRef, useState } from 'react'; import { ArrowUpRight, FileCode2, LoaderCircle, RefreshCw, Search } from 'lucide-react'; import { api } from './api'; import type { CodeQuery, CodeQueryKind, CodeResult, Project, ProjectCacheStatus } from './types'; import './CodeIndexPanel.css'; const kindNames: Record = { function: '函数', method: '方法', class: '类', interface: '接口', type: '类型', enum: '枚举', binding: '变量' }; const resultName = (result: CodeResult) => result.resultKind === 'symbol' ? result.qualifiedName || result.name : result.name; const reasons: Record = { cold: '首次建立', memory: '本次进程复用', disk: '从本机派生缓存载入', disabled: '缓存已关闭', 'process-only': '仅本次进程', 'incomplete-scan': '读取范围不完整,重新分析可读文件', 'identity-changed': '分支、解析配置或项目身份变化', 'source-or-access-changed': '源码或读取权限变化', 'files-added-or-removed': '文件新增、删除或重命名', corrupt: '派生材料损坏,已重建', unavailable: '派生缓存不可用,已重建', capacity: '超出缓存容量,保留当前检索结果', stored: '已保存派生材料', 'cleared-during-refresh': '核对期间已清除,未重新填入缓存' }; const reason = (value: string) => reasons[value] ?? value; const size = (bytes: number) => bytes < 1024 * 1024 ? (bytes / 1024).toFixed(1) + ' KiB' : (bytes / (1024 * 1024)).toFixed(1) + ' MiB'; export function CodeIndexPanel({ project, active, onOpen }: { project: Project; active: boolean; onOpen: (path: string, line: number, sha256: string) => void; }) { const [query, setQuery] = useState(''); const [kind, setKind] = useState('all'); const [result, setResult] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(''); const [cacheInfo, setCacheInfo] = useState(null); const [cacheMessage, setCacheMessage] = useState(''); const [cacheError, setCacheError] = useState(''); const request = useRef(0); const initialLoad = useRef(false); async function search(nextKind = kind) { const current = ++request.current; setBusy(true); setError(''); setCacheMessage(''); try { const response = await api('/projects/' + project.id + '/code-query?query=' + encodeURIComponent(query.trim()) + '&kind=' + nextKind + '&limit=40'); if (current !== request.current || response.index.projectId !== project.id) return; setResult(response); if (response.index.cache) setCacheInfo({ ...response.index.cache, latest: response.index.cache }); } catch (cause) { if (current === request.current) setError(cause instanceof Error ? cause.message : String(cause)); } finally { if (current === request.current) setBusy(false); } } async function changeCache(action: 'toggle' | 'clear', enabled?: boolean) { const current = ++request.current; setBusy(true); setCacheError(''); setCacheMessage(''); try { const response = await (action === 'toggle' ? api('/project-cache', { enabled }) : api('/projects/' + project.id + '/index-cache', {}, 'DELETE')); if (current !== request.current) return; setCacheInfo(response); setCacheMessage(action === 'clear' ? '本项目派生缓存已清除;下次查询重新分析。工程、会话与原工件保留。' : enabled ? '本机解析缓存已启用;下次查询按当前源码核对。' : '本机解析缓存已关闭;后续查询从当前源码重新分析。'); } catch (cause) { if (current === request.current) setCacheError(cause instanceof Error ? cause.message : String(cause)); } finally { if (current === request.current) setBusy(false); } } useEffect(() => { request.current++; initialLoad.current = false; setResult(null); setCacheInfo(null); setCacheMessage(''); setCacheError(''); return () => { request.current++; initialLoad.current = false; }; }, [project.id, project.root]); useEffect(() => { if (active && !initialLoad.current) { initialLoad.current = true; void search(); } }, [active, project.id, project.root]); const index = result?.index, coverage = index?.coverage, metrics = cacheInfo?.latest ?? index?.cache; return ; }