@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 116 lines 4.32 KB
Raw
1 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2 import type { DesktopBridge, DesktopUpdateChannel, DesktopUpdateState } from "@t3tools/contracts";
3 import { acceptsTrackSwitchRelease } from "@t3tools/shared/cliRelease";
4 import { msg } from "gt-react";
5
6 import { m, type MessageResolver } from "../../i18n/messages";
7
8 type TrackSwitchBridge = Pick<DesktopBridge, "setUpdateChannel">;
9
10 /** Saves the track, then, when this app must reinstall, finds a build the database check covers.
11 * The saved track is the user's intent and stays even when no such build is ready yet: a track
12 * saved toward Stable never offers an older build outside a guarded switch, and "Switch them"
13 * retries. Saving when nothing reinstalls lets picking the old track undo a partial switch. */
14 export async function prepareLocalTrackSwitch(
15 bridge: TrackSwitchBridge,
16 input: {
17 readonly channel: DesktopUpdateChannel;
18 readonly version: string;
19 readonly reinstall: boolean;
20 },
21 ): Promise<
22 | { readonly ok: true; readonly local: DesktopUpdateState | null }
23 | { readonly ok: false; readonly message: string }
24 > {
25 let local: DesktopUpdateState;
26 try {
27 // A build on another track re-checks the feed, older releases allowed down to the checked one.
28 local = await bridge.setUpdateChannel(
29 input.channel,
30 input.reinstall ? input.version : undefined,
31 );
32 } catch (error) {
33 return {
34 ok: false,
35 message:
36 error instanceof Error
37 ? (thrownErrorMessage(error, m) ?? error.message)
38 : m(msg("The update track did not change.")),
39 };
40 }
41 if (!input.reinstall) return { ok: true, local: null };
42 const found = local.downloadedVersion ?? local.availableVersion;
43 if (
44 (local.status === "available" || local.status === "downloaded") &&
45 found !== null &&
46 acceptsTrackSwitchRelease(found, input.channel, input.version)
47 ) {
48 return { ok: true, local };
49 }
50 return {
51 ok: false,
52 message:
53 found !== null
54 ? m(
55 msg(
56 "This app found {found}, which is older than {version}. Try again in a few minutes.",
57 { found, version: input.version },
58 ),
59 )
60 : (local.message ??
61 m(msg("This app found no build of {version} to install.", { version: input.version }))),
62 };
63 }
64
65 /** When no machine moves, confirming only saves the track; when that track is already saved too,
66 * confirming would do nothing. Track-only hosts are not rows but still get a request. */
67 export function nothingToMove(
68 input: {
69 readonly label: string;
70 readonly channel: DesktopUpdateChannel;
71 // Null until the desktop reports it; an unknown track may still need saving.
72 readonly savedChannel: DesktopUpdateChannel | null;
73 readonly switchesLocal: boolean;
74 readonly crossesLocal: boolean;
75 readonly rows: ReadonlyArray<{ readonly skipped: string | null }>;
76 readonly hasTargets: boolean;
77 },
78 resolve: MessageResolver = m,
79 ): { readonly note: string | null; readonly disabledReason: string | null } {
80 if (input.switchesLocal || input.rows.some((row) => row.skipped === null)) {
81 return { note: null, disabledReason: null };
82 }
83 const label = input.label;
84 const allOnTrack = input.rows.length === 0 && !input.crossesLocal;
85 if (!input.hasTargets && input.savedChannel === input.channel) {
86 return {
87 note: null,
88 disabledReason: allOnTrack
89 ? resolve(msg("Every connected machine is already on {label}.", { label }))
90 : resolve(msg("No machine can switch to {label} now.", { label })),
91 };
92 }
93 return {
94 note: allOnTrack
95 ? resolve(
96 msg("Every connected machine is already on {label}. This only saves the track.", {
97 label,
98 }),
99 )
100 : resolve(msg("No machine can switch to {label} now. This only saves the track.", { label })),
101 disabledReason: null,
102 };
103 }
104
105 /**
106 * Whether this app stays put while connected machines move: its updater is off, as for a .deb
107 * install or without a feed. Off only because a policy rules out its nightly channel, it moves.
108 */
109 export function localUpdaterStaysPut(
110 state: Pick<DesktopUpdateState, "enabled" | "channelBlockedByPolicy"> | null | undefined,
111 target: DesktopUpdateChannel,
112 ): boolean {
113 return (
114 state?.enabled === false && !(target === "latest" && state.channelBlockedByPolicy === true)
115 );
116 }