All checks were successful
Push & PR Check / check (push) Successful in 1m2s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
190 lines
7.5 KiB
TypeScript
190 lines
7.5 KiB
TypeScript
// 一键接入面板(agent-client-parity P8g):plan_onboarding 预览 → 人确认 → onboard_project → 分步结果。
|
||
// 全局挂载在 App,由 useUIStore.openOnboard 打开。判定全部来自后端,映射见 onboardView.ts。
|
||
import { useCallback, useEffect, useRef, useState } from "react";
|
||
import { useQueryClient } from "@tanstack/react-query";
|
||
import { onboardProject, planOnboarding, type OnboardReport, type OnboardingPlan } from "../../lib/commands";
|
||
import { useUIStore } from "../../store/ui";
|
||
import {
|
||
DEFAULT_SELECTION,
|
||
buildPlanView,
|
||
buildReportView,
|
||
confirmRequest,
|
||
selectionOptions,
|
||
setBypass,
|
||
toggleSkip,
|
||
type OnboardSelection,
|
||
} from "./onboardView";
|
||
import { PlanPreview, ReportResult } from "./OnboardSections";
|
||
|
||
type Phase =
|
||
| { kind: "planning" }
|
||
| { kind: "preview"; plan: OnboardingPlan; planned: OnboardSelection }
|
||
| { kind: "applying"; plan: OnboardingPlan; planned: OnboardSelection }
|
||
| { kind: "report"; report: OnboardReport }
|
||
| { kind: "error"; message: string };
|
||
|
||
export function OnboardPanel() {
|
||
const target = useUIStore((s) => s.onboardTarget);
|
||
if (!target) return null;
|
||
// key:换项目即重置面板状态
|
||
return <OnboardPanelBody key={target.projectId} />;
|
||
}
|
||
|
||
function OnboardPanelBody() {
|
||
const target = useUIStore((s) => s.onboardTarget)!;
|
||
const closeOnboard = useUIStore((s) => s.closeOnboard);
|
||
const showToast = useUIStore((s) => s.showToast);
|
||
const qc = useQueryClient();
|
||
const [phase, setPhase] = useState<Phase>({ kind: "planning" });
|
||
const [selection, setSelection] = useState<OnboardSelection>(DEFAULT_SELECTION);
|
||
const lastReport = useRef<OnboardReport | null>(null);
|
||
const requestSeq = useRef(0);
|
||
|
||
const plan = useCallback(
|
||
async (chosen: OnboardSelection) => {
|
||
const seq = ++requestSeq.current;
|
||
setPhase({ kind: "planning" });
|
||
try {
|
||
const next = await planOnboarding(target.projectId, selectionOptions(chosen));
|
||
if (seq === requestSeq.current) setPhase({ kind: "preview", plan: next, planned: chosen });
|
||
} catch (e) {
|
||
if (seq === requestSeq.current) setPhase({ kind: "error", message: String(e) });
|
||
}
|
||
},
|
||
[target.projectId],
|
||
);
|
||
|
||
useEffect(() => {
|
||
void plan(DEFAULT_SELECTION);
|
||
}, [plan]);
|
||
|
||
const applyConfirmed = async () => {
|
||
if (phase.kind !== "preview") return;
|
||
const request = confirmRequest(phase.plan, phase.planned, selection);
|
||
if (!request) return;
|
||
const seq = ++requestSeq.current;
|
||
setPhase({ kind: "applying", plan: phase.plan, planned: phase.planned });
|
||
try {
|
||
const report = await onboardProject(request.projectId, request.expectedDigest, request.confirmed, request.options);
|
||
if (seq !== requestSeq.current) return;
|
||
lastReport.current = report;
|
||
setPhase({ kind: "report", report });
|
||
showToast(buildReportView(report).toast, 4000);
|
||
void qc.invalidateQueries({ queryKey: ["projects"] });
|
||
} catch (e) {
|
||
if (seq === requestSeq.current) setPhase({ kind: "error", message: String(e) });
|
||
}
|
||
};
|
||
|
||
/** refused 附当前计划:回到预览,按新计划重新确认(选项沿用后端拒绝时的那组) */
|
||
const reviewReplan = (next: OnboardingPlan) => {
|
||
if (phase.kind !== "report") return;
|
||
setPhase({ kind: "preview", plan: next, planned: selection });
|
||
};
|
||
|
||
const close = () => {
|
||
if (phase.kind === "applying") return; // 写入进行中不允许关闭,避免误以为已取消
|
||
requestSeq.current += 1;
|
||
const report = lastReport.current;
|
||
closeOnboard();
|
||
target.onClosed?.(report);
|
||
};
|
||
|
||
const title = `一键接入${target.projectName ? ` · ${target.projectName}` : ""}`;
|
||
return (
|
||
<div className="fixed inset-0 z-[60] flex items-center justify-center bg-black/50">
|
||
<div className="bg-white rounded-xl shadow-2xl w-full max-w-3xl mx-4 flex flex-col" style={{ maxHeight: "88vh" }}>
|
||
<div className="flex items-center justify-between px-6 py-4 border-b border-gray-100 shrink-0">
|
||
<h2 className="text-base font-semibold text-gray-900">{title}</h2>
|
||
<button
|
||
onClick={close}
|
||
disabled={phase.kind === "applying"}
|
||
className="text-gray-400 hover:text-gray-600 text-xl leading-none disabled:opacity-30"
|
||
aria-label="关闭"
|
||
>
|
||
×
|
||
</button>
|
||
</div>
|
||
<div className="flex-1 min-h-0 overflow-y-auto px-6 py-4 space-y-4 text-sm">
|
||
{phase.kind === "planning" && <p className="text-gray-400">正在生成接入计划(只读,不写任何文件)…</p>}
|
||
{phase.kind === "error" && (
|
||
<p className="text-red-600 font-mono text-xs break-all">❌ {phase.message}</p>
|
||
)}
|
||
{(phase.kind === "preview" || phase.kind === "applying") && (
|
||
<PlanPreview
|
||
view={buildPlanView(phase.plan, phase.planned, selection)}
|
||
disabled={phase.kind === "applying"}
|
||
onToggleSkip={(item) => setSelection((s) => toggleSkip(s, item))}
|
||
onBypass={(on) => setSelection((s) => setBypass(s, on))}
|
||
/>
|
||
)}
|
||
{phase.kind === "report" && (
|
||
<ReportResult view={buildReportView(phase.report)} onReviewReplan={reviewReplan} />
|
||
)}
|
||
</div>
|
||
<PanelFooter
|
||
phase={phase}
|
||
selection={selection}
|
||
onCancel={close}
|
||
onReplan={() => void plan(selection)}
|
||
onConfirm={() => void applyConfirmed()}
|
||
/>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const btnCls = "px-4 py-2 rounded-lg text-sm border transition-colors disabled:opacity-50";
|
||
|
||
function PanelFooter({
|
||
phase,
|
||
selection,
|
||
onCancel,
|
||
onReplan,
|
||
onConfirm,
|
||
}: {
|
||
phase: Phase;
|
||
selection: OnboardSelection;
|
||
onCancel: () => void;
|
||
onReplan: () => void;
|
||
onConfirm: () => void;
|
||
}) {
|
||
const view = phase.kind === "preview" ? buildPlanView(phase.plan, phase.planned, selection) : null;
|
||
return (
|
||
<div className="flex items-center justify-end gap-3 px-6 py-4 border-t border-gray-100 shrink-0">
|
||
{view?.confirm.reason && <p className="flex-1 text-xs text-amber-700">{view.confirm.reason}</p>}
|
||
{phase.kind === "applying" && (
|
||
<p className="flex-1 text-xs text-gray-500">正在接入:首次可能 30 秒以上,完成前请勿关闭…</p>
|
||
)}
|
||
{phase.kind === "report" ? (
|
||
<button onClick={onCancel} className={`${btnCls} border-gray-200 text-gray-700 hover:bg-gray-50`}>
|
||
关闭
|
||
</button>
|
||
) : (
|
||
<button
|
||
onClick={onCancel}
|
||
disabled={phase.kind === "applying"}
|
||
className={`${btnCls} border-gray-200 text-gray-600 hover:bg-gray-50`}
|
||
title={phase.kind === "error" ? "关闭面板;若错误发生在确认接入之后,可能已有部分写入,请重新计划核对" : "取消不写入任何文件"}
|
||
>
|
||
取消
|
||
</button>
|
||
)}
|
||
{(phase.kind === "error" || (view && (view.needsReplan || view.blockingCount > 0))) && (
|
||
<button onClick={onReplan} className={`${btnCls} border-blue-200 text-blue-700 hover:bg-blue-50`}>
|
||
{phase.kind === "error" ? "重试" : "重新计划"}
|
||
</button>
|
||
)}
|
||
{(phase.kind === "preview" || phase.kind === "applying") && (
|
||
<button
|
||
onClick={onConfirm}
|
||
disabled={phase.kind === "applying" || !view?.confirm.enabled}
|
||
className={`${btnCls} border-transparent bg-blue-600 text-white hover:bg-blue-700`}
|
||
>
|
||
{phase.kind === "applying" ? "接入中…" : "确认接入"}
|
||
</button>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|