{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-billing-invoices",
  "type": "registry:block",
  "title": "App, invoice list",
  "description": "A list of invoices as a table: number, date, amount, a status badge (paid, due, failed: always a word) and a download link named for its invoice that stays in view while the other columns scroll, and a failed invoice that can be paid again or have its card updated. Filled and empty states.",
  "categories": [
    "app"
  ],
  "dependencies": [
    "lucide-react@^1.49.0",
    "zod@^4.6.5"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/badge.json",
    "https://ui.klarhimmel.com/r/block-kit.json",
    "https://ui.klarhimmel.com/r/button.json",
    "https://ui.klarhimmel.com/r/editing.json",
    "https://ui.klarhimmel.com/r/empty-state.json",
    "https://ui.klarhimmel.com/r/kit-app.json"
  ],
  "files": [
    {
      "path": "src/blocks/app-billing-invoices/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-billing-invoices/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, link, req, t } from '../_shared/schema';\n\nconst schema = z.object({\n  eyebrow: t(60).optional(),\n  title: t(120),\n  description: t(300).optional(),\n  caption: t(80),\n  columns: z.object({ number: t(30), date: t(30), amount: t(30), status: t(30) }),\n  rows: z\n    .array(z.object({ number: t(30), date: t(30), amount: t(30), status: z.enum(['paid', 'due', 'failed']), href: t(300) }))\n    .min(1)\n    .max(8),\n  /** The words for each status. The badge always carries the word, never only a colour. */\n  statusLabels: z.object({ paid: t(20), due: t(20), failed: t(20) }),\n  /** The download link's text; the invoice number follows it for screen readers. */\n  download: t(30),\n  /** Shown under the table while some of it is cut off at the edge. */\n  scrollHint: t(40),\n  /** What a failed invoice offers: pay again (the status becomes `processing` and focus moves to it) or update the card. */\n  recovery: z.object({ payAgain: t(30), updateCard: link, processing: t(30), said: t(80) }),\n  state: z.enum(['filled', 'empty']),\n  messages: z.object({ emptyTitle: req(80), emptyBody: t(160) }),\n});\n\nexport const spec = defineSpec({\n  type: 'app-billing-invoices',\n  aliases: ['app/billing-invoices'],\n  name: 'App, invoice list',\n  category: 'app',\n  role: 'body',\n  description: 'A list of invoices as a table: number, date, amount, a status badge (paid, due, failed: always a word) and a download link named for its invoice that stays in view while the other columns scroll, and a failed invoice that can be paid again or have its card updated. Filled and empty states.',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Billing\",\n    \"title\": \"Invoices\",\n    \"description\": \"Say whose invoices these are and what can be done with one.\",\n    \"caption\": \"Name the list and its order\",\n    \"columns\": {\n      \"number\": \"Invoice\",\n      \"date\": \"Date\",\n      \"amount\": \"Amount\",\n      \"status\": \"Status\"\n    },\n    \"rows\": [\n      {\n        \"number\": \"XXX-0001\",\n        \"date\": \"1 Jan 0000\",\n        \"amount\": \"$00\",\n        \"status\": \"due\",\n        \"href\": \"#inv-1042\"\n      },\n      {\n        \"number\": \"XXX-0002\",\n        \"date\": \"1 Jan 0000\",\n        \"amount\": \"$00\",\n        \"status\": \"paid\",\n        \"href\": \"#inv-1031\"\n      },\n      {\n        \"number\": \"XXX-0003\",\n        \"date\": \"1 Jan 0000\",\n        \"amount\": \"$00\",\n        \"status\": \"paid\",\n        \"href\": \"#inv-1019\"\n      },\n      {\n        \"number\": \"XXX-0004\",\n        \"date\": \"1 Jan 0000\",\n        \"amount\": \"$00\",\n        \"status\": \"failed\",\n        \"href\": \"#inv-1007\"\n      }\n    ],\n    \"statusLabels\": {\n      \"paid\": \"Paid\",\n      \"due\": \"Due\",\n      \"failed\": \"Failed\"\n    },\n    \"download\": \"Download PDF\",\n    \"scrollHint\": \"Say there are more columns\",\n    \"recovery\": {\n      \"payAgain\": \"Pay again\",\n      \"updateCard\": {\n        \"label\": \"Update card\",\n        \"href\": \"#card\"\n      },\n      \"processing\": \"Processing\",\n      \"said\": \"Say that a new attempt started and where the result is sent.\"\n    },\n    \"state\": \"filled\",\n    \"messages\": {\n      \"emptyTitle\": \"No invoices yet\",\n      \"emptyBody\": \"Say when the first item will appear here and what causes it.\"\n    }\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-billing-invoices/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-billing-invoices/index.tsx",
      "content": "import { useEffect, useId, useRef } from 'react';\nimport { Download, Receipt } from 'lucide-react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { Badge } from '../../ui/badge';\nimport { Button, ButtonLink } from '../../ui/button';\nimport { EmptyState } from '../../ui/empty-state';\nimport { Container, Section, SectionHeader, useEditing } from '../_shared/kit';\nimport { AppPanel, ScrollRegion, StateSlot, useSynced } from '../_shared/app-kit';\nimport { spec } from './spec';\nimport type { BlockProps } from '../types';\nimport type { z } from 'zod';\n\nexport { spec };\ntype Props = z.infer<typeof spec.schema>;\n\nconst tone = { paid: 'neutral', due: 'accent', failed: 'outline' } as const;\n\nfunction AppBillingInvoices({ props }: BlockProps<Props>) {\n  const id = useId();\n  const { state, columns } = props;\n  const editing = useEditing();\n  const [retried, setRetried] = useSynced<number[]>([], props.rows);\n  const badges = useRef<Record<number, HTMLElement | null>>({});\n  const last = useRef<number | null>(null);\n  // Pay again removes the button that had focus: focus goes to the row's status, which now says it is processing.\n  useEffect(() => {\n    if (last.current !== null) badges.current[last.current]?.focus();\n    last.current = null;\n  }, [retried]);\n  const th = 'px-4 py-3 text-left font-medium text-kh-ink-2';\n  /** A row's status and, for a failed payment, its two ways out. Shared by the table and the stacked list. */\n  const status = (r: Props['rows'][number], i: number) => (\n      <div className=\"flex items-center gap-1 whitespace-nowrap [&>*]:shrink-0 [&>*:first-child]:mr-1\">\n        {retried.includes(i) ? (\n          <span\n            tabIndex={-1}\n            ref={(el) => {\n              if (el && (el.offsetParent !== null || !badges.current[i]?.isConnected)) badges.current[i] = el; // the visible one of table and list\n            }}\n            className=\"rounded-kh-pill focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)\"\n          >\n            <Badge tone=\"accent\">\n              <Text path={['recovery', 'processing']} value={props.recovery.processing} as=\"span\" />\n            </Badge>\n          </span>\n        ) : (\n          // A status the block does not know (an app's own, like 'sent') shows as written in a neutral pill, never a blank page\n          // (Klar Portal 10-24: D1's 'sent'/'overdue' gave Text an undefined label and /ws Billing rendered nothing).\n          <Badge tone={tone[r.status] ?? 'neutral'}>\n            {props.statusLabels[r.status] !== undefined ? <Text path={['statusLabels', r.status]} value={props.statusLabels[r.status]} as=\"span\" /> : <span>{String(r.status ?? '')}</span>}\n          </Badge>\n        )}\n        {r.status === 'failed' && !retried.includes(i) && (\n          <>\n            <Button\n              variant=\"secondary\"\n              size=\"sm\"\n              onClick={() => {\n                last.current = i;\n                setRetried((x) => [...x, i]);\n              }}\n            >\n              <Text path={['recovery', 'payAgain']} value={props.recovery.payAgain} as=\"span\" />{' '}\n              <span className=\"sr-only\">{r.number}</span>\n            </Button>\n            <ButtonLink href={props.recovery.updateCard.href} variant=\"ghost\" size=\"sm\">\n              <Text path={['recovery', 'updateCard', 'label']} value={props.recovery.updateCard.label} as=\"span\" />{' '}\n              <span className=\"sr-only\">{r.number}</span>\n            </ButtonLink>\n          </>\n        )}\n      </div>\n  );\n\n  return (\n    <Section labelledBy={id}>\n      <Container>\n        <SectionHeader id={id} eyebrow={props.eyebrow} title={props.title} description={props.description} />\n        <AppPanel className=\"mt-10 overflow-hidden @container\">\n          <StateSlot show={state === 'filled'}>\n            <div className={editing ? undefined : 'hidden @md:block'}>\n            <ScrollRegion label={props.caption} hint={<Text path={['scrollHint']} value={props.scrollHint} as=\"span\" />}>\n              <table className=\"w-full min-w-[32rem] border-collapse text-kh-small\">\n                <caption className=\"sr-only\">\n                  <Text path={['caption']} value={props.caption} as=\"span\" />\n                </caption>\n                <thead>\n                  <tr className=\"border-b border-kh-line bg-kh-surface-2\">\n                    <th scope=\"col\" className={th}><Text path={['columns', 'number']} value={columns.number} as=\"span\" /></th>\n                    <th scope=\"col\" className={th}><Text path={['columns', 'date']} value={columns.date} as=\"span\" /></th>\n                    <th scope=\"col\" className={`${th} text-right`}><Text path={['columns', 'amount']} value={columns.amount} as=\"span\" /></th>\n                    <th scope=\"col\" className={th}><Text path={['columns', 'status']} value={columns.status} as=\"span\" /></th>\n                    <th scope=\"col\" className=\"sticky right-0 bg-kh-surface-2 px-4 py-3\"><span className=\"sr-only\"><Text path={['download']} value={props.download} as=\"span\" /></span></th>\n                  </tr>\n                </thead>\n                <tbody className=\"divide-y divide-kh-line\">\n                  {props.rows.map((r, i) => (\n                    <tr key={i}>\n                      <th scope=\"row\" className=\"whitespace-nowrap px-4 py-3 text-left font-medium tabular-nums\">\n                        <Text path={['rows', i, 'number']} value={r.number} as=\"span\" />\n                      </th>\n                      <td className=\"whitespace-nowrap px-4 py-3 text-kh-ink-2\"><Text path={['rows', i, 'date']} value={r.date} as=\"span\" /></td>\n                      <td className=\"whitespace-nowrap px-4 py-3 text-right tabular-nums\"><Text path={['rows', i, 'amount']} value={r.amount} as=\"span\" /></td>\n                      <td className=\"px-4 py-3\">\n                        {status(r, i)}\n                      </td>\n                      <td className=\"sticky right-0 bg-kh-surface px-2 py-1 text-right\">\n                        {/* No PDF (href empty): no link that goes nowhere (Klar Portal 10-24). */}\n                        {(r.href || editing) && (\n                          <ButtonLink href={r.href} variant=\"ghost\" size=\"md\" className=\"whitespace-nowrap\" icon={<Download aria-hidden className=\"size-4\" />}>\n                            {/* Under 48rem of the panel the icon carries it (the name stays for screen readers), so the row fits without scrolling. */}\n                            <span className=\"sr-only @3xl:not-sr-only\"><Text path={['download']} value={props.download} as=\"span\" /></span>\n                            <span className=\"sr-only\">{r.number}</span>\n                          </ButtonLink>\n                        )}\n                      </td>\n                    </tr>\n                  ))}\n                </tbody>\n              </table>\n            </ScrollRegion>\n            </div>\n            {/* Under 28rem of the panel (a phone): one stacked row per invoice, no sideways scroll (Klar Portal 22:08, Felix 19:21). */}\n            {!editing && (\n              <ul aria-label={props.caption} className=\"divide-y divide-kh-line @md:hidden\">\n                {props.rows.map((r, i) => (\n                  <li key={i} className=\"flex items-center gap-3 px-4 py-3\">\n                    <div className=\"min-w-0 flex-1\">\n                      <p className=\"text-kh-small\"><span className=\"font-medium tabular-nums\">{r.number}</span><span className=\"text-kh-ink-2\"> · {r.date}</span></p>\n                      <div className=\"mt-1.5 flex flex-wrap items-center gap-x-3 gap-y-2 text-kh-small\">\n                        <span className=\"whitespace-nowrap tabular-nums\">{r.amount}</span>\n                        {status(r, i)}\n                      </div>\n                    </div>\n                    {r.href && (\n                      <ButtonLink href={r.href} variant=\"ghost\" size=\"md\" className=\"shrink-0 self-start\" icon={<Download aria-hidden className=\"size-4\" />}>\n                        <span className=\"sr-only\">{props.download} {r.number}</span>\n                      </ButtonLink>\n                    )}\n                  </li>\n                ))}\n              </ul>\n            )}\n          </StateSlot>\n          <p role=\"status\" className=\"sr-only\">\n            {retried.length > 0 ? props.recovery.said : ''}\n          </p>\n          <StateSlot show={false}>\n            <Text path={['recovery', 'said']} value={props.recovery.said} as=\"p\" />\n            {retried.length === 0 && <Text path={['recovery', 'processing']} value={props.recovery.processing} as=\"p\" />}\n            {!props.rows.some((r) => r.status === 'failed') && (\n              <>\n                <Text path={['recovery', 'payAgain']} value={props.recovery.payAgain} as=\"p\" />\n                <Text path={['recovery', 'updateCard', 'label']} value={props.recovery.updateCard.label} as=\"p\" />\n              </>\n            )}\n          </StateSlot>\n          <StateSlot show={state === 'empty'}>\n            <EmptyState icon={<Receipt />} title={<Text path={['messages', 'emptyTitle']} value={props.messages.emptyTitle} as=\"span\" />} level={3}>\n              <Text path={['messages', 'emptyBody']} value={props.messages.emptyBody} as=\"span\" />\n            </EmptyState>\n          </StateSlot>\n        </AppPanel>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(AppBillingInvoices);\n"
    }
  ]
}
