Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { PROVIDER_DISPLAY_NAMES } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4import { useMemo } from "react";
5
6import { APP_VERSION } from "~/branding";
7import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
8import { isElectron } from "~/env";
9import { useEnvironments } from "~/state/environments";
10import { useNpmReleasePublished } from "~/state/npmRelease";
11import { serverEnvironment } from "~/state/server";
12import { environmentSession } from "~/state/session";
13import { useProviderUpdates } from "../ProviderUpdatesAction";
14import { canUpdateServer, useServerUpdate } from "../ServerUpdateAction";
15import {
16 collectRestartingMachines,
17 collectServerUpdateTargets,
18 describeUpdateEverything,
19 runUpdateEverything,
20} from "./updateEverything.logic";
21
22/**
23 * The remote half of the sidebar update button: outdated providers on every
24 * connected machine, and servers behind the version this client runs or is
25 * about to install (`localUpdateVersion`). Servers that install from npm appear
26 * only once npm confirms that version. `run` performs the whole pass, including
27 * this app's own download and install when given.
28 */
29export function useUpdateEverything(localUpdateVersion: string | null) {
30 const targetVersion = localUpdateVersion ?? APP_VERSION;
31 const { environments } = useEnvironments();
32 const machinesAtom = useMemo(
33 () =>
34 Atom.make((get) =>
35 environments.map((environment) => ({
36 environmentId: environment.environmentId,
37 label: environment.label,
38 serverConfig: environment.serverConfig,
39 connected: environment.connection.phase === "connected",
40 hostedByThisApp:
41 isElectron &&
42 (environment.entry.target._tag === "PrimaryConnectionTarget" ||
43 isDesktopLocalConnectionTarget(environment.entry.target)),
44 canMaintain: canUpdateServer(
45 get(environmentSession.sessionStateAtom(environment.environmentId)),
46 ),
47 updateState: get(serverEnvironment.updateStateAtom(environment.environmentId)),
48 })),
49 ),
50 [environments],
51 );
52 const machines = useAtomValue(machinesAtom);
53 const behindServers = collectServerUpdateTargets(machines, targetVersion);
54 // A desktop-managed server installs from its own app's update feed, so only
55 // servers that download t3 from npm wait for npm to publish the version.
56 const needsNpm = behindServers.some((server) => server.selfUpdate !== "desktop-managed");
57 const published = useNpmReleasePublished(needsNpm ? targetVersion : null);
58 const servers = published
59 ? behindServers
60 : behindServers.filter((server) => server.selfUpdate === "desktop-managed");
61 const restarts = collectRestartingMachines(machines, servers, localUpdateVersion !== null);
62 const providers = useProviderUpdates();
63 const updateServer = useServerUpdate();
64
65 const { summary, lines } = describeUpdateEverything({
66 localVersion: localUpdateVersion,
67 servers,
68 providerMachines: providers.machines.map((machine) => ({
69 environmentId: machine.environmentId,
70 label: machine.label,
71 providers: machine.candidates.map(
72 (candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver,
73 ),
74 })),
75 });
76
77 return {
78 hasUpdates: servers.length > 0 || providers.machines.length > 0,
79 /** Short label for the tooltip and the confirmation title. */
80 summary,
81 /** One line per machine, for the confirmation. */
82 lines,
83 /** The servers this run restarts, which stops their running threads. */
84 restarts,
85 run: (local: { downloadLocal?: () => Promise<boolean>; installLocal?: () => Promise<void> }) =>
86 runUpdateEverything({
87 ...local,
88 updateProviders: providers.updateAll,
89 updateServers: async () => {
90 await Promise.all(servers.map((server) => updateServer(server)));
91 },
92 }),
93 };
94}