codeburn/app/renderer/components/TopBar.tsx
ozymandiashh d1dcc82ee5 Wire drill-through into the desktop app: entries, chips, contributions, drawer, history
Every report grows real entries: Overview day bars, expensive sessions
(provider+id matched), model names, task categories, Spend projects,
and PR cards. The Sessions destination renders the selection chips with
per-chip removal and Clear, fetches the contributions report once per
population (widened to cover the selected days so a day outside the top-bar
period still reconciles), computes every chip combination client-side over
the full population, and shows each row's contribution beside its full
cost. Row clicks open the side drawer (Escape closes, focus returns); the
top bar gains Back/Forward (Cmd+[ / Cmd+] on macOS, Alt+arrows elsewhere)
over the in-app history; the position survives a renderer restart. IPC
gains getSessionsContributions with the same argv validation as the plain
report.
2026-09-12 12:22:19 -07:00

184 lines
6.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useEffect, useRef, useState, type ReactNode } from 'react'
import type { ClaudeConfigSelector, DateRange } from '../lib/types'
import { Dropdown } from './Dropdown'
import { ProviderPop, type ProviderOption } from './ProviderPop'
import { RangeCalendar } from './RangeCalendar'
import { SegTabs, type SegOption } from './SegTabs'
/** Sentinel option value: no --claude-config-source flag (aggregate all configs). */
const ALL_CONFIGS = ''
/** The real CLI period vocabulary (`codeburn ... --period`, src/cli-date.ts). */
export const PERIOD_OPTIONS: SegOption[] = [
{ value: 'today', label: 'Today' },
{ value: 'week', label: '7D' },
{ value: '30days', label: '30D' },
{ value: 'month', label: 'Month' },
{ value: 'all', label: '6M' },
{ value: 'lifetime', label: 'Life' },
]
/** The `.bar` top bar: back/forward history controls, title, scope caption,
* period SegTabs, provider ProviderPop. */
export function TopBar({
title,
canBack = false,
canForward = false,
onBack,
onForward,
scope,
period,
onPeriodChange,
customRange,
onRangeSelect,
provider,
providerLabel,
providerOptions,
onProviderSelect,
claudeConfigs,
configSource,
onConfigSelect,
}: {
title: ReactNode
/** In-app Back/Forward history (drill-through restores filters, sort,
* page depth, and the open drawer). Hidden when no handler is provided. */
canBack?: boolean
canForward?: boolean
onBack?: () => void
onForward?: () => void
scope?: ReactNode
period: string
onPeriodChange: (value: string) => void
customRange: DateRange | null
onRangeSelect: (range: DateRange) => void
provider: string
providerLabel: string
providerOptions: ProviderOption[]
onProviderSelect: (value: string) => void
claudeConfigs?: ClaudeConfigSelector
configSource: string | null
onConfigSelect: (id: string) => void
}) {
return (
<div className="bar">
{onBack && onForward && (
<div className="bar-nav" role="group" aria-label="Navigation history">
<button
type="button"
className="bar-nav-btn"
aria-label="Back"
title="Back"
disabled={!canBack}
onClick={() => { if (canBack) onBack() }}
>
‹
</button>
<button
type="button"
className="bar-nav-btn"
aria-label="Forward"
title="Forward"
disabled={!canForward}
onClick={() => { if (canForward) onForward() }}
>
›
</button>
</div>
)}
<div className="t">{title}</div>
{scope !== undefined && <span className="scope">{scope}</span>}
<div className="sp" />
<SegTabs options={PERIOD_OPTIONS} value={customRange ? '' : period} onChange={onPeriodChange} />
<CalendarPop value={customRange} onSelect={onRangeSelect} />
<ProviderPop value={provider} label={providerLabel} options={providerOptions} onSelect={onProviderSelect} />
{claudeConfigs && <ConfigPicker configs={claudeConfigs} value={configSource} onSelect={onConfigSelect} />}
</div>
)
}
/** Claude config source switcher. Only getOverview honors the selection, so the
* footer names the limit; the active label is also echoed in the scope line. */
function ConfigPicker({ configs, value, onSelect }: { configs: ClaudeConfigSelector; value: string | null; onSelect: (id: string) => void }) {
const options = [
{ value: ALL_CONFIGS, label: 'All Claude configs' },
...configs.options.map(option => ({ value: option.id, label: option.label })),
]
return (
<Dropdown
id="claude-config-select"
ariaLabel="Claude config source"
value={value ?? ALL_CONFIGS}
options={options}
onChange={onSelect}
width={168}
footer="Applies to the overview data. Manage config folders with the codeburn CLI."
/>
)
}
function formatRange(range: DateRange): string {
const from = new Date(`${range.from}T12:00:00`)
const to = new Date(`${range.to}T12:00:00`)
const sameYear = from.getFullYear() === to.getFullYear()
const sameMonth = sameYear && from.getMonth() === to.getMonth()
const left = from.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: sameYear ? undefined : 'numeric' })
const right = to.toLocaleDateString('en-US', { month: sameMonth ? undefined : 'short', day: 'numeric', year: sameYear ? undefined : 'numeric' })
return `${left} – ${right}`
}
export function rangeLabel(range: DateRange): string {
return formatRange(range)
}
function CalendarPop({ value, onSelect }: { value: DateRange | null; onSelect: (range: DateRange) => void }) {
const [open, setOpen] = useState(false)
const wrapRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (!open) return
const onPointerDown = (event: MouseEvent) => {
if (!wrapRef.current?.contains(event.target as Node)) setOpen(false)
}
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') setOpen(false)
}
document.addEventListener('mousedown', onPointerDown)
document.addEventListener('keydown', onKeyDown)
return () => {
document.removeEventListener('mousedown', onPointerDown)
document.removeEventListener('keydown', onKeyDown)
}
}, [open])
const label = value ? formatRange(value) : 'Choose date range'
return (
<div className="calendar-wrap" ref={wrapRef}>
<button
type="button"
className={`calendar-trigger${value ? ' on' : ''}`}
aria-label={label}
aria-haspopup="dialog"
aria-expanded={open}
onClick={() => setOpen(current => !current)}
>
<svg viewBox="0 0 16 16" aria-hidden="true">
<rect x="2.25" y="3.25" width="11.5" height="10.5" rx="1.5" />
<path d="M5 1.75v3M11 1.75v3M2.5 6.25h11" />
</svg>
{value && <span>{label}</span>}
</button>
{open && (
<div className="calendar-popover" role="dialog" aria-label="Choose date range">
<RangeCalendar
value={value}
onSelect={range => {
onSelect(range)
setOpen(false)
}}
/>
</div>
)}
</div>
)
}