import { Link } from 'react-router';
import { Check, ChevronsUpDown, Loader2, Settings2 } from 'lucide-react';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { invoicesSectionPath } from './shared';
import { useInvoiceShelfAccountActions, useInvoiceShelfAccounts, useInvoiceShelfHealth } from './useInvoiceShelfData';
// Which books you are looking at, and how to change them.
//
// The nav header's subtitle was already the connection state, so the switcher takes that line rather than
// adding a second control saying nearly the same thing. With one account it stays exactly what it was: a
// line of text plus a way into the settings screen.
//
// Each entry shows its company under the label, because two accounts on the same instance differing only by
// company is the normal case here — the label alone would not tell them apart.
//
// Switching invalidates the whole ['invoiceshelf'] key: it changes the answer to every query in the
// workspace without changing any of their inputs.
export const AccountSwitcher = () => {
const { data } = useInvoiceShelfAccounts();
const { data: health } = useInvoiceShelfHealth();
const { activate } = useInvoiceShelfAccountActions();
const accounts = data?.accounts ?? [];
const active = accounts.find((account) => account.isActive) ?? null;
// Before the registry answers, fall back to health — it is the query that was already driving this line.
const status = active?.label ?? (health?.ok ? (health.version ?? 'connected') : 'not connected');
if (accounts.length < 2) {
return (
{status}
);
}
return (
{status}
{activate.isPending ? (
) : (
)}
InvoiceShelf accounts
{accounts.map((account) => (
void activate.mutateAsync(account.id).catch(() => undefined)}
className="gap-2"
>
{account.label}
{account.companyName ?? account.url}
))}
Manage accounts
);
};