Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderUpdatePrimaryNotification.tsx

312 lines10.0 KB

1import { useNavigate } from "@tanstack/react-router";
2import { useAtomValue } from "@effect/atom-react";
3import { DownloadIcon } from "lucide-react";
4import { useCallback, useEffect, useMemo, useRef } from "react";
5import { type ServerProvider, type ProviderInstanceId } from "@t3tools/contracts";
6​
7import { primaryServerProvidersAtom, serverEnvironment } from "../state/server";
8import { usePrimaryEnvironment } from "../state/environments";
9import { useDismissedProviderUpdateNotificationKeys } from "../providerUpdateDismissal";
10import { ProviderInstanceIcon } from "./chat/ProviderInstanceIcon";
11import {
12 canOneClickUpdateProviderCandidate,
13 collectProviderUpdateCandidates,
14 collectUpdatedProviderSnapshots,
15 firstFailedProviderUpdateMessage,
16 getProviderUpdateInitialToastView,
17 getProviderUpdateProgressToastView,
18 getProviderUpdateRejectedToastView,
19 providerUpdateNotificationKey,
20 shouldShowPrimaryProviderUpdateToast,
21 type ProviderUpdateToastView,
22} from "./ProviderUpdateLaunchNotification.logic";
23import { hiddenToastActionProps, stackedThreadToast, toastManager } from "./ui/toast";
24import { useAtomCommand } from "../state/use-atom-command";
25​
26const seenProviderUpdateNotificationKeys = new Set<string>();
27type ProviderUpdateToastId = ReturnType<typeof toastManager.add>;
28​
29type ActiveProviderUpdateToast =
30 | { readonly kind: "prompt"; readonly key: string; readonly toastId: ProviderUpdateToastId }
31 | {
32 readonly kind: "update";
33 readonly key: string;
34 readonly providerInstanceIds: ReadonlySet<ProviderInstanceId>;
35 readonly providerCount: number;
36 };
37​
38function ProviderUpdateToastIcon({ provider }: { provider: ServerProvider }) {
39 return (
40 <span className="relative inline-flex size-4 shrink-0 items-center justify-center">
41 <ProviderInstanceIcon
42 driverKind={provider.driver}
43 displayName={provider.displayName ?? provider.driver}
44 acpRegistryIconUrl={provider.iconUrl}
45 iconClassName="size-4"
46 />
47 <span className="absolute -right-1 -bottom-1 inline-flex size-3 items-center justify-center rounded-full bg-popover">
48 <DownloadIcon aria-hidden="true" className="size-2.5 text-success" strokeWidth={2.5} />
49 </span>
50 </span>
51 );
53​
54function addProviderUpdateToast(input: {
55 readonly view: ProviderUpdateToastView;
56 readonly openSettings: (toastId: ProviderUpdateToastId) => void;
57}) {
58 if (input.view.type === "loading" || input.view.type === "success") {
59 return toastManager.add({
60 type: input.view.type,
61 title: input.view.title,
62 description: input.view.description,
63 timeout: 0,
64 actionProps: hiddenToastActionProps,
65 data: {
66 hideCopyButton: true,
67 ...(input.view.dismissAfterVisibleMs !== undefined
68 ? { dismissAfterVisibleMs: input.view.dismissAfterVisibleMs }
69 : {}),
70 },
71 });
72 }
73​
74 let toastId!: ProviderUpdateToastId;
75 toastId = toastManager.add(
76 stackedThreadToast({
77 type: input.view.type,
78 title: input.view.title,
79 description: input.view.description,
80 timeout: 0,
81 actionProps: {
82 children: "Settings",
83 onClick: () => input.openSettings(toastId),
84 },
85 actionVariant: "outline",
86 data: {
87 hideCopyButton: true,
88 },
89 }),
90 );
91 return toastId;
93​
94/**
95 * The single-prompt provider update notification used when there is only one
96 * local environment (no WSL backend). Non-WSL users see exactly this flow — the
97 * per-environment split is gated behind WSL presence.
98 */
99export function ProviderUpdatePrimaryNotification() {
100 const navigate = useNavigate();
101 const providers = useAtomValue(primaryServerProvidersAtom);
102 const primaryEnvironment = usePrimaryEnvironment();
103 const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
104 reportFailure: false,
105 });
106 const activeToastRef = useRef<ActiveProviderUpdateToast | null>(null);
107 const { dismissedNotificationKeys, dismissNotificationKey } =
108 useDismissedProviderUpdateNotificationKeys();
109​
110 // If this flow unmounts (e.g. a WSL backend appears and we switch to the
111 // per-environment popover), close any prompt it owns so it does not linger.
112 useEffect(() => {
113 return () => {
114 const activeToast = activeToastRef.current;
115 if (activeToast?.kind === "prompt") {
116 toastManager.close(activeToast.toastId);
117 }
118 activeToastRef.current = null;
119 };
120 }, []);
121​
122 const updateProviders = useMemo(() => collectProviderUpdateCandidates(providers), [providers]);
123 const notificationKey = useMemo(
124 () => providerUpdateNotificationKey(updateProviders),
125 [updateProviders],
126 );
127 const oneClickProviders = useMemo(
128 () =>
129 updateProviders.filter((provider) => canOneClickUpdateProviderCandidate(provider, providers)),
130 [providers, updateProviders],
131 );
132​
133 const openProviderSettings = useCallback(
134 (toastId?: ProviderUpdateToastId) => {
135 const activeToast = activeToastRef.current;
136 if (toastId !== undefined) {
137 toastManager.close(toastId);
138 } else if (activeToast?.kind === "prompt") {
139 toastManager.close(activeToast.toastId);
140 }
141 if (
142 activeToast &&
143 (toastId === undefined ||
144 (activeToast.kind === "prompt" && activeToast.toastId === toastId))
145 ) {
146 activeToastRef.current = null;
147 }
148 void navigate({ to: "/settings/providers" });
149 },
150 [navigate],
151 );
152​
153 useEffect(() => {
154 const activeToast = activeToastRef.current;
155 if (activeToast?.kind !== "update") {
156 return;
157 }
158​
159 const activeProviders = providers.filter((provider) =>
160 activeToast.providerInstanceIds.has(provider.instanceId),
161 );
162 const view = getProviderUpdateProgressToastView({
163 providers: activeProviders,
164 providerCount: activeToast.providerCount,
165 });
166 if (!shouldShowPrimaryProviderUpdateToast(view)) {
167 return;
168 }
169​
170 addProviderUpdateToast({ view, openSettings: openProviderSettings });
171 activeToastRef.current = null;
172 }, [providers, openProviderSettings]);
173​
174 useEffect(() => {
175 const activeToast = activeToastRef.current;
176 if (activeToast?.kind === "prompt" && activeToast.key !== notificationKey) {
177 toastManager.close(activeToast.toastId);
178 activeToastRef.current = null;
179 }
180​
181 if (
182 !notificationKey ||
183 dismissedNotificationKeys.has(notificationKey) ||
184 seenProviderUpdateNotificationKeys.has(notificationKey) ||
185 activeToastRef.current
186 ) {
187 return;
188 }
189​
190 seenProviderUpdateNotificationKeys.add(notificationKey);
191​
192 const initialView = getProviderUpdateInitialToastView({ updateProviders, oneClickProviders });
193​
194 let toastId!: ProviderUpdateToastId;
195 let updateStarted = false;
196 const openSettings = () => openProviderSettings(toastId);
197 const dismissPrompt = () => {
198 dismissNotificationKey(notificationKey);
199 };
200​
201 const runUpdates = () => {
202 if (updateStarted || oneClickProviders.length === 0 || !primaryEnvironment) {
203 return;
204 }
205 updateStarted = true;
206​
207 const providerCount = oneClickProviders.length;
208 const providerInstanceIds = new Set(oneClickProviders.map((provider) => provider.instanceId));
209 const activeUpdate: ActiveProviderUpdateToast = {
210 kind: "update",
211 key: notificationKey,
212 providerInstanceIds,
213 providerCount,
214 };
215 activeToastRef.current = activeUpdate;
216​
217 toastManager.close(toastId);
218​
219 void (async () => {
220 const results = [];
221 for (const provider of oneClickProviders) {
222 results.push(
223 await updateProvider({
224 environmentId: primaryEnvironment.environmentId,
225 input: {
226 provider: provider.driver,
227 instanceId: provider.instanceId,
228 },
229 }),
230 );
231 }
232​
233 const activeUpdateToast = activeToastRef.current;
234 if (activeUpdateToast !== activeUpdate) {
235 return;
236 }
237​
238 const failedMessage = firstFailedProviderUpdateMessage(results);
239 if (failedMessage) {
240 addProviderUpdateToast({
241 view: getProviderUpdateRejectedToastView(providerCount, failedMessage),
242 openSettings: openProviderSettings,
243 });
244 activeToastRef.current = null;
245 return;
246 }
247​
248 const updatedProviderSnapshots = collectUpdatedProviderSnapshots({
249 results,
250 providerInstanceIds,
251 });
252 const view = getProviderUpdateProgressToastView({
253 providers: updatedProviderSnapshots,
254 providerCount,
255 });
256 if (shouldShowPrimaryProviderUpdateToast(view)) {
257 addProviderUpdateToast({ view, openSettings: openProviderSettings });
258 activeToastRef.current = null;
259 }
260 })();
261 };
262​
263 toastId = toastManager.add(
264 stackedThreadToast({
265 type: initialView.type,
266 title: initialView.title,
267 description: initialView.description,
268 timeout: 0,
269 actionProps:
270 oneClickProviders.length > 0
271 ? {
272 children: "Update",
273 onClick: runUpdates,
274 }
275 : {
276 children: "Settings",
277 onClick: openSettings,
278 },
279 actionVariant: "outline",
280 data: {
281 leadingIcon:
282 updateProviders.length === 1 ? (
283 <ProviderUpdateToastIcon provider={updateProviders[0]!} />
284 ) : undefined,
285 hideCopyButton: true,
286 onClose: dismissPrompt,
287 ...(oneClickProviders.length > 0
288 ? {
289 secondaryActionProps: {
290 children: "Settings",
291 onClick: openSettings,
292 },
293 secondaryActionVariant: "outline" as const,
294 }
295 : {}),
296 },
297 }),
298 );
299 activeToastRef.current = { kind: "prompt", key: notificationKey, toastId };
300 }, [
301 updateProvider,
302 dismissNotificationKey,
303 dismissedNotificationKeys,
304 notificationKey,
305 oneClickProviders,
306 openProviderSettings,
307 primaryEnvironment,
308 updateProviders,
309 ]);
310​
311 return null;