forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktopUpdate.logic.ts
More file actions
110 lines (97 loc) · 4.24 KB
/
desktopUpdate.logic.ts
File metadata and controls
110 lines (97 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts";
export type DesktopUpdateButtonAction = "download" | "install" | "none";
export function resolveDesktopUpdateButtonAction(
state: DesktopUpdateState,
): DesktopUpdateButtonAction {
if (state.downloadedVersion) {
return "install";
}
if (state.status === "available") {
return "download";
}
if (state.status === "error") {
if (state.errorContext === "download" && state.availableVersion) {
return "download";
}
}
return "none";
}
export function shouldShowDesktopUpdateButton(state: DesktopUpdateState | null): boolean {
if (!state || !state.enabled) {
return false;
}
if (state.status === "downloading") {
return true;
}
return resolveDesktopUpdateButtonAction(state) !== "none";
}
export function shouldShowArm64IntelBuildWarning(state: DesktopUpdateState | null): boolean {
return state?.hostArch === "arm64" && state.appArch === "x64";
}
export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean {
return state?.status === "downloading";
}
export function getArm64IntelBuildWarningDescription(state: DesktopUpdateState): string {
if (!shouldShowArm64IntelBuildWarning(state)) {
return "This install is using the correct architecture.";
}
const action = resolveDesktopUpdateButtonAction(state);
if (action === "download") {
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.";
}
if (action === "install") {
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.";
}
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.";
}
export function getDesktopUpdateButtonTooltip(state: DesktopUpdateState): string {
if (state.status === "available") {
return `Update ${state.availableVersion ?? "available"} ready to download`;
}
if (state.status === "downloading") {
const progress =
typeof state.downloadPercent === "number" ? ` (${Math.floor(state.downloadPercent)}%)` : "";
return `Downloading update${progress}`;
}
if (state.status === "downloaded") {
return `Update ${state.downloadedVersion ?? state.availableVersion ?? "ready"} downloaded. Click to restart and install.`;
}
if (state.status === "error") {
if (state.errorContext === "download" && state.availableVersion) {
return `Download failed for ${state.availableVersion}. Click to retry.`;
}
if (state.errorContext === "install" && state.downloadedVersion) {
return `Install failed for ${state.downloadedVersion}. Click to retry.`;
}
return state.message ?? "Update failed";
}
return "Up to date";
}
export function getDesktopUpdateInstallConfirmationMessage(
state: Pick<DesktopUpdateState, "availableVersion" | "downloadedVersion">,
): string {
const version = state.downloadedVersion ?? state.availableVersion;
return `Install update${version ? ` ${version}` : ""} and restart T3 Code?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.`;
}
export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null {
if (!result.accepted || result.completed) return null;
if (typeof result.state.message !== "string") return null;
const message = result.state.message.trim();
return message.length > 0 ? message : null;
}
export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean {
return getDesktopUpdateActionError(result) !== null;
}
export function shouldHighlightDesktopUpdateError(state: DesktopUpdateState | null): boolean {
if (!state || state.status !== "error") return false;
return state.errorContext === "download" || state.errorContext === "install";
}
export function canCheckForUpdate(state: DesktopUpdateState | null): boolean {
if (!state || !state.enabled) return false;
return (
state.status !== "checking" &&
state.status !== "downloading" &&
state.status !== "downloaded" &&
state.status !== "disabled"
);
}