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

ProviderUpdatePrimaryNotification.tsx

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