diff --git a/apps/blogai/components/header.tsx b/apps/blogai/components/header.tsx index b3e29bc..3be151b 100644 --- a/apps/blogai/components/header.tsx +++ b/apps/blogai/components/header.tsx @@ -433,9 +433,8 @@ export function DetailPageHeader({ data }: { data: any }) { setProgress(match[1] + "%"); if (match[1] === "100") { - setStatusText("运行中"); - setProgressBarColor("bg-green-500"); - setShowDelete(true); // 部署完成允许删除 + console.log("部署完成 ✅,重新拉取状态!"); + fetchDeployStatus(); // ✅ 直接重新请求后端最新状态 } } @@ -555,75 +554,143 @@ export function DetailPageHeader({ data }: { data: any }) { } }; + const fetchDeployStatus = async () => { + try { + const result = await fetch("/api/v1/deploy/status", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ id: data?.id }), + }).then((res) => res.json()); + + console.log("====> deploy status result:", result); + setStatusLoaded(true); + + if (!result) { + setStatusText("接口响应为空"); + setShowDelete(false); + return; + } + + const code = result?.header?.code; + const status = result?.data?.data?.status; + const userData = JSON.parse(localStorage.getItem("UserData") || "null"); + const userName = userData?.user_name; + const id = data?.id; + + console.log("🟡 状态码 code =", code, "状态 status =", status); + + if (code === 1006) { + setStatusText("尚未部署"); + setShowDelete(false); + setProgress("0%"); + setShowProgressBar(false); + return; + } + + if (status === "deploying" && userName && id && !hasWSConnected) { + setStatusText("检测到正在部署,连接中..."); + initWebSocket(userName, id); + setShowProgressBar(true); + } + + if (status === "running" || status === "stopped") { + console.log("✅ 允许删除(status =", status, ")"); + setShowDelete(true); + setProgress("100%"); + setShowProgressBar(true); + + if (status === "running") { + setStatusText("运行中"); + setProgressBarColor("bg-green-500"); + } else if (status === "stopped") { + setStatusText("已停止"); + setProgressBarColor("bg-gray-400"); + } + } else { + console.log("❌ 不允许删除(status =", status, ")"); + setShowDelete(false); + setProgressBarColor("bg-blue-500"); + } + } catch (err) { + console.error("获取状态失败:", err); + setStatusText("状态拉取失败"); + setShowDelete(false); + } + }; useEffect(() => { - const fetchDeployStatus = async () => { - try { - const result = await fetch("/api/v1/deploy/status", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ id: data?.id }), - }).then((res) => res.json()); - - console.log("====> deploy status result:", result); - setStatusLoaded(true); - - if (!result) { - setStatusText("接口响应为空"); - setShowDelete(false); - return; - } - - const code = result?.header?.code; - const status = result?.data?.data?.status; - const userData = JSON.parse(localStorage.getItem("UserData") || "null"); - const userName = userData?.user_name; - const id = data?.id; - - console.log("🟡 状态码 code =", code, "状态 status =", status); - - if (code === 1006) { - setStatusText("尚未部署"); - setShowDelete(false); - setProgress("0%"); - setShowProgressBar(false) - return; - } - - if (status === "deploying" && userName && id && !hasWSConnected) { - setStatusText("检测到正在部署,连接中..."); - initWebSocket(userName, id); - setShowProgressBar(true) - } - - if (status === "running" || status === "stopped") { - console.log("✅ 允许删除(status =", status, ")"); - setShowDelete(true); - setProgress("100%"); - setShowProgressBar(true) - - if (status === "running") { - setStatusText("运行中"); - setProgressBarColor("bg-green-500"); - } else if (status === "stopped") { - setStatusText("已停止"); - setProgressBarColor("bg-gray-400"); - } - } else { - console.log("❌ 不允许删除(status =", status, ")"); - setShowDelete(false); - setProgressBarColor("bg-blue-500"); // 回到默认蓝色 - } - } catch (err) { - console.error("获取状态失败:", err); - setStatusText("状态拉取失败"); - setShowDelete(false); - } - }; - fetchDeployStatus(); }, [data?.id]); + + // useEffect(() => { + // const fetchDeployStatus = async () => { + // try { + // const result = await fetch("/api/v1/deploy/status", { + // method: "POST", + // headers: { "Content-Type": "application/json" }, + // body: JSON.stringify({ id: data?.id }), + // }).then((res) => res.json()); + + // console.log("====> deploy status result:", result); + // setStatusLoaded(true); + + // if (!result) { + // setStatusText("接口响应为空"); + // setShowDelete(false); + // return; + // } + + // const code = result?.header?.code; + // const status = result?.data?.data?.status; + // const userData = JSON.parse(localStorage.getItem("UserData") || "null"); + // const userName = userData?.user_name; + // const id = data?.id; + + // console.log("🟡 状态码 code =", code, "状态 status =", status); + + // if (code === 1006) { + // setStatusText("尚未部署"); + // setShowDelete(false); + // setProgress("0%"); + // setShowProgressBar(false) + // return; + // } + + // if (status === "deploying" && userName && id && !hasWSConnected) { + // setStatusText("检测到正在部署,连接中..."); + // initWebSocket(userName, id); + // setShowProgressBar(true) + // } + + // if (status === "running" || status === "stopped") { + // console.log("✅ 允许删除(status =", status, ")"); + // setShowDelete(true); + // setProgress("100%"); + // setShowProgressBar(true) + + // if (status === "running") { + // setStatusText("运行中"); + // setProgressBarColor("bg-green-500"); + // } else if (status === "stopped") { + // setStatusText("已停止"); + // setProgressBarColor("bg-gray-400"); + // } + // } else { + // console.log("❌ 不允许删除(status =", status, ")"); + // setShowDelete(false); + // setProgressBarColor("bg-blue-500"); // 回到默认蓝色 + // } + // } catch (err) { + // console.error("获取状态失败:", err); + // setStatusText("状态拉取失败"); + // setShowDelete(false); + // } + // }; + + // fetchDeployStatus(); + // }, [data?.id]); + useEffect(() => { return () => { if (socketRef.current) {