🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
refactor(web): Migrate billing invoices table to design system (#18341)
Ben Bachemd2e787dd5c6b
diff --git a/web/src/components/design-system/table/columns/createBadgeTableColumn.tsx b/web/src/components/design-system/table/columns/createBadgeTableColumn.tsxindex b731b441c0..774decac17 100644--- a/web/src/components/design-system/table/columns/createBadgeTableColumn.tsx+++ b/web/src/components/design-system/table/columns/createBadgeTableColumn.tsx@@ -31,7 +31,10 @@ type NeutralBadgeVariant = Extract<"default" | "secondary" | "tertiary">;-type GetBadge<TData extends RowData, TVariant extends BadgeVariant> = (+type GetBadge<+ TData extends RowData,+ TVariant extends BadgeVariant | "unknown",+> = (value: string,context: CellContext<TData, string | null | undefined>,) => {@@ -51,7 +54,7 @@ type BadgeTableColumnOptions<TData extends RowData> =})| (TableColumnOptions<TData, string> & {range: "semantic";- getBadge: GetBadge<TData, SemanticBadgeVariant>;+ getBadge: GetBadge<TData, SemanticBadgeVariant | "unknown">;});export function createBadgeTableColumn<TData extends RowData>({@@ -80,7 +83,11 @@ export function createBadgeTableColumn<TData extends RowData>({return (<Badge- variant={badge.variant ?? "secondary"}+ variant={+ badge.variant === "unknown"+ ? "default"+ : (badge.variant ?? "secondary")+ }className="max-w-fit gap-1 truncate rounded-sm px-1 font-normal"title={badge.value}>diff --git a/web/src/ee/features/billing/components/BillingInvoiceTable.stories.tsx b/web/src/ee/features/billing/components/BillingInvoiceTable.stories.tsxnew file mode 100644index 0000000000..aed1dd043c--- /dev/null+++ b/web/src/ee/features/billing/components/BillingInvoiceTable.stories.tsx@@ -0,0 +1,123 @@+import { expect, fn, within } from "storybook/test";++import preview from "../../../../../.storybook/preview";+import {+ BillingInvoiceTable,+ type BillingInvoiceRow,+} from "./BillingInvoiceTable";++const invoices: BillingInvoiceRow[] = [+ {+ id: "invoice-1",+ number: "INV-001",+ status: "paid",+ currency: "USD",+ created: new Date("2026-09-01T12:00:00Z"),+ hostedInvoiceUrl: "https://example.com/invoices/1",+ invoicePdfUrl: "https://example.com/invoices/1.pdf",+ breakdown: {+ subscriptionCents: 19900,+ usageCents: 5000,+ discountCents: -1000,+ taxCents: 2000,+ totalCents: 25900,+ },+ },+ {+ id: "preview",+ number: null,+ status: "draft",+ currency: "USD",+ created: new Date("2026-10-01T12:00:00Z"),+ hostedInvoiceUrl: null,+ invoicePdfUrl: null,+ breakdown: {+ subscriptionCents: 19900,+ usageCents: 2500,+ discountCents: 0,+ taxCents: 0,+ totalCents: 22400,+ },+ },+];++const defaultArgs = {+ showBreakdownColumns: true,+ pagination: {+ mode: "cursor" as const,+ state: { pageIndex: 0, pageSize: 10 },+ onChange: fn(),+ hasNextPage: true,+ },+};++const meta = preview.meta({+ component: BillingInvoiceTable,+});++export const Stripe = meta.story({+ name: "(Test) Stripe",+ args: { ...defaultArgs, data: { status: "success", data: invoices } },+ play: async ({ canvas, canvasElement, userEvent }) => {+ await expect(+ canvas.getByRole("columnheader", { name: "Subscription" }),+ ).toBeVisible();+ await userEvent.click(+ canvas.getAllByRole("button", { name: "Open actions menu" })[0]!,+ );+ const menu = within(canvasElement.ownerDocument.body);+ await expect(menu.getByRole("link", { name: "View" })).toHaveAttribute(+ "href",+ invoices[0]!.hostedInvoiceUrl,+ );+ await expect(menu.getByRole("link", { name: "PDF" })).toHaveAttribute(+ "href",+ invoices[0]!.invoicePdfUrl,+ );+ await userEvent.keyboard("{Escape}");+ },+});++export const ClickHouse = meta.story({+ name: "(Test) ClickHouse",+ args: {+ ...defaultArgs,+ showBreakdownColumns: false,+ data: { status: "success", data: [invoices[0]!] },+ pagination: {+ mode: "cursor",+ state: { pageIndex: 0, pageSize: 10 },+ onChange: fn(),+ hasNextPage: false,+ },+ },+ play: async ({ canvas }) => {+ await expect(+ canvas.queryByRole("columnheader", { name: "Subscription" }),+ ).not.toBeInTheDocument();+ await expect(+ canvas.getByRole("columnheader", { name: "Total" }),+ ).toBeVisible();+ await expect(+ canvas.getByRole("button", { name: "Go to next page" }),+ ).toBeDisabled();+ },+});++export const Loading = meta.story({+ args: { ...defaultArgs, data: { status: "loading" }, loadingRowCount: 5 },+});++export const Empty = meta.story({+ args: { ...defaultArgs, data: { status: "success", data: [] } },+});++export const Error = meta.story({+ args: {+ ...defaultArgs,+ data: {+ status: "error",+ error: "Failed to load invoices. Please try again.",+ },+ },+});diff --git a/web/src/ee/features/billing/components/BillingInvoiceTable.tsx b/web/src/ee/features/billing/components/BillingInvoiceTable.tsxindex ad0424f121..2916116361 100644--- a/web/src/ee/features/billing/components/BillingInvoiceTable.tsx+++ b/web/src/ee/features/billing/components/BillingInvoiceTable.tsx@@ -1,308 +1,143 @@-/* eslint-disable no-nested-ternary */-/* eslint-disable @repo/no-null-render */-import { DataTable } from "@/src/components/table/data-table";-import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";-import { type LangfuseColumnDef } from "@/src/components/table/types";-import { createNumberTableColumn } from "@/src/components/design-system/table/columns/createNumberTableColumn";-import { Button } from "@/src/components/ui/button";-import { Badge, type BadgeProps } from "@/src/components/ui/badge";-import { api } from "@/src/utils/api";-import { costFormatter } from "@/src/utils/numbers";+import { useCallback, useMemo } from "react";import { Download, ExternalLink } from "lucide-react";-import { useEffect, useMemo, useState } from "react";-import { useBillingInformation } from "./useBillingInformation";-import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";+import { SettingsTable } from "@/src/components/SettingsTable/SettingsTable";+import { type PaginationBarProps } from "@/src/components/design-system/PaginationBar/PaginationBar";+import { type TableProps } from "@/src/components/design-system/table/Table";+import { createDateTableColumn } from "@/src/components/design-system/table/columns/createDateTableColumn";+import { createBadgeTableColumn } from "@/src/components/design-system/table/columns/createBadgeTableColumn";+import { createNumberTableColumn } from "@/src/components/design-system/table/columns/createNumberTableColumn";+import { type LangfuseColumnDef } from "@/src/components/table/types";+import { type RouterOutputs } from "@/src/utils/api";+import { costFormatter } from "@/src/utils/numbers";-type InvoiceRow = {- id: string;- number: string | null;- status: string | null;- currency: string;- created: Date;- hostedInvoiceUrl: string | null;- invoicePdfUrl: string | null;- breakdown?: {- subscriptionCents: number;- usageCents: number;- discountCents: number;- taxCents: number;- totalCents: number;- };+export type BillingInvoiceRow = Omit<+ RouterOutputs["cloudBilling"]["getInvoices"]["invoices"][number],+ "created"+> & { created: Date };++type BillingInvoiceTableProps = Pick<+ TableProps<BillingInvoiceRow>,+ "data" | "loadingRowCount"+> & {+ showBreakdownColumns: boolean;+ pagination: PaginationBarProps;};-export function BillingInvoiceTable() {- const { organization, billingProvider } = useBillingInformation();- const isCloudBillingAvailable = useIsCloudBillingAvailable();- // ClickHouse Billing reports a single total per invoice, so the- // Stripe-only breakdown columns would always read $0.00.- const showBreakdownColumns = billingProvider !== "clickhouse";- // Provider-agnostic: getInvoices dispatches to whichever provider bills the- // org, so the gate is "does this org have a billing identity at all", not- // "does it have a Stripe customer". A CHB org never populates- // stripe.customerId and would otherwise never see its invoice history.- const hasBillingIdentity = Boolean(- organization?.cloudConfig?.stripe?.customerId ??- organization?.cloudConfig?.clickhouse?.organizationId,+export function BillingInvoiceTable({+ showBreakdownColumns,+ pagination,+ ...tableProps+}: BillingInvoiceTableProps) {+ const columns = useMemo<LangfuseColumnDef<BillingInvoiceRow>[]>(+ () => [+ createDateTableColumn<BillingInvoiceRow>({+ accessorKey: "created",+ header: "Date",+ size: 150,+ }),+ createBadgeTableColumn<BillingInvoiceRow>({+ accessorKey: "status",+ header: "Status",+ size: 100,+ range: "semantic",+ nullValue: "-",+ getBadge: (value) => {+ const status = value.toLowerCase();+ const variant = (() => {+ if (status === "paid") return "success";+ if (status === "open") return "warning";+ if (status === "uncollectible" || status === "void") return "error";+ return "unknown";+ })();+ return { value: status, variant };+ },+ }),+ ...(showBreakdownColumns+ ? [+ createNumberTableColumn<BillingInvoiceRow>({+ accessorFn: (row) =>+ (row.breakdown?.subscriptionCents ?? 0) / 100,+ id: "subscription",+ header: "Subscription",+ size: 110,+ formatter: costFormatter,+ }),+ createNumberTableColumn<BillingInvoiceRow>({+ accessorFn: (row) => (row.breakdown?.usageCents ?? 0) / 100,+ id: "usage",+ header: "Usage",+ size: 90,+ formatter: costFormatter,+ }),+ createNumberTableColumn<BillingInvoiceRow>({+ accessorFn: (row) => (row.breakdown?.discountCents ?? 0) / 100,+ id: "discounts",+ header: "Discounts",+ size: 100,+ formatter: costFormatter,+ }),+ createNumberTableColumn<BillingInvoiceRow>({+ accessorFn: (row) => (row.breakdown?.taxCents ?? 0) / 100,+ id: "tax",+ header: "Tax",+ size: 90,+ formatter: costFormatter,+ }),+ ]+ : []),+ createNumberTableColumn<BillingInvoiceRow>({+ accessorFn: (row) => (row.breakdown?.totalCents ?? 0) / 100,+ id: "total",+ header: "Total",+ size: 90,+ formatter: costFormatter,+ }),+ ],+ [showBreakdownColumns],);- const shouldShowTable = isCloudBillingAvailable && hasBillingIdentity;-- const [virtualTotal, setVirtualTotal] = useState(9999);- const [paginationState, setPaginationState] = useState<{- pageIndex: number;- pageSize: number;- startingAfter?: string;- endingBefore?: string;- }>({ pageIndex: 0, pageSize: 10 });-- const invoicesQuery = api.cloudBilling.getInvoices.useQuery(- {- orgId: organization?.id ?? "",- limit: paginationState.pageSize,- startingAfter: paginationState.startingAfter,- endingBefore: paginationState.endingBefore,- },- {- enabled: shouldShowTable,- retry: false,- },+ const actions = useCallback<+ NonNullable<TableProps<BillingInvoiceRow>["actions"]>+ >(+ (invoice) => [+ ...(invoice.hostedInvoiceUrl+ ? [+ {+ id: "view",+ type: "item" as const,+ title: "View",+ icon: ExternalLink,+ href: invoice.hostedInvoiceUrl,+ linkTarget: "_blank" as const,+ },+ ]+ : []),+ ...(invoice.invoicePdfUrl+ ? [+ {+ id: "pdf",+ type: "item" as const,+ title: "PDF",+ icon: Download,+ href: invoice.invoicePdfUrl,+ linkTarget: "_blank" as const,+ },+ ]+ : []),+ ],+ [],);- const isFirstPage =- !paginationState.startingAfter && !paginationState.endingBefore;- const hasMore = invoicesQuery.data?.hasMore ?? false;-- const rows = useMemo(() => {- const data = invoicesQuery.data?.invoices ?? [];- return data.map((i: any) => ({- id: i.id,- number: i.number,- status: i.status ?? null,- currency: i.currency,- created: i.created,- hostedInvoiceUrl: i.hostedInvoiceUrl,- invoicePdfUrl: i.invoicePdfUrl,- breakdown: i.breakdown,- }));- }, [invoicesQuery.data]);-- const data = useMemo(() => {- if (invoicesQuery.isPending) {- return { isLoading: true, isError: false } as const;- }- if (invoicesQuery.isError) {- // setting the error causes the table to remaining in loading state- // instead we just return an empty array- return {- isLoading: false,- isError: false,- data: [] as InvoiceRow[],- } as const;- }- return { isLoading: false, isError: false, data: rows } as const;- }, [rows, invoicesQuery.isPending, invoicesQuery.isError]);-- useEffect(() => {- if (isFirstPage) setVirtualTotal(9999);- }, [organization?.id, paginationState.pageSize, isFirstPage]);-- // When we fetch a page that reports hasMore === false, lock in the exact size- useEffect(() => {- if (!invoicesQuery.isFetching && !hasMore) {- const finalCount =- paginationState.pageIndex * paginationState.pageSize + rows.length;- setVirtualTotal(finalCount); // one-way only; stays stable afterwards- }- }, [- hasMore,- invoicesQuery.isFetching,- paginationState.pageIndex,- paginationState.pageSize,- rows.length,- ]);-- const columns: LangfuseColumnDef<InvoiceRow>[] = [- {- accessorKey: "created",- id: "created",- header: "Date",- cell: ({ row }) => {- const value = row.getValue("created") as InvoiceRow["created"];- if (!value) return undefined;- const date = new Date(value);- const year = date.getFullYear();- const month = date.toLocaleDateString("en-US", { month: "short" });- const day = String(date.getDate()).padStart(2, "0");- return `${year}-${month}-${day}`;- },- size: 90,- },- {- accessorKey: "status",- id: "status",- header: "Status",- size: 100,- cell: ({ row }) => {- const status = (row.getValue("status") as string | null)?.toLowerCase();- if (!status) return null;- const variant: NonNullable<BadgeProps["variant"]> =- status === "paid"- ? "secondary"- : status === "open"- ? "outline-solid"- : "default";- return <Badge variant={variant}>{status}</Badge>;- },- },- ...(showBreakdownColumns- ? [- createNumberTableColumn<InvoiceRow>({- accessorFn: (row) => (row.breakdown?.subscriptionCents ?? 0) / 100,- id: "subscription",- header: "Subscription",- size: 100,- formatter: costFormatter,- }),- createNumberTableColumn<InvoiceRow>({- accessorFn: (row) => (row.breakdown?.usageCents ?? 0) / 100,- id: "usage",- header: "Usage",- size: 90,- formatter: costFormatter,- }),- createNumberTableColumn<InvoiceRow>({- accessorFn: (row) => (row.breakdown?.discountCents ?? 0) / 100,- id: "discounts",- header: "Discounts",- size: 90,- formatter: costFormatter,- }),- createNumberTableColumn<InvoiceRow>({- accessorFn: (row) => (row.breakdown?.taxCents ?? 0) / 100,- id: "tax",- header: "Tax",- size: 90,- formatter: costFormatter,- }),- ]- : []),- createNumberTableColumn<InvoiceRow>({- accessorFn: (row) => (row.breakdown?.totalCents ?? 0) / 100,- id: "total",- header: "Total",- size: 90,- formatter: costFormatter,- }),- {- accessorKey: "actions",- id: "actions",- header: "Actions",- size: 160,- cell: ({ row }) => {- const { hostedInvoiceUrl, invoicePdfUrl } = row.original;- return (- <div className="flex gap-2">- {hostedInvoiceUrl ? (- <a href={hostedInvoiceUrl} target="_blank" rel="noreferrer">- <Button size="sm" variant="ghost">- <ExternalLink className="icon-base text-icon-foreground mr-1" />{" "}- View- </Button>- </a>- ) : null}- {invoicePdfUrl ? (- <a href={invoicePdfUrl} target="_blank" rel="noreferrer">- <Button size="sm" variant="ghost">- <Download className="icon-base text-icon-foreground mr-1" />{" "}- </Button>- </a>- ) : null}- </div>- );- },- },- ];-- // Helpers to derive cursors from the current page rows (exclude preview) as fallback- const firstNonPreviewId = rows.find((r) => r.id !== "preview")?.id;- const lastNonPreviewId = [...rows]- .reverse()- .find((r) => r.id !== "preview")?.id;-- // 3) Guard "Next" when already at the end to avoid useless queries + flicker- const onPaginationChange = (updater: any) => {- const next =- typeof updater === "function" ? updater(paginationState) : updater;-- // forward click but no more pages? ignore- if (next.pageIndex > paginationState.pageIndex && !hasMore) {- return;- }-- // ... your existing handler unchanged below- if (next.pageSize !== paginationState.pageSize) {- setPaginationState({- ...next,- startingAfter: undefined,- endingBefore: undefined,- });- return;- }- if (next.pageIndex === paginationState.pageIndex) return;-- const freshNext =- (invoicesQuery.data as any)?.cursors?.next ?? lastNonPreviewId;- const freshPrev =- (invoicesQuery.data as any)?.cursors?.prev ?? firstNonPreviewId;-- if (next.pageIndex === 0 && paginationState.pageIndex > 0) {- setPaginationState({- ...next,- startingAfter: undefined,- endingBefore: undefined,- });- } else if (next.pageIndex > paginationState.pageIndex) {- setPaginationState({- ...next,- startingAfter: freshNext,- endingBefore: undefined,- });- } else {- setPaginationState({- ...next,- startingAfter: undefined,- endingBefore: freshPrev,- });- }- };-- if (!shouldShowTable) {- // users on hobby plan who never had a subscription- return null;- }-return (- <div className="space-y-0">- <div className="flex items-center justify-between pt-4">- <h3 className="font-bold">Invoice History</h3>- </div>- <DataTableToolbar columns={columns} tableName="billing-invoices" />- <DataTable- tableName="invoices"+ <div className="space-y-2 pt-4">+ <h3 className="font-bold">Invoice History</h3>+ <SettingsTable+ tableName="billing-invoices"columns={columns}- data={data}- pagination={{- totalCount: virtualTotal,- hideTotalCount: true,- canJumpPages: false,- onChange: onPaginationChange,- state: {- pageIndex: paginationState.pageIndex,- pageSize: paginationState.pageSize,- },- options: [10, 20, 30, 40, 50],- }}+ actions={actions}+ pagination={pagination}+ noResultsMessage="No invoices found."+ {...tableProps}/></div>);diff --git a/web/src/ee/features/billing/components/BillingSettings.tsx b/web/src/ee/features/billing/components/BillingSettings.tsxindex 8123bd96c6..ddbfe85a36 100644--- a/web/src/ee/features/billing/components/BillingSettings.tsx+++ b/web/src/ee/features/billing/components/BillingSettings.tsx@@ -10,7 +10,7 @@ import { Alert } from "@/src/components/design-system/Alert/Alert";import { BillingUsageChart } from "./BillingUsageChart";import { BillingActionButtons } from "./BillingActionButtons";import { BillingScheduleNotification } from "./BillingScheduleNotification";-import { BillingInvoiceTable } from "./BillingInvoiceTable";+import { ConnectedBillingInvoiceTable } from "./ConnectedBillingInvoiceTable";import { BillingDiscountView } from "./BillingDiscountView";import { BillingPlanPeriodView } from "@/src/ee/features/billing/components/BillingPlanPeriodView";import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";@@ -111,7 +111,12 @@ export const BillingSettings = () => {/>)}<BillingActionButtons />- <BillingInvoiceTable />+ {isCloudBillingAvailable &&+ organization &&+ Boolean(+ organization.cloudConfig?.stripe?.customerId ??+ organization.cloudConfig?.clickhouse?.organizationId,+ ) && <ConnectedBillingInvoiceTable key={organization.id} />}{isSpendAlertEntitled && orgId && hasActiveSubscription && (<SpendAlertsSection orgId={orgId} />)}diff --git a/web/src/ee/features/billing/components/ConnectedBillingInvoiceTable.tsx b/web/src/ee/features/billing/components/ConnectedBillingInvoiceTable.tsxnew file mode 100644index 0000000000..d7609c7619--- /dev/null+++ b/web/src/ee/features/billing/components/ConnectedBillingInvoiceTable.tsx@@ -0,0 +1,100 @@+import { useRef, useState } from "react";++import { type PaginationBarState } from "@/src/components/design-system/PaginationBar/PaginationBar";+import { type AsyncTableData } from "@/src/components/design-system/table/Table";+import { api } from "@/src/utils/api";+import { useBillingInformation } from "./useBillingInformation";+import {+ BillingInvoiceTable,+ type BillingInvoiceRow,+} from "./BillingInvoiceTable";++export function ConnectedBillingInvoiceTable() {+ const { organization, billingProvider } = useBillingInformation();+ const [pagination, setPagination] = useState<+ PaginationBarState &+ (+ | { startingAfter?: never; endingBefore?: never }+ | { startingAfter: string; endingBefore?: never }+ | { startingAfter?: never; endingBefore: string }+ )+ >({ pageIndex: 0, pageSize: 10 });+ // Reuse the requests for visited pages even when the current request fails.+ const pageHistory = useRef([pagination]);++ const invoicesQuery = api.cloudBilling.getInvoices.useQuery(+ {+ orgId: organization?.id ?? "",+ limit: pagination.pageSize,+ startingAfter: pagination.startingAfter,+ endingBefore: pagination.endingBefore,+ },+ { enabled: Boolean(organization?.id), retry: false },+ );+ const rows = invoicesQuery.data?.invoices ?? [];+ const hasMore = invoicesQuery.data?.hasMore ?? false;+ const data = ((): AsyncTableData<BillingInvoiceRow[]> => {+ if (invoicesQuery.isPending) return { status: "loading" };+ if (invoicesQuery.isError) {+ return {+ status: "error",+ error: "Failed to load invoices. Please try again.",+ };+ }+ return {+ status: "success",+ data: rows.map((row) => ({ ...row, created: new Date(row.created) })),+ };+ })();++ const onPaginationChange = (next: PaginationBarState) => {+ if (next.pageSize !== pagination.pageSize || next.pageIndex === 0) {+ const firstPage = { pageIndex: 0, pageSize: next.pageSize };+ pageHistory.current = [firstPage];+ setPagination(firstPage);+ return;+ }+ if (next.pageIndex === pagination.pageIndex) return;+ if (next.pageIndex < pagination.pageIndex) {+ const previousPage = pageHistory.current[next.pageIndex];+ if (previousPage) setPagination(previousPage);+ return;+ }+ if (invoicesQuery.isFetching) return;+ const cursors = {+ next: undefined,+ prev: undefined,+ ...invoicesQuery.data?.cursors,+ };+ if (next.pageIndex > pagination.pageIndex) {+ if (!hasMore) return;+ // Preview invoices are not valid provider cursors.+ const startingAfter =+ cursors.next ?? rows.findLast((row) => row.id !== "preview")?.id;+ if (!startingAfter) return;+ const nextPage = {+ pageIndex: next.pageIndex,+ pageSize: next.pageSize,+ startingAfter,+ };+ pageHistory.current[next.pageIndex] = nextPage;+ setPagination(nextPage);+ return;+ }+ };++ return (+ <BillingInvoiceTable+ data={data}+ showBreakdownColumns={billingProvider !== "clickhouse"}+ loadingRowCount={pagination.pageSize}+ pagination={{+ mode: "cursor",+ state: pagination,+ onChange: onPaginationChange,+ hasNextPage: hasMore,+ isLoadingNextPage: invoicesQuery.isFetching,+ }}+ />+ );+}