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