{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "lib-queue-parts",
  "title": "Queue filter, table and pager parts",
  "description": "The filter bar, filter chips, result count, table and pager a cursor-paged queue composes itself from.",
  "dependencies": [
    "lucide-react@1.43.0"
  ],
  "registryDependencies": [
    "@registrystack/casework-content",
    "@registrystack/lib-content",
    "@registrystack/lib-format",
    "@registrystack/shell-loading",
    "@registrystack/ui-button"
  ],
  "files": [
    {
      "path": "lib/queue-parts.tsx",
      "content": "import { ChevronLeft, ChevronRight } from \"lucide-react\";\nimport { type FormEvent, type ReactNode } from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Loading } from \"@/blocks/shell/loading\";\nimport { useBlockContent, useLocale } from \"@/blocks/lib/content\";\nimport { useCaseworkContent } from \"@/blocks/casework/casework-content\";\nimport { fill } from \"@/blocks/lib/format\";\n\n/**\n * The spec-conforming building blocks of a BReg-direct queue, held apart from\n * the request queue that composes them so a second queue can use the same\n * filter bar, chips, range sentence, table and pager. Columns, rows and\n * filters are passed in; each part keeps its own content lookups for the\n * wording that belongs to the pattern rather than to a list.\n */\n\nexport const PAGE_SIZES = [25, 50, 100] as const;\nexport const DEFAULT_SIZE = 25;\n/** More rendered rows than this and the table drops to dense padding. */\nconst DENSE_ROW_LIMIT = 25;\n\n/**\n * The paged part of any list view, all of it carried in the hash query\n * string; each list adds its own filter on top.\n */\nexport interface PagedView {\n  size: number;\n  cursor?: string;\n  page: number;\n}\n\n/** Every part of a queue view: the stage filter plus the paging. */\nexport interface QueueView extends PagedView {\n  state: string;\n}\n\n/** One remembered step towards the current page, for Previous only. */\nexport interface CursorEntry {\n  cursor: string | undefined;\n  page: number;\n}\n\n/** The hash query is the only source of truth for a list's paging. */\nexport function parsePaging(query: URLSearchParams): PagedView {\n  const size = Number(query.get(\"size\"));\n  const requested = Number(query.get(\"page\"));\n  const cursor = query.get(\"cursor\") ?? \"\";\n  return {\n    size: (PAGE_SIZES as readonly number[]).includes(size)\n      ? size\n      : DEFAULT_SIZE,\n    // A page position without a cursor is meaningless, because pagination is\n    // cursor-only: pin page 1 so the range sentence never counts imaginary\n    // rows on a first-page slice.\n    page:\n      cursor && Number.isInteger(requested) && requested >= 1 ? requested : 1,\n    cursor: cursor || undefined,\n  };\n}\n\n/** Adds a list's paging to its query string; defaults are left out. */\nexport function writePaging(params: URLSearchParams, next: PagedView) {\n  if (next.size !== DEFAULT_SIZE) params.set(\"size\", String(next.size));\n  if (next.cursor) params.set(\"cursor\", next.cursor);\n  if (next.page > 1) params.set(\"page\", String(next.page));\n}\n\n/** The hash query is the only source of truth for a queue view. */\nexport function parseView(query: URLSearchParams): QueueView {\n  return { state: query.get(\"state\") ?? \"\", ...parsePaging(query) };\n}\n\n/** The route a view is, given the queue's own path. */\nexport function writeView(path: string, next: QueueView): string {\n  const params = new URLSearchParams();\n  if (next.state) params.set(\"state\", next.state);\n  writePaging(params, next);\n  const search = params.toString();\n  return `${path}${search ? `?${search}` : \"\"}`;\n}\n\n/**\n * A filter control is built from the advertised descriptor, never from a\n * hard-coded list, and a filter the host does not advertise for this profile\n * never renders at all — so the caller decides whether to render this at all,\n * and supplies the options.\n */\nexport function QueueFilterBar({\n  id,\n  name,\n  label,\n  value,\n  anyLabel,\n  options,\n  onApply,\n}: {\n  id: string;\n  name: string;\n  label: string;\n  /** The applied value, as read from the URL. */\n  value: string;\n  /** The \"no filter\" option, first in the list. */\n  anyLabel: string;\n  options: readonly { value: string; label: string }[];\n  onApply: (value: string) => void;\n}) {\n  const c = useCaseworkContent();\n  function apply(event: FormEvent<HTMLFormElement>) {\n    event.preventDefault();\n    const field = event.currentTarget.elements.namedItem(name);\n    onApply(field instanceof HTMLSelectElement ? field.value : \"\");\n  }\n  return (\n    <form className=\"filter-bar\" onSubmit={apply}>\n      <div>\n        <label htmlFor={id}>{label}</label>\n        {/* Uncontrolled on purpose: the applied filter lives in the URL,\n            and the key re-mounts the control whenever the URL changes\n            without this form, so it always shows the applied stage. */}\n        <select id={id} name={name} key={value} defaultValue={value}>\n          <option value=\"\">{anyLabel}</option>\n          {options.map((option) => (\n            <option key={option.value} value={option.value}>\n              {option.label}\n            </option>\n          ))}\n        </select>\n      </div>\n      <Button type=\"submit\" variant=\"outline\">\n        {c.filterApply}\n      </Button>\n    </form>\n  );\n}\n\n/** One applied filter, named as the officer reads it. */\nexport interface ActiveFilter {\n  name: string;\n  label: string;\n}\n\n/** What is currently narrowing the list, each removable on its own. */\nexport function QueueFilterChips({\n  filters,\n  onRemove,\n  clearHref,\n  onClear,\n}: {\n  filters: ActiveFilter[];\n  onRemove: (filter: ActiveFilter) => void;\n  /** The route of the same view with nothing applied. */\n  clearHref: string;\n  /** Handles an ordinary same-page clear; modified clicks follow the link. */\n  onClear?: () => void;\n}) {\n  const c = useCaseworkContent();\n  const locale = useLocale();\n  if (filters.length === 0) return null;\n  return (\n    <ul className=\"filter-chips\">\n      {filters.map((filter) => (\n        <li className=\"filter-chip\" key={filter.name}>\n          <span>{filter.label}</span>\n          <button\n            type=\"button\"\n            aria-label={fill(c.removeFilter, { label: filter.label }, locale)}\n            onClick={() => onRemove(filter)}\n          >\n            ×\n          </button>\n        </li>\n      ))}\n      <li>\n        {/* A link, not a button: the unfiltered view is a URL, and it\n            clears the cursor with the chips. */}\n        <a\n          className=\"clear-filters-link\"\n          href={`#${clearHref}`}\n          onClick={(event) => {\n            if (\n              !event.defaultPrevented &&\n              event.button === 0 &&\n              !event.altKey &&\n              !event.ctrlKey &&\n              !event.metaKey &&\n              !event.shiftKey\n            ) {\n              if (!onClear) return;\n              event.preventDefault();\n              onClear();\n            }\n          }}\n        >\n          {c.clearAllFilters}\n        </a>\n      </li>\n    </ul>\n  );\n}\n\n/**\n * \"Showing 26 to 50\". No total is available for a cursor list, so the sentence\n * never invents one, and it is absent rather than zeroed on an empty page.\n */\nexport function QueueResultCount({\n  start,\n  count,\n}: {\n  start: number;\n  count: number;\n}) {\n  const c = useCaseworkContent();\n  const locale = useLocale();\n  if (count === 0) return null;\n  return (\n    <p className=\"result-count\">\n      {fill(c.showingRange, { start, end: start + count - 1 }, locale)}\n    </p>\n  );\n}\n\n/** One column of a queue table. Exactly one column is the row header. */\nexport interface QueueColumn<Row> {\n  key: string;\n  header: string;\n  /** The cell that names the row, rendered as `th scope=\"row\"`. */\n  rowHeader?: boolean;\n  cell: (row: Row) => ReactNode;\n}\n\n/**\n * The list itself. Loading replaces the table body only: the filters and chips\n * above stay mounted, and the column headers keep their places.\n */\nexport function QueueTable<Row>({\n  caption,\n  columns,\n  rows,\n  rowKey,\n  loading = false,\n}: {\n  /** Names the scrollable region and captions the table. */\n  caption: string;\n  columns: readonly QueueColumn<Row>[];\n  rows: readonly Row[];\n  rowKey: (row: Row) => string;\n  loading?: boolean;\n}) {\n  return (\n    <div className=\"table-wrap\" tabIndex={0} role=\"region\" aria-label={caption}>\n      <table\n        className={rows.length > DENSE_ROW_LIMIT ? \"table-dense\" : undefined}\n      >\n        <caption className=\"sr-only\">{caption}</caption>\n        <thead>\n          <tr>\n            {columns.map((column) => (\n              <th key={column.key} scope=\"col\">\n                {column.header}\n              </th>\n            ))}\n          </tr>\n        </thead>\n        <tbody>\n          {loading ? (\n            <tr>\n              <td colSpan={columns.length}>\n                <Loading />\n              </td>\n            </tr>\n          ) : (\n            rows.map((row) => (\n              <tr key={rowKey(row)}>\n                {columns.map((column) =>\n                  column.rowHeader ? (\n                    <th key={column.key} scope=\"row\">\n                      {column.cell(row)}\n                    </th>\n                  ) : (\n                    <td key={column.key}>{column.cell(row)}</td>\n                  ),\n                )}\n              </tr>\n            ))\n          )}\n        </tbody>\n      </table>\n    </div>\n  );\n}\n\n/**\n * Previous and Next are links, so a page is a URL; a step that does not exist\n * is a disabled button rather than a missing control. The page size restarts\n * the list, which is the caller's business.\n */\nexport function QueuePager({\n  previousHref,\n  onPrevious,\n  nextHref,\n  onNext,\n  sizeId,\n  size,\n  onSize,\n}: {\n  /** Absent disables Previous. */\n  previousHref?: string;\n  onPrevious?: () => void;\n  /** Absent disables Next. */\n  nextHref?: string;\n  onNext?: () => void;\n  sizeId: string;\n  size: number;\n  onSize: (size: number) => void;\n}) {\n  const b = useBlockContent();\n  const c = useCaseworkContent();\n  const locale = useLocale();\n  // The size select's options name themselves (\"25 per page\"), so its label\n  // is for assistive technology only; Previous and Next are chevrons whose\n  // names are the same words in text a screen reader reads.\n  const previous = (\n    <>\n      <ChevronLeft aria-hidden=\"true\" />\n      <span className=\"sr-only\">{b.previous}</span>\n    </>\n  );\n  const next = (\n    <>\n      <ChevronRight aria-hidden=\"true\" />\n      <span className=\"sr-only\">{b.next}</span>\n    </>\n  );\n  return (\n    <div className=\"pager flex-row items-center\">\n      <div>\n        <label htmlFor={sizeId} className=\"sr-only\">\n          {fill(c.perPage, { size }, locale)}\n        </label>\n        <select\n          id={sizeId}\n          className=\"h-(--control-h-sm) min-h-0 w-auto min-w-0 rounded-md border border-input bg-background px-2 py-0 text-sm text-foreground\"\n          value={String(size)}\n          onChange={(event) => onSize(Number(event.target.value))}\n        >\n          {PAGE_SIZES.map((option) => (\n            <option key={option} value={String(option)}>\n              {fill(c.perPage, { size: option }, locale)}\n            </option>\n          ))}\n        </select>\n      </div>\n      <div className=\"actions w-auto gap-1\">\n        {previousHref ? (\n          <Button\n            render={<a href={`#${previousHref}`} onClick={onPrevious} />}\n            variant=\"outline\"\n            size=\"icon-sm\"\n          >\n            {previous}\n          </Button>\n        ) : (\n          <Button variant=\"outline\" size=\"icon-sm\" disabled>\n            {previous}\n          </Button>\n        )}\n        {nextHref ? (\n          <Button\n            render={<a href={`#${nextHref}`} onClick={onNext} />}\n            variant=\"outline\"\n            size=\"icon-sm\"\n          >\n            {next}\n          </Button>\n        ) : (\n          <Button variant=\"outline\" size=\"icon-sm\" disabled>\n            {next}\n          </Button>\n        )}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:lib",
      "target": "@lib/queue-parts.tsx"
    }
  ],
  "type": "registry:lib"
}