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

ProviderUpdateLaunchNotification.tsx

300 lines9.3 KB

1import { useNavigate } from "@tanstack/react-router";
2import { DownloadIcon } from "lucide-react";
3import { useCallback, useEffect, useMemo, useRef } from "react";
4import { type ProviderDriverKind, type ProviderInstanceId } from "@t3tools/contracts";
5​
6import { ensureLocalApi } from "../localApi";
7import { useDismissedProviderUpdateNotificationKeys } from "../providerUpdateDismissal";
8import { useServerProviders } from "../rpc/serverState";
9import { PROVIDER_ICON_BY_PROVIDER } from "./chat/providerIconUtils";
10import {
11 canOneClickUpdateProviderCandidate,
12 collectProviderUpdateCandidates,
13 collectUpdatedProviderSnapshots,
14 firstRejectedProviderUpdateMessage,
15 getProviderUpdateInitialToastView,
16 getProviderUpdateProgressToastView,
17 getProviderUpdateRejectedToastView,
18 getProviderUpdateRunningToastView,
19 providerUpdateNotificationKey,
20 type ProviderUpdateToastView,
21} from "./ProviderUpdateLaunchNotification.logic";
22import { stackedThreadToast, toastManager } from "./ui/toast";
23​
24const seenProviderUpdateNotificationKeys = new Set<string>();
25type ProviderUpdateToastId = ReturnType<typeof toastManager.add>;
26​
27type ActiveProviderUpdateToast =
28 | { readonly kind: "prompt"; readonly key: string; readonly toastId: ProviderUpdateToastId }
29 | {
30 readonly kind: "update";
31 readonly key: string;
32 readonly toastId: ProviderUpdateToastId;
33 readonly providerInstanceIds: ReadonlySet<ProviderInstanceId>;
34 readonly providerCount: number;
35 };
36​
37function ProviderUpdateToastIcon({ provider }: { provider: ProviderDriverKind }) {
38 const ProviderIcon = PROVIDER_ICON_BY_PROVIDER[provider];
39​
40 if (!ProviderIcon) {
41 return (
42 <span className="relative inline-flex size-4 shrink-0 items-center justify-center">
43 <DownloadIcon aria-hidden="true" className="size-4 text-success" strokeWidth={2.5} />
44 </span>
45 );
46 }
47​
48 return (
49 <span className="relative inline-flex size-4 shrink-0 items-center justify-center">
50 <ProviderIcon aria-hidden="true" className="size-4" />
51 <span className="absolute -right-1 -bottom-1 inline-flex size-3 items-center justify-center rounded-full bg-popover">
52 <DownloadIcon aria-hidden="true" className="size-2.5 text-success" strokeWidth={2.5} />
53 </span>
54 </span>
55 );
57​
58function updateProviderUpdateToast(input: {
59 readonly toastId: ProviderUpdateToastId;
60 readonly view: ProviderUpdateToastView;
61 readonly openSettings: () => void;
62}) {
63 if (input.view.type === "loading" || input.view.type === "success") {
64 toastManager.update(input.toastId, {
65 type: input.view.type,
66 title: input.view.title,
67 description: input.view.description,
68 timeout: 0,
69 data: {
70 hideCopyButton: true,
71 ...(input.view.dismissAfterVisibleMs !== undefined
72 ? { dismissAfterVisibleMs: input.view.dismissAfterVisibleMs }
73 : {}),
74 },
75 });
76 return;
77 }
78​
79 toastManager.update(
80 input.toastId,
81 stackedThreadToast({
82 type: input.view.type,
83 title: input.view.title,
84 description: input.view.description,
85 timeout: 0,
86 actionProps: {
87 children: "Settings",
88 onClick: input.openSettings,
89 },
90 actionVariant: "outline",
91 data: {
92 hideCopyButton: true,
93 },
94 }),
95 );
97​
98function isTerminalProviderUpdateToastView(view: ProviderUpdateToastView) {
99 return view.phase === "failed" || view.phase === "unchanged" || view.phase === "succeeded";
101​
102export function ProviderUpdateLaunchNotification() {
103 const navigate = useNavigate();
104 const providers = useServerProviders();
105 const activeToastRef = useRef<ActiveProviderUpdateToast | null>(null);
106 const { dismissedNotificationKeys, dismissNotificationKey } =
107 useDismissedProviderUpdateNotificationKeys();
108​
109 const updateProviders = useMemo(() => collectProviderUpdateCandidates(providers), [providers]);
110 const notificationKey = useMemo(
111 () => providerUpdateNotificationKey(updateProviders),
112 [updateProviders],
113 );
114 const oneClickProviders = useMemo(
115 () =>
116 updateProviders.filter((provider) => canOneClickUpdateProviderCandidate(provider, providers)),
117 [providers, updateProviders],
118 );
119​
120 const openProviderSettings = useCallback(
121 (toastId?: ProviderUpdateToastId) => {
122 const activeToast = activeToastRef.current;
123 if (toastId !== undefined) {
124 toastManager.close(toastId);
125 } else if (activeToast) {
126 toastManager.close(activeToast.toastId);
127 }
128 if (activeToast && (toastId === undefined || activeToast.toastId === toastId)) {
129 activeToastRef.current = null;
130 }
131 void navigate({ to: "/settings/providers" });
132 },
133 [navigate],
134 );
135​
136 useEffect(() => {
137 const activeToast = activeToastRef.current;
138 if (activeToast?.kind !== "update") {
139 return;
140 }
141​
142 const activeProviders = providers.filter((provider) =>
143 activeToast.providerInstanceIds.has(provider.instanceId),
144 );
145 const view = getProviderUpdateProgressToastView({
146 providers: activeProviders,
147 providerCount: activeToast.providerCount,
148 });
149 updateProviderUpdateToast({
150 toastId: activeToast.toastId,
151 view,
152 openSettings: () => openProviderSettings(activeToast.toastId),
153 });
154​
155 if (isTerminalProviderUpdateToastView(view)) {
156 activeToastRef.current = null;
157 }
158 }, [providers, openProviderSettings]);
159​
160 useEffect(() => {
161 const activeToast = activeToastRef.current;
162 if (activeToast?.kind === "prompt" && activeToast.key !== notificationKey) {
163 toastManager.close(activeToast.toastId);
164 activeToastRef.current = null;
165 }
166​
167 if (
168 !notificationKey ||
169 dismissedNotificationKeys.has(notificationKey) ||
170 seenProviderUpdateNotificationKeys.has(notificationKey) ||
171 activeToastRef.current
172 ) {
173 return;
174 }
175​
176 seenProviderUpdateNotificationKeys.add(notificationKey);
177​
178 const initialView = getProviderUpdateInitialToastView({ updateProviders, oneClickProviders });
179​
180 let toastId!: ProviderUpdateToastId;
181 let updateStarted = false;
182 const openSettings = () => openProviderSettings(toastId);
183 const dismissPrompt = () => {
184 dismissNotificationKey(notificationKey);
185 };
186​
187 const runUpdates = () => {
188 if (updateStarted || oneClickProviders.length === 0) {
189 return;
190 }
191 updateStarted = true;
192​
193 const providerCount = oneClickProviders.length;
194 const providerInstanceIds = new Set(oneClickProviders.map((provider) => provider.instanceId));
195 activeToastRef.current = {
196 kind: "update",
197 key: notificationKey,
198 toastId,
199 providerInstanceIds,
200 providerCount,
201 };
202​
203 updateProviderUpdateToast({
204 toastId,
205 view: getProviderUpdateRunningToastView(providerCount),
206 openSettings,
207 });
208​
209 void Promise.allSettled(
210 oneClickProviders.map(async (provider) =>
211 ensureLocalApi().server.updateProvider({
212 provider: provider.driver,
213 instanceId: provider.instanceId,
214 }),
215 ),
216 ).then((results) => {
217 const activeUpdateToast = activeToastRef.current;
218 if (activeUpdateToast?.kind !== "update" || activeUpdateToast.toastId !== toastId) {
219 return;
220 }
221​
222 const rejectedMessage = firstRejectedProviderUpdateMessage(results);
223 if (rejectedMessage) {
224 updateProviderUpdateToast({
225 toastId,
226 view: getProviderUpdateRejectedToastView(providerCount, rejectedMessage),
227 openSettings,
228 });
229 activeToastRef.current = null;
230 return;
231 }
232​
233 const updatedProviderSnapshots = collectUpdatedProviderSnapshots({
234 results,
235 providerInstanceIds,
236 });
237 const view = getProviderUpdateProgressToastView({
238 providers: updatedProviderSnapshots,
239 providerCount,
240 });
241 updateProviderUpdateToast({
242 toastId,
243 view,
244 openSettings,
245 });
246​
247 if (isTerminalProviderUpdateToastView(view)) {
248 activeToastRef.current = null;
249 }
250 });
251 };
252​
253 toastId = toastManager.add(
254 stackedThreadToast({
255 type: initialView.type,
256 title: initialView.title,
257 description: initialView.description,
258 timeout: 0,
259 actionProps:
260 oneClickProviders.length > 0
261 ? {
262 children: "Update",
263 onClick: runUpdates,
264 }
265 : {
266 children: "Settings",
267 onClick: openSettings,
268 },
269 actionVariant: oneClickProviders.length > 0 ? "default" : "outline",
270 data: {
271 leadingIcon:
272 updateProviders.length === 1 ? (
273 <ProviderUpdateToastIcon provider={updateProviders[0]!.driver} />
274 ) : undefined,
275 hideCopyButton: true,
276 onClose: dismissPrompt,
277 ...(oneClickProviders.length > 0
278 ? {
279 secondaryActionProps: {
280 children: "Settings",
281 onClick: openSettings,
282 },
283 secondaryActionVariant: "outline" as const,
284 }
285 : {}),
286 },
287 }),
288 );
289 activeToastRef.current = { kind: "prompt", key: notificationKey, toastId };
290 }, [
291 dismissNotificationKey,
292 dismissedNotificationKeys,
293 notificationKey,
294 oneClickProviders,
295 openProviderSettings,
296 updateProviders,
297 ]);
298​
299 return null;