"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import { ChevronDown } from "lucide-react"; import { useAuth } from "@/auth/clerk"; import type { ProviderCredentialRead, ProviderUsageLiveRead, SubscriptionWindowRead, } from "@/api/generated/model"; import { getProviderUsageLiveApiV1ProviderCredentialsCredentialIdUsageGet, listProviderCredentialsApiV1ProviderCredentialsGet, } from "@/api/generated/provider-credentials/provider-credentials"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; type NavbarProviderId = "anthropic" | "openai"; type ProviderNavbarItem = { providerId: NavbarProviderId; label: string; status: "Active" | "Inactive" | "Not configured" | "Syncing"; sessionRemaining: number | null; modelsRemaining: number | null; reset: string; }; const NAVBAR_PROVIDER_IDS: NavbarProviderId[] = ["anthropic", "openai"]; function providerShortLabel(provider: string): string { if (provider === "anthropic") return "Claude"; if (provider === "openai") return "GPT"; return provider; } function clampPct(pct: number): number { return Math.max(0, Math.min(100, pct)); } function fmtResetMs(ms: number | null | undefined): string { if (ms == null || ms <= 0) return "< 1m"; const s = Math.floor(ms / 1000); if (s < 60) return `${s}s`; const m = Math.floor(s / 60); if (m < 60) return `${m}m ${s % 60}s`; const h = Math.floor(m / 60); return `${h}h ${m % 60}m`; } function remainingPct(window: SubscriptionWindowRead | null): number | null { if (!window) return null; return clampPct(100 - window.pct_used); } function remainingColor(pct: number | null): string { if (pct == null) return "bg-[color:var(--text-quiet)]"; if (pct <= 10) return "bg-[color:var(--danger)]"; if (pct <= 25) return "bg-[color:var(--warning)]"; return "bg-[color:var(--success)]"; } function findSessionWindow(usage: ProviderUsageLiveRead | null | undefined) { const windows = usage?.subscription_windows ?? []; return ( windows.find((window) => { const key = `${window.key} ${window.label}`.toLowerCase(); return ( key.includes("current") || key.includes("session") || key.includes("five") || key.includes("hour") ); }) ?? windows[0] ?? null ); } function findModelsWindow( usage: ProviderUsageLiveRead | null | undefined, sessionWindow: SubscriptionWindowRead | null, ) { const windows = usage?.subscription_windows ?? []; return ( windows.find((window) => { if (window.key === sessionWindow?.key) return false; const key = `${window.key} ${window.label}`.toLowerCase(); return key.includes("all") || key.includes("model") || key.includes("week"); }) ?? windows.find((window) => window.key !== sessionWindow?.key) ?? null ); } function resetLabel( sessionWindow: SubscriptionWindowRead | null, modelsWindow: SubscriptionWindowRead | null, ): string { const resetInMs = sessionWindow?.reset_in_ms ?? modelsWindow?.reset_in_ms; return resetInMs == null ? "-" : fmtResetMs(resetInMs); } function formatRemaining(pct: number | null): string { return pct == null ? "-" : `${Math.round(pct)}% left`; } function providerStatusColor(status: ProviderNavbarItem["status"]): string { if (status === "Active") return "bg-[color:var(--success)]"; if (status === "Syncing") return "bg-[color:var(--accent)]"; if (status === "Inactive") return "bg-[color:var(--warning)]"; return "bg-[color:var(--text-quiet)]"; } function MiniRemainingBar({ pct, className = "w-10", }: { pct: number | null; className?: string; }) { if (pct == null) return null; const width = clampPct(pct); return (