import { useEffect, useState } from "react"; import { canRetryDatabaseStartup, type DatabaseStartupState } from "@zcode/shared"; import { useZCodeIntl } from "@/i18n/IntlProvider.js"; import { Button } from "@/components/ui/button.js"; import { RootStartupLoading } from "@/root/RootStartupLoading.js"; export function GlobalDatabaseStartupLoading({ state, onRetry, onCopy, onExit, }: { state: DatabaseStartupState | null; onRetry: () => void; onCopy: (details: string) => Promise; onExit: () => void; }) { const { intl } = useZCodeIntl(); const [now, setNow] = useState(Date.now); const [copyStatus, setCopyStatus] = useState<"copied" | "copyFailed" | null>(null); const failed = state?.phase === "failed"; const showProgress = state?.migration !== undefined && state.migration.kind !== "none"; const visible = failed || showProgress; useEffect(() => { if (failed || !visible) return; const timer = setInterval(() => setNow(Date.now()), 1000); return () => clearInterval(timer); }, [failed, visible]); useEffect(() => setCopyStatus(null), [state?.attemptId]); const elapsed = Math.max( 0, (failed ? state.updatedAt : Math.max(now, state?.updatedAt ?? now)) - (state?.startedAt ?? now), ); const phase = state?.phase ?? "starting"; const labelId = startupLabelId(state, visible); const label = intl.formatMessage({ id: labelId }); const errorCode = state?.errorCode ?? "sql_failed"; const canRetry = state ? canRetryDatabaseStartup(state) : false; const copy = async () => { try { await onCopy(JSON.stringify(state, null, 2)); setCopyStatus("copied"); } catch { setCopyStatus("copyFailed"); } }; if (!visible) return ( ); return (

{label}

{intl.formatMessage({ id: failed ? state.failedPhase === "starting_services" ? "startup.global.servicesFailed" : `startup.global.error.${errorCode}` : "startup.global.help", })}

{!failed && labelId === "startup.global.waiting" ? (

{intl.formatMessage({ id: "startup.database.waiting_for_lock" })}

) : null}

{intl.formatMessage( { id: "startup.database.elapsed" }, { minutes: Math.floor(elapsed / 60_000), seconds: Math.floor(elapsed / 1000) % 60, }, )}

{!failed && elapsed >= 10 * 60_000 ? (

{intl.formatMessage({ id: "startup.database.longRunning" })}

) : null} {failed ? (

{intl.formatMessage({ id: "startup.global.diagnostic" })}: {state.attemptId}

) : null}
{canRetry ? ( ) : null} {failed ? ( ) : null}
{copyStatus ? (

{intl.formatMessage({ id: `startup.global.${copyStatus}` })}

) : null}
); } /** 迁移提示覆盖本次准备;最后一个库之前不展示保存/收尾,避免跨库倒序。 */ function startupLabelId(state: DatabaseStartupState | null, visible: boolean): string { if (state?.phase === "failed") return "startup.global.failed"; if (!visible) return "startup.global.silent"; if ( state?.phase === "starting_services" || state?.phase === "ready" || (state?.finalDatabase && state.databasePhase === "ready") ) return "startup.global.finishing"; const current = state?.currentMigration; if (state?.databasePhase === "waiting_for_lock" && current && current.kind !== "none") return "startup.global.waiting"; if ( state?.finalDatabase && state.databasePhase === "committing" && current && current.executedCount > current.committedCount ) return "startup.global.saving"; return state?.migration?.kind === "initialize" ? "startup.global.initializing" : "startup.global.upgrading"; }