{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "casework-review-history",
  "title": "Review history",
  "description": "What has happened on a request and the clocks running against it, one entry per recorded event.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/casework-content",
    "@registrystack/casework-shared",
    "@registrystack/lib-content",
    "@registrystack/lib-format",
    "@registrystack/ui-button"
  ],
  "files": [
    {
      "path": "casework/review-history.tsx",
      "content": "import { useState, type ReactNode } from \"react\";\nimport { notAuthorized, problemView, type ReviewHistoryEntry } from \"@registrystack/app-runtime\";\nimport {\n  useAuthority,\n  useReviewAccountability,\n  useReviewClocks,\n  useReviewHistory,\n} from \"@registrystack/app-runtime/react\";\nimport { Button } from \"@/components/ui/button\";\nimport { useCaseworkContent, type CaseworkContent } from \"@/blocks/casework/casework-content\";\nimport { caseworkDateTime, caseworkPerson, Loading } from \"@/blocks/casework/shared\";\nimport { useLocale } from \"@/blocks/lib/content\";\nimport { fill } from \"@/blocks/lib/format\";\n\n/**\n * What has happened on a request, and the clocks running against it. Each\n * panel fetches its own data from the request id; a read error is rendered by\n * the caller, which knows how the app shows a failed read.\n */\n\nconst unknownHistoryKinds = new Set<string>();\n/** What a history entry's kind reads as; a kind with no words warns once and reads as the fallback. */\nfunction useHistoryKind(): (kind: string) => string {\n  const c = useCaseworkContent();\n  return (kind) => {\n    const words = Object.hasOwn(c.historyKinds, kind)\n      ? c.historyKinds[kind]\n      : undefined;\n    if (words === undefined && !unknownHistoryKinds.has(kind)) {\n      unknownHistoryKinds.add(kind);\n      console.warn(`No content label for history kind \"${kind}\"; using the fallback.`);\n    }\n    return words ?? c.historyUnknownKind;\n  };\n}\n\n/**\n * What a failed read of who decided says: a supervisor's reading only, or no such\n * record, or the host's own words for the problem, or, with none, that it could not\n * be read.\n */\nexport function accountabilityErrorText(c: CaseworkContent, error: Error) {\n  if (notAuthorized(error)) return c.accountabilityUnavailable;\n  const problem = problemView(error);\n  if (problem.kind === \"unavailable\") return c.accountabilityNotFound;\n  return problem.message ?? c.accountabilityNotRead;\n}\n\nfunction Accountability({ entry }: { entry: ReviewHistoryEntry }) {\n  const c = useCaseworkContent();\n  const locale = useLocale();\n  const [asked, setAsked] = useState(false);\n  const record = useReviewAccountability(entry.eventId, asked);\n  if (!asked)\n    return (\n      <Button variant=\"ghost\" size=\"sm\" onClick={() => setAsked(true)}>\n        {c.whoDecided}\n      </Button>\n    );\n  if (record.error)\n    return (\n      <span className=\"muted\">\n        {accountabilityErrorText(c, record.error)}\n      </span>\n    );\n  if (!record.data) return <Loading />;\n  return (\n    <span>\n      {fill(\n        c.accountabilityLine,\n        {\n          person: caseworkPerson(record.data.actor),\n          profile: record.data.profileId,\n          decision: record.data.decision,\n        },\n        locale,\n      )}\n    </span>\n  );\n}\n\n/** What has happened on a request: its own history, decided entries named to whoever a supervisor may ask. */\nexport function HistoryPanel({\n  requestId,\n  renderError,\n}: {\n  requestId: string;\n  /** Renders the history's own read error in place of its entries. */\n  renderError: (error: unknown, retry: () => void) => ReactNode;\n}) {\n  const c = useCaseworkContent();\n  const locale = useLocale();\n  const session = useAuthority();\n  const kindWords = useHistoryKind();\n  const history = useReviewHistory(requestId);\n  const entries = history.data ?? [];\n  let body: ReactNode;\n  if (history.error && !history.data)\n    body = renderError(history.error, () => void history.refetch());\n  else if (!history.data) body = <Loading />;\n  else if (entries.length === 0)\n    body = <p className=\"muted\">{c.historyEmpty}</p>;\n  else\n    body = (\n      <ol className=\"review-history\">\n        {entries.map((entry) => (\n          <li key={entry.eventId}>\n            <span>{kindWords(entry.kind)}</span>\n            {entry.byYou && <span> {c.historyByYou}</span>}\n            <span className=\"muted\">\n              {\" \"}\n              {caseworkDateTime(entry.occurredAt, locale)}\n            </span>\n            {entry.kind === \"review_decided\" &&\n              session.caseworkProfile === \"supervisor\" && (\n                <>\n                  {\" \"}\n                  <Accountability entry={entry} />\n                </>\n              )}\n          </li>\n        ))}\n      </ol>\n    );\n  return (\n    <section>\n      <h2>{c.historyHeading}</h2>\n      {body}\n      {history.hasNextPage && (\n        <Button\n          variant=\"outline\"\n          disabled={history.isFetchingNextPage}\n          onClick={() => void history.fetchNextPage()}\n        >\n          {c.historyMore}\n        </Button>\n      )}\n    </section>\n  );\n}\n\n/** The service clocks running against a request, and what they answer. */\nexport function ClocksPanel({\n  requestId,\n  names,\n  stages,\n  renderError,\n}: {\n  requestId: string;\n  /**\n   * Names a deadline by its clock id, as the review's policy names it; a clock\n   * with no entry is named `clockName`, by its place in the list.\n   */\n  names?: Readonly<Record<string, string>>;\n  /**\n   * Names a stage by its stage id; a stage clock whose stage has an entry is\n   * named `clockStageName`, a clock on the request's subject by its name alone.\n   */\n  stages?: Readonly<Record<string, string>>;\n  /** Renders the clocks' own read error in place of the list. */\n  renderError: (error: unknown, retry: () => void) => ReactNode;\n}) {\n  const c = useCaseworkContent();\n  const locale = useLocale();\n  const clocks = useReviewClocks(requestId);\n  return (\n    <section>\n      <h2>{c.clocksHeading}</h2>\n      {clocks.error ? (\n        renderError(clocks.error, () => void clocks.refetch())\n      ) : !clocks.data ? (\n        <Loading />\n      ) : clocks.data.length === 0 ? (\n        <p className=\"muted\">{c.clocksEmpty}</p>\n      ) : (\n        <ul>\n          {clocks.data.map((clock, index) => {\n            const name =\n              names?.[clock.clockId] ??\n              fill(c.clockName, { number: String(index + 1) }, locale);\n            const stage = clock.stageId ? stages?.[clock.stageId] : undefined;\n            return (\n              <li key={clock.clockOccurrenceId}>\n                {fill(\n                  clock.dueAt ? c.clockLineDue : c.clockLine,\n                  {\n                    name: stage\n                      ? fill(c.clockStageName, { name, stage }, locale)\n                      : name,\n                    state: c.clockStates[clock.state] ?? clock.state,\n                    time: clock.dueAt ? caseworkDateTime(clock.dueAt, locale) : \"\",\n                  },\n                  locale,\n                )}\n              </li>\n            );\n          })}\n        </ul>\n      )}\n    </section>\n  );\n}\n",
      "type": "registry:block",
      "target": "@components/casework/review-history.tsx"
    }
  ],
  "type": "registry:block"
}