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

ProviderUpdatesAction.tsx

128 lines4.2 KB

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 with every result when every update has finished. `quiet` skips the
24 * toast, for a caller that reports the results itself.
25 */
26export function useProviderUpdates() {
27 const { environments } = useEnvironments();
28 const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
29 reportFailure: false,
30 });
31 const machines = useMemo(
32 () =>
33 environments.flatMap((environment) => {
34 const providers = environment.serverConfig?.providers ?? [];
35 if (environment.connection.phase !== "connected") {
36 return [];
37 }
38 const candidates = collectProviderUpdateCandidates(providers).filter((candidate) =>
39 canOneClickUpdateProviderCandidate(candidate, providers),
40 );
41 return candidates.length > 0
42 ? [{ environmentId: environment.environmentId, label: environment.label, candidates }]
43 : [];
44 }),
45 [environments],
46 );
47​
48 const updateAll = async ({ quiet = false }: { readonly quiet?: boolean } = {}) => {
49 const runs = await Promise.all(
50 machines.flatMap(({ environmentId, label, candidates }) =>
51 candidates.map(async (candidate): Promise<ProviderUpdateRun> => ({
52 environmentId,
53 machineLabel: label,
54 driver: candidate.driver,
55 instanceId: candidate.instanceId,
56 result: await updateProvider({
57 environmentId,
58 input: { provider: candidate.driver, instanceId: candidate.instanceId },
59 }),
60 })),
61 ),
62 );
63 const view = quiet ? null : getProviderUpdateRunToastView(runs);
64 if (view) {
65 toastManager.add(
66 stackedThreadToast({
67 ...view,
68 description: <span className="whitespace-pre-line">{view.description}</span>,
69 }),
70 );
71 }
72 return runs;
73 };
74​
75 return { machines, updateAll };
77​
78/** Provider settings button for {@link useProviderUpdates}. Renders nothing
79 when no machine has a one-click update. */
80export function ProviderUpdatesAction() {
81 const { machines, updateAll } = useProviderUpdates();
82 const pending = useRef(false);
83 const [isPending, setIsPending] = useState(false);
84 // Candidates leave the list as soon as their servers report them queued, so
85 // keep the button while the run is in flight.
86 if (machines.length === 0 && !isPending) {
87 return null;
88 }
89​
90 const handleUpdate = async () => {
91 if (pending.current) return;
92 pending.current = true;
93 setIsPending(true);
94 try {
95 await updateAll();
96 } finally {
97 pending.current = false;
98 setIsPending(false);
99 }
100 };
101​
102 return (
103 <Tooltip>
104 <TooltipTrigger
105 render={
106 <Button
107 size="xs"
108 variant="ghost-muted"
109 disabled={isPending}
110 onClick={() => void handleUpdate()}
111 >
112 {isPending ? "Updating…" : "Update all"}
113 </Button>
114 }
115 />
116 <TooltipPopup side="top">
117 {machines.map((machine) => (
118 <div key={machine.environmentId}>
119 {machine.label}:{" "}
120 {machine.candidates
121 .map((candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver)
122 .join(", ")}
123 </div>
124 ))}
125 </TooltipPopup>
126 </Tooltip>
127 );