{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "conflict-comparison",
  "title": "Conflict comparison",
  "description": "The comparison shown when a save is refused because the record changed since it was read: what the person started from, what they entered and what it is now, with a choice between keeping the entry and taking the latest.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/ui-button",
    "@registrystack/lib-content",
    "@registrystack/field-renderers"
  ],
  "files": [
    {
      "path": "record/conflict-comparison.tsx",
      "content": "import { useEffect, useRef } from \"react\";\nimport type { FieldModel, JsonValue } from \"@registrystack/app-runtime\";\nimport { Button } from \"@/components/ui/button\";\nimport { useBlockContent } from \"@/blocks/lib/content\";\nimport { FieldValue } from \"@/blocks/fields/field-value\";\n\nexport interface ConflictRow {\n  field: FieldModel;\n  /** What the person started from when they opened the form. */\n  started: JsonValue | undefined;\n  /** What the person entered. */\n  mine: JsonValue | undefined;\n  /** What the registry returned when the write was refused. */\n  latest: JsonValue | undefined;\n}\n\nconst same = (left: JsonValue | undefined, right: JsonValue | undefined) =>\n  JSON.stringify(left) === JSON.stringify(right);\n\n/**\n * The fields either side moved away from where the person started, with the\n * three values side by side. A value the registry did not return stays\n * absent, and no value is combined with another.\n */\nexport function conflictRows(\n  fields: readonly FieldModel[],\n  started: Readonly<Record<string, JsonValue>>,\n  mine: Readonly<Record<string, JsonValue>>,\n  latest: Readonly<Record<string, JsonValue>>,\n): ConflictRow[] {\n  return fields\n    .map((field) => ({\n      field,\n      started: started[field.id],\n      mine: mine[field.id],\n      latest: latest[field.id],\n    }))\n    .filter(\n      (row) => !same(row.started, row.mine) || !same(row.started, row.latest),\n    );\n}\n\nfunction Value({\n  field,\n  value,\n}: {\n  field: FieldModel;\n  value: JsonValue | undefined;\n}) {\n  const c = useBlockContent();\n  return value === undefined ? (\n    <span className=\"muted\">{c.notSupplied}</span>\n  ) : (\n    <FieldValue field={field} value={value} />\n  );\n}\n\n/**\n * The comparison shown when a save is refused because the record changed\n * since it was read. It reports only what the server returned and leaves the\n * choice to the person: keep the whole entry, or take the whole latest.\n */\nexport function ConflictComparison({\n  rows,\n  onKeepMine,\n  onTakeLatest,\n}: {\n  rows: readonly ConflictRow[];\n  onKeepMine: () => void;\n  onTakeLatest: () => void;\n}) {\n  const c = useBlockContent();\n  const section = useRef<HTMLElement>(null);\n  // The comparison arrives below the form the person was filling in.\n  useEffect(() => {\n    section.current?.scrollIntoView?.({ block: \"nearest\" });\n  }, []);\n  return (\n    <section\n      ref={section}\n      role=\"alert\"\n      className=\"conflict\"\n      aria-labelledby=\"conflict-title\"\n    >\n      <h2 id=\"conflict-title\">{c.conflictTitle}</h2>\n      <p>{c.conflictBody}</p>\n      <div className=\"table-wrap\">\n        <table className=\"conflict-table\">\n          <caption className=\"sr-only\">{c.conflictTitle}</caption>\n          <thead>\n            <tr>\n              <th scope=\"col\">{c.conflictFieldColumn}</th>\n              <th scope=\"col\">{c.conflictStartedColumn}</th>\n              <th scope=\"col\">{c.conflictMineColumn}</th>\n              <th scope=\"col\">{c.conflictLatestColumn}</th>\n            </tr>\n          </thead>\n          <tbody>\n            {rows.map((row) => {\n              const both =\n                !same(row.started, row.mine) &&\n                !same(row.started, row.latest) &&\n                !same(row.mine, row.latest);\n              return (\n                <tr key={row.field.id} data-conflict={both || undefined}>\n                  <th scope=\"row\">\n                    {row.field.label}\n                    {both && (\n                      <span className=\"muted conflict-both\">\n                        {c.conflictBothChanged}\n                      </span>\n                    )}\n                  </th>\n                  <td>\n                    <Value field={row.field} value={row.started} />\n                  </td>\n                  <td>\n                    <Value field={row.field} value={row.mine} />\n                  </td>\n                  <td>\n                    <Value field={row.field} value={row.latest} />\n                  </td>\n                </tr>\n              );\n            })}\n          </tbody>\n        </table>\n      </div>\n      <div className=\"actions\">\n        <Button onClick={onKeepMine}>{c.conflictKeepMine}</Button>\n        <Button variant=\"outline\" onClick={onTakeLatest}>\n          {c.conflictTakeLatest}\n        </Button>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:block",
      "target": "@components/record/conflict-comparison.tsx"
    }
  ],
  "type": "registry:block"
}