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