import {localeTag} from '../lib/i18n'; import { tr } from '../lib/i18n'; import { useMemo, useState } from 'react'; import { Btn, Icon } from './ui'; import { Brand } from './Brand'; import { useNative } from '../lib/native'; import { go, useStore } from '../lib/store'; import { tokens } from '../lib/format'; import { calendarDays, usageDashboard, type UsagePeriod } from '../lib/usage-dashboard'; const colors = ['#4d82d8', '#6998e6', '#8aaeea', '#a6c1ee', '#c4d7f4', '#dce7f8']; export function UsageDashboard({ loaded }: { loaded: boolean; }) { const native = useNative(), remoteModel = useStore(s => s.remoteModel); const canChat = loaded || !!remoteModel || !!native?.instances.some(i => i.id === native.defaultId && i.status === 'ready'); const sessions = useStore(s => s.sessions), library = useStore(s => s.library); const [tab, setTab] = useState<'overview' | 'models'>('overview'), [period, setPeriod] = useState('all'), [expanded, setExpanded] = useState(false); const now = new Date(); const usage = useMemo(() => usageDashboard(sessions, period), [sessions, period]); const label = (id: string | null) => library?.variants.find(v => v.id === id)?.family ?? id ?? '—'; const start = new Date(now); start.setHours(0, 0, 0, 0); const earliest = [...usage.days.keys()].sort()[0]; const earliestDate = earliest ? new Date(`${earliest}T00:00:00`) : null; const span = period === '7d' ? 7 : period === '30d' ? 30 : earliestDate ? (Date.UTC(start.getFullYear(), start.getMonth(), start.getDate()) - Date.UTC(earliestDate.getFullYear(), earliestDate.getMonth(), earliestDate.getDate())) / 86400000 + 1 : 91; start.setDate(start.getDate() - span + 1); const chartDays = calendarDays(start, now); const heatStart = new Date(start); if (span > 365) heatStart.setDate(heatStart.getDate() + (span - 365)); if (span < 182) heatStart.setDate(heatStart.getDate() - (182 - span)); heatStart.setDate(heatStart.getDate() - heatStart.getDay()); const heatDays = calendarDays(heatStart, now); while (heatDays.length % 7) heatDays.push(''); const maxMessages = Math.max(1, ...[...usage.days.values()].map(d => d.messages)); // Up to 90 columns. Longer ranges are grouped into consecutive date bins. const binSize = Math.max(1, Math.ceil(chartDays.length / 90)); const bins = Array.from({ length: Math.ceil(chartDays.length / binSize) }, (_, i) => { const dates = chartDays.slice(i * binSize, (i + 1) * binSize), amounts: Record = {}; for (const date of dates) for (const [id, n] of Object.entries(usage.days.get(date)?.models ?? {})) amounts[id] = (amounts[id] ?? 0) + n; return { dates, amounts, total: Object.values(amounts).reduce((n, v) => n + v, 0) }; }); const maxTokens = Math.max(1, ...bins.map(b => b.total)); const dateLabel = (key: string) => new Date(`${key}T00:00:00`).toLocaleDateString(localeTag(), { month: 'short', day: 'numeric' }); const peak = usage.peakHour === null ? '—' : new Date(2026, 0, 1, usage.peakHour).toLocaleTimeString(localeTag(), { hour: 'numeric', minute: '2-digit' }); const firstChat = !sessions.some(s => !s.showcase && s.messages.some(m => m.role === 'user' && !m.example && !m.timeSkipId)); if (firstChat && canChat) return

{tr("Hello, I\u2019m Strata.")}

{tr("Say hello, or jump straight in.")}

; return

{tr("What do we start with?")}

{tr((['overview', 'models'] as const).map(v => ))}
{tr((['all', '30d', '7d'] as const).map(v => ))}
{tr(tab === 'overview' ? <>
{tr([{ label: 'Sessions', value: usage.sessions.toLocaleString(localeTag()) }, { label: 'Messages', value: usage.messages.toLocaleString(localeTag()) }, { label: 'Total tokens', value: tokens(usage.total) }, { label: 'Active days', value: usage.activeDays.toLocaleString(localeTag()) }, { label: 'Peak hour', value: peak }, { label: 'Favorite model', value: label(usage.favorite) }].map(stat =>
{tr(stat.label)}
{tr(stat.value)}
))}
{tr(heatDays.map((date, i) => { const n = usage.days.get(date)?.messages ?? 0; return
{tr(dateLabel(heatDays[0]))} – {tr(dateLabel(chartDays.at(-1)!))}{tr("Less ")}{tr(" More")}
: <> {tr((usage.total ? [0, .5, 1] : [0]).map(f => {tr(tokens(Math.round(maxTokens * f)))}))} {tr(bins.map((bin, i) => { let y = 175; const width = 638 / bins.length; return {tr(dateLabel(bin.dates[0]))}{tr(bin.dates.length > 1 ? ` – ${dateLabel(bin.dates.at(-1)!)}` : '')}: {tr(bin.total.toLocaleString(localeTag()))}{tr(" tokens")}{tr(usage.models.map((model, m) => { const h = (bin.amounts[model.id] ?? 0) / maxTokens * 145; y -= h; return h ? : null; }))}; }))} {tr([0, Math.floor((bins.length - 1) / 2), bins.length - 1].filter((n, i, a) => a.indexOf(n) === i).map(i => {tr(dateLabel(bins[i].dates[0]))}))} {tr(usage.models.length ?
{tr(usage.models.slice(0, expanded ? undefined : 6).map((model, i) =>
{tr(label(model.id))}{tr(library?.variants.find(v => v.id === model.id)?.quant)}{tr(tokens(model.input))}{tr(" in \u00B7 ")}{tr(tokens(model.output))}{tr(" out")}{tr(((model.input + model.output) / Math.max(1, usage.total) * 100).toFixed(1))}%
))}{tr(usage.models.length > 6 ? : null)}
:

{tr("No reported model usage in this period.")}

)} )}
{tr(!canChat ? go('models')}>{tr("Choose a model")} : null)}
; }