Open History to see the last commit
1import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts";
2import { msg } from "gt-react";
3
4import { m, type MessageResolver } from "../i18n/messages";
5
6export type DesktopUpdateButtonAction = "download" | "install" | "none";
7
8const DEFAULT_DESKTOP_RELEASE_REPOSITORY = "pingdotgg/t3code";
9const GITHUB_REPOSITORY_SEGMENT = /^[A-Za-z0-9._-]+$/u;
10
11function isValidRepositorySegment(value: string | undefined): value is string {
12 return (
13 value !== undefined && value !== "." && value !== ".." && GITHUB_REPOSITORY_SEGMENT.test(value)
14 );
15}
16
17function desktopReleaseRepositoryUrl(repository: string | undefined): string {
18 const configuredRepository = repository?.trim() ?? "";
19 const [owner, name, ...extra] = configuredRepository.split("/");
20 const releaseRepository =
21 isValidRepositorySegment(owner) && isValidRepositorySegment(name) && extra.length === 0
22 ? configuredRepository
23 : DEFAULT_DESKTOP_RELEASE_REPOSITORY;
24 return `https://github.com/${releaseRepository}/releases`;
25}
26
27/** The download RPC can beat the updater's downloadedVersion event; fall back to the requested version. */
28export function getDesktopUpdateDownloadedVersion(state: DesktopUpdateState): string | null {
29 return state.downloadedVersion ?? state.availableVersion;
30}
31
32export function getDesktopUpdateReleaseUrl(
33 version: string | null,
34 repository: string | undefined = import.meta.env.T3CODE_DESKTOP_UPDATE_REPOSITORY,
35): string | null {
36 const normalizedVersion = version?.trim();
37 if (!normalizedVersion) return null;
38 return `${desktopReleaseRepositoryUrl(repository)}/tag/v${encodeURIComponent(normalizedVersion)}`;
39}
40
41export function getDesktopUpdateReleaseHistoryUrl(
42 repository: string | undefined = import.meta.env.T3CODE_DESKTOP_UPDATE_REPOSITORY,
43): string {
44 return desktopReleaseRepositoryUrl(repository);
45}
46
47export function resolveDesktopUpdateButtonAction(
48 state: DesktopUpdateState,
49): DesktopUpdateButtonAction {
50 if (
51 state.downloadedVersion &&
52 (state.status === "downloaded" ||
53 (state.status === "error" &&
54 (state.errorContext === null || state.errorContext === "install")))
55 ) {
56 return "install";
57 }
58 if (state.status === "available") {
59 return "download";
60 }
61 if (state.status === "error") {
62 if (state.errorContext === "download" && state.availableVersion) {
63 return "download";
64 }
65 }
66 return "none";
67}
68
69export function shouldShowArm64IntelBuildWarning(state: DesktopUpdateState | null): boolean {
70 return state?.hostArch === "arm64" && state.appArch === "x64";
71}
72
73export function isDesktopUpdateButtonDisabled(state: DesktopUpdateState | null): boolean {
74 return state?.status === "downloading";
75}
76
77export function getArm64IntelBuildWarningDescription(
78 state: DesktopUpdateState,
79 resolve: MessageResolver = m,
80): string {
81 if (!shouldShowArm64IntelBuildWarning(state)) {
82 return resolve(msg("This install is using the correct architecture."));
83 }
84
85 const action = resolveDesktopUpdateButtonAction(state);
86 if (action === "download") {
87 return resolve(
88 msg(
89 "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.",
90 ),
91 );
92 }
93 if (action === "install") {
94 return resolve(
95 msg(
96 "This Mac has Apple Silicon, but T3 Code is still running the Intel build under Rosetta. Restart to install the downloaded Apple Silicon build.",
97 ),
98 );
99 }
100 return resolve(
101 msg(
102 "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.",
103 ),
104 );
105}
106
107export function getDesktopUpdateButtonTooltip(
108 state: DesktopUpdateState,
109 resolve: MessageResolver = m,
110): string {
111 if (
112 state.notAppliedVersion !== undefined &&
113 (state.status === "available" || state.status === "downloaded") &&
114 getDesktopUpdateDownloadedVersion(state) === state.notAppliedVersion
115 ) {
116 return resolve(
117 msg("Update {version} did not apply. Click to retry.", { version: state.notAppliedVersion }),
118 );
119 }
120 if (
121 (state.status === "available" || state.status === "error") &&
122 state.errorContext === "download" &&
123 state.availableVersion
124 ) {
125 return resolve(
126 msg("Download failed for {version}. Click to retry.", { version: state.availableVersion }),
127 );
128 }
129 if (state.status === "available") {
130 return state.availableVersion === null
131 ? resolve(msg("Update available ready to download"))
132 : resolve(msg("Update {version} ready to download", { version: state.availableVersion }));
133 }
134 if (state.status === "downloading") {
135 return typeof state.downloadPercent === "number"
136 ? resolve(
137 msg("Downloading update ({percent}%)", { percent: Math.floor(state.downloadPercent) }),
138 )
139 : resolve(msg("Downloading update"));
140 }
141 if (state.status === "downloaded") {
142 const version = state.downloadedVersion ?? state.availableVersion;
143 return version === null
144 ? resolve(msg("Update ready downloaded. Click to restart and install."))
145 : resolve(msg("Update {version} downloaded. Click to restart and install.", { version }));
146 }
147 if (state.status === "error") {
148 if (state.errorContext === "install" && state.downloadedVersion) {
149 return resolve(
150 msg("Install failed for {version}. Click to retry.", { version: state.downloadedVersion }),
151 );
152 }
153 if (state.downloadedVersion) {
154 return resolve(
155 msg("Update {version} downloaded. Click to restart and install.", {
156 version: state.downloadedVersion,
157 }),
158 );
159 }
160 return state.message ?? resolve(msg("Update failed"));
161 }
162 return resolve(msg("Up to date"));
163}
164
165export function getDesktopUpdateInstallConfirmationMessage(
166 state: Pick<DesktopUpdateState, "availableVersion" | "downloadedVersion">,
167 resolve: MessageResolver = m,
168): string {
169 const version = state.downloadedVersion ?? state.availableVersion;
170 return version
171 ? resolve(
172 msg(
173 "Install update {version} and restart T3 Code?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.",
174 { version },
175 ),
176 )
177 : resolve(
178 msg(
179 "Install update and restart T3 Code?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.",
180 ),
181 );
182}
183
184export function getDesktopUpdateActionError(result: DesktopUpdateActionResult): string | null {
185 if (!result.accepted || result.completed) return null;
186 if (typeof result.state.message !== "string") return null;
187 const message = result.state.message.trim();
188 return message.length > 0 ? message : null;
189}
190
191export function shouldToastDesktopUpdateActionResult(result: DesktopUpdateActionResult): boolean {
192 return getDesktopUpdateActionError(result) !== null;
193}
194
195export function canCheckForUpdate(state: DesktopUpdateState | null): boolean {
196 if (!state || !state.enabled) return false;
197 return (
198 state.status !== "checking" && state.status !== "downloading" && state.status !== "disabled"
199 );
200}
201
202/** `managedDenial` is this machine's policy refusing updates (`managedUpdatesDenial`); it wins. */
203export function getDesktopUpdateDisabledReason(
204 state: Pick<DesktopUpdateState, "enabled" | "status" | "message"> | null,
205 managedDenial?: string,
206 resolve: MessageResolver = m,
207): string | null {
208 if (managedDenial !== undefined) return managedDenial;
209 if (!state || (state.enabled && state.status !== "disabled")) return null;
210 return (
211 state.message?.trim() || resolve(msg("Automatic updates are not available in this build."))
212 );
213}
214
215export function getDesktopUpdateIdleTooltip(
216 state: Pick<DesktopUpdateState, "enabled" | "status" | "message"> | null,
217 managedDenial?: string,
218 resolve: MessageResolver = m,
219) {
220 const disabledReason = getDesktopUpdateDisabledReason(state, managedDenial, resolve);
221 return disabledReason
222 ? { title: resolve(msg("Updates unavailable")), detail: disabledReason }
223 : { title: resolve(msg("Check for updates")), detail: null };
224}