GlobalDatabaseStartupLoading.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { useEffect, useState } from "react";
  2. import { canRetryDatabaseStartup, type DatabaseStartupState } from "@zcode/shared";
  3. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  4. import { Button } from "@/components/ui/button.js";
  5. import { RootStartupLoading } from "@/root/RootStartupLoading.js";
  6. export function GlobalDatabaseStartupLoading({
  7. state,
  8. onRetry,
  9. onCopy,
  10. onExit,
  11. }: {
  12. state: DatabaseStartupState | null;
  13. onRetry: () => void;
  14. onCopy: (details: string) => Promise<void>;
  15. onExit: () => void;
  16. }) {
  17. const { intl } = useZCodeIntl();
  18. const [now, setNow] = useState(Date.now);
  19. const [copyStatus, setCopyStatus] = useState<"copied" | "copyFailed" | null>(null);
  20. const failed = state?.phase === "failed";
  21. const showProgress = state?.migration !== undefined && state.migration.kind !== "none";
  22. const visible = failed || showProgress;
  23. useEffect(() => {
  24. if (failed || !visible) return;
  25. const timer = setInterval(() => setNow(Date.now()), 1000);
  26. return () => clearInterval(timer);
  27. }, [failed, visible]);
  28. useEffect(() => setCopyStatus(null), [state?.attemptId]);
  29. const elapsed = Math.max(
  30. 0,
  31. (failed ? state.updatedAt : Math.max(now, state?.updatedAt ?? now)) - (state?.startedAt ?? now),
  32. );
  33. const phase = state?.phase ?? "starting";
  34. const labelId = startupLabelId(state, visible);
  35. const label = intl.formatMessage({ id: labelId });
  36. const errorCode = state?.errorCode ?? "sql_failed";
  37. const canRetry = state ? canRetryDatabaseStartup(state) : false;
  38. const copy = async () => {
  39. try {
  40. await onCopy(JSON.stringify(state, null, 2));
  41. setCopyStatus("copied");
  42. } catch {
  43. setCopyStatus("copyFailed");
  44. }
  45. };
  46. if (!visible)
  47. return (
  48. <RootStartupLoading label={label}>
  49. <span
  50. hidden
  51. data-testid="database-startup-silent"
  52. data-phase={phase}
  53. data-database-phase={state?.databasePhase}
  54. />
  55. </RootStartupLoading>
  56. );
  57. return (
  58. <RootStartupLoading label={label} busy={!failed}>
  59. <div
  60. className="flex w-full max-w-md flex-col items-center gap-3 px-6 text-center"
  61. data-testid="database-startup-status"
  62. data-phase={phase}
  63. data-database-phase={state?.databasePhase}
  64. data-error-code={state?.errorCode}
  65. >
  66. <h1 className="text-ui-lg font-medium" role={failed ? "alert" : undefined}>
  67. {label}
  68. </h1>
  69. <p className="text-ui-base text-foreground-subtle">
  70. {intl.formatMessage({
  71. id: failed
  72. ? state.failedPhase === "starting_services"
  73. ? "startup.global.servicesFailed"
  74. : `startup.global.error.${errorCode}`
  75. : "startup.global.help",
  76. })}
  77. </p>
  78. {!failed && labelId === "startup.global.waiting" ? (
  79. <p className="text-ui-base">
  80. {intl.formatMessage({ id: "startup.database.waiting_for_lock" })}
  81. </p>
  82. ) : null}
  83. <p className="text-ui-caption text-foreground-subtle">
  84. {intl.formatMessage(
  85. { id: "startup.database.elapsed" },
  86. {
  87. minutes: Math.floor(elapsed / 60_000),
  88. seconds: Math.floor(elapsed / 1000) % 60,
  89. },
  90. )}
  91. </p>
  92. {!failed && elapsed >= 10 * 60_000 ? (
  93. <p className="text-ui-caption text-foreground-subtle">
  94. {intl.formatMessage({ id: "startup.database.longRunning" })}
  95. </p>
  96. ) : null}
  97. {failed ? (
  98. <p className="break-all text-ui-caption text-foreground-subtle">
  99. {intl.formatMessage({ id: "startup.global.diagnostic" })}: {state.attemptId}
  100. </p>
  101. ) : null}
  102. <div className="flex flex-wrap justify-center gap-2">
  103. {canRetry ? (
  104. <Button data-testid="database-startup-retry" onClick={onRetry}>
  105. {intl.formatMessage({ id: "common.retry" })}
  106. </Button>
  107. ) : null}
  108. {failed ? (
  109. <Button variant="outline" onClick={() => void copy()}>
  110. {intl.formatMessage({ id: "startup.global.copy" })}
  111. </Button>
  112. ) : null}
  113. <Button variant="ghost" onClick={onExit}>
  114. {intl.formatMessage({ id: "startup.global.exit" })}
  115. </Button>
  116. </div>
  117. {copyStatus ? (
  118. <p role="status" className="text-ui-caption">
  119. {intl.formatMessage({ id: `startup.global.${copyStatus}` })}
  120. </p>
  121. ) : null}
  122. </div>
  123. </RootStartupLoading>
  124. );
  125. }
  126. /** 迁移提示覆盖本次准备;最后一个库之前不展示保存/收尾,避免跨库倒序。 */
  127. function startupLabelId(state: DatabaseStartupState | null, visible: boolean): string {
  128. if (state?.phase === "failed") return "startup.global.failed";
  129. if (!visible) return "startup.global.silent";
  130. if (
  131. state?.phase === "starting_services" ||
  132. state?.phase === "ready" ||
  133. (state?.finalDatabase && state.databasePhase === "ready")
  134. )
  135. return "startup.global.finishing";
  136. const current = state?.currentMigration;
  137. if (state?.databasePhase === "waiting_for_lock" && current && current.kind !== "none")
  138. return "startup.global.waiting";
  139. if (
  140. state?.finalDatabase &&
  141. state.databasePhase === "committing" &&
  142. current &&
  143. current.executedCount > current.committedCount
  144. )
  145. return "startup.global.saving";
  146. return state?.migration?.kind === "initialize"
  147. ? "startup.global.initializing"
  148. : "startup.global.upgrading";
  149. }