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

SidebarProviderUpdatePill.tsx

219 lines8.3 KB

1import { Spinner } from "~/components/ui/spinner";
2import { useNavigate } from "@tanstack/react-router";
3import { useAtomValue } from "@effect/atom-react";
4import type { ServerProvider } from "@t3tools/contracts";
5import { CircleCheckIcon, DownloadIcon, TriangleAlertIcon, XIcon } from "lucide-react";
6import { T, useGT, useMessages } from "gt-react";
7import { useEffect, useEffectEvent, useState, type CSSProperties } from "react";
8​
9import { primaryServerProvidersAtom } from "../../state/server";
10import {
11 getProviderUpdateSidebarPillView,
12 type ProviderUpdateSidebarPillView,
13} from "../ProviderUpdateLaunchNotification.logic";
14import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
15​
16// Matches the pill's `duration-180` class.
17const PROVIDER_UPDATE_PILL_EXIT_MS = 180;
18​
19const PROVIDER_UPDATE_PILL_STYLES = {
20 loading:
21 "bg-sidebar-control-surface text-sidebar-foreground group-has-[button[data-provider-update-main]:hover]/provider-update:bg-sidebar-row-hover",
22 success:
23 "bg-sidebar-control-surface text-sidebar-foreground group-has-[button[data-provider-update-main]:hover]/provider-update:bg-sidebar-row-hover",
24 warning:
25 "bg-warning/12 text-warning group-has-[button[data-provider-update-main]:hover]/provider-update:bg-warning/18",
26 error:
27 "bg-destructive/12 text-destructive group-has-[button[data-provider-update-main]:hover]/provider-update:bg-destructive/18",
28} as const;
29​
30const PROVIDER_UPDATE_PILL_PROGRESS_STYLES = {
31 success: "bg-foreground/8",
32 warning: "bg-warning/14",
33 error: "bg-destructive/14",
34} as const;
35​
36function latestProviderCheckedAt(
37 providers: ReadonlyArray<Pick<ServerProvider, "checkedAt">>,
38): string | undefined {
39 return providers.reduce<string | undefined>(
40 (latest, provider) =>
41 latest === undefined || provider.checkedAt > latest ? provider.checkedAt : latest,
42 undefined,
43 );
45​
46export function SidebarProviderUpdatePill() {
47 const gt = useGT();
48 const messages = useMessages();
49 const navigate = useNavigate();
50 const providers = useAtomValue(primaryServerProvidersAtom);
51 const [dismissedKeys, setDismissedKeys] = useState<ReadonlySet<string>>(() => new Set());
52 const [renderedView, setRenderedView] = useState<ProviderUpdateSidebarPillView | null>(null);
53 const [pendingView, setPendingView] = useState<ProviderUpdateSidebarPillView | null>(null);
54 const [exitingKey, setExitingKey] = useState<string | null>(null);
55 const [dismissAfterExitKey, setDismissAfterExitKey] = useState<string | null>(null);
56 const [visibleAfterIso, setVisibleAfterIso] = useState<string | undefined>();
57 const effectiveVisibleAfterIso = visibleAfterIso ?? latestProviderCheckedAt(providers);
58 const view = getProviderUpdateSidebarPillView(
59 providers,
60 {
61 ...(effectiveVisibleAfterIso !== undefined
62 ? { visibleAfterIso: effectiveVisibleAfterIso }
63 : {}),
64 dismissedKeys,
65 },
66 messages,
67 );
68​
69 if (visibleAfterIso === undefined && effectiveVisibleAfterIso !== undefined) {
70 setVisibleAfterIso(effectiveVisibleAfterIso);
71 }
72​
73 const openProviderSettings = () => {
74 void navigate({ to: "/settings/providers" });
75 };
76 const displayedView = renderedView?.key === view?.key ? view : (renderedView ?? view);
77 const dismissAfterVisibleMs = displayedView?.dismissAfterVisibleMs;
78 const viewKey = displayedView?.key ?? null;
79 const showDismissProgress =
80 dismissAfterVisibleMs !== undefined &&
81 displayedView?.tone !== "loading" &&
82 exitingKey !== viewKey;
83​
84 const startExit = (
85 key: string,
86 nextView: ProviderUpdateSidebarPillView | null,
87 dismissKey?: string,
88 ) => {
89 if (exitingKey === key) {
90 return;
91 }
92 setPendingView(nextView);
93 setExitingKey(key);
94 setDismissAfterExitKey(dismissKey ?? null);
95 };
96​
97 if (exitingKey === null) {
98 if (!renderedView) {
99 if (view) {
100 setRenderedView(view);
101 }
102 } else if (!view) {
103 startExit(renderedView.key, null);
104 } else if (view.key !== renderedView.key) {
105 startExit(renderedView.key, view);
106 }
107 }
108​
109 const exitAfterVisible = useEffectEvent((key: string) => startExit(key, null, key));
110​
111 const finishExit = useEffectEvent(() => {
112 if (dismissAfterExitKey !== null) {
113 setDismissedKeys((previous) => new Set(previous).add(dismissAfterExitKey));
114 }
115 setRenderedView(pendingView);
116 setPendingView(null);
117 setExitingKey(null);
118 setDismissAfterExitKey(null);
119 });
120​
121 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- fallback timer that finishes the exit if the CSS transition never ends
122 useEffect(() => {
123 if (exitingKey === null) return;
124 // A cancelled or skipped CSS transition must not keep a hidden spinner mounted.
125 const timeoutId = window.setTimeout(finishExit, PROVIDER_UPDATE_PILL_EXIT_MS);
126 return () => window.clearTimeout(timeoutId);
127 }, [exitingKey]);
128​
129 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- timer that dismisses the pill after it has been visible for dismissAfterVisibleMs
130 useEffect(() => {
131 if (!dismissAfterVisibleMs || !viewKey) {
132 return;
133 }
134 if (exitingKey === viewKey) {
135 return;
136 }
137 const timeoutId = window.setTimeout(() => {
138 exitAfterVisible(viewKey);
139 }, dismissAfterVisibleMs);
140​
141 return () => window.clearTimeout(timeoutId);
142 }, [dismissAfterVisibleMs, exitingKey, viewKey]);
143​
144 if (!displayedView) {
145 return null;
146 }
147​
148 return (
149 <div
150 className={`group/provider-update relative flex min-h-7 w-full shrink-0 items-center overflow-hidden rounded-lg text-2xs leading-4 font-medium transform-gpu transition-all duration-180 ease-drawer will-change-transform ${
151 PROVIDER_UPDATE_PILL_STYLES[displayedView.tone]
152 } ${
153 exitingKey === displayedView.key
154 ? "pointer-events-none translate-y-1.5 opacity-0"
155 : "translate-y-0 opacity-100"
156 }`}
157 >
158 {showDismissProgress ? (
159 <div
160 key={displayedView.key}
161 aria-hidden="true"
162 className={`pointer-events-none absolute inset-y-0 left-0 w-full origin-left scale-x-0 transition-transform duration-(--provider-update-pill-dismiss-ms) ease-linear starting:scale-x-100 border-r border-current/15 inset-shadow-2xs inset-shadow-white/8 ${
163 PROVIDER_UPDATE_PILL_PROGRESS_STYLES[displayedView.tone]
164 }`}
165 style={
166 {
167 "--provider-update-pill-dismiss-ms": `${dismissAfterVisibleMs}ms`,
168 } as CSSProperties
169 }
170 />
171 ) : null}
172 <div className="pointer-events-none absolute inset-0 rounded-lg transition-colors" />
173 <Tooltip>
174 <TooltipTrigger
175 render={
176 <button
177 type="button"
178 aria-label={displayedView.description}
179 data-provider-update-main
180 className="relative z-[1] flex min-w-0 flex-1 items-center gap-2 px-2 py-1.5 text-left"
181 onClick={openProviderSettings}
182 >
183 {displayedView.tone === "loading" ? (
184 <Spinner size="sm" className="shrink-0" />
185 ) : displayedView.tone === "success" ? (
186 <CircleCheckIcon className="size-3.5 shrink-0" />
187 ) : displayedView.tone === "error" ? (
188 <TriangleAlertIcon className="size-3.5 shrink-0" />
189 ) : (
190 <DownloadIcon className="size-3.5 shrink-0" />
191 )}
192 <span className="min-w-0 wrap-break-word">{displayedView.title}</span>
193 </button>
194 }
195 />
196 <TooltipPopup side="top">{displayedView.description}</TooltipPopup>
197 </Tooltip>
198 {displayedView.dismissible && (
199 <Tooltip>
200 <TooltipTrigger
201 render={
202 <button
203 type="button"
204 aria-label={gt("Dismiss provider update notice")}
205 className="relative z-[1] mr-1 flex size-5 shrink-0 cursor-pointer items-center justify-center rounded-md opacity-70 outline-none hover:opacity-100 focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
206 onClick={() => startExit(displayedView.key, null, displayedView.key)}
207 >
208 <XIcon className="size-3.5 shrink-0" />
209 </button>
210 }
211 />
212 <TooltipPopup side="top">
213 <T>Dismiss until provider status changes</T>
214 </TooltipPopup>
215 </Tooltip>
216 )}
217 </div>
218 );