updateStatusModel.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import type {
  2. ElectronReleaseChannel,
  3. PostUpdateReleaseNotesPayload,
  4. UpdateStatePayload,
  5. } from "@zcode/shared";
  6. export type UpdateStatusDialogPhase =
  7. | "before-download"
  8. | "downloading"
  9. | "downloaded";
  10. export type UpdateActionInFlight =
  11. | "download"
  12. | "cancel"
  13. | "skip"
  14. | "restart"
  15. | null;
  16. export type UpdateStatusViewModel = {
  17. dialogPhase: UpdateStatusDialogPhase;
  18. displayVersion: string | null;
  19. progressLabel: string | null;
  20. progressValue: number;
  21. releaseNotesPayload: PostUpdateReleaseNotesPayload | undefined;
  22. skippableVersion: string | null;
  23. updateChannel: ElectronReleaseChannel | undefined;
  24. };
  25. export function deriveUpdateStatusViewModel({
  26. legacyReadyVersion,
  27. updateState,
  28. }: {
  29. legacyReadyVersion: string | null;
  30. updateState: UpdateStatePayload | null;
  31. }): UpdateStatusViewModel {
  32. const isDownloadingUpdate = updateState?.kind === "download-progress";
  33. const readyVersion = resolveReadyVersion({ legacyReadyVersion, updateState });
  34. const progressVersion = isDownloadingUpdate ? updateState.version : null;
  35. const availableVersion =
  36. updateState?.kind === "update-available" ? updateState.version : null;
  37. const displayVersion =
  38. readyVersion ??
  39. progressVersion ??
  40. availableVersion ??
  41. // download-progress.version 是协议可选字段。下载态必须按 kind 保持 UI,
  42. // 否则某一帧缺少版本号会让入口卸载并关闭已打开弹窗。
  43. (isDownloadingUpdate ? "…" : null);
  44. return {
  45. dialogPhase: readyVersion
  46. ? "downloaded"
  47. : isDownloadingUpdate
  48. ? "downloading"
  49. : "before-download",
  50. displayVersion,
  51. progressLabel: getUpdateDownloadProgressLabel(updateState),
  52. progressValue: getUpdateDownloadProgressValue(updateState),
  53. releaseNotesPayload: getUpdateReleaseNotesPayload(updateState),
  54. skippableVersion:
  55. updateState?.kind === "update-available" ||
  56. updateState?.kind === "download-progress"
  57. ? (updateState.version ?? null)
  58. : null,
  59. updateChannel:
  60. updateState?.kind === "update-available" ||
  61. updateState?.kind === "download-progress" ||
  62. updateState?.kind === "update-downloaded"
  63. ? updateState.channel
  64. : undefined,
  65. };
  66. }
  67. export function isUpdateActionCompleted(
  68. action: UpdateActionInFlight,
  69. updateState: UpdateStatePayload | null,
  70. ) {
  71. return (
  72. // 点击下载后 main 侧可能先广播 checking/idle 等过渡态。
  73. // 这些状态不代表下载已经进入可观察阶段,不能释放按钮锁并触发弹窗卸载;
  74. // 只有真正进入下载进度或下载完成,才算下载命令完成。
  75. (action === "download" &&
  76. (updateState?.kind === "download-progress" ||
  77. updateState?.kind === "update-downloaded")) ||
  78. (action === "cancel" && updateState?.kind !== "download-progress") ||
  79. (action === "skip" &&
  80. updateState?.kind !== "update-available" &&
  81. updateState?.kind !== "download-progress")
  82. );
  83. }
  84. export function getUpdateDownloadProgressLabel(
  85. updateState: UpdateStatePayload | null,
  86. ) {
  87. if (updateState?.kind !== "download-progress") {
  88. return null;
  89. }
  90. const { totalBytes, transferredBytes } = updateState;
  91. if (
  92. typeof transferredBytes === "number" &&
  93. Number.isFinite(transferredBytes) &&
  94. transferredBytes >= 0 &&
  95. typeof totalBytes === "number" &&
  96. Number.isFinite(totalBytes) &&
  97. totalBytes > 0
  98. ) {
  99. return `${formatMegabytes(transferredBytes)} / ${formatMegabytes(totalBytes)}`;
  100. }
  101. // 下载进度文案已经改为展示已下载/总大小。只有百分比时继续显示
  102. // “0% / 42%” 会和新的大小口径冲突,且启动下载的临时态会残留一个 0%。
  103. return null;
  104. }
  105. function resolveReadyVersion({
  106. legacyReadyVersion,
  107. updateState,
  108. }: {
  109. legacyReadyVersion: string | null;
  110. updateState: UpdateStatePayload | null;
  111. }) {
  112. if (updateState?.kind === "update-downloaded") {
  113. return updateState.version;
  114. }
  115. // legacy UpdateReady 只是一份“曾经 ready”的缓存。
  116. // 一旦新的 UpdateState 已明确同步到 renderer,idle/checking/error 都应以新状态为准,
  117. // 不能继续用旧 version 撑出“重启以更新”按钮。
  118. return updateState === null ? legacyReadyVersion : null;
  119. }
  120. function getUpdateDownloadProgressValue(updateState: UpdateStatePayload | null) {
  121. const rawProgressValue =
  122. updateState?.kind === "download-progress"
  123. ? Number(updateState.progress)
  124. : 0;
  125. return Number.isFinite(rawProgressValue)
  126. ? Math.max(0, Math.min(100, rawProgressValue))
  127. : 0;
  128. }
  129. function getUpdateReleaseNotesPayload(updateState: UpdateStatePayload | null) {
  130. return updateState?.kind === "update-available" ||
  131. updateState?.kind === "download-progress" ||
  132. updateState?.kind === "update-downloaded"
  133. ? updateState.releaseNotes
  134. : undefined;
  135. }
  136. function formatMegabytes(bytes: number) {
  137. return `${(bytes / 1024 / 1024).toFixed(1)}MB`;
  138. }