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