Open History to see the last commit
1import { useNavigate } from "@tanstack/react-router";
2import { DownloadIcon } from "lucide-react";
3import { useEffect, useRef, useState } from "react";
4
5import { useStableCallback } from "~/hooks/useStableCallback";
6import { useEnvironments } from "~/state/environments";
7import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
8import { useDismissedProviderUpdateNotificationKeys } from "../providerUpdateDismissal";
9import { ProviderUpdateEnvironmentRows } from "./ProviderUpdateEnvironmentRows";
10import { useLocalEnvironmentUpdateGroups } from "./ProviderUpdateLaunchNotification.environments";
11import {
12 collectProviderUpdateCandidates,
13 environmentGroupsWithUpdates,
14 getProviderUpdateInitialToastView,
15 localEnvironmentUpdateNotificationKey,
16} from "./ProviderUpdateLaunchNotification.logic";
17import { ProviderUpdatePrimaryNotification } from "./ProviderUpdatePrimaryNotification";
18import { stackedThreadToast, toastManager } from "./ui/toast";
19
20function useHasLocalSecondaryEnvironment(): boolean {
21 const { environments } = useEnvironments();
22 return environments.some((environment) =>
23 isDesktopLocalConnectionTarget(environment.entry.target),
24 );
25}
26
27export function ProviderUpdateLaunchNotification() {
28 const hasLocalSecondary = useHasLocalSecondaryEnvironment();
29
30 return hasLocalSecondary ? (
31 <ProviderUpdateEnvironmentsNotification />
32 ) : (
33 <ProviderUpdatePrimaryNotification />
34 );
35}
36
37const seenProviderUpdateNotificationKeys = new Set<string>();
38type ProviderUpdateToastId = ReturnType<typeof toastManager.add>;
39
40// While a local backend (e.g. WSL) is still connecting, defer the popover so it
41// reflects every environment. Cap the wait so a stuck or failed backend can't
42// suppress the primary's updates indefinitely.
43const SETTLING_GRACE_MS = 30_000;
44
45function ProviderUpdateEnvironmentsNotification() {
46 const navigate = useNavigate();
47 const { groups, isAnySettling } = useLocalEnvironmentUpdateGroups();
48 const { dismissedNotificationKeys, dismissNotificationKey } =
49 useDismissedProviderUpdateNotificationKeys();
50
51 const activeToastRef = useRef<{
52 readonly toastId: ProviderUpdateToastId;
53 readonly key: string;
54 } | null>(null);
55 // Whether the user has triggered an update from the current toast. Until they
56 // do, the prompt is replaced when the available updates change; afterward it
57 // is kept so in-progress rows are not torn down.
58 const hasInteractedRef = useRef(false);
59
60 useEffect(() => {
61 return () => {
62 if (activeToastRef.current !== null) {
63 toastManager.close(activeToastRef.current.toastId);
64 activeToastRef.current = null;
65 }
66 };
67 }, []);
68
69 const updateGroups = environmentGroupsWithUpdates(groups);
70 const notificationKey = localEnvironmentUpdateNotificationKey(groups);
71 const candidateUnion = collectProviderUpdateCandidates(
72 updateGroups.flatMap((group) => group.candidates),
73 );
74
75 const [settleGraceElapsed, setSettleGraceElapsed] = useState(false);
76 // The grace period restarts with the next settling phase.
77 if (!isAnySettling && settleGraceElapsed) {
78 setSettleGraceElapsed(false);
79 }
80 useEffect(() => {
81 if (!isAnySettling) return;
82 const timer = setTimeout(() => setSettleGraceElapsed(true), SETTLING_GRACE_MS);
83 return () => clearTimeout(timer);
84 }, [isAnySettling]);
85 const isGated = isAnySettling && !settleGraceElapsed;
86
87 const openProviderSettings = () => {
88 const active = activeToastRef.current;
89 if (active !== null) {
90 toastManager.close(active.toastId);
91 activeToastRef.current = null;
92 }
93 void navigate({ to: "/settings/providers" });
94 };
95
96 // The toast closes long after the effect that opened it, so it dismisses the key
97 // the updates have moved to by then.
98 const dismissPrompt = useStableCallback(() => {
99 if (notificationKey) {
100 dismissNotificationKey(notificationKey);
101 }
102 activeToastRef.current = null;
103 });
104
105 useEffect(() => {
106 const canShowPrompt =
107 notificationKey !== null &&
108 !isGated &&
109 !dismissedNotificationKeys.has(notificationKey) &&
110 !seenProviderUpdateNotificationKeys.has(notificationKey);
111
112 const active = activeToastRef.current;
113 if (
114 active &&
115 active.key !== notificationKey &&
116 !hasInteractedRef.current &&
117 (notificationKey === null || !isGated)
118 ) {
119 toastManager.close(active.toastId);
120 activeToastRef.current = null;
121 }
122
123 if (!notificationKey || !canShowPrompt || activeToastRef.current !== null) {
124 return;
125 }
126
127 seenProviderUpdateNotificationKeys.add(notificationKey);
128 hasInteractedRef.current = false;
129
130 const toastId = toastManager.add(
131 stackedThreadToast({
132 type: "warning",
133 title: getProviderUpdateInitialToastView({
134 updateProviders: candidateUnion,
135 oneClickProviders: candidateUnion,
136 }).title,
137 description: (
138 <ProviderUpdateEnvironmentRows
139 onInteract={() => {
140 hasInteractedRef.current = true;
141 }}
142 />
143 ),
144 timeout: 0,
145 actionProps: {
146 children: "Settings",
147 onClick: openProviderSettings,
148 },
149 actionVariant: "outline",
150 data: {
151 hideCopyButton: true,
152 leadingIcon: <DownloadIcon aria-hidden="true" className="size-4 text-success" />,
153 onClose: dismissPrompt,
154 },
155 }),
156 );
157 activeToastRef.current = { toastId, key: notificationKey };
158 }, [
159 notificationKey,
160 isGated,
161 candidateUnion,
162 dismissedNotificationKeys,
163 dismissPrompt,
164 openProviderSettings,
165 ]);
166
167 return null;
168}