Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(usage-log-panel): handle empty usage log data gracefully

Steven Enamakel1a5c3ed4ed37
diff --git a/app/src/components/dashboard/UsageLogPanel.tsx b/app/src/components/dashboard/UsageLogPanel.tsx
index 337a86d2ae..6cff42e483 100644
--- a/app/src/components/dashboard/UsageLogPanel.tsx
+++ b/app/src/components/dashboard/UsageLogPanel.tsx
@@ -1,12 +1,31 @@
+import { RefreshCw } from 'lucide-react';
import { useMemo, useState } from 'react';
import { type CostUsageRecord, useCostUsageLog } from '../../hooks/useCostDashboard';
import { useT } from '../../lib/i18n/I18nContext';
-import { SettingsStatusLine } from '../settings/controls';
-import { Button, Card } from '../ui';
+import { Badge, Button, EmptyState, NativeSelect, StatusLine } from '../ui';
+import DataTable, { type DataTableColumn } from '../ui/DataTable';
import { formatCurrency, formatTokens } from './formatCurrency';
const ALL = '';
+const PERIODS = [1, 7, 30, 90, 365];
+
+/**
+ * Two-line timestamp — date over time — in the user's locale, matching the
+ * Approval history table; an unparseable value is shown raw.
+ */
+const DateTimeCell = ({ value }: { value: string }) => {
+ const date = new Date(value);
+ if (Number.isNaN(date.getTime())) return <>{value}</>;
+ return (
+ <span className="flex flex-col leading-tight" title={date.toLocaleString()}>
+ <span className="text-content">{date.toLocaleDateString()}</span>
+ <span className="text-xs text-content-muted">
+ {date.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' })}
+ </span>
+ </span>
+ );
+};
/** Detailed local usage records. Filters apply to the fetched, bounded window. */
const UsageLogPanel = () => {
@@ -39,184 +58,208 @@ const UsageLogPanel = () => {
);
}, [data, category, provider, source, query]);
const filteredCost = records.reduce((sum, record) => sum + record.cost_usd, 0);
+ const currency = data?.currency ?? 'USD';
+
+ const columns: DataTableColumn<CostUsageRecord>[] = [
+ {
+ id: 'when',
+ header: t('settings.costDashboard.when'),
+ className: 'w-px whitespace-nowrap tabular-nums',
+ cell: record => <DateTimeCell value={record.timestamp} />,
+ },
+ {
+ id: 'model',
+ header: t('settings.costDashboard.model'),
+ // `w-full max-w-0` is what lets a table cell truncate instead of growing.
+ className: 'w-full max-w-0',
+ cell: record => (
+ <div className="min-w-0">
+ <p className="truncate font-medium text-content" title={record.model}>
+ {record.model}
+ </p>
+ <p className="truncate text-xs text-content-muted">
+ {record.provider ?? t('settings.costDashboard.unknownProvider')}
+ </p>
+ </div>
+ ),
+ },
+ {
+ id: 'category',
+ header: t('settings.costDashboard.category'),
+ className: 'w-px whitespace-nowrap',
+ cell: record => <Badge>{record.category}</Badge>,
+ },
+ {
+ id: 'input',
+ header: t('settings.costDashboard.inputTokens'),
+ align: 'right',
+ className: 'w-px whitespace-nowrap tabular-nums text-content-secondary',
+ cell: record => formatTokens(record.input_tokens),
+ },
+ {
+ id: 'output',
+ header: t('settings.costDashboard.outputTokens'),
+ align: 'right',
+ className: 'w-px whitespace-nowrap tabular-nums text-content-secondary',
+ cell: record => formatTokens(record.output_tokens),
+ },
+ {
+ id: 'cost',
+ header: t('settings.costDashboard.cost'),
+ align: 'right',
+ className: 'w-px whitespace-nowrap tabular-nums font-medium text-content',
+ cell: record => formatCurrency(record.cost_usd, currency),
+ },
+ {
+ id: 'source',
+ header: t('settings.costDashboard.costSource'),
+ className: 'w-px whitespace-nowrap',
+ cell: record =>
+ record.cost_source === 'provider_charged' ? (
+ <Badge variant="success">{t('settings.costDashboard.providerCharged')}</Badge>
+ ) : (
+ <Badge>{t('settings.costDashboard.estimated')}</Badge>
+ ),
+ },
+ {
+ id: 'session',
+ header: t('settings.costDashboard.session'),
+ className: 'w-px whitespace-nowrap font-mono text-xs text-content-muted',
+ cell: record => <span title={record.session_id}>{record.session_id.slice(0, 8)}</span>,
+ },
+ ];
return (
- <div className="p-4 space-y-4" data-testid="usage-log-panel">
- <div className="flex items-start justify-between gap-3">
- <p className="text-xs text-content-muted max-w-prose">
+ <div className="space-y-3" data-testid="usage-log-panel">
+ <div className="flex flex-wrap items-center justify-between gap-x-4 gap-y-1">
+ <p className="max-w-prose text-xs text-content-muted">
{t('settings.costDashboard.usageLogHint')
.replace('{days}', String(days))
.replace('{limit}', '1000')}
</p>
- <Button
- type="button"
- variant="secondary"
- size="xs"
- onClick={() => void refetch()}
- disabled={isFetching}>
- {t('settings.costDashboard.refresh')}
- </Button>
+ {data && (
+ <span className="text-xs tabular-nums text-content-muted">
+ {t('settings.costDashboard.filteredTotal')
+ .replace('{shown}', String(records.length))
+ .replace('{loaded}', String(data.records.length))
+ .replace('{cost}', formatCurrency(filteredCost, data.currency))}
+ </span>
+ )}
</div>
- {error && <SettingsStatusLine saving={false} error={error} savingLabel="" />}
- <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-5">
- <Filter label={t('settings.costDashboard.period')}>
- <select
- value={days}
- onChange={event => setDays(Number(event.target.value))}
- className="w-full rounded-lg border border-line bg-surface px-2 py-1.5 text-xs">
- {[1, 7, 30, 90, 365].map(value => (
- <option key={value} value={value}>
- {t('settings.costDashboard.periodDays').replace('{days}', String(value))}
- </option>
- ))}
- </select>
- </Filter>
- <Filter label={t('settings.costDashboard.category')}>
- <select
- value={category}
- onChange={event => setCategory(event.target.value)}
- className="w-full rounded-lg border border-line bg-surface px-2 py-1.5 text-xs">
- <option value="">{t('settings.costDashboard.all')}</option>
- {categories.map(value => (
- <option key={value} value={value}>
- {value}
- </option>
- ))}
- </select>
- </Filter>
- <Filter label={t('settings.costDashboard.provider')}>
- <select
- value={provider}
- onChange={event => setProvider(event.target.value)}
- className="w-full rounded-lg border border-line bg-surface px-2 py-1.5 text-xs">
- <option value="">{t('settings.costDashboard.all')}</option>
- {providers.filter(Boolean).map(value => (
- <option key={value} value={value}>
- {value}
+
+ <DataTable<CostUsageRecord>
+ columns={columns}
+ rows={records}
+ rowKey={record => record.id}
+ ariaLabel={t('settings.costDashboard.usageLog')}
+ toolbarStart={
+ <>
+ <FilterSelect
+ label={t('settings.costDashboard.period')}
+ value={String(days)}
+ onChange={value => setDays(Number(value))}>
+ {PERIODS.map(value => (
+ <option key={value} value={value}>
+ {t('settings.costDashboard.periodDays').replace('{days}', String(value))}
+ </option>
+ ))}
+ </FilterSelect>
+ <FilterSelect
+ label={t('settings.costDashboard.category')}
+ value={category}
+ onChange={setCategory}>
+ <option value="">{t('settings.costDashboard.all')}</option>
+ {categories.map(value => (
+ <option key={value} value={value}>
+ {value}
+ </option>
+ ))}
+ </FilterSelect>
+ <FilterSelect
+ label={t('settings.costDashboard.provider')}
+ value={provider}
+ onChange={setProvider}>
+ <option value="">{t('settings.costDashboard.all')}</option>
+ {providers.filter(Boolean).map(value => (
+ <option key={value} value={value}>
+ {value}
+ </option>
+ ))}
+ </FilterSelect>
+ <FilterSelect
+ label={t('settings.costDashboard.costSource')}
+ value={source}
+ onChange={setSource}>
+ <option value="">{t('settings.costDashboard.all')}</option>
+ <option value="estimated">{t('settings.costDashboard.estimated')}</option>
+ <option value="provider_charged">
+ {t('settings.costDashboard.providerCharged')}
</option>
- ))}
- </select>
- </Filter>
- <Filter label={t('settings.costDashboard.costSource')}>
- <select
- value={source}
- onChange={event => setSource(event.target.value)}
- className="w-full rounded-lg border border-line bg-surface px-2 py-1.5 text-xs">
- <option value="">{t('settings.costDashboard.all')}</option>
- <option value="estimated">{t('settings.costDashboard.estimated')}</option>
- <option value="provider_charged">{t('settings.costDashboard.providerCharged')}</option>
- </select>
- </Filter>
- <Filter label={t('settings.costDashboard.searchModelSession')}>
- <input
- value={query}
- onChange={event => setQuery(event.target.value)}
- className="w-full rounded-lg border border-line bg-surface px-2 py-1.5 text-xs"
- />
- </Filter>
- </div>
- <Card
- padded
- divided={false}
- className="bg-surface/40"
- title={t('settings.costDashboard.usageLog')}
- headerRight={
- data && (
- <span className="text-[11px] text-content-muted">
- {t('settings.costDashboard.filteredTotal')
- .replace('{shown}', String(records.length))
- .replace('{loaded}', String(data.records.length))
- .replace('{cost}', formatCurrency(filteredCost, data.currency))}
- </span>
- )
- }>
- {data ? (
- <UsageLogTable records={records} currency={data.currency} />
- ) : isLoading ? (
- <div className="text-xs text-content-muted">{t('settings.costDashboard.loading')}</div>
- ) : null}
- </Card>
+ </FilterSelect>
+ </>
+ }
+ search={{
+ value: query,
+ onChange: setQuery,
+ placeholder: t('settings.costDashboard.searchModelSession'),
+ ariaLabel: t('settings.costDashboard.searchModelSession'),
+ }}
+ toolbarEnd={
+ <Button
+ type="button"
+ variant="secondary"
+ size="sm"
+ leadingIcon={
+ <RefreshCw
+ className={`h-3.5 w-3.5 ${isFetching ? 'animate-spin' : ''}`}
+ aria-hidden
+ />
+ }
+ onClick={() => void refetch()}
+ disabled={isFetching}>
+ {t('settings.costDashboard.refresh')}
+ </Button>
+ }
+ loading={!data && isLoading}
+ loadingLabel={t('settings.costDashboard.loading')}
+ error={error ? <StatusLine saving={false} error={error} savingLabel="" /> : undefined}
+ empty={
+ data ? (
+ <div className="rounded-xl border border-dashed border-line py-6 text-center">
+ <EmptyState className="p-0" label={t('settings.costDashboard.noUsageLog')} />
+ </div>
+ ) : undefined
+ }
+ />
</div>
);
};
-const Filter = ({ label, children }: { label: string; children: React.ReactNode }) => (
- <label className="space-y-1 text-xs text-content-secondary">
+/**
+ * A compact labelled select for the table toolbar. The label is visually
+ * hidden (the option text says what is selected) but stays the accessible name.
+ */
+const FilterSelect = ({
+ label,
+ value,
+ onChange,
+ children,
+}: {
+ label: string;
+ value: string;
+ onChange: (value: string) => void;
+ children: React.ReactNode;
+}) => (
+ <label className="flex shrink-0 items-center gap-1.5 text-xs text-content-muted">
<span>{label}</span>
- {children}
+ <NativeSelect
+ inputSize="sm"
+ value={value}
+ onChange={event => onChange(event.target.value)}
+ className="text-xs">
+ {children}
+ </NativeSelect>
</label>
);
-const UsageLogTable = ({ records, currency }: { records: CostUsageRecord[]; currency: string }) => {
- const { t } = useT();
- if (records.length === 0)
- return (
- <div className="text-xs text-content-muted italic py-2">
- {t('settings.costDashboard.noUsageLog')}
- </div>
- );
- return (
- <div className="overflow-x-auto -mx-1">
- <table className="w-full min-w-[850px] text-xs">
- <thead>
- <tr className="border-b border-line text-left text-[10px] uppercase tracking-wide text-content-muted">
- <th className="px-2 py-2">{t('settings.costDashboard.when')}</th>
- <th className="px-2 py-2">{t('settings.costDashboard.category')}</th>
- <th className="px-2 py-2">{t('settings.costDashboard.model')}</th>
- <th className="px-2 py-2 text-right">{t('settings.costDashboard.inputTokens')}</th>
- <th className="px-2 py-2 text-right">{t('settings.costDashboard.outputTokens')}</th>
- <th className="px-2 py-2 text-right">{t('settings.costDashboard.cost')}</th>
- <th className="px-2 py-2">{t('settings.costDashboard.costSource')}</th>
- <th className="px-2 py-2">{t('settings.costDashboard.session')}</th>
- </tr>
- </thead>
- <tbody>
- {records.map(record => (
- <tr
- key={record.id}
- className="border-b border-line-subtle last:border-0 hover:bg-surface-muted/60">
- <td className="px-2 py-2 whitespace-nowrap">{formatDateTime(record.timestamp)}</td>
- <td className="px-2 py-2">{record.category}</td>
- <td className="px-2 py-2">
- <div
- className="max-w-[16rem] truncate font-medium text-content"
- title={record.model}>
- {record.model}
- </div>
- <div className="text-content-muted">
- {record.provider ?? t('settings.costDashboard.unknownProvider')}
- </div>
- </td>
- <td className="px-2 py-2 text-right">{formatTokens(record.input_tokens)}</td>
- <td className="px-2 py-2 text-right">{formatTokens(record.output_tokens)}</td>
- <td className="px-2 py-2 text-right font-semibold">
- {formatCurrency(record.cost_usd, currency)}
- </td>
- <td className="px-2 py-2">
- {record.cost_source === 'provider_charged'
- ? t('settings.costDashboard.providerCharged')
- : t('settings.costDashboard.estimated')}
- </td>
- <td className="px-2 py-2 font-mono text-content-muted" title={record.session_id}>
- {record.session_id.slice(0, 8)}
- </td>
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- );
-};
-
-function formatDateTime(value: string): string {
- const date = new Date(value);
- if (Number.isNaN(date.getTime())) return value;
- return new Intl.DateTimeFormat(undefined, {
- month: 'short',
- day: 'numeric',
- hour: 'numeric',
- minute: '2-digit',
- }).format(date);
-}
-
export default UsageLogPanel;