The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { Spinner } from "~/components/ui/spinner"; |
| 2 | import { useNavigate } from "@tanstack/react-router"; |
| 3 | import { useAtomValue } from "@effect/atom-react"; |
| 4 | import type { ServerProvider } from "@t3tools/contracts"; |
| 5 | import { CircleCheckIcon, DownloadIcon, TriangleAlertIcon, XIcon } from "lucide-react"; |
| 6 | import { T, useGT, useMessages } from "gt-react"; |
| 7 | import { useEffect, useEffectEvent, useState, type CSSProperties } from "react"; |
| 8 | |
| 9 | import { primaryServerProvidersAtom } from "../../state/server"; |
| 10 | import { |
| 11 | getProviderUpdateSidebarPillView, |
| 12 | type ProviderUpdateSidebarPillView, |
| 13 | } from "../ProviderUpdateLaunchNotification.logic"; |
| 14 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 15 | |
| 16 | // Matches the pill's `duration-180` class. |
| 17 | const PROVIDER_UPDATE_PILL_EXIT_MS = 180; |
| 18 | |
| 19 | const 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 | |
| 30 | const PROVIDER_UPDATE_PILL_PROGRESS_STYLES = { |
| 31 | success: "bg-foreground/8", |
| 32 | warning: "bg-warning/14", |
| 33 | error: "bg-destructive/14", |
| 34 | } as const; |
| 35 | |
| 36 | function 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 | ); |
| 44 | } |
| 45 | |
| 46 | export 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 [dismissAfterExitKeys, setDismissAfterExitKeys] = useState<ReadonlyArray<string>>([]); |
| 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 | dismissKeys: ReadonlyArray<string> = [], |
| 88 | ) => { |
| 89 | if (exitingKey === key) { |
| 90 | return; |
| 91 | } |
| 92 | setPendingView(nextView); |
| 93 | setExitingKey(key); |
| 94 | setDismissAfterExitKeys(dismissKeys); |
| 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) => |
| 110 | startExit(key, null, displayedView?.dismissKeys), |
| 111 | ); |
| 112 | |
| 113 | const finishExit = useEffectEvent(() => { |
| 114 | if (dismissAfterExitKeys.length > 0) { |
| 115 | setDismissedKeys((previous) => new Set([...previous, ...dismissAfterExitKeys])); |
| 116 | } |
| 117 | setRenderedView(pendingView); |
| 118 | setPendingView(null); |
| 119 | setExitingKey(null); |
| 120 | setDismissAfterExitKeys([]); |
| 121 | }); |
| 122 | |
| 123 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- fallback timer that finishes the exit if the CSS transition never ends |
| 124 | useEffect(() => { |
| 125 | if (exitingKey === null) return; |
| 126 | // A cancelled or skipped CSS transition must not keep a hidden spinner mounted. |
| 127 | const timeoutId = window.setTimeout(finishExit, PROVIDER_UPDATE_PILL_EXIT_MS); |
| 128 | return () => window.clearTimeout(timeoutId); |
| 129 | }, [exitingKey]); |
| 130 | |
| 131 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- timer that dismisses the pill after it has been visible for dismissAfterVisibleMs |
| 132 | useEffect(() => { |
| 133 | if (!dismissAfterVisibleMs || !viewKey) { |
| 134 | return; |
| 135 | } |
| 136 | if (exitingKey === viewKey) { |
| 137 | return; |
| 138 | } |
| 139 | const timeoutId = window.setTimeout(() => { |
| 140 | exitAfterVisible(viewKey); |
| 141 | }, dismissAfterVisibleMs); |
| 142 | |
| 143 | return () => window.clearTimeout(timeoutId); |
| 144 | }, [dismissAfterVisibleMs, exitingKey, viewKey]); |
| 145 | |
| 146 | if (!displayedView) { |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | return ( |
| 151 | <div |
| 152 | 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 ${ |
| 153 | PROVIDER_UPDATE_PILL_STYLES[displayedView.tone] |
| 154 | } ${ |
| 155 | exitingKey === displayedView.key |
| 156 | ? "pointer-events-none translate-y-1.5 opacity-0" |
| 157 | : "translate-y-0 opacity-100" |
| 158 | }`} |
| 159 | > |
| 160 | {showDismissProgress ? ( |
| 161 | <div |
| 162 | key={displayedView.key} |
| 163 | aria-hidden="true" |
| 164 | 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 ${ |
| 165 | PROVIDER_UPDATE_PILL_PROGRESS_STYLES[displayedView.tone] |
| 166 | }`} |
| 167 | style={ |
| 168 | { |
| 169 | "--provider-update-pill-dismiss-ms": `${dismissAfterVisibleMs}ms`, |
| 170 | } as CSSProperties |
| 171 | } |
| 172 | /> |
| 173 | ) : null} |
| 174 | <div className="pointer-events-none absolute inset-0 rounded-lg transition-colors" /> |
| 175 | <Tooltip> |
| 176 | <TooltipTrigger |
| 177 | render={ |
| 178 | <button |
| 179 | type="button" |
| 180 | aria-label={displayedView.description} |
| 181 | data-provider-update-main |
| 182 | className="relative z-[1] flex min-w-0 flex-1 items-center gap-2 px-2 py-1.5 text-left" |
| 183 | onClick={openProviderSettings} |
| 184 | > |
| 185 | {displayedView.tone === "loading" ? ( |
| 186 | <Spinner size="sm" className="shrink-0" /> |
| 187 | ) : displayedView.tone === "success" ? ( |
| 188 | <CircleCheckIcon className="size-3.5 shrink-0" /> |
| 189 | ) : displayedView.tone === "error" ? ( |
| 190 | <TriangleAlertIcon className="size-3.5 shrink-0" /> |
| 191 | ) : ( |
| 192 | <DownloadIcon className="size-3.5 shrink-0" /> |
| 193 | )} |
| 194 | <span className="min-w-0 wrap-break-word">{displayedView.title}</span> |
| 195 | </button> |
| 196 | } |
| 197 | /> |
| 198 | <TooltipPopup side="top">{displayedView.description}</TooltipPopup> |
| 199 | </Tooltip> |
| 200 | {displayedView.dismissible && ( |
| 201 | <Tooltip> |
| 202 | <TooltipTrigger |
| 203 | render={ |
| 204 | <button |
| 205 | type="button" |
| 206 | aria-label={gt("Dismiss provider update notice")} |
| 207 | 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" |
| 208 | onClick={() => startExit(displayedView.key, null, displayedView.dismissKeys)} |
| 209 | > |
| 210 | <XIcon className="size-3.5 shrink-0" /> |
| 211 | </button> |
| 212 | } |
| 213 | /> |
| 214 | <TooltipPopup side="top"> |
| 215 | <T>Dismiss until provider status changes</T> |
| 216 | </TooltipPopup> |
| 217 | </Tooltip> |
| 218 | )} |
| 219 | </div> |
| 220 | ); |
| 221 | } |