diff --git a/apps/web/src/components/ProviderUpdateEnvironmentRows.tsx b/apps/web/src/components/ProviderUpdateEnvironmentRows.tsx new file mode 100644 index 0000000000..90aba1abda --- /dev/null +++ b/apps/web/src/components/ProviderUpdateEnvironmentRows.tsx @@ -0,0 +1,281 @@ +import { CheckIcon } from "lucide-react"; +import { type ReactNode, useCallback, useMemo, useRef, useState } from "react"; +import { PROVIDER_DISPLAY_NAMES, type EnvironmentId } from "@t3tools/contracts"; + +import { cn } from "~/lib/utils"; +import { updateProvidersInEnvironment } from "../environmentApi"; +import { useLocalEnvironmentUpdateGroups } from "./ProviderUpdateLaunchNotification.environments"; +import { + collectProviderUpdateOutcomeSnapshots, + firstRejectedProviderUpdateMessage, + getProviderUpdateProgressToastView, + getProviderUpdateSidebarPillView, + type LocalEnvironmentUpdateGroup, + type ProviderUpdateSidebarPillView, + type ProviderUpdateToastView, +} from "./ProviderUpdateLaunchNotification.logic"; +import { Button } from "./ui/button"; +import { Spinner } from "./ui/spinner"; + +// If neither the dispatch result nor server state ever reports the update (e.g. +// the request never reached the backend), stop the spinner after this long so +// the row reverts to its Update button instead of spinning forever. +const PENDING_EXPIRY_MS = 20_000; + +type RowStatusKind = "idle" | "loading" | "success" | "failed" | "unchanged"; + +interface RowStatus { + readonly kind: RowStatusKind; + readonly text: string; +} + +function providerNamesFor(group: LocalEnvironmentUpdateGroup): string { + return group.candidates + .map((candidate) => PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver) + .join(", "); +} + +/** + * Resolve a row's display from every available signal, in priority order: + * a transport rejection, then the dispatch's own result payload (reliable even + * when a secondary backend's config does not re-sync), then live server state + * (reliable even when the dispatch RPC is lost to a reconnect), then the + * optimistic pending spinner, then the idle "ready to update" state. + */ +function resolveRowStatus(input: { + readonly group: LocalEnvironmentUpdateGroup; + readonly error: string | undefined; + readonly result: ProviderUpdateToastView | undefined; + readonly pill: ProviderUpdateSidebarPillView | null; + readonly isPending: boolean; +}): RowStatus { + if (input.error) { + return { kind: "failed", text: input.error }; + } + if (input.result) { + switch (input.result.phase) { + case "succeeded": + return { kind: "success", text: "Updated" }; + case "failed": + return { kind: "failed", text: input.result.description }; + case "unchanged": + return { kind: "unchanged", text: input.result.description }; + default: + return { kind: "loading", text: "Updating…" }; + } + } + if (input.pill) { + switch (input.pill.tone) { + case "success": + return { kind: "success", text: "Updated" }; + case "error": + return { kind: "failed", text: input.pill.description }; + case "warning": + return { kind: "unchanged", text: input.pill.description }; + default: + return { kind: "loading", text: "Updating…" }; + } + } + if (input.isPending) { + return { kind: "loading", text: "Updating…" }; + } + return { kind: "idle", text: providerNamesFor(input.group) }; +} + +function rowToneClass(kind: RowStatusKind): string { + switch (kind) { + case "failed": + return "text-destructive"; + case "unchanged": + return "text-warning"; + case "success": + return "text-success"; + default: + return "text-muted-foreground"; + } +} + +function EnvironmentUpdateRow({ + group, + status, + onUpdate, +}: { + readonly group: LocalEnvironmentUpdateGroup; + readonly status: RowStatus; + readonly onUpdate: () => void; +}) { + let trailing: ReactNode; + switch (status.kind) { + case "loading": + trailing = ; + break; + case "success": + trailing =