Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

desktopUpdate.logic.ts

129 lines5.2 KB

1import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts";
2​
3export type DesktopUpdateButtonAction = "download" | "install" | "none";
4​
5const DESKTOP_RELEASE_HISTORY_URL = "https://github.com/pingdotgg/t3code/releases";
6const DESKTOP_RELEASE_TAG_URL = `${DESKTOP_RELEASE_HISTORY_URL}/tag`;
7​
8/**
9 * The main process fills `downloadedVersion` from the updater's `update-downloaded`
10 * event, which is dispatched on its own fiber. A download RPC can therefore resolve
11 * before that write lands, so fall back to the version the download was started for.
12 */
13export function getDesktopUpdateDownloadedVersion(state: DesktopUpdateState): string | null {
14 return state.downloadedVersion ?? state.availableVersion;
16​
17export function getDesktopUpdateReleaseUrl(version: string | null): string | null {
18 const normalizedVersion = version?.trim();
19 if (!normalizedVersion) return null;
20 return `${DESKTOP_RELEASE_TAG_URL}/v${encodeURIComponent(normalizedVersion)}`;
22​
23export function getDesktopUpdateReleaseHistoryUrl(): string {
24 return DESKTOP_RELEASE_HISTORY_URL;
26​
27export function resolveDesktopUpdateButtonAction(
28 state: DesktopUpdateState,
29): DesktopUpdateButtonAction {
30 if (
31 state.downloadedVersion &&
32 (state.status === "downloaded" ||
33 (state.status === "error" &&
34 (state.errorContext === null || state.errorContext === "install")))
35 ) {
36 return "install";
37 }
38 if (state.status === "available") {
39 return "download";
40 }
41 if (state.status === "error") {
42 if (state.errorContext === "download" && state.availableVersion) {
43 return "download";
44 }
45 }
46 return "none";
48​
49export function shouldShowArm64IntelBuildWarning(state: DesktopUpdateState | null): boolean {
50 return state?.hostArch === "arm64" && state.appArch === "x64";
52​
53export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean {
54 return state?.status === "downloading";
56​
57export function getArm64IntelBuildWarningDescription(state: DesktopUpdateState): string {
58 if (!shouldShowArm64IntelBuildWarning(state)) {
59 return "This install is using the correct architecture.";
60 }
61​
62 const action = resolveDesktopUpdateButtonAction(state);
63 if (action === "download") {
64 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.";
65 }
66 if (action === "install") {
67 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.";
68 }
69 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.";
71​
72export function getDesktopUpdateButtonTooltip(state: DesktopUpdateState): string {
73 if (
74 state.notAppliedVersion !== undefined &&
75 (state.status === "available" || state.status === "downloaded") &&
76 getDesktopUpdateDownloadedVersion(state) === state.notAppliedVersion
77 ) {
78 return `Update ${state.notAppliedVersion} did not apply. Click to retry.`;
79 }
80 if (state.status === "available") {
81 return `Update ${state.availableVersion ?? "available"} ready to download`;
82 }
83 if (state.status === "downloading") {
84 const progress =
85 typeof state.downloadPercent === "number" ? ` (${Math.floor(state.downloadPercent)}%)` : "";
86 return `Downloading update${progress}`;
87 }
88 if (state.status === "downloaded") {
89 return `Update ${state.downloadedVersion ?? state.availableVersion ?? "ready"} downloaded. Click to restart and install.`;
90 }
91 if (state.status === "error") {
92 if (state.errorContext === "download" && state.availableVersion) {
93 return `Download failed for ${state.availableVersion}. Click to retry.`;
94 }
95 if (state.errorContext === "install" && state.downloadedVersion) {
96 return `Install failed for ${state.downloadedVersion}. Click to retry.`;
97 }
98 if (state.downloadedVersion) {
99 return `Update ${state.downloadedVersion} downloaded. Click to restart and install.`;
100 }
101 return state.message ?? "Update failed";
102 }
103 return "Up to date";
105​
106export function getDesktopUpdateInstallConfirmationMessage(
107 state: Pick<DesktopUpdateState, "availableVersion" | "downloadedVersion">,
108): string {
109 const version = state.downloadedVersion ?? state.availableVersion;
110 return `Install update${version ? ` ${version}` : ""} and restart T3 Code?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.`;
112​
113export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null {
114 if (!result.accepted || result.completed) return null;
115 if (typeof result.state.message !== "string") return null;
116 const message = result.state.message.trim();
117 return message.length > 0 ? message : null;
119​
120export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean {
121 return getDesktopUpdateActionError(result) !== null;
123​
124export function canCheckForUpdate(state: DesktopUpdateState | null): boolean {
125 if (!state || !state.enabled) return false;
126 return (
127 state.status !== "checking" && state.status !== "downloading" && state.status !== "disabled"
128 );