Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

updateTrackSwitch.logic.ts

109 lines4.2 KB

1import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2import type { DesktopBridge, DesktopUpdateChannel, DesktopUpdateState } from "@t3tools/contracts";
3import { acceptsTrackSwitchRelease } from "@t3tools/shared/cliRelease";
4import { msg } from "gt-react";
5​
6import { m, type MessageResolver } from "../../i18n/messages";
7​
8type 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. */
14export 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`This app found ${{ found }}, which is older than ${{ version: input.version }}. Try again in a few minutes.`,
56 )
57 : (local.message ??
58 m(msg`This app found no build of ${{ version: input.version }} to install.`)),
59 };
61​
62/** When no machine moves, confirming only saves the track; when that track is already saved too,
63 * confirming would do nothing. Track-only hosts are not rows but still get a request. */
64export function nothingToMove(
65 input: {
66 readonly label: string;
67 readonly channel: DesktopUpdateChannel;
68 // Null until the desktop reports it; an unknown track may still need saving.
69 readonly savedChannel: DesktopUpdateChannel | null;
70 readonly switchesLocal: boolean;
71 readonly crossesLocal: boolean;
72 readonly rows: ReadonlyArray<{ readonly skipped: string | null }>;
73 readonly hasTargets: boolean;
74 },
75 resolve: MessageResolver = m,
76): { readonly note: string | null; readonly disabledReason: string | null } {
77 if (input.switchesLocal || input.rows.some((row) => row.skipped === null)) {
78 return { note: null, disabledReason: null };
79 }
80 const label = input.label;
81 const allOnTrack = input.rows.length === 0 && !input.crossesLocal;
82 if (!input.hasTargets && input.savedChannel === input.channel) {
83 return {
84 note: null,
85 disabledReason: allOnTrack
86 ? resolve(msg`Every connected machine is already on ${{ label }}.`)
87 : resolve(msg`No machine can switch to ${{ label }} now.`),
88 };
89 }
90 return {
91 note: allOnTrack
92 ? resolve(msg`Every connected machine is already on ${{ label }}. This only saves the track.`)
93 : resolve(msg`No machine can switch to ${{ label }} now. This only saves the track.`),
94 disabledReason: null,
95 };
97​
98/**
99 * Whether this app stays put while connected machines move: its updater is off, as for a .deb
100 * install or without a feed. Off only because a policy rules out its nightly channel, it moves.
101 */
102export function localUpdaterStaysPut(
103 state: Pick<DesktopUpdateState, "enabled" | "channelBlockedByPolicy"> | null | undefined,
104 target: DesktopUpdateChannel,
105): boolean {
106 return (
107 state?.enabled === false && !(target === "latest" && state.channelBlockedByPolicy === true)
108 );