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
17export function useProviderUpdates() {
18 const { environments } = useEnvironments();
19 const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
20 reportFailure: false,
21 });
22 const machines = useMemo(
23 () =>
24 environments.flatMap((environment) => {
25 const providers = environment.serverConfig?.providers ?? [];
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 [environments],
37 );
38
39 const updateAll = async ({ quiet = false }: { readonly quiet?: boolean } = {}) => {
40 const runs = await Promise.all(
41 machines.flatMap(({ environmentId, label, candidates }) =>
42 candidates.map(async (candidate): Promise<ProviderUpdateRun> => ({
43 environmentId,
44 machineLabel: label,
45 driver: candidate.driver,
46 instanceId: candidate.instanceId,
47 result: await updateProvider({
48 environmentId,
49 input: { provider: candidate.driver, instanceId: candidate.instanceId },
50 }),
51 })),
52 ),
53 );
54 const view = quiet ? null : getProviderUpdateRunToastView(runs);
55 if (view) {
56 toastManager.add(
57 stackedThreadToast({
58 ...view,
59 description: <span className="whitespace-pre-line">{view.description}</span>,
60 }),
61 );
62 }
63 return runs;
64 };
65
66 return { machines, updateAll };
67}
68
69export function ProviderUpdatesAction() {
70 const { machines, updateAll } = useProviderUpdates();
71 const pending = useRef(false);
72 const [isPending, setIsPending] = useState(false);
73 if (machines.length === 0 && !isPending) {
74 return null;
75 }
76
77 const handleUpdate = async () => {
78 if (pending.current) return;
79 pending.current = true;
80 setIsPending(true);
81 try {
82 await updateAll();
83 } finally {
84 pending.current = false;
85 setIsPending(false);
86 }
87 };
88
89 return (
90 <Tooltip>
91 <TooltipTrigger
92 render={
93 <Button
94 size="xs"
95 variant="ghost-muted"
96 disabled={isPending}
97 onClick={() => void handleUpdate()}
98 >
99 {isPending ? "Updating…" : "Update all"}
100 </Button>
101 }
102 />
103 <TooltipPopup side="top">
104 {machines.map((machine) => (
105 <div key={machine.environmentId}>
106 {machine.label}:{" "}
107 {machine.candidates
108 .map((candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver)
109 .join(", ")}
110 </div>
111 ))}
112 </TooltipPopup>
113 </Tooltip>
114 );
115}