// The subscription-account dropdown every provider shares. Its content comes
// from `subscriptionAccountMenuContent`; data sources own loading,
// authorization, usage freshness and selection.
import { ChevronDownIcon, Loader2Icon } from "lucide-react";
import type { ReactNode } from "react";
import {
DropdownMenu,
DropdownMenuCheck,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuMeta,
DropdownMenuNote,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "../../../ui/dropdown-menu";
import { cn } from "../../lib/utils";
export type SubscriptionAccountMenuRow = {
id: string;
label: string;
plan?: string | null | undefined;
status?: string | null;
detail?: string;
personal?: boolean;
disabled?: boolean;
/** Per-window readings, not an aggregate capacity/eligibility estimate. */
usageSummary?: string | undefined;
/** The row's most-used window, drawn as a small bar beside the summary. */
usageBar?: { label: string; usedPercent: number } | undefined;
};
export type SubscriptionAccountUsageWindow = {
label: string;
/** Null means unknown; values over 100 can represent overage. */
usedPercent: number | null;
detail?: string;
};
export type SubscriptionAccountMenuUsage = {
checking?: boolean | undefined;
unavailable?: boolean | undefined;
message?: string | undefined;
note?: string | undefined;
windows?: SubscriptionAccountUsageWindow[] | undefined;
};
function UsageRow({ label, usedPercent, detail }: SubscriptionAccountUsageWindow) {
const known = typeof usedPercent === "number" && Number.isFinite(usedPercent);
const pct = known ? Math.min(100, Math.max(0, usedPercent)) : 0;
const text = known ? `${Math.round(usedPercent)}% used` : "Unknown";
return (
{label}
{known ? (
= 90 ? "bg-status-waiting" : "bg-brand",
)}
style={{ width: `${pct}%` }}
/>
) : (
)}
{text}
{detail ?
{detail}
: null}
);
}
function RowUsageBar({ label, usedPercent }: { label: string; usedPercent: number }) {
const pct = Math.min(100, Math.max(0, usedPercent));
return (
= 90 ? "bg-status-waiting" : "bg-brand")}
style={{ width: `${pct}%` }}
/>
);
}
export function SubscriptionAccountMenu({
ariaLabel,
triggerMark,
headerMark,
selectionLabel,
displayName,
metadata,
identity,
tone = "ok",
usage,
accounts,
selectedId,
automaticSelected,
choosing,
choosingTarget,
actionLabel = "Use for next turn",
automaticLabel = "Automatic",
emptyMessage,
error,
note,
onOpenChange,
onChoose,
}: {
ariaLabel: string;
triggerMark: ReactNode;
headerMark: ReactNode;
selectionLabel: string;
displayName: string;
metadata: string;
identity?: string | null | undefined;
tone?: "ok" | "warn" | "bad" | undefined;
usage: SubscriptionAccountMenuUsage;
accounts: SubscriptionAccountMenuRow[];
selectedId: string | null;
automaticSelected: boolean;
choosing: boolean;
choosingTarget: string | null | undefined;
actionLabel?: string | undefined;
automaticLabel?: string | undefined;
emptyMessage?: string | undefined;
error?: string | null | undefined;
note: string;
onOpenChange?: ((open: boolean) => void) | undefined;
onChoose: (connectionId: string | null) => void;
}) {
const row = (account: SubscriptionAccountMenuRow) => (
{
event.preventDefault();
onChoose(account.id);
}}
className="cursor-pointer"
>
{account.label}
{account.usageSummary ? (
{account.usageBar ? : null}
{account.usageSummary}
) : null}
{account.plan ? {account.plan} : null}
{account.status ? (
{account.status}
) : null}
{choosingTarget === account.id ? (
) : (
)}
);
const personal = accounts.filter((account) => account.personal);
return (
{headerMark}
{selectionLabel}
{displayName}
{identity ? (
{identity}
) : null}
{metadata}
{usage.checking ? (
Checking usageā¦
) : usage.unavailable ? (
Usage unavailable
) : usage.message ? (
{usage.message}
) : (
{usage.windows?.map((window) => (
))}
)}
{usage.note ?
{usage.note}
: null}
{actionLabel}
{
event.preventDefault();
onChoose(null);
}}
className="cursor-pointer"
>
{automaticLabel}
{choosingTarget === null ? (
) : (
)}
{accounts.filter((account) => !account.personal).map(row)}
{personal.length > 0 ? (
<>
Your accounts
{personal.map(row)}
>
) : null}
{accounts.length === 0 && emptyMessage ? (
{emptyMessage}
) : null}
{error ? (
{error}
) : (
{note}
)}
);
}