{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dashboard-widget",
  "title": "Dashboard widget",
  "description": "One dashboard card: source, title, period choice, refresh, the figure, chart or table the host's widget calls for, help for each disclosure, and the method behind a closed disclosure. A failed read is worded as a failure with a retry, never as a zero.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0",
    "lucide-react@1.43.0"
  ],
  "registryDependencies": [
    "@registrystack/dashboard-figure",
    "@registrystack/dashboard-chart",
    "@registrystack/dashboard-table",
    "@registrystack/dashboard-content",
    "@registrystack/dashboard-cells",
    "@registrystack/ui-button",
    "@registrystack/lib-notice",
    "@registrystack/lib-format",
    "@registrystack/lib-content"
  ],
  "files": [
    {
      "path": "dashboard/dashboard-widget.tsx",
      "content": "import { useId, useState } from \"react\";\nimport { RefreshCw } from \"lucide-react\";\nimport {\n  problemView,\n  type DashboardWidgetResult,\n  type DashboardWidgetView,\n} from \"@registrystack/app-runtime\";\nimport { useDashboardWidget } from \"@registrystack/app-runtime/react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Notice } from \"@/blocks/lib/notice\";\nimport { useLocale } from \"@/blocks/lib/content\";\nimport { fill } from \"@/blocks/lib/format\";\nimport { DashboardChart } from \"@/blocks/dashboard/dashboard-chart\";\nimport {\n  useDashboardContent,\n  type DashboardContent,\n} from \"@/blocks/dashboard/dashboard-content\";\nimport { DashboardFigure } from \"@/blocks/dashboard/dashboard-figure\";\nimport { DashboardTable } from \"@/blocks/dashboard/dashboard-table\";\nimport { minimumCountOf } from \"@/blocks/dashboard/dashboard-values\";\n\nconst retrieved = /^(\\d{4}-\\d{2}-\\d{2})T(\\d{2}:\\d{2}:\\d{2})/;\n\n/** A timestamp as UTC words; a date alone is a date. */\nfunction utc(value: string, c: DashboardContent, locale: string): string {\n  const parts = retrieved.exec(value);\n  return parts\n    ? fill(c.utcDateTime, { date: parts[1]!, time: parts[2]! }, locale)\n    : fill(c.referenceDateValue, { date: value }, locale);\n}\n\n/**\n * What a figure counts and how it was read, behind a disclosure the reader\n * opens: the unit, the dates, the live evaluation or the release, BReg's\n * disclosure rules for a released count, and when the host read it.\n */\nexport function DashboardCalculation({\n  widget,\n  result,\n}: {\n  widget: DashboardWidgetView;\n  result: DashboardWidgetResult;\n}) {\n  const c = useDashboardContent(),\n    locale = useLocale();\n  const context = result.context;\n  if (!context) return null;\n  const flow = context.periodKind === \"flow\";\n  const facts: [string, string][] = [\n    [c.countedUnit, context.unit],\n    flow\n      ? [\n          c.dateInterval,\n          fill(\n            c.interval,\n            { start: context.start, end: context.end },\n            locale,\n          ),\n        ]\n      : [\n          c.referenceDate,\n          fill(\n            c.referenceDateValue,\n            { date: context.referenceDate },\n            locale,\n          ),\n        ],\n  ];\n  if (context.evaluatedAt) facts.push([c.evaluated, utc(context.evaluatedAt, c, locale)]);\n  if (context.version !== undefined)\n    facts.push([\n      c.release,\n      fill(\n        c.version,\n        {\n          version: String(context.version),\n          status: context.releaseStatus ?? \"\",\n        },\n        locale,\n      ),\n    ]);\n  if (context.disclosure)\n    facts.push([\n      c.disclosureRules,\n      fill(\n        c.disclosureRulesValue,\n        {\n          minimumCount: context.disclosure.minimumCount,\n          roundingBase: context.disclosure.roundingBase,\n        },\n        locale,\n      ),\n    ]);\n  facts.push([c.retrieved, utc(result.retrievedAt, c, locale)]);\n  return (\n    <details className=\"dashboard-calculation\" data-widget={widget.id}>\n      <summary>{c.calculation}</summary>\n      <dl className=\"dashboard-context\">\n        {facts.map(([term, value]) => (\n          <div key={term}>\n            <dt>{term}</dt>\n            <dd>{value}</dd>\n          </div>\n        ))}\n      </dl>\n    </details>\n  );\n}\n\n/** Help that applies to the counts of a ready result, in the order a reader needs it. */\nfunction helpLines(\n  c: DashboardContent,\n  widget: DashboardWidgetView,\n  result: DashboardWidgetResult,\n  locale: string,\n): string[] {\n  const rows = [\n    ...(result.rows ?? []),\n    ...(result.total ? [result.total] : []),\n  ];\n  const minimum = minimumCountOf(result.context);\n  const lines = [widget.source === \"live\" ? c.liveHelp : c.releasedHelp];\n  if (rows.some((row) => row.status === \"suppressed\"))\n    lines.push(\n      minimum === undefined\n        ? c.withheldHelpWithoutMinimum\n        : fill(c.withheldHelp, { count: minimum }, locale),\n    );\n  if (rows.some((row) => row.status === \"rounded\")) lines.push(c.roundedHelp);\n  if (rows.some((row) => row.drillthrough)) lines.push(c.openRecordsHelp);\n  return lines;\n}\n\n/**\n * One widget of the dashboard. It reads its own counts through the host, so a\n * failure of one never blanks the others. A read that fails is worded as a\n * failure and offers a retry; it is never shown as a count of zero. The count\n * is drawn as BReg disclosed it; this block neither rounds nor retries.\n */\nexport function DashboardWidget({ widget }: { widget: DashboardWidgetView }) {\n  const c = useDashboardContent(),\n    locale = useLocale(),\n    id = useId();\n  const [period, setPeriod] = useState(widget.periods?.default);\n  const query = useDashboardWidget(widget.id, period);\n  const result = query.data;\n  // A failed refresh keeps the last good data in the query; it is never shown as current.\n  const ready = !query.error && result?.state === \"ready\" ? result : undefined;\n  const failure = query.error\n    ? problemView(query.error).kind === \"session-ended\"\n      ? c.expired\n      : c.unavailable\n    : undefined;\n  const message =\n    failure ??\n    (!query.error && result && result.state !== \"ready\"\n      ? (result.message ??\n        (result.state === \"denied\"\n          ? c.denied\n          : result.state === \"missing\"\n            ? c.missing\n            : c.unavailable))\n      : undefined);\n  const announcement = query.isPending\n    ? c.loading\n    : ready\n      ? fill(c.ready, { period: ready.period }, locale)\n      : \"\";\n  return (\n    <section\n      className=\"dashboard-widget\"\n      aria-labelledby={`${id}-title`}\n      aria-busy={query.isFetching}\n    >\n      <header>\n        <div>\n          <p className=\"dashboard-source\">\n            {widget.source === \"live\" ? c.live : c.released}\n          </p>\n          <h2 id={`${id}-title`}>{widget.title}</h2>\n        </div>\n        <button\n          type=\"button\"\n          className=\"dashboard-refresh\"\n          aria-label={fill(c.refresh, { widget: widget.title }, locale)}\n          disabled={query.isFetching}\n          onClick={() => void query.refetch()}\n        >\n          <RefreshCw size={16} aria-hidden=\"true\" />\n        </button>\n      </header>\n      {widget.description && <p>{widget.description}</p>}\n      {widget.periods && widget.periods.choices.length > 1 ? (\n        <div className=\"dashboard-period\">\n          <label htmlFor={`${id}-period`}>{c.period}</label>\n          <select\n            id={`${id}-period`}\n            value={period}\n            onChange={(event) => setPeriod(event.target.value)}\n          >\n            {widget.periods.choices.map((choice) => (\n              <option key={choice} value={choice}>\n                {choice}\n              </option>\n            ))}\n          </select>\n        </div>\n      ) : (\n        ready && (\n          <p className=\"dashboard-period\">\n            {fill(c.periodFixed, { period: ready.period }, locale)}\n          </p>\n        )\n      )}\n      <p\n        role=\"status\"\n        aria-live=\"polite\"\n        aria-atomic=\"true\"\n        className=\"sr-only\"\n      >\n        {announcement}\n      </p>\n      {query.isPending && <p className=\"dashboard-note\">{c.loading}</p>}\n      {message && (\n        <Notice tone=\"warning\">\n          <p>{message}</p>\n          {failure && (\n            <Button variant=\"outline\" onClick={() => void query.refetch()}>\n              {c.retry}\n            </Button>\n          )}\n        </Notice>\n      )}\n      {ready && (\n        <>\n          {widget.type === \"figure\" ? (\n            <DashboardFigure widget={widget} result={ready} />\n          ) : widget.type === \"chart\" ? (\n            <DashboardChart widget={widget} result={ready} />\n          ) : (\n            <DashboardTable widget={widget} result={ready} />\n          )}\n          {helpLines(c, widget, ready, locale).map((line) => (\n            <p key={line} className=\"dashboard-note\">\n              {line}\n            </p>\n          ))}\n          <DashboardCalculation widget={widget} result={ready} />\n        </>\n      )}\n    </section>\n  );\n}\n",
      "type": "registry:block",
      "target": "@components/dashboard/dashboard-widget.tsx"
    }
  ],
  "type": "registry:block"
}