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

desktopUpdate.logic.ts

133 lines5.4 KB

1import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts";
2import { isWindowsPlatform } from "../lib/utils";
3​
4export type DesktopUpdateButtonAction = "download" | "install" | "none";
5​
6const DESKTOP_RELEASE_TAG_URL = "https://github.com/pingdotgg/t3code/releases/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​
17/** Release notes for an exact downloaded build; nightly suffixes are part of the tag. */
18export function getDesktopUpdateReleaseUrl(version: string | null): string | null {
19 const normalizedVersion = version?.trim();
20 if (!normalizedVersion) return null;
21 return `${DESKTOP_RELEASE_TAG_URL}/v${encodeURIComponent(normalizedVersion)}`;
23​
24export function resolveDesktopUpdateButtonAction(
25 state: DesktopUpdateState,
26): DesktopUpdateButtonAction {
27 if (state.downloadedVersion) {
28 return "install";
29 }
30 if (state.status === "available") {
31 return "download";
32 }
33 if (state.status === "error") {
34 if (state.errorContext === "download" && state.availableVersion) {
35 return "download";
36 }
37 }
38 return "none";
40​
41export function shouldShowDesktopUpdateButton(state: DesktopUpdateState | null): boolean {
42 if (!state || !state.enabled) {
43 return false;
44 }
45 if (state.status === "downloading") {
46 return true;
47 }
48 return resolveDesktopUpdateButtonAction(state) !== "none";
50​
51export function shouldShowArm64IntelBuildWarning(state: DesktopUpdateState | null): boolean {
52 return state?.hostArch === "arm64" && state.appArch === "x64";
54​
55export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean {
56 return state?.status === "downloading";
58​
59export function getArm64IntelBuildWarningDescription(state: DesktopUpdateState): string {
60 if (!shouldShowArm64IntelBuildWarning(state)) {
61 return "This install is using the correct architecture.";
62 }
63​
64 const action = resolveDesktopUpdateButtonAction(state);
65 if (action === "download") {
66 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.";
67 }
68 if (action === "install") {
69 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.";
70 }
71 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.";
73​
74export function getDesktopUpdateButtonTooltip(state: DesktopUpdateState): string {
75 if (state.status === "available") {
76 return `Update ${state.availableVersion ?? "available"} ready to download`;
77 }
78 if (state.status === "downloading") {
79 const progress =
80 typeof state.downloadPercent === "number" ? ` (${Math.floor(state.downloadPercent)}%)` : "";
81 return `Downloading update${progress}`;
82 }
83 if (state.status === "downloaded") {
84 return `Update ${state.downloadedVersion ?? state.availableVersion ?? "ready"} downloaded. Click to restart and install.`;
85 }
86 if (state.status === "error") {
87 if (state.errorContext === "download" && state.availableVersion) {
88 return `Download failed for ${state.availableVersion}. Click to retry.`;
89 }
90 if (state.errorContext === "install" && state.downloadedVersion) {
91 return `Install failed for ${state.downloadedVersion}. Click to retry.`;
92 }
93 return state.message ?? "Update failed";
94 }
95 return "Up to date";
97​
98export function getDesktopUpdateInstallConfirmationMessage(
99 state: Pick<DesktopUpdateState, "availableVersion" | "downloadedVersion">,
100 platform = "",
101): string {
102 const version = state.downloadedVersion ?? state.availableVersion;
103 const windowsInstallWarning = isWindowsPlatform(platform)
104 ? "\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."
105 : "";
106 return `Install update${version ? ` ${version}` : ""} and restart T3 Code?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.${windowsInstallWarning}`;
108​
109export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null {
110 if (!result.accepted || result.completed) return null;
111 if (typeof result.state.message !== "string") return null;
112 const message = result.state.message.trim();
113 return message.length > 0 ? message : null;
115​
116export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean {
117 return getDesktopUpdateActionError(result) !== null;
119​
120export function shouldHighlightDesktopUpdateError(state: DesktopUpdateState | null): boolean {
121 if (!state || state.status !== "error") return false;
122 return state.errorContext === "download" || state.errorContext === "install";
124​
125export function canCheckForUpdate(state: DesktopUpdateState | null): boolean {
126 if (!state || !state.enabled) return false;
127 return (
128 state.status !== "checking" &&
129 state.status !== "downloading" &&
130 state.status !== "downloaded" &&
131 state.status !== "disabled"
132 );