{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"report-header","type":"registry:block","title":"Report header","description":"A report's title with its live status, a period switcher, a date range, compare and export.","dependencies":["lucide-react"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/badge.json","https://dashral.com/r/breadcrumb.json","https://dashral.com/r/button.json","https://dashral.com/r/calendar.json","https://dashral.com/r/date-range-picker.json","https://dashral.com/r/dropdown-menu.json","https://dashral.com/r/segmented-control.json"],"files":[{"path":"registry/dashral/blocks/report-header.tsx","type":"registry:file","target":"components/dashral/blocks/report-header.tsx","content":"\"use client\"\n\nimport { useEffect, useState } from \"react\"\nimport { ChevronDown, Download, FileSpreadsheet, FileText, GitCompareArrows, Plus, Share2, X } from \"lucide-react\"\nimport { Badge } from \"../badge\"\nimport { Breadcrumb } from \"../breadcrumb\"\nimport { Button } from \"../button\"\nimport type { DateRange } from \"../calendar\"\nimport { DateRangePicker } from \"../date-range-picker\"\nimport { DropdownMenu } from \"../dropdown-menu\"\nimport { SegmentedControl } from \"../segmented-control\"\nimport { cn } from \"../lib/utils\"\n\nconst periods = { \"7d\": 7, \"30d\": 30, \"90d\": 90, \"12m\": 365 } as const\nexport type ReportPeriod = keyof typeof periods\ntype Period = ReportPeriod\n\n/** The last `days` days, ending today. */\nfunction lastDays(days: number): DateRange {\n  const to = new Date()\n  const from = new Date(to)\n  from.setDate(to.getDate() - (days - 1))\n  return { from, to }\n}\n\n/**\n * A report's header: where it is, its title with a live status, and its controls — a period switcher that sets the\n * date range (or pick any range, and the switcher lets go), compare with the previous period, export and share. The\n * filters underneath can be removed one by one. Every part comes from props, and each control tells your handler.\n */\nexport function ReportHeader({\n  breadcrumb = [{ label: \"Reports\", href: \"#\" }, { label: \"Sales\", href: \"#\" }, { label: \"Revenue\" }],\n  title = \"Revenue\",\n  live = true,\n  description = \"Updated 2 minutes ago · Net of refunds, in USD\",\n  defaultPeriod = \"30d\",\n  onRangeChange,\n  compare: compareProp,\n  onCompareChange,\n  exports = [\n    { id: \"csv\", label: \"CSV\", icon: <FileText /> },\n    { id: \"excel\", label: \"Excel\", icon: <FileSpreadsheet /> },\n    { id: \"pdf\", label: \"PDF report\", icon: <FileText /> },\n  ],\n  onExport,\n  onShare,\n  filters: filtersProp,\n  onFiltersChange,\n  onAddFilter,\n}: {\n  breadcrumb?: { label: string; href?: string }[]\n  title?: React.ReactNode\n  /** The Live badge beside the title. */\n  live?: boolean\n  description?: React.ReactNode\n  defaultPeriod?: ReportPeriod\n  /** The dates picked, from the switcher (with its period) or the picker (with none). */\n  onRangeChange?: (range: DateRange | undefined, period: ReportPeriod | null) => void\n  /** Compare with the previous period (the header keeps it without it). */\n  compare?: boolean\n  onCompareChange?: (compare: boolean) => void\n  /** The export menu's formats; none hides the button. */\n  exports?: { id: string; label: string; icon?: React.ReactNode }[]\n  onExport?: (id: string) => void\n  onShare?: () => void\n  /** The filters under the title (kept by the header without it); removing one tells onFiltersChange. */\n  filters?: string[]\n  onFiltersChange?: (filters: string[]) => void\n  onAddFilter?: () => void\n}) {\n  const [period, setPeriod] = useState<Period | \"\">(defaultPeriod)\n  // Today's dates are worked out in the browser, after the first render: a page rendered ahead of time (a static build,\n  // a cache) would otherwise show the day it was built and fail to hydrate.\n  const [range, setRange] = useState<DateRange | undefined>()\n  useEffect(() => setRange((current) => current ?? lastDays(periods[defaultPeriod])), [defaultPeriod])\n  const [ownCompare, setOwnCompare] = useState(true)\n  const compare = compareProp ?? ownCompare\n  const setCompare = (next: boolean) => {\n    setOwnCompare(next)\n    onCompareChange?.(next)\n  }\n  const [ownFilters, setOwnFilters] = useState([\"Store: All stores\", \"Channel: Online\", \"Country: Indonesia\"])\n  const filters = filtersProp ?? ownFilters\n  const setFilters = (next: string[]) => {\n    setOwnFilters(next)\n    onFiltersChange?.(next)\n  }\n\n  return (\n    <header className=\"border-b bg-background\">\n      <div className=\"space-y-5 px-4 pt-5 pb-4 sm:px-6\">\n        {breadcrumb.length > 0 && <Breadcrumb items={breadcrumb} />}\n\n        <div className=\"flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between\">\n          <div className=\"min-w-0\">\n            <div className=\"flex items-center gap-3\">\n              <h1 className=\"text-2xl font-semibold tracking-tight\">{title}</h1>\n              {live && (\n                <Badge variant=\"soft\" color=\"success\" className=\"gap-1.5 rounded-[calc(var(--radius)*11/14)]\">\n                  <span aria-hidden className=\"relative flex size-1.5\">\n                    <span className=\"absolute inline-flex size-full animate-ping rounded-full bg-success opacity-60 motion-reduce:animate-none\" />\n                    <span className=\"relative inline-flex size-1.5 rounded-full bg-success\" />\n                  </span>\n                  Live\n                </Badge>\n              )}\n            </div>\n            {description && <p className=\"mt-1 text-sm text-muted-foreground\">{description}</p>}\n          </div>\n\n          <div className=\"flex flex-wrap items-center gap-2\">\n            <SegmentedControl\n              aria-label=\"Period\"\n              value={period}\n              onValueChange={(value) => {\n                const next = lastDays(periods[value as Period])\n                setPeriod(value as Period)\n                setRange(next)\n                onRangeChange?.(next, value as Period)\n              }}\n              options={[\n                { value: \"7d\", label: \"7D\" },\n                { value: \"30d\", label: \"30D\" },\n                { value: \"90d\", label: \"90D\" },\n                { value: \"12m\", label: \"12M\" },\n              ]}\n            />\n            <DateRangePicker\n              aria-label=\"Date range\"\n              value={range}\n              onValueChange={(next) => {\n                setRange(next)\n                setPeriod(\"\")\n                onRangeChange?.(next, null)\n              }}\n              className=\"w-auto\"\n            />\n            <Button variant=\"outline\" aria-pressed={compare} onClick={() => setCompare(!compare)} className={cn(compare && \"border-primary/40 bg-primary/8 text-primary-text hover:bg-primary/12\")}>\n              <GitCompareArrows aria-hidden /> Compare\n            </Button>\n            {exports.length > 0 && (\n              <DropdownMenu\n                align=\"end\"\n                className=\"w-48\"\n                trigger={\n                  <Button variant=\"outline\">\n                    <Download aria-hidden /> Export <ChevronDown className=\"text-muted-foreground\" aria-hidden />\n                  </Button>\n                }\n                items={exports.map((format) => ({ label: format.label, icon: format.icon, onSelect: () => onExport?.(format.id) }))}\n              />\n            )}\n            <Button onClick={onShare}>\n              <Share2 aria-hidden /> Share\n            </Button>\n          </div>\n        </div>\n\n        <div className=\"flex flex-wrap items-center gap-2\">\n          {filters.map((filter) => (\n            <span key={filter} className=\"inline-flex h-7 items-center gap-1 rounded-(--radius) border bg-card ps-3 pe-1 text-xs\">\n              {filter}\n              <button\n                type=\"button\"\n                aria-label={`Remove ${filter}`}\n                onClick={() => setFilters(filters.filter((f) => f !== filter))}\n                className=\"grid size-5 place-items-center rounded-[max(0px,calc(var(--radius)_-_5px))] text-muted-foreground transition-colors outline-none hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50\"\n              >\n                <X className=\"size-3\" aria-hidden />\n              </button>\n            </span>\n          ))}\n          <Button variant=\"ghost\" size=\"sm\" className=\"h-7 rounded-(--radius) text-xs text-muted-foreground\" onClick={onAddFilter}>\n            <Plus aria-hidden /> Add filter\n          </Button>\n          {compare && <span className=\"ms-auto hidden text-xs text-muted-foreground sm:inline\">Compared with the previous period</span>}\n        </div>\n      </div>\n    </header>\n  )\n}\n"}],"cssVars":{"theme":{"color-primary-text":"var(--primary-text, var(--primary))","color-success":"var(--success)","color-success-foreground":"var(--success-foreground)","color-success-text":"var(--success-text)"},"light":{"success":"oklch(0.508 0.118 165.612)","success-foreground":"oklch(0.985 0 0)","success-text":"oklch(0.432 0.095 166.913)"},"dark":{"success":"oklch(0.696 0.17 162.48)","success-foreground":"oklch(0.2 0.04 160)","success-text":"oklch(0.765 0.177 163.223)"}}}