Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function SidebarLink({ category, isFilterMode }: SidebarLinkProps
className="mr-2"
/>
)}
<span className="flex-grow">{category.name}</span>
<span className="grow">{category.name}</span>
<IconCaretRight
className={cn(
'transition-transform duration-300 stroke-[#09090b] dark:stroke-[#FAFAFA]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function ChatChatbotDetails() {
</div>
</div>
</div>
<div className="size-24 absolute border-[4px] border-[#388DE2] right-0 top-0 translate-x-[25%] rounded-full translate-y-[-25%] dark:bg-[#131316] bg-white">
<div className="size-24 absolute border-4 border-[#388DE2] right-0 top-0 translate-x-1/4 rounded-full translate-y-1/4 dark:bg-[#131316] bg-white">
<Image
className="transition-opacity duration-300 rounded-full select-none size-full ring-1 ring-zinc-100/10 hover:opacity-80"
src={activeChatbot?.avatar || randomChatbot?.avatar || ''}
Expand Down
25 changes: 25 additions & 0 deletions apps/masterbots.ai/lib/hooks/use-sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use client'

import { usePathname } from 'next/navigation'
import { toSlug } from 'mb-lib'
import { getCategories } from '@/services/hasura'
import { Category, Chatbot } from 'mb-genql'
import * as React from 'react'
Expand Down Expand Up @@ -89,6 +91,29 @@ export function SidebarProvider({ children }: SidebarProviderProps) {
})
}

const pathname = usePathname()
React.useEffect(() => {
const pathParts = pathname.split('/')
if (pathParts.length >= 4 && pathParts[1] === 'c') {
const categorySlug = pathParts[2]
const chatbotName = pathParts[3]

const category = categories?.categoriesChatbots.find(
cat => toSlug(cat.name) === categorySlug
)

if (category) {
setActiveCategory(category.categoryId)
const chatbot = category.chatbots.find(
c => c.chatbot.name.toLowerCase() === chatbotName
)
if (chatbot) {
setActiveChatbot(chatbot.chatbot)
}
}
}
}, [pathname, categories])
Comment on lines 94 to 129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider Debouncing or Throttling URL Parsing

If pathname changes frequently, such as during rapid navigation, consider debouncing or throttling the effect to improve performance.

Comment on lines +98 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Document URL structure assumptions and consider separating concerns.

The effect hook makes assumptions about URL structure (/c/[category]/[chatbot]) but lacks documentation. It also handles multiple state updates which could be separated for better maintainability.

Consider refactoring into smaller, focused functions:

// Add JSDoc to document URL structure
/**
 * Handles sidebar state updates based on URL changes.
 * Expected URL format: /c/[category-slug]/[chatbot-name]
 */
React.useEffect(() => {
  if (!pathname || !categories) return
  
  const updateSidebarState = () => {
    const pathParts = pathname.split('/')
    if (pathParts[1] !== 'c') return
    
    const categorySlug = pathParts[2]
    const chatbotName = pathParts[3]
    
    const category = findCategoryBySlug(categorySlug)
    if (!category) return
    
    updateCategoryState(category)
    updateChatbotState(category, chatbotName)
  }
  
  const findCategoryBySlug = (slug: string) => 
    categories.categoriesChatbots.find(cat => toSlug(cat.name) === slug)
    
  const updateCategoryState = (category: Category) => {
    setActiveCategory(category.categoryId)
    setExpandedCategories([category.categoryId])
  }
  
  const updateChatbotState = (category: Category, chatbotName?: string) => {
    if (!chatbotName) {
      setActiveChatbot(null)
      return
    }
    
    const chatbot = category.chatbots.find(
      c => c.chatbot.name.toLowerCase() === chatbotName
    )
    setActiveChatbot(chatbot?.chatbot ?? null)
  }
  
  updateSidebarState()
}, [pathname, categories])


const toggleChatbotSelection = React.useCallback((chatbotId: number) => {
setSelectedChatbots(prev =>
prev.includes(chatbotId)
Expand Down