"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 totalMinutes = Math.floor(ms / 60000); const d = Math.floor(totalMinutes / (60 * 24)); const h = Math.floor((totalMinutes % (60 * 24)) / 60); const m = totalMinutes % 60; const parts: string[] = []; if (d > 0) parts.push(`${d}d`); if (h > 0) parts.push(`${h}hrs`); if (m > 0 || parts.length === 0) parts.push(`${m}m`); return parts.join(" "); } function fmtResetDate(ms: number | null | undefined): string | null { if (ms == null || ms <= 0) return null; const resetAt = new Date(Date.now() + ms); return resetAt.toLocaleString(undefined, { month: "short", day: "numeric", hour: "numeric", minute: "2-digit", }); } 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; if (resetInMs == null) return "-"; const date = fmtResetDate(resetInMs); return date ? `${fmtResetMs(resetInMs)} ยท Full reset at ${date}` : 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 (