ai-branch
dedevs-ui/ai-branchFreeComponent
AI conversation branch component for displaying branched conversations
See it running
Open the demo on dedevs-ui
ui.dedevs.com/components/ai-branchInstall it
npx shadcn@latest add https://ui.dedevs.com/r/ai-branch.jsonSource
1'use client';23import { ChevronLeft, ChevronRight } from 'lucide-react';4import type { HTMLAttributes, ReactElement, ReactNode } from 'react';5import { createContext, useContext, useEffect, useState } from 'react';6import { Button } from '@repo/shadcn-ui/components/ui/button';7import { cn } from '@repo/shadcn-ui/lib/utils';89type AIBranchContextType = {10 currentBranch: number;11 totalBranches: number;12 goToPrevious: () => void;13 goToNext: () => void;14 branches: ReactElement[];15 setBranches: (branches: ReactElement[]) => void;16};1718const AIBranchContext = createContext<AIBranchContextType | null>(null);1920const useAIBranch = () => {21 const context = useContext(AIBranchContext);2223 if (!context) {24 throw new Error('AIBranch components must be used within AIBranch');25 }2627 return context;28};2930export type AIBranchProps = HTMLAttributes<HTMLDivElement> & {31 defaultBranch?: number;32 onBranchChange?: (branchIndex: number) => void;33};3435export const AIBranch = ({36 defaultBranch = 0,37 onBranchChange,38 className,39 ...props40}: AIBranchProps) => {41 const [currentBranch, setCurrentBranch] = useState(defaultBranch);42 const [branches, setBranches] = useState<ReactElement[]>([]);4344 const handleBranchChange = (newBranch: number) => {45 setCurrentBranch(newBranch);46 onBranchChange?.(newBranch);47 };4849 const goToPrevious = () => {50 const newBranch =51 currentBranch > 0 ? currentBranch - 1 : branches.length - 1;52 handleBranchChange(newBranch);53 };5455 const goToNext = () => {56 const newBranch =57 currentBranch < branches.length - 1 ? currentBranch + 1 : 0;58 handleBranchChange(newBranch);59 };6061 const contextValue: AIBranchContextType = {62 currentBranch,63 totalBranches: branches.length,64 goToPrevious,65 goToNext,66 branches,67 setBranches,68 };6970 return (71 <AIBranchContext.Provider value={contextValue}>72 <div73 className={cn('grid w-full gap-2 [&>div]:pb-0', className)}74 {...props}75 />76 </AIBranchContext.Provider>77 );78};7980export type AIBranchMessagesProps = {81 children: ReactElement | ReactElement[];82};8384export const AIBranchMessages = ({ children }: AIBranchMessagesProps) => {85 const { currentBranch, setBranches, branches } = useAIBranch();86 const childrenArray = Array.isArray(children) ? children : [children];8788 // Use useEffect to update branches when they change89 useEffect(() => {90 if (branches.length !== childrenArray.length) {91 setBranches(childrenArray);92 }93// … truncated
Files it writes
More from dedevs-ui
All 30 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| ai-conversationai-conversation | dedevs-ui | Components | Free | no deps | |
| ai-inputai-input | dedevs-ui | Components | Free | no deps | |
| ai-messageai-message | dedevs-ui | Components | Free | no deps | |
| ai-reasoningai-reasoning | dedevs-ui | Components | Free | no deps | |
| ai-responseai-response | dedevs-ui | Components | Free | no deps | |
| ai-serverai-server | dedevs-ui | Components | Free | no deps | |
| ai-simpleai-simple | dedevs-ui | Components | Free | no deps | |
| ai-sourceai-source | dedevs-ui | Components | Free | no deps |
