29 lines
4.1 KiB
TypeScript
29 lines
4.1 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import { ClipboardCheck, FolderOpen, Monitor, RotateCw, Square } from 'lucide-react';
|
||
import type { OpenFile, Project, Session } from './types';
|
||
import { desktop, type DesktopHostStatus, type DesktopTaskScope } from './desktop';
|
||
|
||
export function DesktopPanel({ project, session, file, onProject, onReview }: {
|
||
project?: Project; session: Session | null; file: OpenFile | null; onProject(project: Project): Promise<void>; onReview(scope: DesktopTaskScope): void;
|
||
}) {
|
||
const [status, setStatus] = useState<DesktopHostStatus | null>(null), [busy, setBusy] = useState(''), [error, setError] = useState('');
|
||
const alive = useRef(true);
|
||
useEffect(() => { alive.current = true; void refresh(); return () => { alive.current = false; }; }, []);
|
||
async function act(name: string, action: () => Promise<unknown>) {
|
||
if (busy || !desktop) return;
|
||
setBusy(name); setError('');
|
||
try { await action(); } catch (value) { if (alive.current) setError(value instanceof Error ? value.message : '本机操作失败,请核对当前宿主。'); }
|
||
finally { if (alive.current) setBusy(''); }
|
||
}
|
||
async function refresh() { await act('refresh', async () => { const value = await desktop!.status(); if (alive.current) setStatus(value); }); }
|
||
const task = session?.execution?.task, scope = project && session && task ? { projectId: project.id, sessionId: session.id, taskId: task.id } : null;
|
||
return <div className="desktop-panel">
|
||
<div className="desktop-host-heading"><Monitor size={21} /><div><strong>{status?.state === 'ready' ? '执行宿主正在运行' : status?.state === 'stopping' ? '所属任务正在停止' : '正在核对执行宿主'}</strong><p>关闭窗口后,后台任务继续。重新打开可查看原任务进度。</p></div><button className="button secondary" disabled={!!busy} onClick={() => void refresh()}><RotateCw size={14} />刷新</button></div>
|
||
{status?.identity && <dl className="desktop-host-facts"><dt>当前程序</dt><dd>v{status.identity.version} · PID {status.identity.pid}</dd><dt>后台任务</dt><dd>{status.activity?.active ? '有所属操作正在执行' : '当前没有活动操作'} · {status.activity?.unresolvedTools || 0} 项结果待核对</dd><dt>桌面窗口</dt><dd>{status.desktopWindows?.count || 0} 个已登记窗口</dd><dt>会话与连接</dt><dd className="code-utility">{status.identity.dataRoot}</dd><dt>运行目录</dt><dd className="code-utility">{status.identity.runtimeRoot}</dd></dl>}
|
||
<div className="desktop-project-actions"><button className="button primary" disabled={!!busy} onClick={() => void act('pick', async () => { const picked = await desktop!.pickProject(); if (picked && alive.current) await onProject(picked); })}><FolderOpen size={15} />选择项目文件夹</button><button className="button secondary" disabled={!!busy || !project} onClick={() => void act('folder', () => desktop!.projectFolder(project!.id))}>打开项目目录</button><button className="button secondary" disabled={!!busy || !scope} onClick={() => void act('review', async () => { const original = scope!; await desktop!.reviewTask(original); if (alive.current) onReview(original); })}><ClipboardCheck size={15} />审查当前任务</button><button className="button secondary" disabled={!!busy || !scope || !file} onClick={() => void act('reveal', () => desktop!.revealFile({ ...scope!, path: file!.path, sha256: file!.sha256 }))}>定位当前文件</button></div>
|
||
{!scope && <p className="desktop-scope-note">选择已有任务后,可核对任务归属并审查成果。读取文件后可在文件管理器中定位。</p>}
|
||
{error && <p className="desktop-error" role="alert">{error}</p>}
|
||
<div className="desktop-stop-row"><div><strong>退出执行宿主</strong><p>停止所属任务、模型请求和工具进程,再退出桌面。系统会要求确认。</p></div><button className="button secondary" disabled={!!busy} onClick={() => void act('stop', async () => { const result = await desktop!.stopHost(); if (result.cancelled && alive.current) { const updated = await desktop!.status(); if (alive.current) setStatus(updated); } })}><Square size={13} />停止并退出</button></div>
|
||
</div>;
|
||
}
|