{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "message-branches-demo",
  "title": "Message Branches Demo",
  "description": "A worked example of Message Branches: The 2 / 3 switcher on a message that was edited or regenerated, with an overview of every place the conversation forked and which path is on screen.",
  "registryDependencies": [
    "https://ui.tinkererslabs.com/r/message-branches.json"
  ],
  "files": [
    {
      "path": "components/demos/message-branches-demo.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { Message } from \"@/registry/default/message/message\"\nimport {\n  MessageBranches,\n  type BranchNode,\n} from \"@/registry/default/message-branches/message-branches\"\n\ntype Turn = BranchNode & { role: \"user\" | \"assistant\"; text: string }\n\nconst turns: Turn[] = [\n  {\n    id: \"q1\",\n    parentId: null,\n    role: \"user\",\n    label: \"Undo the last commit, keep changes\",\n    text: \"How do I undo my last commit but keep the changes?\",\n  },\n  {\n    id: \"a1\",\n    parentId: \"q1\",\n    role: \"assistant\",\n    label: \"git reset --soft HEAD~1\",\n    text: \"Run git reset --soft HEAD~1. The commit goes away and its changes stay staged, ready to commit again.\",\n  },\n  {\n    id: \"a1-retry\",\n    parentId: \"q1\",\n    role: \"assistant\",\n    label: \"git reset HEAD~1\",\n    text: \"Run git reset HEAD~1. That uses the default mixed mode, so the commit goes away and your edits stay in the working tree, unstaged.\",\n  },\n  {\n    id: \"q2\",\n    parentId: null,\n    role: \"user\",\n    label: \"Undo a commit already pushed\",\n    text: \"How do I undo a commit I already pushed?\",\n  },\n  {\n    id: \"a2\",\n    parentId: \"q2\",\n    role: \"assistant\",\n    label: \"git revert <commit>\",\n    text: \"Use git revert with the commit hash. It adds a new commit that reverses the old one, so nobody else's history is rewritten.\",\n  },\n  {\n    id: \"q2-follow\",\n    parentId: \"a2\",\n    role: \"user\",\n    label: \"Does that work for a merge?\",\n    text: \"Does that work for a merge commit?\",\n  },\n  {\n    id: \"a2-follow\",\n    parentId: \"q2-follow\",\n    role: \"assistant\",\n    label: \"git revert -m 1\",\n    text: \"Yes. Add -m 1 to say which parent to keep: git revert -m 1 followed by the merge commit's hash.\",\n  },\n  {\n    id: \"q3\",\n    parentId: null,\n    role: \"user\",\n    label: \"Change the last commit message\",\n    text: \"How do I change the message of my last commit?\",\n  },\n  {\n    id: \"a3\",\n    parentId: \"q3\",\n    role: \"assistant\",\n    label: \"git commit --amend\",\n    text: \"Run git commit --amend and edit the message. If the commit is already pushed this rewrites history, so keep it to your own branch.\",\n  },\n]\n\nconst byId = new Map(turns.map((turn) => [turn.id, turn]))\n\n/** From the chosen message up to the first, then down its newest replies. */\nfunction pathThrough(id: string) {\n  const path: string[] = []\n  for (let at = byId.get(id); at; at = byId.get(at.parentId ?? \"\")) {\n    path.unshift(at.id)\n  }\n\n  for (;;) {\n    const last = path[path.length - 1]\n    const replies = turns.filter((turn) => turn.parentId === last)\n    if (replies.length === 0) return path\n    path.push(replies[replies.length - 1]!.id)\n  }\n}\n\nexport function MessageBranchesDemo() {\n  const [activePath, setActivePath] = React.useState(() => pathThrough(\"q2\"))\n\n  return (\n    <div className=\"bg-background border-border w-full max-w-[36rem] rounded-[1.25rem] border p-4 shadow-sm sm:p-5\">\n      {activePath.map((id) => {\n        const turn = byId.get(id)!\n\n        return (\n          <div\n            key={id}\n            className={`flex flex-col ${turn.role === \"user\" ? \"items-end\" : \"items-start\"}`}\n          >\n            <Message className=\"w-full\" role={turn.role}>\n              {turn.text}\n            </Message>\n            <MessageBranches\n              activePath={activePath}\n              className=\"-mt-2 mb-1\"\n              messageId={id}\n              nodes={turns}\n              onSelect={(branchId) => setActivePath(pathThrough(branchId))}\n            />\n          </div>\n        )\n      })}\n    </div>\n  )\n}\n",
      "type": "registry:example"
    }
  ],
  "type": "registry:example"
}