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

ProviderUpdatesAction.tsx

110 lines3.5 KB

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