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 { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { m as resolveMessage } from "~/i18n/messages"; |
| 3 | import { useAtomValue } from "@effect/atom-react"; |
| 4 | import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 5 | import type { DesktopUpdateChannel } from "@t3tools/contracts"; |
| 6 | import { acceptsTrackSwitchRelease, desktopUpdateChannelOf } from "@t3tools/shared/cliRelease"; |
| 7 | import { isMacPlatform } from "@t3tools/shared/keybindings"; |
| 8 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 9 | import { useState } from "react"; |
| 10 | import { AsyncResult } from "effect/reactivity"; |
| 11 | |
| 12 | import { APP_VERSION } from "~/branding"; |
| 13 | import { isLocalEnvironmentDisabled } from "~/localEnvironment"; |
| 14 | import { useDesktopUpdateState } from "~/state/desktopUpdate"; |
| 15 | import { useEnvironments } from "~/state/environments"; |
| 16 | import { npmReleaseAtom, npmReleaseFailureMessage } from "~/state/npmRelease"; |
| 17 | import { downloadLocalUpdate, installLocalUpdate } from "../sidebar/SidebarUpdatePill"; |
| 18 | import { UpdateEverythingDialog } from "../sidebar/UpdateEverythingDialog"; |
| 19 | import { |
| 20 | channelSwitchLabel, |
| 21 | localSwitchBlockReason, |
| 22 | type ChannelSwitchRelease, |
| 23 | } from "../sidebar/updateEverything.logic"; |
| 24 | import { isHostedByThisApp, useUpdateEverything } from "../sidebar/useUpdateEverything"; |
| 25 | import { |
| 26 | localUpdaterStaysPut, |
| 27 | nothingToMove, |
| 28 | prepareLocalTrackSwitch, |
| 29 | } from "./updateTrackSwitch.logic"; |
| 30 | import { |
| 31 | AlertDialog, |
| 32 | AlertDialogClose, |
| 33 | AlertDialogDescription, |
| 34 | AlertDialogFooter, |
| 35 | AlertDialogHeader, |
| 36 | AlertDialogPopup, |
| 37 | AlertDialogTitle, |
| 38 | } from "../ui/alert-dialog"; |
| 39 | import { Button } from "../ui/button"; |
| 40 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 41 | |
| 42 | /** Moves this app and every connected machine to `channel`. */ |
| 43 | export function UpdateTrackSwitchDialog({ |
| 44 | channel, |
| 45 | onClose, |
| 46 | }: { |
| 47 | readonly channel: DesktopUpdateChannel; |
| 48 | readonly onClose: () => void; |
| 49 | }) { |
| 50 | const gt = useGT(); |
| 51 | const m = useMessages(); |
| 52 | // npm dist-tags match the desktop channel names. |
| 53 | const release = useAtomValue(npmReleaseAtom(channel)); |
| 54 | if (AsyncResult.isSuccess(release) && release.value !== null) { |
| 55 | return <SwitchPlan release={{ channel, ...release.value }} onClose={onClose} />; |
| 56 | } |
| 57 | const label = channelSwitchLabel(channel, m); |
| 58 | return ( |
| 59 | <AlertDialog open onOpenChange={(open) => !open && onClose()}> |
| 60 | <AlertDialogPopup> |
| 61 | <AlertDialogHeader> |
| 62 | <AlertDialogTitle> |
| 63 | <T> |
| 64 | Switch to <Var>{label}</Var> |
| 65 | </T> |
| 66 | </AlertDialogTitle> |
| 67 | <AlertDialogDescription> |
| 68 | {AsyncResult.isFailure(release) |
| 69 | ? npmReleaseFailureMessage(release.cause, m) |
| 70 | : AsyncResult.isSuccess(release) |
| 71 | ? gt( |
| 72 | "Could not find the latest {label} release in this environment's npm registry.", |
| 73 | { |
| 74 | label, |
| 75 | }, |
| 76 | ) |
| 77 | : gt("Checking releases…")} |
| 78 | </AlertDialogDescription> |
| 79 | </AlertDialogHeader> |
| 80 | <AlertDialogFooter> |
| 81 | <AlertDialogClose render={<Button variant="outline" />}> |
| 82 | <T>Cancel</T> |
| 83 | </AlertDialogClose> |
| 84 | </AlertDialogFooter> |
| 85 | </AlertDialogPopup> |
| 86 | </AlertDialog> |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | function SwitchPlan({ |
| 91 | release, |
| 92 | onClose, |
| 93 | }: { |
| 94 | readonly release: ChannelSwitchRelease; |
| 95 | readonly onClose: () => void; |
| 96 | }) { |
| 97 | const gt = useGT(); |
| 98 | const m = useMessages(); |
| 99 | const updateState = useDesktopUpdateState(); |
| 100 | const { environments } = useEnvironments(); |
| 101 | const crossesLocal = desktopUpdateChannelOf(APP_VERSION) !== release.channel; |
| 102 | const updatesOff = localUpdaterStaysPut(updateState, release.channel); |
| 103 | // After a partial switch this app may already be on the track; then only remotes move. |
| 104 | const switchesLocal = crossesLocal && !updatesOff; |
| 105 | const [checking, setChecking] = useState(false); |
| 106 | const everything = useUpdateEverything(switchesLocal ? release.version : null, release); |
| 107 | const label = channelSwitchLabel(release.channel, m); |
| 108 | const idle = nothingToMove( |
| 109 | { |
| 110 | label, |
| 111 | channel: release.channel, |
| 112 | savedChannel: updateState?.channel ?? null, |
| 113 | switchesLocal, |
| 114 | crossesLocal, |
| 115 | rows: everything.rows, |
| 116 | hasTargets: everything.hasUpdates, |
| 117 | }, |
| 118 | m, |
| 119 | ); |
| 120 | // This app's reinstall restarts every server it hosts, so each of their databases must allow it. |
| 121 | const localBlocked = localSwitchBlockReason( |
| 122 | APP_VERSION, |
| 123 | release, |
| 124 | environments.filter(isHostedByThisApp).map((environment) => ({ |
| 125 | connected: environment.connection.phase === "connected", |
| 126 | databaseMigration: environment.serverConfig?.environment.databaseMigration, |
| 127 | })), |
| 128 | window.desktopBridge?.getLocalEnvironmentBootstraps().length ?? 0, |
| 129 | !isLocalEnvironmentDisabled(), |
| 130 | m, |
| 131 | ); |
| 132 | |
| 133 | const run = async () => { |
| 134 | const bridge = window.desktopBridge; |
| 135 | if (!bridge) return; |
| 136 | // Stay open while the feed is checked; the run's own progress takes over after. |
| 137 | setChecking(true); |
| 138 | const fail = (description: string) => |
| 139 | toastManager.add( |
| 140 | stackedThreadToast({ |
| 141 | type: "error", |
| 142 | title: gt("Could not switch to {label}", { label }), |
| 143 | description, |
| 144 | }), |
| 145 | ); |
| 146 | const prepared = await prepareLocalTrackSwitch(bridge, { |
| 147 | channel: release.channel, |
| 148 | version: release.version, |
| 149 | reinstall: switchesLocal, |
| 150 | }); |
| 151 | onClose(); |
| 152 | if (!prepared.ok) { |
| 153 | fail(prepared.message); |
| 154 | return; |
| 155 | } |
| 156 | const { local } = prepared; |
| 157 | const result = await settlePromise(() => |
| 158 | everything.run( |
| 159 | local === null |
| 160 | ? {} |
| 161 | : { |
| 162 | ...(local.status === "available" |
| 163 | ? { |
| 164 | downloadLocal: (reportError) => |
| 165 | downloadLocalUpdate(bridge, { notifyDownloaded: false, reportError }), |
| 166 | } |
| 167 | : {}), |
| 168 | // Install only a build the database check covers, pinned so nothing replaces it. |
| 169 | installLocal: async (reportError) => { |
| 170 | const { downloadedVersion } = await bridge.getUpdateState(); |
| 171 | if ( |
| 172 | downloadedVersion === null || |
| 173 | !acceptsTrackSwitchRelease(downloadedVersion, release.channel, release.version) |
| 174 | ) { |
| 175 | reportError( |
| 176 | gt("Could not install update"), |
| 177 | downloadedVersion === null |
| 178 | ? gt("This app downloaded nothing, not {version} or newer.", { |
| 179 | version: release.version, |
| 180 | }) |
| 181 | : gt("This app downloaded {downloaded}, not {version} or newer.", { |
| 182 | downloaded: downloadedVersion, |
| 183 | version: release.version, |
| 184 | }), |
| 185 | ); |
| 186 | return false; |
| 187 | } |
| 188 | return installLocalUpdate(bridge, reportError, downloadedVersion); |
| 189 | }, |
| 190 | }, |
| 191 | ), |
| 192 | ); |
| 193 | if (result._tag === "Failure") { |
| 194 | const error = squashAtomCommandFailure(result); |
| 195 | fail( |
| 196 | error instanceof Error |
| 197 | ? (thrownErrorMessage(error, resolveMessage) ?? error.message) |
| 198 | : gt("An unexpected error occurred."), |
| 199 | ); |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | return ( |
| 204 | <UpdateEverythingDialog |
| 205 | open |
| 206 | onOpenChange={(open) => !open && !checking && onClose()} |
| 207 | title={gt("Switch to {label}?", { label })} |
| 208 | version={release.version} |
| 209 | providerCount={0} |
| 210 | rows={everything.rows} |
| 211 | notes={[ |
| 212 | ...(idle.note === null ? [] : [idle.note]), |
| 213 | ...(crossesLocal && updatesOff |
| 214 | ? [gt("This app's updates are off here, so reinstall it yourself to switch.")] |
| 215 | : []), |
| 216 | ...(switchesLocal && isMacPlatform(navigator.platform) |
| 217 | ? [gt("If T3 Code does not reopen after it restarts, open it again.")] |
| 218 | : []), |
| 219 | ]} |
| 220 | restarts={everything.restarts} |
| 221 | confirmLabel={checking ? gt("Checking…") : gt("Switch to {label}", { label })} |
| 222 | busy={checking} |
| 223 | disabledReason={switchesLocal && localBlocked !== null ? localBlocked : idle.disabledReason} |
| 224 | onConfirm={() => void run()} |
| 225 | /> |
| 226 | ); |
| 227 | } |