UpdateStatusDialog.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import { CalendarDays } from "lucide-react";
  2. import { cn } from "@/components/lib/utils.js";
  3. import { Button } from "@/components/ui/button.js";
  4. import {
  5. Dialog,
  6. DialogContent,
  7. DialogFooter,
  8. DialogHeader,
  9. DialogTitle,
  10. } from "@/components/ui/dialog.js";
  11. import { Checkbox } from "@/components/ui/checkbox.js";
  12. import { Progress } from "@/components/ui/progress.js";
  13. import type { IntlInstance } from "@/i18n/IntlProvider.js";
  14. import type { UpdateStatusDialogPhase } from "@/updateStatusModel.js";
  15. const macosDockIconUrl = new URL("../../../public/icon_512@2x.png", import.meta.url).href;
  16. type LocalizedUpdateReleaseNotes = {
  17. markdown: string;
  18. title: string;
  19. };
  20. export function UpdateStatusDialog({
  21. displayVersion,
  22. edgeToEdge = false,
  23. intl,
  24. isUpdateActionPending,
  25. autoDownloadAndInstallUpdates,
  26. onAutoDownloadAndInstallUpdatesChange,
  27. onCancelDownload,
  28. onDownloadUpdate,
  29. onOpenChange,
  30. onRestartUpdate,
  31. onSkipUpdate,
  32. open,
  33. phase,
  34. progressLabel,
  35. progressValue,
  36. releaseDateLabel,
  37. showOverlay = true,
  38. skippableVersion,
  39. }: {
  40. displayVersion: string;
  41. edgeToEdge?: boolean;
  42. intl: IntlInstance;
  43. isUpdateActionPending: boolean;
  44. autoDownloadAndInstallUpdates: boolean;
  45. localizedUpdateReleaseNotes: LocalizedUpdateReleaseNotes | null;
  46. onAutoDownloadAndInstallUpdatesChange: (enabled: boolean) => Promise<void>;
  47. onCancelDownload: () => Promise<void>;
  48. onDownloadUpdate: () => Promise<void>;
  49. onOpenChange: (open: boolean) => void;
  50. onOpenReleaseNotesExternalUrl: (url: string) => void;
  51. onRestartUpdate: () => Promise<void>;
  52. onSkipUpdate: () => Promise<void>;
  53. open: boolean;
  54. phase: UpdateStatusDialogPhase;
  55. progressLabel: string | null;
  56. progressValue: number;
  57. releaseDateLabel: string | null;
  58. showOverlay?: boolean;
  59. skippableVersion: string | null;
  60. }) {
  61. const isBeforeDownload = phase === "before-download";
  62. const isDownloading = phase === "downloading";
  63. const isDownloaded = phase === "downloaded";
  64. const dialogTitleId = isDownloaded
  65. ? "updateDialog.readyTitle"
  66. : isDownloading
  67. ? "updateDialog.downloadingTitle"
  68. : "updateDialog.availableTitle";
  69. const titleParts = getDialogTitleParts({
  70. displayVersion,
  71. intl,
  72. titleId: dialogTitleId,
  73. });
  74. const showSkipVersion = isBeforeDownload && Boolean(skippableVersion);
  75. const showLaterButton = !isDownloading;
  76. const titleClassName =
  77. "flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1 text-ui-base font-medium leading-5 text-foreground";
  78. const titleContent = (
  79. <>
  80. {titleParts.prefix ? <span>{titleParts.prefix}</span> : null}
  81. {titleParts.hasVersion ? (
  82. <span className="whitespace-nowrap">{titleParts.versionText}</span>
  83. ) : null}
  84. {titleParts.suffix ? <span>{titleParts.suffix}</span> : null}
  85. </>
  86. );
  87. const contentClassName = cn(
  88. "max-h-[calc(100vh-2rem)] max-w-[calc(100vw-2rem)] overflow-hidden p-5 sm:max-w-lg",
  89. edgeToEdge && !isDownloading
  90. ? // 普通/下载完成状态没有中间内容,不能继续使用 minmax(0,1fr)。
  91. // 否则 footer 会占满剩余窗口高度,按钮被垂直居中后看起来像中间有大块空白。
  92. "grid-rows-[auto_auto]"
  93. : "grid-rows-[auto_minmax(0,1fr)_auto]",
  94. edgeToEdge
  95. ? // 独立更新窗口已经由 BrowserWindow 提供窗口边框和阴影。
  96. // 这里不能再保留页内 DialogContent 的边框、阴影和视口边距,否则会出现“窗中窗”。
  97. "grid h-dvh max-h-none w-dvw max-w-none gap-5 border-0 bg-popover/98 pt-10 text-ui-base/relaxed text-foreground shadow-none sm:max-w-none [app-region:drag]"
  98. : "gap-5",
  99. );
  100. const content = (
  101. <>
  102. <DialogHeader className="gap-0">
  103. <div className="flex min-w-0 items-center gap-3 [app-region:no-drag]">
  104. <img
  105. src={macosDockIconUrl}
  106. alt=""
  107. aria-hidden="true"
  108. className="pointer-events-none -ml-[5px] size-12 shrink-0 select-none shadow-none drop-shadow-none"
  109. draggable={false}
  110. />
  111. <div className="min-w-0 flex-1 space-y-1.5">
  112. {edgeToEdge ? (
  113. <h2 className={titleClassName}>{titleContent}</h2>
  114. ) : (
  115. <DialogTitle className={titleClassName}>{titleContent}</DialogTitle>
  116. )}
  117. {releaseDateLabel ? (
  118. <div className="inline-flex max-w-full items-center gap-1 text-ui-xs font-normal leading-4 text-foreground-subtle">
  119. <CalendarDays className="size-3 shrink-0" aria-hidden="true" />
  120. <span className="truncate">{releaseDateLabel}</span>
  121. </div>
  122. ) : null}
  123. </div>
  124. </div>
  125. </DialogHeader>
  126. {isDownloading ? (
  127. <div className="min-h-0 overflow-y-auto [app-region:no-drag]">
  128. <section className="space-y-2" aria-live="polite">
  129. <div className="flex items-center justify-between gap-3">
  130. <span className="text-ui-base font-medium leading-5 text-foreground">
  131. {intl.formatMessage({ id: "updateDialog.downloadProgress" })}
  132. </span>
  133. {progressLabel ? (
  134. <span className="font-mono text-ui-base leading-5 text-foreground">
  135. {progressLabel}
  136. </span>
  137. ) : null}
  138. </div>
  139. <Progress
  140. value={progressValue}
  141. className="zcode-update-charge-progress h-2 bg-primary/15 dark:bg-primary/20"
  142. indicatorClassName="bg-primary"
  143. />
  144. </section>
  145. </div>
  146. ) : null}
  147. <div className={cn("[app-region:no-drag]", isDownloading ? "-mt-2" : null)}>
  148. {isBeforeDownload ? (
  149. <label className="mb-4 flex min-w-0 items-center gap-2 text-ui-base leading-5 text-foreground">
  150. <Checkbox
  151. checked={autoDownloadAndInstallUpdates}
  152. disabled={isUpdateActionPending}
  153. onCheckedChange={(checked) => {
  154. void onAutoDownloadAndInstallUpdatesChange(checked === true);
  155. }}
  156. />
  157. <span className="min-w-0">
  158. {intl.formatMessage({
  159. id: "updateDialog.autoDownloadAndInstall",
  160. })}
  161. </span>
  162. </label>
  163. ) : null}
  164. <DialogFooter
  165. className={cn(
  166. "p-0 [app-region:no-drag]",
  167. edgeToEdge
  168. ? // 独立更新窗口宽度只有 480px,小于 Tailwind 的 sm 断点。
  169. // 继续依赖 sm:flex-row 会退化成移动端竖排,导致底部按钮布局和原桌面弹窗不一致。
  170. cn(
  171. "flex-row items-center gap-3",
  172. showSkipVersion ? "justify-between" : "justify-end",
  173. )
  174. : showSkipVersion
  175. ? "sm:justify-between"
  176. : "sm:justify-end",
  177. )}
  178. >
  179. {showSkipVersion ? (
  180. <div
  181. className={cn(
  182. "flex w-full flex-col-reverse gap-2 sm:w-auto sm:flex-row",
  183. edgeToEdge ? "w-auto flex-row" : null,
  184. )}
  185. >
  186. <Button
  187. type="button"
  188. variant="secondary"
  189. size="lg"
  190. className="h-9 self-start px-4"
  191. disabled={isUpdateActionPending}
  192. onClick={() => void onSkipUpdate()}
  193. >
  194. {intl.formatMessage({ id: "updateDialog.skipVersion" })}
  195. </Button>
  196. </div>
  197. ) : null}
  198. <div
  199. className={cn(
  200. "flex w-full flex-col-reverse gap-2 sm:w-auto sm:flex-row sm:justify-end",
  201. edgeToEdge ? "w-auto flex-row justify-end" : null,
  202. )}
  203. >
  204. {showLaterButton ? (
  205. <Button
  206. type="button"
  207. variant="secondary"
  208. size="lg"
  209. className="h-9 px-4"
  210. disabled={isUpdateActionPending}
  211. onClick={() => onOpenChange(false)}
  212. >
  213. {intl.formatMessage({ id: "updateDialog.later" })}
  214. </Button>
  215. ) : null}
  216. {isDownloaded ? (
  217. <Button
  218. type="button"
  219. size="lg"
  220. className="h-9 px-4"
  221. disabled={isUpdateActionPending}
  222. onClick={() => void onRestartUpdate()}
  223. >
  224. {intl.formatMessage({ id: "updateDialog.restartToUpdate" })}
  225. </Button>
  226. ) : isDownloading ? (
  227. <Button
  228. type="button"
  229. variant="secondary"
  230. size="lg"
  231. className="h-9 px-4"
  232. disabled={isUpdateActionPending}
  233. onClick={() => void onCancelDownload()}
  234. >
  235. {intl.formatMessage({ id: "updateDialog.cancelDownload" })}
  236. </Button>
  237. ) : (
  238. <Button
  239. type="button"
  240. size="lg"
  241. className="h-9 px-4"
  242. disabled={isUpdateActionPending}
  243. onClick={() => void onDownloadUpdate()}
  244. >
  245. {intl.formatMessage({ id: "updateDialog.downloadAndUpdate" })}
  246. </Button>
  247. )}
  248. </div>
  249. </DialogFooter>
  250. </div>
  251. </>
  252. );
  253. if (edgeToEdge) {
  254. if (!open) {
  255. return null;
  256. }
  257. return (
  258. <div
  259. role="dialog"
  260. aria-modal="true"
  261. aria-describedby={undefined}
  262. className={contentClassName}
  263. >
  264. {content}
  265. </div>
  266. );
  267. }
  268. return (
  269. <Dialog open={open} onOpenChange={onOpenChange}>
  270. <DialogContent
  271. // 去掉标题下方可见描述后,显式关闭 aria-describedby,避免 Radix 在开发环境提示缺少 Description。
  272. aria-describedby={undefined}
  273. showOverlay={showOverlay}
  274. showCloseButton={false}
  275. className={contentClassName}
  276. >
  277. {content}
  278. </DialogContent>
  279. </Dialog>
  280. );
  281. }
  282. function getDialogTitleParts({
  283. displayVersion,
  284. intl,
  285. titleId,
  286. }: {
  287. displayVersion: string;
  288. intl: IntlInstance;
  289. titleId: string;
  290. }) {
  291. const marker = "__ZCODE_UPDATE_VERSION__";
  292. const text = intl.formatMessage({ id: titleId }, { version: marker });
  293. const index = text.indexOf(marker);
  294. const rawPrefix = index >= 0 ? text.slice(0, index) : "";
  295. const versionPrefix = rawPrefix.endsWith("v") ? "v" : "";
  296. const prefix = (versionPrefix ? rawPrefix.slice(0, -versionPrefix.length) : rawPrefix).trim();
  297. return {
  298. hasVersion: index >= 0,
  299. prefix,
  300. suffix: index >= 0 ? text.slice(index + marker.length).trim() : text.trim(),
  301. versionText: `${versionPrefix}${displayVersion}`,
  302. };
  303. }