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

ProviderUpdatesAction.tsx

109 lines3.4 KB

1import { PROVIDER_DISPLAY_NAMES } from "@t3tools/contracts";
2import { settlePromise } from "@t3tools/client-runtime/state/runtime";
3import { useRef, useState } from "react";
4​
5import { useEnvironments } from "~/state/environments";
6import { serverEnvironment } from "~/state/server";
7import { useAtomCommand } from "~/state/use-atom-command";
8import {
9 canOneClickUpdateProviderCandidate,
10 collectProviderUpdateCandidates,
11 getProviderUpdateRunToastView,
12 type ProviderUpdateRun,
13} from "./ProviderUpdateLaunchNotification.logic";
14import { Button } from "./ui/button";
15import { stackedThreadToast, toastManager } from "./ui/toast";
16import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
17​
18export function useProviderUpdates() {
19 const { environments } = useEnvironments();
20 const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
21 reportFailure: false,
22 });
23 const machines = environments.flatMap((environment) => {
24 const providers = environment.serverConfig?.providers ?? [];
25 if (environment.connection.phase !== "connected") {
26 return [];
27 }
28 const candidates = collectProviderUpdateCandidates(providers).filter((candidate) =>
29 canOneClickUpdateProviderCandidate(candidate, providers),
30 );
31 return candidates.length > 0
32 ? [{ environmentId: environment.environmentId, label: environment.label, candidates }]
33 : [];
34 });
35​
36 const updateAll = async ({ quiet = false }: { readonly quiet?: boolean } = {}) => {
37 const runs = await Promise.all(
38 machines.flatMap(({ environmentId, label, candidates }) =>
39 candidates.map(async (candidate): Promise<ProviderUpdateRun> => ({
40 environmentId,
41 machineLabel: label,
42 driver: candidate.driver,
43 instanceId: candidate.instanceId,
44 result: await updateProvider({
45 environmentId,
46 input: { provider: candidate.driver, instanceId: candidate.instanceId },
47 }),
48 })),
49 ),
50 );
51 const view = quiet ? null : getProviderUpdateRunToastView(runs);
52 if (view) {
53 toastManager.add(
54 stackedThreadToast({
55 ...view,
56 description: <span className="whitespace-pre-line">{view.description}</span>,
57 }),
58 );
59 }
60 return runs;
61 };
62​
63 return { machines, updateAll };
65​
66export function ProviderUpdatesAction() {
67 const { machines, updateAll } = useProviderUpdates();
68 const pending = useRef(false);
69 const [isPending, setIsPending] = useState(false);
70 if (machines.length === 0 && !isPending) {
71 return null;
72 }
73​
74 const handleUpdate = async () => {
75 if (pending.current) return;
76 pending.current = true;
77 setIsPending(true);
78 await settlePromise(() => updateAll());
79 pending.current = false;
80 setIsPending(false);
81 };
82​
83 return (
84 <Tooltip>
85 <TooltipTrigger
86 render={
87 <Button
88 size="xs"
89 variant="ghost-muted"
90 disabled={isPending}
91 onClick={() => void handleUpdate()}
92 >
93 {isPending ? "Updating…" : "Update all"}
94 </Button>
95 }
96 />
97 <TooltipPopup side="top">
98 {machines.map((machine) => (
99 <div key={machine.environmentId}>
100 {machine.label}:{" "}
101 {machine.candidates
102 .map((candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver)
103 .join(", ")}
104 </div>
105 ))}
106 </TooltipPopup>
107 </Tooltip>
108 );