Files
t3code/apps/web/src/components/ProviderUpdatePrimaryNotification.tsx
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

313 lines
10 KiB
TypeScript

import { useNavigate } from "@tanstack/react-router";
import { useAtomValue } from "@effect/atom-react";
import { DownloadIcon } from "lucide-react";
import { useCallback, useEffect, useMemo, useRef } from "react";
import { type ServerProvider, type ProviderInstanceId } from "@t3tools/contracts";
import { primaryServerProvidersAtom, serverEnvironment } from "../state/server";
import { usePrimaryEnvironment } from "../state/environments";
import { useDismissedProviderUpdateNotificationKeys } from "../providerUpdateDismissal";
import { ProviderInstanceIcon } from "./chat/ProviderInstanceIcon";
import {
canOneClickUpdateProviderCandidate,
collectProviderUpdateCandidates,
collectUpdatedProviderSnapshots,
firstFailedProviderUpdateMessage,
getProviderUpdateInitialToastView,
getProviderUpdateProgressToastView,
getProviderUpdateRejectedToastView,
providerUpdateNotificationKey,
shouldShowPrimaryProviderUpdateToast,
type ProviderUpdateToastView,
} from "./ProviderUpdateLaunchNotification.logic";
import { hiddenToastActionProps, stackedThreadToast, toastManager } from "./ui/toast";
import { useAtomCommand } from "../state/use-atom-command";
const seenProviderUpdateNotificationKeys = new Set<string>();
type ProviderUpdateToastId = ReturnType<typeof toastManager.add>;
type ActiveProviderUpdateToast =
| { readonly kind: "prompt"; readonly key: string; readonly toastId: ProviderUpdateToastId }
| {
readonly kind: "update";
readonly key: string;
readonly providerInstanceIds: ReadonlySet<ProviderInstanceId>;
readonly providerCount: number;
};
function ProviderUpdateToastIcon({ provider }: { provider: ServerProvider }) {
return (
<span className="relative inline-flex size-4 shrink-0 items-center justify-center">
<ProviderInstanceIcon
driverKind={provider.driver}
displayName={provider.displayName ?? provider.driver}
acpRegistryIconUrl={provider.iconUrl}
iconClassName="size-4"
/>
<span className="absolute -right-1 -bottom-1 inline-flex size-3 items-center justify-center rounded-full bg-popover">
<DownloadIcon aria-hidden="true" className="size-2.5 text-success" strokeWidth={2.5} />
</span>
</span>
);
}
function addProviderUpdateToast(input: {
readonly view: ProviderUpdateToastView;
readonly openSettings: (toastId: ProviderUpdateToastId) => void;
}) {
if (input.view.type === "loading" || input.view.type === "success") {
return toastManager.add({
type: input.view.type,
title: input.view.title,
description: input.view.description,
timeout: 0,
actionProps: hiddenToastActionProps,
data: {
hideCopyButton: true,
...(input.view.dismissAfterVisibleMs !== undefined
? { dismissAfterVisibleMs: input.view.dismissAfterVisibleMs }
: {}),
},
});
}
let toastId!: ProviderUpdateToastId;
toastId = toastManager.add(
stackedThreadToast({
type: input.view.type,
title: input.view.title,
description: input.view.description,
timeout: 0,
actionProps: {
children: "Settings",
onClick: () => input.openSettings(toastId),
},
actionVariant: "outline",
data: {
hideCopyButton: true,
},
}),
);
return toastId;
}
/**
* The single-prompt provider update notification used when there is only one
* local environment (no WSL backend). Non-WSL users see exactly this flow — the
* per-environment split is gated behind WSL presence.
*/
export function ProviderUpdatePrimaryNotification() {
const navigate = useNavigate();
const providers = useAtomValue(primaryServerProvidersAtom);
const primaryEnvironment = usePrimaryEnvironment();
const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
reportFailure: false,
});
const activeToastRef = useRef<ActiveProviderUpdateToast | null>(null);
const { dismissedNotificationKeys, dismissNotificationKey } =
useDismissedProviderUpdateNotificationKeys();
// If this flow unmounts (e.g. a WSL backend appears and we switch to the
// per-environment popover), close any prompt it owns so it does not linger.
useEffect(() => {
return () => {
const activeToast = activeToastRef.current;
if (activeToast?.kind === "prompt") {
toastManager.close(activeToast.toastId);
}
activeToastRef.current = null;
};
}, []);
const updateProviders = useMemo(() => collectProviderUpdateCandidates(providers), [providers]);
const notificationKey = useMemo(
() => providerUpdateNotificationKey(updateProviders),
[updateProviders],
);
const oneClickProviders = useMemo(
() =>
updateProviders.filter((provider) => canOneClickUpdateProviderCandidate(provider, providers)),
[providers, updateProviders],
);
const openProviderSettings = useCallback(
(toastId?: ProviderUpdateToastId) => {
const activeToast = activeToastRef.current;
if (toastId !== undefined) {
toastManager.close(toastId);
} else if (activeToast?.kind === "prompt") {
toastManager.close(activeToast.toastId);
}
if (
activeToast &&
(toastId === undefined ||
(activeToast.kind === "prompt" && activeToast.toastId === toastId))
) {
activeToastRef.current = null;
}
void navigate({ to: "/settings/providers" });
},
[navigate],
);
useEffect(() => {
const activeToast = activeToastRef.current;
if (activeToast?.kind !== "update") {
return;
}
const activeProviders = providers.filter((provider) =>
activeToast.providerInstanceIds.has(provider.instanceId),
);
const view = getProviderUpdateProgressToastView({
providers: activeProviders,
providerCount: activeToast.providerCount,
});
if (!shouldShowPrimaryProviderUpdateToast(view)) {
return;
}
addProviderUpdateToast({ view, openSettings: openProviderSettings });
activeToastRef.current = null;
}, [providers, openProviderSettings]);
useEffect(() => {
const activeToast = activeToastRef.current;
if (activeToast?.kind === "prompt" && activeToast.key !== notificationKey) {
toastManager.close(activeToast.toastId);
activeToastRef.current = null;
}
if (
!notificationKey ||
dismissedNotificationKeys.has(notificationKey) ||
seenProviderUpdateNotificationKeys.has(notificationKey) ||
activeToastRef.current
) {
return;
}
seenProviderUpdateNotificationKeys.add(notificationKey);
const initialView = getProviderUpdateInitialToastView({ updateProviders, oneClickProviders });
let toastId!: ProviderUpdateToastId;
let updateStarted = false;
const openSettings = () => openProviderSettings(toastId);
const dismissPrompt = () => {
dismissNotificationKey(notificationKey);
};
const runUpdates = () => {
if (updateStarted || oneClickProviders.length === 0 || !primaryEnvironment) {
return;
}
updateStarted = true;
const providerCount = oneClickProviders.length;
const providerInstanceIds = new Set(oneClickProviders.map((provider) => provider.instanceId));
const activeUpdate: ActiveProviderUpdateToast = {
kind: "update",
key: notificationKey,
providerInstanceIds,
providerCount,
};
activeToastRef.current = activeUpdate;
toastManager.close(toastId);
void (async () => {
const results = [];
for (const provider of oneClickProviders) {
results.push(
await updateProvider({
environmentId: primaryEnvironment.environmentId,
input: {
provider: provider.driver,
instanceId: provider.instanceId,
},
}),
);
}
const activeUpdateToast = activeToastRef.current;
if (activeUpdateToast !== activeUpdate) {
return;
}
const failedMessage = firstFailedProviderUpdateMessage(results);
if (failedMessage) {
addProviderUpdateToast({
view: getProviderUpdateRejectedToastView(providerCount, failedMessage),
openSettings: openProviderSettings,
});
activeToastRef.current = null;
return;
}
const updatedProviderSnapshots = collectUpdatedProviderSnapshots({
results,
providerInstanceIds,
});
const view = getProviderUpdateProgressToastView({
providers: updatedProviderSnapshots,
providerCount,
});
if (shouldShowPrimaryProviderUpdateToast(view)) {
addProviderUpdateToast({ view, openSettings: openProviderSettings });
activeToastRef.current = null;
}
})();
};
toastId = toastManager.add(
stackedThreadToast({
type: initialView.type,
title: initialView.title,
description: initialView.description,
timeout: 0,
actionProps:
oneClickProviders.length > 0
? {
children: "Update",
onClick: runUpdates,
}
: {
children: "Settings",
onClick: openSettings,
},
actionVariant: "outline",
data: {
leadingIcon:
updateProviders.length === 1 ? (
<ProviderUpdateToastIcon provider={updateProviders[0]!} />
) : undefined,
hideCopyButton: true,
onClose: dismissPrompt,
...(oneClickProviders.length > 0
? {
secondaryActionProps: {
children: "Settings",
onClick: openSettings,
},
secondaryActionVariant: "outline" as const,
}
: {}),
},
}),
);
activeToastRef.current = { kind: "prompt", key: notificationKey, toastId };
}, [
updateProvider,
dismissNotificationKey,
dismissedNotificationKeys,
notificationKey,
oneClickProviders,
openProviderSettings,
primaryEnvironment,
updateProviders,
]);
return null;
}