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

desktopUpdate.logic.ts

133 lines5.3 KB

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