{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-files-list",
  "type": "registry:block",
  "title": "App, file list with status",
  "description": "Files and where each one is: uploading (a native progress bar with its percentage), done, or failed with a reason and Retry. Retry restarts the row at a stated percentage, Remove deletes it; both name their file, keep focus in the list and are announced. Filled and empty.",
  "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-files-list/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-files-list/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, t, req } from '../_shared/schema';\n\nconst file = z.object({\n  name: t(60),\n  size: t(20),\n  status: z.enum(['uploading', 'done', 'failed']),\n  /** How far an upload has got, as a plain percentage written as text (\"60\"). Used while uploading. */\n  percent: t(4),\n  /** Why a failed upload failed. Optional. */\n  reason: t(80).optional(),\n});\n\nconst schema = z.object({\n  eyebrow: t(60).optional(),\n  title: t(120),\n  description: t(300).optional(),\n  files: z.array(file).min(1).max(8),\n  /** The status as a word. A status is never only a colour or an icon. */\n  statusLabels: z.object({ uploading: t(20), done: t(20), failed: t(20) }),\n  actions: z.object({ retry: t(20), remove: t(20) }),\n  /** Said in a live region after an action. */\n  /** Said after an action, followed by the file name: \"Upload restarted: house-rules.pdf\". Shown and announced. */\n  notices: z.object({ retrying: t(40), removed: t(40) }),\n  /** How far a restarted upload shows (\"25\"): a stand-in, your app drives the real number. */\n  restartAt: t(4),\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-files-list',\n  aliases: ['app/files-list'],\n  name: 'App, file list with status',\n  category: 'app',\n  role: 'body',\n  description: 'Files and where each one is: uploading (a native progress bar with its percentage), done, or failed with a reason and Retry. Retry restarts the row at a stated percentage, Remove deletes it; both name their file, keep focus in the list and are announced. Filled and empty.',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Files\",\n    \"title\": \"Uploads\",\n    \"description\": \"Say whose files these are.\",\n    \"files\": [\n      {\n        \"name\": \"first-file.pdf\",\n        \"size\": \"0.0 MB\",\n        \"status\": \"done\",\n        \"percent\": \"100\"\n      },\n      {\n        \"name\": \"second-file.jpg\",\n        \"size\": \"0.0 MB\",\n        \"status\": \"uploading\",\n        \"percent\": \"60\"\n      },\n      {\n        \"name\": \"third-file.pdf\",\n        \"size\": \"000 KB\",\n        \"status\": \"failed\",\n        \"percent\": \"0\",\n        \"reason\": \"Say why it failed.\"\n      }\n    ],\n    \"statusLabels\": {\n      \"uploading\": \"Uploading\",\n      \"done\": \"Done\",\n      \"failed\": \"Failed\"\n    },\n    \"actions\": {\n      \"retry\": \"Retry\",\n      \"remove\": \"Remove\"\n    },\n    \"notices\": {\n      \"retrying\": \"Say it restarted:\",\n      \"removed\": \"Removed:\"\n    },\n    \"restartAt\": \"25\",\n    \"state\": \"filled\",\n    \"messages\": {\n      \"emptyTitle\": \"No files yet\",\n      \"emptyBody\": \"Say what makes files appear here.\"\n    }\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-files-list/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-files-list/index.tsx",
      "content": "import { useEffect, useId, useRef } from 'react';\nimport { FileText, Files } from 'lucide-react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { Badge } from '../../ui/badge';\nimport { Button } from '../../ui/button';\nimport { EmptyState } from '../../ui/empty-state';\nimport { Container, Opt, Section, SectionHeader } from '../_shared/kit';\nimport { AppPanel, 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>;\ntype Status = Props['files'][number]['status'];\n\nconst tone = { uploading: 'accent', done: 'neutral', failed: 'outline' } as const;\nconst pct = (s: string) => Math.min(100, Math.max(0, Number(s.replace('%', '')) || 0));\nconst bar = 'mt-2 h-1.5 w-full appearance-none overflow-hidden rounded-kh-pill [&::-moz-progress-bar]:bg-kh-accent [&::-webkit-progress-bar]:bg-kh-line-strong [&::-webkit-progress-value]:bg-kh-accent';\n\nfunction AppFilesList({ props }: BlockProps<Props>) {\n  const id = useId();\n  const [gone, setGone] = useSynced<number[]>([], props.files);\n  const [again, setAgain] = useSynced<number[]>([], props.files);\n  const [notice, setNotice] = useSynced<{ text: string; n: number }>({ text: '', n: 0 }, props.files);\n  const status = (i: number): Status => (again.includes(i) ? 'uploading' : props.files[i]!.status);\n  const left = props.files.map((f, i) => ({ f, i })).filter(({ i }) => !gone.includes(i));\n  const refs = useRef<{ badge: Record<number, HTMLElement | null>; remove: Record<number, HTMLButtonElement | null> }>({ badge: {}, remove: {} });\n  const heading = useRef<HTMLParagraphElement>(null);\n  const next = useRef<{ kind: 'badge' | 'after'; i: number } | null>(null);\n  // Retry removes the button that had focus: it goes to the row's status. Remove: on to the next row, or back, or the count.\n  useEffect(() => {\n    const n = next.current;\n    next.current = null;\n    if (!n) return;\n    if (n.kind === 'badge') refs.current.badge[n.i]?.focus();\n    else {\n      const rest = props.files.map((_, k) => k).filter((k) => !gone.includes(k));\n      const target = rest.find((k) => k > n.i && refs.current.remove[k]) ?? [...rest].reverse().find((k) => k < n.i && refs.current.remove[k]);\n      (target !== undefined ? refs.current.remove[target] : heading.current)?.focus();\n    }\n  }, [again, gone, props.files]);\n  const say = (text: string) => setNotice((x) => ({ text, n: x.n + 1 }));\n  return (\n    <Section labelledBy={id}>\n      <Container width=\"narrow\">\n        <SectionHeader id={id} eyebrow={props.eyebrow} title={props.title} description={props.description} />\n        <AppPanel className=\"mt-10\">\n          <div className=\"border-b border-kh-line px-6 py-3 text-kh-small @xl:px-8\">\n            <p ref={heading} tabIndex={-1} className=\"tabular-nums text-kh-muted outline-none\">\n              {left.length} / {props.files.length}\n            </p>\n            <p role=\"status\" key={notice.n} className=\"min-h-5 text-kh-ink\">\n              {notice.text}\n            </p>\n          </div>\n          <StateSlot show={false}>\n            <Text path={['notices', 'retrying']} value={props.notices.retrying} as=\"p\" />\n            <Text path={['notices', 'removed']} value={props.notices.removed} as=\"p\" />\n            <Text path={['restartAt']} value={props.restartAt} as=\"p\" />\n          </StateSlot>\n          <StateSlot show={props.state === 'filled' && left.length > 0}>\n            <ul className=\"divide-y divide-kh-line\">\n              {left.map(({ f, i }) => {\n                const st = status(i);\n                const nid = `${id}-n${i}`;\n                const value = again.includes(i) ? pct(props.restartAt) : pct(f.percent);\n                return (\n                  <li key={i} className=\"flex flex-col gap-3 px-6 py-4 @xl:flex-row @xl:items-center @xl:gap-4 @xl:px-8\">\n                    <div className=\"flex min-w-0 flex-1 items-start gap-3\">\n                      <FileText aria-hidden className=\"mt-1 size-5 shrink-0 text-kh-muted\" />\n                      <div className=\"min-w-0 flex-1\">\n                        <p className=\"flex flex-wrap items-baseline gap-x-3\">\n                          <Text path={['files', i, 'name']} value={f.name} as=\"span\" className=\"truncate text-kh-body font-medium\" />\n                          <Text path={['files', i, 'size']} value={f.size} as=\"span\" className=\"text-kh-small tabular-nums text-kh-muted\" />\n                        </p>\n                        <span id={nid} hidden>\n                          {f.name}\n                        </span>\n                        {st === 'uploading' && (\n                          <div className=\"flex items-center gap-3\">\n                            <progress aria-labelledby={nid} value={value} max={100} className={bar} />\n                            <span className=\"mt-2 w-10 shrink-0 text-right text-kh-small tabular-nums text-kh-muted\">{value}%</span>\n                          </div>\n                        )}\n                        <StateSlot show={st === 'failed'}>\n                          <Opt path={['files', i, 'reason']} value={f.reason} as=\"p\" className=\"mt-1 text-kh-small text-kh-danger\" placeholder=\"Reason\" />\n                        </StateSlot>\n                      </div>\n                    </div>\n                    <div className=\"flex flex-wrap items-center gap-2\">\n                      <span\n                        tabIndex={-1}\n                        ref={(el) => {\n                          refs.current.badge[i] = el;\n                        }}\n                        className=\"rounded-kh-pill outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)\"\n                      >\n                        <Badge tone={tone[st]}>\n                          <Text path={['statusLabels', st]} value={props.statusLabels[st]} as=\"span\" />\n                        </Badge>\n                      </span>\n                      {st === 'failed' && (\n                        <Button\n                          variant=\"secondary\"\n                          onClick={() => {\n                            next.current = { kind: 'badge', i };\n                            setAgain((a) => [...a, i]);\n                            say(`${props.notices.retrying} ${f.name}`);\n                          }}\n                        >\n                          <Text path={['actions', 'retry']} value={props.actions.retry} as=\"span\" /> <span className=\"sr-only\">{f.name}</span>\n                        </Button>\n                      )}\n                      <Button\n                        variant=\"ghost\"\n                        ref={(el) => {\n                          refs.current.remove[i] = el;\n                        }}\n                        onClick={() => {\n                          next.current = { kind: 'after', i };\n                          setGone((g) => [...g, i]);\n                          say(`${props.notices.removed} ${f.name}`);\n                        }}\n                      >\n                        <Text path={['actions', 'remove']} value={props.actions.remove} as=\"span\" /> <span className=\"sr-only\">{f.name}</span>\n                      </Button>\n                    </div>\n                  </li>\n                );\n              })}\n            </ul>\n          </StateSlot>\n          <StateSlot show={props.state === 'empty' || left.length === 0}>\n            <EmptyState icon={<Files />} 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(AppFilesList);\n"
    }
  ]
}
