Open History to see the last commit
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;
15}
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)}`;
22}
23
24export function getDesktopUpdateReleaseHistoryUrl(): string {
25 return DESKTOP_RELEASE_HISTORY_URL;
26}
27
28export function resolveDesktopUpdateButtonAction(
29 state: DesktopUpdateState,
30): DesktopUpdateButtonAction {
31 if (
32 state.downloadedVersion &&
33 (state.status === "downloaded" ||
34 (state.status === "error" &&
35 (state.errorContext === null || state.errorContext === "install")))
36 ) {
37 return "install";
38 }
39 if (state.status === "available") {
40 return "download";
41 }
42 if (state.status === "error") {
43 if (state.errorContext === "download" && state.availableVersion) {
44 return "download";
45 }
46 }
47 return "none";
48}
49
50export function shouldShowDesktopUpdateButton(state: DesktopUpdateState | null): boolean {
51 if (!state || !state.enabled) {
52 return false;
53 }
54 if (state.status === "downloading") {
55 return true;
56 }
57 return resolveDesktopUpdateButtonAction(state) !== "none";
58}
59
60export function shouldShowArm64IntelBuildWarning(state: DesktopUpdateState | null): boolean {
61 return state?.hostArch === "arm64" && state.appArch === "x64";
62}
63
64export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean {
65 return state?.status === "downloading";
66}
67
68export function getArm64IntelBuildWarningDescription(state: DesktopUpdateState): string {
69 if (!shouldShowArm64IntelBuildWarning(state)) {
70 return "This install is using the correct architecture.";
71 }
72
73 const action = resolveDesktopUpdateButtonAction(state);
74 if (action === "download") {
75 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.";
76 }
77 if (action === "install") {
78 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.";
79 }
80 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.";
81}
82
83export function getDesktopUpdateButtonTooltip(state: DesktopUpdateState): string {
84 if (state.status === "available") {
85 return `Update ${state.availableVersion ?? "available"} ready to download`;
86 }
87 if (state.status === "downloading") {
88 const progress =
89 typeof state.downloadPercent === "number" ? ` (${Math.floor(state.downloadPercent)}%)` : "";
90 return `Downloading update${progress}`;
91 }
92 if (state.status === "downloaded") {
93 return `Update ${state.downloadedVersion ?? state.availableVersion ?? "ready"} downloaded. Click to restart and install.`;
94 }
95 if (state.status === "error") {
96 if (state.errorContext === "download" && state.availableVersion) {
97 return `Download failed for ${state.availableVersion}. Click to retry.`;
98 }
99 if (state.errorContext === "install" && state.downloadedVersion) {
100 return `Install failed for ${state.downloadedVersion}. Click to retry.`;
101 }
102 if (state.downloadedVersion) {
103 return `Update ${state.downloadedVersion} downloaded. Click to restart and install.`;
104 }
105 return state.message ?? "Update failed";
106 }
107 return "Up to date";
108}
109
110export function getDesktopUpdateInstallConfirmationMessage(
111 state: Pick<DesktopUpdateState, "availableVersion" | "downloadedVersion">,
112): string {
113 const version = state.downloadedVersion ?? state.availableVersion;
114 return `Install update${version ? ` ${version}` : ""} and restart T3 Code?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.`;
115}
116
117export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null {
118 if (!result.accepted || result.completed) return null;
119 if (typeof result.state.message !== "string") return null;
120 const message = result.state.message.trim();
121 return message.length > 0 ? message : null;
122}
123
124export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean {
125 return getDesktopUpdateActionError(result) !== null;
126}
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 );
133}