|
| 1 | +import { PROVIDER_DISPLAY_NAMES } from "@t3tools/contracts"; |
| 2 | +import { useMemo, useRef, useState } from "react"; |
| 3 | + |
| 4 | +import { useEnvironments } from "~/state/environments"; |
| 5 | +import { serverEnvironment } from "~/state/server"; |
| 6 | +import { useAtomCommand } from "~/state/use-atom-command"; |
| 7 | +import { |
| 8 | + canOneClickUpdateProviderCandidate, |
| 9 | + collectProviderUpdateCandidates, |
| 10 | + getProviderUpdateRunToastView, |
| 11 | + type ProviderUpdateRun, |
| 12 | +} from "./ProviderUpdateLaunchNotification.logic"; |
| 13 | +import { Button } from "./ui/button"; |
| 14 | +import { stackedThreadToast, toastManager } from "./ui/toast"; |
| 15 | +import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Updates every outdated provider on every connected machine at once, then |
| 19 | + * reports the results in one toast. Each server queues updates that share an |
| 20 | + * installer, so sending them all together is safe. The server still checks |
| 21 | + * permissions: a session that cannot operate a machine gets a failure line for |
| 22 | + * it. Renders nothing when no machine has a one-click update. |
| 23 | + */ |
| 24 | +export function ProviderUpdatesAction() { |
| 25 | + const { environments } = useEnvironments(); |
| 26 | + const updateProvider = useAtomCommand(serverEnvironment.updateProvider, { |
| 27 | + reportFailure: false, |
| 28 | + }); |
| 29 | + const pending = useRef(false); |
| 30 | + const [isPending, setIsPending] = useState(false); |
| 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 | + // Candidates leave the list as soon as their servers report them queued, so |
| 48 | + // keep the button while the run is in flight. |
| 49 | + if (machines.length === 0 && !isPending) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + const handleUpdate = async () => { |
| 54 | + if (pending.current) return; |
| 55 | + pending.current = true; |
| 56 | + setIsPending(true); |
| 57 | + try { |
| 58 | + const runs = await Promise.all( |
| 59 | + machines.flatMap(({ environmentId, label, candidates }) => |
| 60 | + candidates.map(async (candidate): Promise<ProviderUpdateRun> => ({ |
| 61 | + machineLabel: label, |
| 62 | + driver: candidate.driver, |
| 63 | + instanceId: candidate.instanceId, |
| 64 | + result: await updateProvider({ |
| 65 | + environmentId, |
| 66 | + input: { provider: candidate.driver, instanceId: candidate.instanceId }, |
| 67 | + }), |
| 68 | + })), |
| 69 | + ), |
| 70 | + ); |
| 71 | + const view = getProviderUpdateRunToastView(runs); |
| 72 | + if (view) { |
| 73 | + toastManager.add( |
| 74 | + stackedThreadToast({ |
| 75 | + ...view, |
| 76 | + description: <span className="whitespace-pre-line">{view.description}</span>, |
| 77 | + }), |
| 78 | + ); |
| 79 | + } |
| 80 | + } finally { |
| 81 | + pending.current = false; |
| 82 | + setIsPending(false); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + return ( |
| 87 | + <Tooltip> |
| 88 | + <TooltipTrigger |
| 89 | + render={ |
| 90 | + <Button |
| 91 | + size="xs" |
| 92 | + variant="ghost-muted" |
| 93 | + disabled={isPending} |
| 94 | + onClick={() => void handleUpdate()} |
| 95 | + > |
| 96 | + {isPending ? "Updating…" : "Update all"} |
| 97 | + </Button> |
| 98 | + } |
| 99 | + /> |
| 100 | + <TooltipPopup side="top"> |
| 101 | + {machines.map((machine) => ( |
| 102 | + <div key={machine.environmentId}> |
| 103 | + {machine.label}:{" "} |
| 104 | + {machine.candidates |
| 105 | + .map((candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver) |
| 106 | + .join(", ")} |
| 107 | + </div> |
| 108 | + ))} |
| 109 | + </TooltipPopup> |
| 110 | + </Tooltip> |
| 111 | + ); |
| 112 | +} |
0 commit comments