{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kpi-provenance",
  "title": "KPI Provenance",
  "description": "A button beside a number that shows how it was computed: what’s counted, the date range, the filters, the query’s name and a link to the rows.",
  "registryDependencies": [
    "button",
    "popover",
    "https://www.dashboardblocks.com/r/radix/link.json",
    "https://www.dashboardblocks.com/r/radix/format.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/kpi-provenance.tsx",
      "content": "'use client'\n\nimport {\n  formatProvenanceFilters,\n  formatProvenanceWindow,\n} from '@/registry/components/dashboardblocks/kpi-provenance-format'\nimport { Link } from '@/registry/components/dashboardblocks/link'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { useId, useRef, useState } from 'react'\n\nimport { Button } from '@/components/ui/button'\nimport {\n  Popover,\n  PopoverContent,\n  PopoverDescription,\n  PopoverHeader,\n  PopoverTitle,\n  PopoverTrigger,\n} from '@/components/ui/popover'\n\nimport { cn } from '@/lib/utils'\n\ninterface Provenance {\n  query: string\n  description: string\n  /** The time range counted, shown as dates in the reader's time zone with `to` as the last day counted, or null for all time. */\n  window: { from: Date; to: Date } | null\n  filters: Record<string, string>\n  href: string\n}\n\ninterface KPIProvenanceProps {\n  className?: string\n  /** The button's name and the popover's title, 'How is this computed?' by default. */\n  label?: string\n  provenance: Provenance\n}\n\nfunction KPIProvenance({\n  className,\n  label = 'How is this computed?',\n  provenance,\n}: KPIProvenanceProps) {\n  const [open, setOpen] = useState(false)\n  const popupRef = useRef<HTMLDivElement>(null)\n  const titleId = useId()\n  const descriptionId = useId()\n  return (\n    <span className={cn('inline-flex h-lh items-center align-top', className)}>\n      <Popover open={open} onOpenChange={setOpen}>\n        <PopoverTrigger asChild>\n          <Button variant='ghost' size='icon-sm' aria-label={label}>\n            <IconPlaceholder\n              lucide='CircleHelpIcon'\n              tabler='IconHelp'\n              hugeicons='HelpCircleIcon'\n              phosphor='QuestionIcon'\n              remixicon='RiQuestionLine'\n            />\n          </Button>\n        </PopoverTrigger>\n        <PopoverContent\n          ref={popupRef}\n          onOpenAutoFocus={(event) => {\n            event.preventDefault()\n            popupRef.current?.focus()\n          }}\n          aria-labelledby={titleId}\n          aria-describedby={descriptionId}\n          align='start'\n          className='max-w-[calc(100vw-2rem)]'\n        >\n          <PopoverHeader>\n            <PopoverTitle id={titleId}>{label}</PopoverTitle>\n            <PopoverDescription id={descriptionId} className='text-pretty'>\n              {provenance.description}\n            </PopoverDescription>\n          </PopoverHeader>\n          <KPIProvenanceDetails\n            provenance={provenance}\n            onFollowLink={() => setOpen(false)}\n          />\n        </PopoverContent>\n      </Popover>\n    </span>\n  )\n}\n\nfunction KPIProvenanceDetails({\n  onFollowLink,\n  provenance,\n}: {\n  onFollowLink: () => void\n  provenance: Provenance\n}) {\n  const filters = formatProvenanceFilters(provenance.filters)\n  return (\n    <>\n      <div\n        role='region'\n        aria-label='Details'\n        // oxlint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- a scrolling region must be focusable so keyboard users can scroll it\n        tabIndex={0}\n        className='max-h-60 overflow-y-auto rounded-sm pe-2 [scrollbar-width:thin] outline-none focus-visible:ring-2 focus-visible:ring-ring/50'\n      >\n        <dl className='flex flex-col gap-2'>\n          <div className='flex flex-col gap-0.5'>\n            <dt className='text-muted-foreground'>Window</dt>\n            <dd className='tabular-nums'>{formatProvenanceWindow(provenance.window)}</dd>\n          </div>\n          <div className='flex flex-col gap-0.5'>\n            <dt className='text-muted-foreground'>Filters</dt>\n            {filters.length === 0 ? (\n              <dd>None</dd>\n            ) : (\n              filters.map((filter) => (\n                <dd key={filter} className='break-words'>\n                  {filter}\n                </dd>\n              ))\n            )}\n          </div>\n          <div className='flex flex-col gap-0.5'>\n            <dt className='text-muted-foreground'>Query</dt>\n            <dd>\n              <code dir='ltr' className='font-mono break-all'>\n                {provenance.query}\n              </code>\n            </dd>\n          </div>\n        </dl>\n      </div>\n      <Link\n        href={provenance.href}\n        className='self-start font-medium underline underline-offset-4'\n        onClick={onFollowLink}\n      >\n        View the rows\n      </Link>\n    </>\n  )\n}\n\nexport { KPIProvenance }\n\nexport type { KPIProvenanceProps, Provenance }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/kpi-provenance.tsx"
    },
    {
      "path": "registry/components/dashboardblocks/kpi-provenance-format.ts",
      "content": "import type { Provenance } from '@/registry/components/dashboardblocks/kpi-provenance'\nimport { formatDateRange } from '@/registry/lib/dashboardblocks/format'\n\nfunction formatProvenanceWindow(window: Provenance['window'], timeZone = 'UTC') {\n  if (!window) return 'All time'\n  return formatDateRange({ end: window.to, start: window.from }, { timeZone, year: true })\n}\n\nfunction formatProvenanceFilters(filters: Provenance['filters']) {\n  return Object.entries(filters).map(([name, value]) => `${name}: ${value}`)\n}\n\nexport { formatProvenanceFilters, formatProvenanceWindow }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/kpi-provenance-format.ts"
    }
  ],
  "type": "registry:component"
}
