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