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