Open History to see the last commit
1import { AuthProvidersManageScope, 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 { useEnvironmentsWithScope } from "~/state/session";
8import { useAtomCommand } from "~/state/use-atom-command";
9import {
10 canOneClickUpdateProviderCandidate,
11 collectProviderUpdateCandidates,
12 getProviderUpdateRunToastView,
13 type ProviderUpdateRun,
14} from "./ProviderUpdateLaunchNotification.logic";
15import { Button } from "./ui/button";
16import { stackedThreadToast, toastManager } from "./ui/toast";
17import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
18
19export function useProviderUpdates() {
20 const { environments } = useEnvironments();
21 const manageableIds = useEnvironmentsWithScope(environments, AuthProvidersManageScope);
22 const providersByEnvironment = useEnvironmentProviders();
23 const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
24 reportFailure: false,
25 });
26 const machines = environments.flatMap((environment) => {
27 const providers = providersByEnvironment.get(environment.environmentId) ?? [];
28 if (
29 environment.connection.phase !== "connected" ||
30 !manageableIds.has(environment.environmentId)
31 ) {
32 return [];
33 }
34 const candidates = collectProviderUpdateCandidates(providers).filter((candidate) =>
35 canOneClickUpdateProviderCandidate(candidate, providers),
36 );
37 return candidates.length > 0
38 ? [{ environmentId: environment.environmentId, label: environment.label, candidates }]
39 : [];
40 });
41
42 const updateAll = async ({ quiet = false }: { readonly quiet?: boolean } = {}) => {
43 const runs = await Promise.all(
44 machines.flatMap(({ environmentId, label, candidates }) =>
45 candidates.map(async (candidate): Promise<ProviderUpdateRun> => ({
46 environmentId,
47 machineLabel: label,
48 driver: candidate.driver,
49 instanceId: candidate.instanceId,
50 result: await updateProvider({
51 environmentId,
52 input: { provider: candidate.driver, instanceId: candidate.instanceId },
53 }),
54 })),
55 ),
56 );
57 const view = quiet ? null : getProviderUpdateRunToastView(runs);
58 if (view) {
59 toastManager.add(
60 stackedThreadToast({
61 ...view,
62 description: <span className="whitespace-pre-line">{view.description}</span>,
63 }),
64 );
65 }
66 return runs;
67 };
68
69 return { machines, updateAll };
70}
71
72export function ProviderUpdatesAction() {
73 const { machines, updateAll } = useProviderUpdates();
74 const pending = useRef(false);
75 const [isPending, setIsPending] = useState(false);
76 if (machines.length === 0 && !isPending) {
77 return null;
78 }
79
80 const handleUpdate = async () => {
81 if (pending.current) return;
82 pending.current = true;
83 setIsPending(true);
84 await settlePromise(() => updateAll());
85 pending.current = false;
86 setIsPending(false);
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}