{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ai-branch",
  "type": "registry:item",
  "title": "Ai Branch",
  "description": "A ai-branch item.",
  "dependencies": [
    "@mui/material",
    "ai",
    "lucide-react",
    "@emotion/react",
    "@emotion/styled"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "components/ai-branch/ai-branch.tsx",
      "target": "src/mui-treasury/components/ai-branch/ai-branch.tsx",
      "content": "'use client';\n\nimport { type ComponentProps, type ReactElement, memo } from 'react';\nimport { createContext, useContext, useEffect, useState } from 'react';\n\nimport Box from '@mui/material/Box';\nimport IconButton from '@mui/material/IconButton';\nimport Typography from '@mui/material/Typography';\nimport type { UIMessage } from 'ai';\nimport { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react';\n\ntype BranchContextType = {\n  currentBranch: number;\n  totalBranches: number;\n  goToPrevious: () => void;\n  goToNext: () => void;\n  branches: ReactElement[];\n  setBranches: (branches: ReactElement[]) => void;\n};\n\nconst BranchContext = createContext<BranchContextType | null>(null);\n\nconst useBranch = () => {\n  const context = useContext(BranchContext);\n\n  if (!context) {\n    throw new Error('Branch components must be used within Branch');\n  }\n\n  return context;\n};\n\nexport type BranchProps = ComponentProps<typeof Box> & {\n  defaultBranch?: number;\n  onBranchChange?: (branchIndex: number) => void;\n};\n\nexport const Branch = ({\n  defaultBranch = 0,\n  onBranchChange,\n  children,\n  sx,\n  ...props\n}: BranchProps) => {\n  const [currentBranch, setCurrentBranch] = useState(defaultBranch);\n  const [branches, setBranches] = useState<ReactElement[]>([]);\n\n  const handleBranchChange = (newBranch: number) => {\n    setCurrentBranch(newBranch);\n    onBranchChange?.(newBranch);\n  };\n\n  const goToPrevious = () => {\n    const newBranch =\n      currentBranch > 0 ? currentBranch - 1 : branches.length - 1;\n    handleBranchChange(newBranch);\n  };\n\n  const goToNext = () => {\n    const newBranch =\n      currentBranch < branches.length - 1 ? currentBranch + 1 : 0;\n    handleBranchChange(newBranch);\n  };\n\n  const contextValue: BranchContextType = {\n    currentBranch,\n    totalBranches: branches.length,\n    goToPrevious,\n    goToNext,\n    branches,\n    setBranches,\n  };\n\n  return (\n    <BranchContext.Provider value={contextValue}>\n      <Box\n        sx={{\n          display: 'grid',\n          width: '100%',\n          gap: 1,\n          '& > div': {\n            pb: 0,\n          },\n          ...sx,\n        }}\n        {...props}\n      >\n        {children}\n      </Box>\n    </BranchContext.Provider>\n  );\n};\n\nexport type BranchMessagesProps = ComponentProps<typeof Box>;\n\nexport const BranchMessages = ({\n  children,\n  sx,\n  ...props\n}: BranchMessagesProps) => {\n  const { currentBranch, setBranches, branches } = useBranch();\n  const childrenArray = Array.isArray(children) ? children : [children];\n\n  // Use useEffect to update branches when they change\n  useEffect(() => {\n    if (branches.length !== childrenArray.length) {\n      setBranches(childrenArray);\n    }\n  }, [childrenArray, branches, setBranches]);\n\n  return childrenArray.map((branch, index) => (\n    <Box\n      sx={{\n        display: index === currentBranch ? 'grid' : 'none',\n        gap: 1,\n        overflow: 'hidden',\n        '& > div': {\n          pb: 0,\n        },\n        ...sx,\n      }}\n      key={branch.key}\n      {...props}\n    >\n      {branch}\n    </Box>\n  ));\n};\n\nexport type BranchSelectorProps = ComponentProps<typeof Box> & {\n  from: UIMessage['role'];\n};\n\nexport const BranchSelector = memo(\n  ({ from, sx, children, ...props }: BranchSelectorProps) => {\n    const { totalBranches } = useBranch();\n\n    // Don't render if there's only one branch\n    if (totalBranches <= 1) {\n      return null;\n    }\n\n    return (\n      <Box\n        sx={{\n          display: 'flex',\n          alignItems: 'center',\n          gap: 1,\n          alignSelf: 'end',\n          px: 5,\n          justifyContent: from === 'assistant' ? 'flex-start' : 'flex-end',\n          ...sx,\n        }}\n        {...props}\n      >\n        {children}\n      </Box>\n    );\n  },\n);\n\nBranchSelector.displayName = 'BranchSelector';\n\nexport type BranchPreviousProps = ComponentProps<typeof IconButton>;\n\nexport const BranchPrevious = ({\n  children,\n  sx,\n  ...props\n}: BranchPreviousProps) => {\n  const { goToPrevious, totalBranches } = useBranch();\n\n  return (\n    <IconButton\n      aria-label=\"Previous branch\"\n      sx={{\n        width: 28,\n        height: 28,\n        borderRadius: '50%',\n        color: 'text.secondary',\n        transition: 'all 0.2s',\n        '&:hover': {\n          bgcolor: 'action.hover',\n          color: 'text.primary',\n        },\n        '&:disabled': {\n          opacity: 0.5,\n          pointerEvents: 'none',\n        },\n        ...sx,\n      }}\n      disabled={totalBranches <= 1}\n      onClick={goToPrevious}\n      size=\"small\"\n      type=\"button\"\n      {...props}\n    >\n      {children ?? <ChevronLeftIcon size={14} />}\n    </IconButton>\n  );\n};\n\nexport type BranchNextProps = ComponentProps<typeof IconButton>;\n\nexport const BranchNext = ({ children, sx, ...props }: BranchNextProps) => {\n  const { goToNext, totalBranches } = useBranch();\n\n  return (\n    <IconButton\n      aria-label=\"Next branch\"\n      sx={{\n        width: 28,\n        height: 28,\n        borderRadius: '50%',\n        color: 'text.secondary',\n        transition: 'all 0.2s',\n        '&:hover': {\n          bgcolor: 'action.hover',\n          color: 'text.primary',\n        },\n        '&:disabled': {\n          opacity: 0.5,\n          pointerEvents: 'none',\n        },\n        ...sx,\n      }}\n      disabled={totalBranches <= 1}\n      onClick={goToNext}\n      size=\"small\"\n      type=\"button\"\n      {...props}\n    >\n      {children ?? <ChevronRightIcon size={14} />}\n    </IconButton>\n  );\n};\n\nexport type BranchPageProps = ComponentProps<typeof Typography>;\n\nexport const BranchPage = ({ sx, ...props }: BranchPageProps) => {\n  const { currentBranch, totalBranches } = useBranch();\n\n  return (\n    <Typography\n      component=\"span\"\n      variant=\"caption\"\n      sx={{\n        fontWeight: 500,\n        color: 'text.secondary',\n        fontVariantNumeric: 'tabular-nums',\n        ...sx,\n      }}\n      {...props}\n    >\n      {currentBranch + 1} of {totalBranches}\n    </Typography>\n  );\n};\n",
      "type": "registry:item"
    },
    {
      "path": "components/ai-branch/index.ts",
      "target": "src/mui-treasury/components/ai-branch/index.ts",
      "content": "export * from './ai-branch';\n",
      "type": "registry:item"
    }
  ],
  "meta": {
    "category": "ai",
    "subcategory": "elements"
  }
}