import { useEffect, useRef, useState } from 'react'; import { Check, Files, LoaderCircle, RotateCcw, ScanLine, ShieldCheck } from 'lucide-react'; import type { ChangeSet, Session } from './types'; import { api } from './api'; import { DiffView, Modal, Status } from './components'; import './ChangeSetPanel.css'; const openStates = ['prepared', 'applying', 'rolling_back', 'unknown', 'partial']; const labels: Record = { before: '原文件', after: '建议内容', external: '外部改动', unavailable: '无法读取', writing: '正在写入', restoring: '正在回退' }; export function ChangeSetPanel({ session, busy, onSession, onError, onFilesChanged }: { session: Session; busy: boolean; onSession: (session: Session) => void; onError: (error: unknown) => void; onFilesChanged: () => Promise; }) { const sets = session.execution?.changeSets ?? [], taskId = session.execution?.task?.id; const pending = session.changes.filter(change => change.taskId === taskId && change.status === 'pending' && !sets.some(set => set.id === change.changeSetId && openStates.includes(set.status))); const current = sets.slice().reverse().find(set => openStates.includes(set.status)) ?? sets.at(-1); const [stage, setStage] = useState<'select' | 'review' | null>(null); const [setId, setSetId] = useState(''), [fileId, setFileId] = useState(''); const [choices, setChoices] = useState([]), [working, setWorking] = useState(false); const [confirmation, setConfirmation] = useState<'rollback' | 'acknowledge' | null>(null); const scope = session.id + ':' + (taskId ?? ''), scopeRef = useRef(scope); scopeRef.current = scope; const filesSignature = sets.filter(set => ['completed', 'rolled_back', 'partial', 'unknown', 'resolved_external'].includes(set.status)) .map(set => set.id + ':' + set.status + ':' + set.finishedAt).join('|'); const lastFiles = useRef({ scope, signature: filesSignature }); useEffect(() => { scopeRef.current = scope; setStage(null); setSetId(''); setFileId(''); setChoices([]); setConfirmation(null); setWorking(false); return () => { if (scopeRef.current === scope) scopeRef.current = ''; }; }, [scope]); useEffect(() => { const previous = lastFiles.current; lastFiles.current = { scope, signature: filesSignature }; if (previous.scope === scope && previous.signature !== filesSignature) void onFilesChanged().catch(error => { if (scopeRef.current === scope) onError(error); }); }, [scope, filesSignature, onFilesChanged, onError]); const selected = sets.find(set => set.id === setId) ?? current; const file = selected?.files.find(value => value.changeId === fileId) ?? selected?.files[0]; const proposal = session.changes.find(change => change.id === file?.changeId); const active = !!selected && ['applying', 'rolling_back'].includes(selected.status); const disabled = busy || working || active; const close = () => { setStage(null); setConfirmation(null); }; async function prepare() { if (!choices.length || choices.length > 32 || disabled) return; const target = scope; setWorking(true); try { const result = await api('/sessions/' + session.id + '/change-sets', { changeIds: choices }); if (scopeRef.current !== target) return; onSession(result); const next = result.execution?.changeSets?.slice().reverse().find(set => set.status === 'prepared'); if (next) { setSetId(next.id); setFileId(next.files[0]?.changeId ?? ''); setStage('review'); } } catch (error) { if (scopeRef.current === target) onError(error); } finally { if (scopeRef.current === target) setWorking(false); } } async function act(set: ChangeSet, action: 'apply' | 'reconcile' | 'rollback' | 'discard' | 'acknowledge') { if (disabled) return; const target = scope; setWorking(true); try { const result = await api('/sessions/' + session.id + '/change-sets/' + set.id + '/' + action, { fingerprint: set.fingerprint, ...(action === 'acknowledge' ? { observationFingerprint: set.observationFingerprint } : {}) }); if (scopeRef.current === target) { onSession(result); setConfirmation(null); } } catch (error) { if (scopeRef.current === target) onError(error); } finally { if (scopeRef.current === target) setWorking(false); } } const canAcknowledge = selected?.status === 'partial' && !!selected.observationFingerprint && !!selected.observations?.length && selected.observations.every(value => ['before', 'external'].includes(value.state)); return <>
{current ? `多文件变更集 · ${current.files.length} 个文件` : '组合审查文件改动'} {current ? current.requiresVerification ? '实际文件需要核对,恢复入口保留在这个任务。' : current.summary : pending.length ? `${pending.length} 份提案可组合为一个审查批次。` : '本任务有提案后,可一起审查与应用。'}
{current && } {!current || !openStates.includes(current.status) ? : null}
{stage === 'select' &&
{pending.map(change => )}
已选择 {choices.length} / 32 项
} {stage === 'review' && selected &&
{selected.summary} {sets.length > 1 && }
{selected.files.map(item => { const observation = selected.observations?.find(value => value.path === item.path); return ; })}
{file?.path}提案差异
{proposal && }
审查指纹{selected.fingerprint}Git基线{selected.baseline.available ? `${selected.baseline.branch ?? '未命名'} · ${selected.baseline.head?.slice(0, 12) ?? '尚无提交'}` : '项目尚未启用Git'}
{confirmation &&
{confirmation === 'rollback' ? '回退本批次已写入的文件' : '保留外部改动并结束恢复'}

{confirmation === 'rollback' ? '只有当前字节仍等于本批次建议时才恢复原文,外部修改会保留。已新建的文件会按同样规则移除。' : '将保留目前已核对的外部内容,并结束这个未成功应用的批次。当前文件再次变化时会拒绝确认。'}

}
{active ? '文件批次正在执行,结果会持续更新。' : selected.requiresVerification ? '先核对实际文件,恢复不会自动续写。' : '应用前会核对所有文件及原审查指纹。'} {working && } {['prepared', 'stale'].includes(selected.status) && } {selected.status === 'prepared' && } {selected.requiresVerification && } {['completed', 'partial', 'unknown'].includes(selected.status) && } {canAcknowledge && }
} ; }