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 { |
| 2 | CHATGPT_USAGE_URL, |
| 3 | collectLimitAccounts, |
| 4 | collectLimitPools, |
| 5 | displayLimitWindows, |
| 6 | type LimitAccount, |
| 7 | type LimitPool, |
| 8 | type LimitPoolMember, |
| 9 | type LimitPoolWindow, |
| 10 | paceRemainingPercent, |
| 11 | remainingPercent, |
| 12 | type ResetCreditChoice, |
| 13 | } from "@t3tools/shared/usageLimits"; |
| 14 | import { T, useGT, useMessages } from "gt-react"; |
| 15 | import type { Popover as PopoverPrimitive } from "@base-ui/react/popover"; |
| 16 | import { normalizeProviderAccentColor } from "@t3tools/client-runtime/state/provider-instance-display"; |
| 17 | import { AlertTriangleIcon, ExternalLinkIcon, TicketIcon } from "lucide-react"; |
| 18 | import { |
| 19 | createContext, |
| 20 | Fragment, |
| 21 | type ReactNode, |
| 22 | type RefObject, |
| 23 | use, |
| 24 | useLayoutEffect, |
| 25 | useRef, |
| 26 | useState, |
| 27 | } from "react"; |
| 28 | |
| 29 | import { ensureLocalApi } from "../../localApi"; |
| 30 | import { usePrimarySettings } from "../../hooks/useSettings"; |
| 31 | import { cn } from "../../lib/utils"; |
| 32 | import { formatUpcomingTimestamp } from "../../timestampFormat"; |
| 33 | import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon"; |
| 34 | import { providerClients } from "../settings/providerDriverMeta"; |
| 35 | import { RedactedSensitiveText } from "../settings/RedactedSensitiveText"; |
| 36 | import { Button } from "../ui/button"; |
| 37 | import { OpenAI } from "../Icons"; |
| 38 | import { Alert, AlertTitle } from "../ui/alert"; |
| 39 | import { Popover, PopoverCreateHandle, PopoverPopup, PopoverTrigger } from "../ui/popover"; |
| 40 | import { PaceIcon, ResetCreditDialog, barColor, usePaceMark, useResetCredit } from "./UsageLimits"; |
| 41 | import { |
| 42 | collectExternalUsageLinks, |
| 43 | collectLimitNotices, |
| 44 | cursorUsageWindowDetails, |
| 45 | formatResetsIn, |
| 46 | resetCreditChoices, |
| 47 | } from "@t3tools/shared/usageLimitsCopy"; |
| 48 | |
| 49 | function accountInitials(email: string): string { |
| 50 | const [local = "", domain = ""] = email.split("@"); |
| 51 | return `${local[0] ?? ""}${domain[0] ?? ""}`.toUpperCase() || "?"; |
| 52 | } |
| 53 | |
| 54 | function accountHue(email: string): number { |
| 55 | let hash = 0; |
| 56 | for (let index = 0; index < email.length; index += 1) { |
| 57 | hash = (hash * 31 + email.charCodeAt(index)) | 0; |
| 58 | } |
| 59 | return Math.abs(hash) % 360; |
| 60 | } |
| 61 | |
| 62 | function AccountChip({ email }: { readonly email: string }) { |
| 63 | const hue = accountHue(email); |
| 64 | const gt = useGT(); |
| 65 | return ( |
| 66 | <span |
| 67 | role="img" |
| 68 | aria-label={gt("Account {initials}", { initials: accountInitials(email) })} |
| 69 | className="inline-flex size-4 shrink-0 items-center justify-center rounded-full text-3xs leading-none font-semibold" |
| 70 | style={{ backgroundColor: `oklch(0.85 0.08 ${hue})`, color: `oklch(0.35 0.1 ${hue})` }} |
| 71 | > |
| 72 | {accountInitials(email)} |
| 73 | </span> |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | function AccountAvatar({ |
| 78 | account, |
| 79 | className, |
| 80 | }: { |
| 81 | readonly account: LimitAccount; |
| 82 | readonly className?: string; |
| 83 | }) { |
| 84 | if (account.redeem) { |
| 85 | return ( |
| 86 | <ProviderInstanceIcon |
| 87 | driverKind={account.driver} |
| 88 | displayName={ |
| 89 | account.displayName ?? |
| 90 | providerClients.get(account.driver)?.label ?? |
| 91 | String(account.driver) |
| 92 | } |
| 93 | accentColor={account.accentColor} |
| 94 | showBadge={Boolean(account.displayName)} |
| 95 | indicatorBackground="var(--popover)" |
| 96 | className={cn("size-5", className)} |
| 97 | iconClassName="size-4 text-foreground/80" |
| 98 | /> |
| 99 | ); |
| 100 | } |
| 101 | return account.email ? <AccountChip email={account.email} /> : null; |
| 102 | } |
| 103 | |
| 104 | function AccountName({ |
| 105 | account, |
| 106 | className, |
| 107 | }: { |
| 108 | readonly account: LimitAccount; |
| 109 | readonly className?: string; |
| 110 | }) { |
| 111 | if (account.displayName) return <span className={className}>{account.displayName}</span>; |
| 112 | if (account.email) { |
| 113 | return ( |
| 114 | <span className={cn("inline-flex min-w-0 items-center", className)}> |
| 115 | <AccountChip email={account.email} /> |
| 116 | </span> |
| 117 | ); |
| 118 | } |
| 119 | return ( |
| 120 | <span className={className}> |
| 121 | {providerClients.get(account.driver)?.label ?? String(account.driver)} |
| 122 | </span> |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | function Row({ label, children }: { readonly label: string; readonly children: ReactNode }) { |
| 127 | return ( |
| 128 | <div className="grid grid-cols-[4.5rem_minmax(0,1fr)] gap-x-3"> |
| 129 | <span className="text-muted-foreground">{label}</span> |
| 130 | <span className="min-w-0 text-foreground tabular-nums">{children}</span> |
| 131 | </div> |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | function SegmentPopover({ |
| 136 | account, |
| 137 | window, |
| 138 | reset, |
| 139 | now, |
| 140 | redeem, |
| 141 | onRedeem, |
| 142 | }: { |
| 143 | readonly account: LimitAccount; |
| 144 | readonly window: LimitPoolMember["window"]; |
| 145 | readonly reset: LimitPoolWindow["resets"][number] | undefined; |
| 146 | readonly now: number; |
| 147 | readonly redeem: ReturnType<typeof useResetCredit> | null; |
| 148 | readonly onRedeem: (choice: ResetCreditChoice) => void; |
| 149 | }) { |
| 150 | const timestampFormat = usePrimarySettings((settings) => settings.timestampFormat); |
| 151 | const gt = useGT(); |
| 152 | const m = useMessages(); |
| 153 | const remaining = remainingPercent(window); |
| 154 | const timeLeft = paceRemainingPercent(window, now); |
| 155 | const resetsIn = formatResetsIn(window, now, m, "in"); |
| 156 | const where = |
| 157 | account.environments.length > 0 |
| 158 | ? account.environments.map((environment) => environment.label).join(", ") |
| 159 | : account.sourceLabel; |
| 160 | const choices = |
| 161 | redeem && account.redeem && account.limits.resetCredits?.availableCount |
| 162 | ? resetCreditChoices(account.limits.resetCredits, account.redeem.input, now, true, m) |
| 163 | : []; |
| 164 | return ( |
| 165 | <div className="flex w-72 max-w-[calc(100vw-3rem)] flex-col gap-2.5 text-xs"> |
| 166 | <div className="flex min-w-0 flex-col gap-0.5"> |
| 167 | <span className="flex items-center gap-2 text-sm font-medium text-foreground"> |
| 168 | <AccountAvatar account={account} /> |
| 169 | <span className="truncate"> |
| 170 | {account.displayName ?? providerClients.get(account.driver)?.label ?? account.driver} |
| 171 | </span> |
| 172 | </span> |
| 173 | {account.email ? ( |
| 174 | <RedactedSensitiveText |
| 175 | value={account.email} |
| 176 | ariaLabel={gt("Toggle account email visibility")} |
| 177 | revealTooltip={gt("Click to reveal email")} |
| 178 | hideTooltip={gt("Click to hide email")} |
| 179 | className="w-fit" |
| 180 | /> |
| 181 | ) : null} |
| 182 | </div> |
| 183 | <div className="flex flex-col gap-1 border-t border-border/60 pt-2.5"> |
| 184 | {account.plan ? ( |
| 185 | <Row label={gt("Plan", { $context: "subscription plan" })}>{account.plan}</Row> |
| 186 | ) : null} |
| 187 | {where ? ( |
| 188 | <Row |
| 189 | label={ |
| 190 | account.environments.length > 0 |
| 191 | ? gt("Signed in") |
| 192 | : gt("Via", { $context: "signed in through a source" }) |
| 193 | } |
| 194 | > |
| 195 | {where} |
| 196 | </Row> |
| 197 | ) : null} |
| 198 | </div> |
| 199 | <div className="flex flex-col gap-1 border-t border-border/60 pt-2.5"> |
| 200 | <Row label={gt("Left", { $context: "remaining usage" })}>{remaining}%</Row> |
| 201 | {window.resetsAt ? ( |
| 202 | <Row label={gt("Resets", { $context: "when the usage window resets" })}> |
| 203 | {formatUpcomingTimestamp(window.resetsAt, timestampFormat, now, undefined, m)} |
| 204 | {resetsIn ? ` · ${resetsIn}` : ""} |
| 205 | </Row> |
| 206 | ) : null} |
| 207 | {timeLeft !== null ? ( |
| 208 | <Row label={gt("Even pace")}>{gt("{timeLeft}% left by now", { timeLeft })}</Row> |
| 209 | ) : null} |
| 210 | {reset && reset.restoresPercent > 0 ? ( |
| 211 | <Row label={gt("Restores", { $context: "a reset credit restores part of the pool" })}> |
| 212 | {gt("+{percent}% of pool", { percent: reset.restoresPercent })} |
| 213 | </Row> |
| 214 | ) : null} |
| 215 | </div> |
| 216 | {choices.length > 0 && redeem ? ( |
| 217 | <div className="flex flex-col gap-1.5 border-t border-border/60 pt-2.5 text-muted-foreground"> |
| 218 | {choices.map((choice) => ( |
| 219 | <span key={choice.key} className="flex items-center gap-3"> |
| 220 | <span className="tabular-nums">{choice.summary}</span> |
| 221 | {choice.availableCount > 0 ? ( |
| 222 | <Button |
| 223 | size="xs" |
| 224 | variant="outline" |
| 225 | disabled={redeem.busy || !redeem.canManageProviders} |
| 226 | className="ms-auto" |
| 227 | onClick={() => onRedeem(choice)} |
| 228 | > |
| 229 | {redeem.busy ? gt("Using…") : choice.actionLabel} |
| 230 | </Button> |
| 231 | ) : null} |
| 232 | </span> |
| 233 | ))} |
| 234 | </div> |
| 235 | ) : null} |
| 236 | </div> |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Every segment on the page shares one popover, so hovering or clicking another |
| 242 | * segment moves it instead of opening a second one. The payload is the segment |
| 243 | * it currently describes. |
| 244 | */ |
| 245 | type SegmentPayload = Omit<Parameters<typeof SegmentPopover>[0], "now" | "onRedeem">; |
| 246 | const SegmentPopoverContext = createContext<ReturnType< |
| 247 | typeof PopoverCreateHandle<SegmentPayload> |
| 248 | > | null>(null); |
| 249 | |
| 250 | function ActiveSegmentPopover({ |
| 251 | handle, |
| 252 | actions, |
| 253 | payload, |
| 254 | pools, |
| 255 | now, |
| 256 | }: { |
| 257 | readonly handle: ReturnType<typeof PopoverCreateHandle<SegmentPayload>>; |
| 258 | readonly actions: RefObject<PopoverPrimitive.Root.Actions | null>; |
| 259 | readonly payload: SegmentPayload; |
| 260 | readonly pools: readonly LimitPool[]; |
| 261 | readonly now: number; |
| 262 | }) { |
| 263 | const available = pools.some((pool) => |
| 264 | displayLimitWindows(pool).some((window) => |
| 265 | window.members.some( |
| 266 | (member) => |
| 267 | member.account.key === payload.account.key && |
| 268 | member.window.id === payload.window.id && |
| 269 | member.window.kind === payload.window.kind && |
| 270 | Boolean(member.account.redeem) === Boolean(payload.redeem), |
| 271 | ), |
| 272 | ), |
| 273 | ); |
| 274 | // The trigger is gone, so the popup has nothing to anchor to: an exit transition would |
| 275 | // slide it to the viewport origin. Unmount it outright, which also clears Base UI's |
| 276 | // active-trigger bookkeeping so the segment can open again when it comes back. |
| 277 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- closes the popover handle and unmounts the Base UI popup before paint when its trigger is gone |
| 278 | useLayoutEffect(() => { |
| 279 | if (available) return; |
| 280 | handle.close(); |
| 281 | actions.current?.unmount(); |
| 282 | }, [available, handle, actions]); |
| 283 | return ( |
| 284 | <PopoverPopup side="top" sideOffset={6}> |
| 285 | <SegmentPopover |
| 286 | {...payload} |
| 287 | now={now} |
| 288 | onRedeem={(choice) => { |
| 289 | handle.close(); |
| 290 | payload.redeem?.confirm(choice); |
| 291 | }} |
| 292 | /> |
| 293 | </PopoverPopup> |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * The confirm is a sibling of the popover, not a child: dialogs stack under |
| 299 | * popovers, and the popover closes as the confirm opens. |
| 300 | */ |
| 301 | function PoolSegment({ |
| 302 | account, |
| 303 | ...props |
| 304 | }: { |
| 305 | readonly account: LimitAccount; |
| 306 | readonly window: LimitPoolMember["window"]; |
| 307 | readonly reset: LimitPoolWindow["resets"][number] | undefined; |
| 308 | readonly refillPercent: number; |
| 309 | readonly color: string; |
| 310 | readonly now: number; |
| 311 | readonly index: number; |
| 312 | readonly showAccountName: boolean; |
| 313 | }) { |
| 314 | return account.redeem ? ( |
| 315 | <RedeemableSegment account={account} redeemAt={account.redeem} {...props} /> |
| 316 | ) : ( |
| 317 | <SegmentTriggers account={account} redeem={null} {...props} /> |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | function RedeemableSegment({ |
| 322 | redeemAt, |
| 323 | ...props |
| 324 | }: Omit<Parameters<typeof SegmentTriggers>[0], "redeem"> & { |
| 325 | readonly redeemAt: NonNullable<LimitAccount["redeem"]>; |
| 326 | }) { |
| 327 | const redeem = useResetCredit(redeemAt.environmentId); |
| 328 | const gt = useGT(); |
| 329 | return ( |
| 330 | <> |
| 331 | <SegmentTriggers redeem={redeem} {...props} /> |
| 332 | <ResetCreditDialog |
| 333 | open={redeem.confirming} |
| 334 | onOpenChange={redeem.setConfirming} |
| 335 | choice={redeem.choice} |
| 336 | onConfirm={() => void redeem.redeem()} |
| 337 | disabled={!redeem.canManageProviders} |
| 338 | /> |
| 339 | {/* The popover closed before the confirm, so progress and the outcome need a home outside it. */} |
| 340 | {redeem.busy || redeem.status ? ( |
| 341 | <span role="status" className="col-span-full text-xs text-muted-foreground"> |
| 342 | <AccountName account={props.account} className="font-medium text-foreground" />{" "} |
| 343 | {redeem.busy ? gt("Using reset credit…") : redeem.status} |
| 344 | </span> |
| 345 | ) : null} |
| 346 | </> |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | function SegmentTriggers({ |
| 351 | account, |
| 352 | window, |
| 353 | reset, |
| 354 | redeem, |
| 355 | refillPercent, |
| 356 | color, |
| 357 | now, |
| 358 | index, |
| 359 | showAccountName, |
| 360 | }: SegmentPayload & { |
| 361 | readonly refillPercent: number; |
| 362 | readonly color: string; |
| 363 | readonly now: number; |
| 364 | readonly index: number; |
| 365 | readonly showAccountName: boolean; |
| 366 | }) { |
| 367 | const popover = use(SegmentPopoverContext) ?? undefined; |
| 368 | const payload: SegmentPayload = { account, window, reset, redeem }; |
| 369 | const remaining = remainingPercent(window); |
| 370 | const timeLeft = paceRemainingPercent(window, now); |
| 371 | const [segmentRef, paceMark] = usePaceMark(timeLeft); |
| 372 | const m = useMessages(); |
| 373 | const resetsIn = formatResetsIn(window, now, m); |
| 374 | const refillIn = formatResetsIn(window, now, m, "refill"); |
| 375 | const credits = account.limits.resetCredits?.availableCount ?? 0; |
| 376 | const gt = useGT(); |
| 377 | const segmentLabel = [ |
| 378 | gt("{name}: {remaining}% left", { |
| 379 | name: |
| 380 | account.displayName ?? (account.email ? accountInitials(account.email) : account.driver), |
| 381 | remaining, |
| 382 | }), |
| 383 | resetsIn, |
| 384 | timeLeft === null ? null : gt("{timeLeft}% of the window left", { timeLeft }), |
| 385 | credits |
| 386 | ? gt( |
| 387 | "{count, plural, one {{count} reset credit banked} other {{count} reset credits banked}}", |
| 388 | { count: credits }, |
| 389 | ) |
| 390 | : null, |
| 391 | ] |
| 392 | .filter(Boolean) |
| 393 | .join(", "); |
| 394 | return ( |
| 395 | <> |
| 396 | <PopoverTrigger |
| 397 | handle={popover} |
| 398 | payload={payload} |
| 399 | openOnHover |
| 400 | render={ |
| 401 | <button |
| 402 | ref={segmentRef} |
| 403 | type="button" |
| 404 | style={{ gridColumn: index, gridRow: 1, ...paceMark?.gappedTrack }} |
| 405 | aria-label={segmentLabel} |
| 406 | className="relative h-5 min-w-0 cursor-pointer rounded-md bg-muted text-start outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background data-[popup-open]:ring-1 data-[popup-open]:ring-inset data-[popup-open]:ring-border @2xl/pool:h-8" |
| 407 | /> |
| 408 | } |
| 409 | > |
| 410 | <div aria-hidden className="absolute inset-0 overflow-hidden rounded-md"> |
| 411 | {/* Clipped inside the rounded clip so the bar's own corners keep their antialiasing. */} |
| 412 | <div className="absolute inset-0" style={paceMark?.gap}> |
| 413 | <div |
| 414 | aria-hidden |
| 415 | className="absolute inset-y-0 left-0 rounded-md opacity-35" |
| 416 | style={{ width: `${remaining}%`, backgroundColor: color }} |
| 417 | /> |
| 418 | {remaining < 100 && reset && refillPercent > 0 ? ( |
| 419 | <div |
| 420 | aria-hidden |
| 421 | className="absolute inset-y-0 right-0 opacity-20" |
| 422 | style={{ |
| 423 | width: `${Math.min(100 - remaining, refillPercent)}%`, |
| 424 | // A bare 45° gradient repeats every 5√2px, so some stripes rasterize thinner; a whole-pixel tile doesn't. |
| 425 | backgroundImage: `repeating-linear-gradient(135deg, transparent 0 1px, ${color} 1px 2px, transparent 2px 50%)`, |
| 426 | backgroundSize: "7px 7px", |
| 427 | }} |
| 428 | /> |
| 429 | ) : null} |
| 430 | </div> |
| 431 | </div> |
| 432 | {paceMark ? ( |
| 433 | <span |
| 434 | aria-hidden |
| 435 | className="absolute inset-y-0.5 w-0.5 -translate-x-1/2 rounded-full bg-foreground" |
| 436 | style={paceMark.tick} |
| 437 | /> |
| 438 | ) : null} |
| 439 | <span |
| 440 | aria-hidden |
| 441 | className="absolute inset-0 flex items-center justify-center overflow-hidden rounded-md text-3xs leading-none font-semibold text-foreground/80 tabular-nums @2xl/pool:hidden" |
| 442 | > |
| 443 | {index} |
| 444 | </span> |
| 445 | <div className="relative hidden h-full min-w-0 items-center gap-1.5 overflow-hidden rounded-md px-2 text-xs @2xl/pool:flex"> |
| 446 | {showAccountName ? ( |
| 447 | <AccountName |
| 448 | account={account} |
| 449 | className="min-w-0 truncate font-medium text-foreground" |
| 450 | /> |
| 451 | ) : null} |
| 452 | {/* A single-account bar repeats the headline percentage. */} |
| 453 | {showAccountName ? ( |
| 454 | <span className="shrink-0 font-semibold text-foreground tabular-nums"> |
| 455 | {remaining}% |
| 456 | </span> |
| 457 | ) : null} |
| 458 | {resetsIn || credits ? ( |
| 459 | <span className="ms-auto flex min-w-0 items-center gap-1.5 overflow-hidden rounded-sm bg-background/85 px-1.5 py-0.5 text-2xs whitespace-nowrap text-foreground tabular-nums"> |
| 460 | {refillIn ?? ""} |
| 461 | {credits ? ( |
| 462 | <> |
| 463 | {resetsIn ? ( |
| 464 | <span aria-hidden className="text-muted-foreground"> |
| 465 | · |
| 466 | </span> |
| 467 | ) : null} |
| 468 | <span aria-hidden className="inline-flex items-center gap-0.5 font-semibold"> |
| 469 | <TicketIcon className="size-3" aria-hidden /> |
| 470 | {credits} |
| 471 | </span> |
| 472 | </> |
| 473 | ) : null} |
| 474 | </span> |
| 475 | ) : null} |
| 476 | </div> |
| 477 | </PopoverTrigger> |
| 478 | <LegendRow payload={payload} color={color} now={now} index={index} /> |
| 479 | </> |
| 480 | ); |
| 481 | } |
| 482 | |
| 483 | function LegendRow({ |
| 484 | payload, |
| 485 | color, |
| 486 | now, |
| 487 | index, |
| 488 | }: { |
| 489 | readonly payload: SegmentPayload; |
| 490 | readonly color: string; |
| 491 | readonly now: number; |
| 492 | readonly index: number; |
| 493 | }) { |
| 494 | const popover = use(SegmentPopoverContext) ?? undefined; |
| 495 | const { account, window } = payload; |
| 496 | const remaining = remainingPercent(window); |
| 497 | const m = useMessages(); |
| 498 | const resetsIn = formatResetsIn(window, now, m, "refill"); |
| 499 | const credits = account.limits.resetCredits?.availableCount ?? 0; |
| 500 | const gt = useGT(); |
| 501 | return ( |
| 502 | <PopoverTrigger |
| 503 | handle={popover} |
| 504 | payload={payload} |
| 505 | style={{ gridColumn: "1 / -1", gridRow: index + 1 }} |
| 506 | render={<Button variant="ghost" size="compact" />} |
| 507 | className="min-w-0 @2xl/pool:hidden" |
| 508 | > |
| 509 | <span className="relative inline-flex size-4 shrink-0 items-center justify-center rounded-sm text-3xs leading-none font-semibold text-foreground/80 tabular-nums"> |
| 510 | <span |
| 511 | aria-hidden |
| 512 | className="absolute inset-0 rounded-sm opacity-35" |
| 513 | style={{ backgroundColor: color }} |
| 514 | /> |
| 515 | <span className="sr-only">{`${gt("Segment")} `}</span> |
| 516 | <span className="relative">{index}</span> |
| 517 | </span> |
| 518 | <AccountName account={account} className="min-w-0 truncate font-medium text-foreground" /> |
| 519 | <span className="shrink-0 font-semibold text-foreground tabular-nums">{remaining}%</span> |
| 520 | <span className="ms-auto flex shrink-0 items-center gap-1.5 text-2xs text-muted-foreground tabular-nums"> |
| 521 | {resetsIn ?? ""} |
| 522 | {credits ? ( |
| 523 | <> |
| 524 | {resetsIn ? <span aria-hidden>·</span> : null} |
| 525 | <span |
| 526 | aria-hidden |
| 527 | className="inline-flex items-center gap-0.5 font-semibold text-foreground" |
| 528 | > |
| 529 | <TicketIcon className="size-3" aria-hidden /> |
| 530 | {credits} |
| 531 | </span> |
| 532 | <span className="sr-only"> |
| 533 | {gt( |
| 534 | "{count, plural, one {{count} reset credit banked} other {{count} reset credits banked}}", |
| 535 | { count: credits }, |
| 536 | )} |
| 537 | </span> |
| 538 | </> |
| 539 | ) : null} |
| 540 | </span> |
| 541 | </PopoverTrigger> |
| 542 | ); |
| 543 | } |
| 544 | |
| 545 | function PoolBar({ |
| 546 | pool, |
| 547 | color, |
| 548 | now, |
| 549 | }: { |
| 550 | readonly pool: LimitPoolWindow; |
| 551 | readonly color: string; |
| 552 | readonly now: number; |
| 553 | }) { |
| 554 | const restores = new Map(pool.resets.map((reset) => [reset.member.account.key, reset])); |
| 555 | const gt = useGT(); |
| 556 | return ( |
| 557 | <div className="@container/pool min-w-0"> |
| 558 | <div |
| 559 | className="grid gap-x-1 gap-y-1" |
| 560 | style={{ gridTemplateColumns: `repeat(${pool.columns.length}, minmax(0, 1fr))` }} |
| 561 | > |
| 562 | {pool.columns.map((member, position) => |
| 563 | member.window ? ( |
| 564 | <PoolSegment |
| 565 | key={member.account.key} |
| 566 | account={member.account} |
| 567 | window={member.window} |
| 568 | reset={restores.get(member.account.key)} |
| 569 | refillPercent={member.refillPercent} |
| 570 | color={normalizeProviderAccentColor(member.account.accentColor) ?? color} |
| 571 | now={now} |
| 572 | index={position + 1} |
| 573 | showAccountName={pool.columns.length > 1} |
| 574 | /> |
| 575 | ) : pool.kind === "monthly" && member.account.limits.zeroSpendCap ? ( |
| 576 | <div |
| 577 | key={member.account.key} |
| 578 | style={{ gridColumn: position + 1, gridRow: 1 }} |
| 579 | className="flex h-5 min-w-0 items-center justify-center gap-1.5 overflow-hidden rounded-md border border-dashed border-border text-3xs whitespace-nowrap text-muted-foreground @2xl/pool:h-8 @2xl/pool:justify-start @2xl/pool:px-2 @2xl/pool:text-xs" |
| 580 | > |
| 581 | <AccountName |
| 582 | account={member.account} |
| 583 | className="hidden min-w-0 truncate @2xl/pool:inline-flex" |
| 584 | /> |
| 585 | {gt("$0 cap")} |
| 586 | </div> |
| 587 | ) : null, |
| 588 | )} |
| 589 | </div> |
| 590 | </div> |
| 591 | ); |
| 592 | } |
| 593 | |
| 594 | function PoolWindowCard({ |
| 595 | pool, |
| 596 | color, |
| 597 | now, |
| 598 | label, |
| 599 | description, |
| 600 | }: { |
| 601 | readonly pool: LimitPoolWindow; |
| 602 | readonly color: string; |
| 603 | readonly now: number; |
| 604 | readonly label?: string | null | undefined; |
| 605 | readonly description?: string | undefined; |
| 606 | }) { |
| 607 | const nextRefill = pool.resets.find((reset) => reset.restoresPercent > 0); |
| 608 | return ( |
| 609 | <div className="grid items-center gap-x-6 gap-y-3 rounded-lg border border-border/60 p-4 md:grid-cols-[11rem_minmax(0,1fr)]"> |
| 610 | <div className="flex flex-col gap-1"> |
| 611 | {label !== null ? ( |
| 612 | <span className="text-sm font-medium text-foreground">{label ?? pool.label}</span> |
| 613 | ) : null} |
| 614 | <span className="flex items-baseline gap-2"> |
| 615 | <span className="text-3xl font-semibold text-foreground tabular-nums"> |
| 616 | {pool.remainingPercent}% |
| 617 | </span> |
| 618 | <span className="inline-flex items-center gap-2 text-sm text-muted-foreground"> |
| 619 | <span> |
| 620 | <T context="remaining usage percentage">left</T> |
| 621 | </span> |
| 622 | {pool.pace ? <PaceIcon pace={pool.pace} /> : null} |
| 623 | </span> |
| 624 | </span> |
| 625 | {nextRefill && pool.columns.length > 1 ? ( |
| 626 | <span className="text-xs font-medium text-foreground tabular-nums"> |
| 627 | ↻ +{nextRefill.restoresPercent}% |
| 628 | </span> |
| 629 | ) : null} |
| 630 | </div> |
| 631 | <PoolBar pool={pool} color={color} now={now} /> |
| 632 | {description ? ( |
| 633 | <p className="text-xs text-muted-foreground md:col-span-2">{description}</p> |
| 634 | ) : null} |
| 635 | </div> |
| 636 | ); |
| 637 | } |
| 638 | |
| 639 | function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: number }) { |
| 640 | const color = barColor(pool.driver); |
| 641 | const label = providerClients.get(pool.driver)?.label ?? String(pool.driver); |
| 642 | const windows = displayLimitWindows(pool); |
| 643 | const m = useMessages(); |
| 644 | return ( |
| 645 | <section className="flex flex-col gap-3"> |
| 646 | <h2 className="flex items-center gap-2 text-sm font-medium text-foreground"> |
| 647 | <ProviderInstanceIcon |
| 648 | driverKind={pool.driver} |
| 649 | displayName={label} |
| 650 | indicatorBackground="var(--background)" |
| 651 | className="size-5" |
| 652 | iconClassName="size-4 text-foreground/80" |
| 653 | /> |
| 654 | {label} |
| 655 | </h2> |
| 656 | {windows.map((window) => { |
| 657 | const details = |
| 658 | pool.driver === "cursor" ? cursorUsageWindowDetails(window.id, pool, m) : undefined; |
| 659 | return ( |
| 660 | <PoolWindowCard |
| 661 | key={`${window.kind}:${window.id}`} |
| 662 | pool={window} |
| 663 | color={color} |
| 664 | now={now} |
| 665 | label={details?.label} |
| 666 | description={details?.description} |
| 667 | /> |
| 668 | ); |
| 669 | })} |
| 670 | </section> |
| 671 | ); |
| 672 | } |
| 673 | |
| 674 | export function UsageLimitsPooled({ |
| 675 | presentations, |
| 676 | now, |
| 677 | }: { |
| 678 | readonly presentations: Parameters<typeof collectLimitAccounts>[0]; |
| 679 | readonly now: number; |
| 680 | }) { |
| 681 | const gt = useGT(); |
| 682 | const m = useMessages(); |
| 683 | const [segmentPopover] = useState(() => PopoverCreateHandle<SegmentPayload>()); |
| 684 | const segmentPopoverActions = useRef<PopoverPrimitive.Root.Actions | null>(null); |
| 685 | const pools = collectLimitPools(collectLimitAccounts(presentations), now); |
| 686 | const notices = collectLimitNotices(presentations, m); |
| 687 | const externalLinks = collectExternalUsageLinks(presentations, m); |
| 688 | return ( |
| 689 | <div className="flex flex-col gap-8"> |
| 690 | {pools.length === 0 && notices.length === 0 && externalLinks.length === 0 ? ( |
| 691 | <p className="text-sm text-muted-foreground"> |
| 692 | <T>No provider on the selected environments reports subscription limits.</T> |
| 693 | </p> |
| 694 | ) : null} |
| 695 | <SegmentPopoverContext value={segmentPopover}> |
| 696 | {pools.map((pool) => ( |
| 697 | <Fragment key={pool.driver}> |
| 698 | <PoolSection pool={pool} now={now} /> |
| 699 | </Fragment> |
| 700 | ))} |
| 701 | </SegmentPopoverContext> |
| 702 | <Popover handle={segmentPopover} actionsRef={segmentPopoverActions}> |
| 703 | {({ payload }) => |
| 704 | payload ? ( |
| 705 | <ActiveSegmentPopover |
| 706 | handle={segmentPopover} |
| 707 | actions={segmentPopoverActions} |
| 708 | payload={payload} |
| 709 | pools={pools} |
| 710 | now={now} |
| 711 | /> |
| 712 | ) : null |
| 713 | } |
| 714 | </Popover> |
| 715 | {externalLinks.map((link) => ( |
| 716 | <section |
| 717 | key={link.url} |
| 718 | className="flex flex-wrap items-center justify-between gap-3 rounded-xl border p-4" |
| 719 | > |
| 720 | <div className="flex min-w-0 flex-1 items-center gap-3"> |
| 721 | {link.url === CHATGPT_USAGE_URL ? ( |
| 722 | <OpenAI className="size-5 shrink-0" aria-hidden="true" /> |
| 723 | ) : null} |
| 724 | <div className="min-w-0 space-y-1"> |
| 725 | <h2 className="text-sm font-medium">{link.label}</h2> |
| 726 | <p className="text-xs break-words text-muted-foreground"> |
| 727 | {link.accounts.join(", ")} |
| 728 | </p> |
| 729 | {link.url === CHATGPT_USAGE_URL ? ( |
| 730 | <p className="text-xs text-muted-foreground"> |
| 731 | <T>View usage in ChatGPT with your connected account.</T> |
| 732 | </p> |
| 733 | ) : link.message ? ( |
| 734 | <p className="max-w-xl text-xs text-muted-foreground">{link.message}</p> |
| 735 | ) : null} |
| 736 | </div> |
| 737 | </div> |
| 738 | <Button |
| 739 | variant="ghost-muted" |
| 740 | size="xs" |
| 741 | onClick={() => void ensureLocalApi().shell.openExternal(link.url)} |
| 742 | > |
| 743 | {gt("Manage usage")} |
| 744 | <ExternalLinkIcon className="size-3.5" aria-hidden="true" /> |
| 745 | </Button> |
| 746 | </section> |
| 747 | ))} |
| 748 | <LimitNotices notices={notices} /> |
| 749 | </div> |
| 750 | ); |
| 751 | } |
| 752 | |
| 753 | function LimitNotices({ notices }: { readonly notices: readonly string[] }) { |
| 754 | if (notices.length === 0) return null; |
| 755 | return ( |
| 756 | <Alert variant="warning" controlAlignment="first-line"> |
| 757 | <AlertTriangleIcon /> |
| 758 | {notices.map((notice) => ( |
| 759 | <AlertTitle key={notice} className="break-words"> |
| 760 | {notice} |
| 761 | </AlertTitle> |
| 762 | ))} |
| 763 | </Alert> |
| 764 | ); |
| 765 | } |