Open History to see the last commit
1import type { DesktopUpdateActionResult, DesktopUpdateState } from "@v12/contracts";
2
3export type DesktopUpdateButtonAction = "download" | "install" | "none";
4
5export function resolveDesktopUpdateButtonAction(
6 state: DesktopUpdateState,
7): DesktopUpdateButtonAction {
8 if (state.downloadedVersion) {
9 return "install";
10 }
11 if (state.status === "available") {
12 return "download";
13 }
14 if (state.status === "error") {
15 if (state.errorContext === "download" && state.availableVersion) {
16 return "download";
17 }
18 }
19 return "none";
20}
21
22export function shouldShowDesktopUpdateButton(state: DesktopUpdateState | null): boolean {
23 if (!state || !state.enabled) {
24 return false;
25 }
26 if (state.status === "downloading") {
27 return true;
28 }
29 return resolveDesktopUpdateButtonAction(state) !== "none";
30}
31
32export function shouldShowArm64IntelBuildWarning(state: DesktopUpdateState | null): boolean {
33 return state?.hostArch === "arm64" && state.appArch === "x64";
34}
35
36export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean {
37 return state?.status === "downloading";
38}
39
40export function getArm64IntelBuildWarningDescription(state: DesktopUpdateState): string {
41 if (!shouldShowArm64IntelBuildWarning(state)) {
42 return "This install is using the correct architecture.";
43 }
44
45 const action = resolveDesktopUpdateButtonAction(state);
46 if (action === "download") {
47 return "This Mac has Apple Silicon, but V12 is still running the Intel build under Rosetta. Download the available update to switch to the native Apple Silicon build.";
48 }
49 if (action === "install") {
50 return "This Mac has Apple Silicon, but V12 is still running the Intel build under Rosetta. Restart to install the downloaded Apple Silicon build.";
51 }
52 return "This Mac has Apple Silicon, but V12 is still running the Intel build under Rosetta. The next app update will replace it with the native Apple Silicon build.";
53}
54
55export function getDesktopUpdateButtonTooltip(state: DesktopUpdateState): string {
56 if (state.status === "available") {
57 return `Update ${state.availableVersion ?? "available"} ready to download`;
58 }
59 if (state.status === "downloading") {
60 const progress =
61 typeof state.downloadPercent === "number" ? ` (${Math.floor(state.downloadPercent)}%)` : "";
62 return `Downloading update${progress}`;
63 }
64 if (state.status === "downloaded") {
65 return `Update ${state.downloadedVersion ?? state.availableVersion ?? "ready"} downloaded. Click to restart and install.`;
66 }
67 if (state.status === "error") {
68 if (state.errorContext === "download" && state.availableVersion) {
69 return `Download failed for ${state.availableVersion}. Click to retry.`;
70 }
71 if (state.errorContext === "install" && state.downloadedVersion) {
72 return `Install failed for ${state.downloadedVersion}. Click to retry.`;
73 }
74 return state.message ?? "Update failed";
75 }
76 return "Up to date";
77}
78
79export function getDesktopUpdateInstallConfirmationMessage(
80 state: Pick<DesktopUpdateState, "availableVersion" | "downloadedVersion">,
81): string {
82 const version = state.downloadedVersion ?? state.availableVersion;
83 return `Install update${version ? ` ${version}` : ""} and restart V12?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.`;
84}
85
86export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null {
87 if (!result.accepted || result.completed) return null;
88 if (typeof result.state.message !== "string") return null;
89 const message = result.state.message.trim();
90 return message.length > 0 ? message : null;
91}
92
93export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean {
94 return getDesktopUpdateActionError(result) !== null;
95}
96
97export function shouldHighlightDesktopUpdateError(state: DesktopUpdateState | null): boolean {
98 if (!state || state.status !== "error") return false;
99 return state.errorContext === "download" || state.errorContext === "install";
100}
101
102export function canCheckForUpdate(state: DesktopUpdateState | null): boolean {
103 if (!state || !state.enabled) return false;
104 return (
105 state.status !== "checking" &&
106 state.status !== "downloading" &&
107 state.status !== "downloaded" &&
108 state.status !== "disabled"
109 );
110}