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