Open History to see the last commit
1import { PROVIDER_DISPLAY_NAMES } from "@t3tools/contracts";
2import { useMemo, useRef, useState } from "react";
3
4import { useEnvironments } from "~/state/environments";
5import { serverEnvironment } from "~/state/server";
6import { useAtomCommand } from "~/state/use-atom-command";
7import {
8 canOneClickUpdateProviderCandidate,
9 collectProviderUpdateCandidates,
10 getProviderUpdateRunToastView,
11 type ProviderUpdateRun,
12} from "./ProviderUpdateLaunchNotification.logic";
13import { Button } from "./ui/button";
14import { stackedThreadToast, toastManager } from "./ui/toast";
15import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
16
17/**
18 * Every outdated provider with a one-click update on every connected machine,
19 * plus a function that updates them all and reports the results in one toast.
20 * Each server queues updates that share an installer, so sending them all
21 * together is safe. The server still checks permissions: a session that
22 * cannot operate a machine gets a failure line for it. The returned promise
23 * settles when every update has finished.
24 */
25export function useProviderUpdates() {
26 const { environments } = useEnvironments();
27 const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
28 reportFailure: false,
29 });
30 const machines = useMemo(
31 () =>
32 environments.flatMap((environment) => {
33 const providers = environment.serverConfig?.providers ?? [];
34 if (environment.connection.phase !== "connected") {
35 return [];
36 }
37 const candidates = collectProviderUpdateCandidates(providers).filter((candidate) =>
38 canOneClickUpdateProviderCandidate(candidate, providers),
39 );
40 return candidates.length > 0
41 ? [{ environmentId: environment.environmentId, label: environment.label, candidates }]
42 : [];
43 }),
44 [environments],
45 );
46
47 const updateAll = async () => {
48 const runs = await Promise.all(
49 machines.flatMap(({ environmentId, label, candidates }) =>
50 candidates.map(async (candidate): Promise<ProviderUpdateRun> => ({
51 machineLabel: label,
52 driver: candidate.driver,
53 instanceId: candidate.instanceId,
54 result: await updateProvider({
55 environmentId,
56 input: { provider: candidate.driver, instanceId: candidate.instanceId },
57 }),
58 })),
59 ),
60 );
61 const view = getProviderUpdateRunToastView(runs);
62 if (view) {
63 toastManager.add(
64 stackedThreadToast({
65 ...view,
66 description: <span className="whitespace-pre-line">{view.description}</span>,
67 }),
68 );
69 }
70 };
71
72 return { machines, updateAll };
73}
74
75/** Provider settings button for {@link useProviderUpdates}. Renders nothing
76 when no machine has a one-click update. */
77export function ProviderUpdatesAction() {
78 const { machines, updateAll } = useProviderUpdates();
79 const pending = useRef(false);
80 const [isPending, setIsPending] = useState(false);
81 // Candidates leave the list as soon as their servers report them queued, so
82 // keep the button while the run is in flight.
83 if (machines.length === 0 && !isPending) {
84 return null;
85 }
86
87 const handleUpdate = async () => {
88 if (pending.current) return;
89 pending.current = true;
90 setIsPending(true);
91 try {
92 await updateAll();
93 } finally {
94 pending.current = false;
95 setIsPending(false);
96 }
97 };
98
99 return (
100 <Tooltip>
101 <TooltipTrigger
102 render={
103 <Button
104 size="xs"
105 variant="ghost-muted"
106 disabled={isPending}
107 onClick={() => void handleUpdate()}
108 >
109 {isPending ? "Updating…" : "Update all"}
110 </Button>
111 }
112 />
113 <TooltipPopup side="top">
114 {machines.map((machine) => (
115 <div key={machine.environmentId}>
116 {machine.label}:{" "}
117 {machine.candidates
118 .map((candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver)
119 .join(", ")}
120 </div>
121 ))}
122 </TooltipPopup>
123 </Tooltip>
124 );
125}