-
Notifications
You must be signed in to change notification settings - Fork 1
fix:sideBar updating URL #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1f53765
0516ea5
f364718
6065154
d6bea16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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' | ||
|
|
@@ -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') { | ||
AnoukRImola marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
| } | ||
AnoukRImola marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| }, [pathname, categories]) | ||
|
Comment on lines
94
to
129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider Debouncing or Throttling URL Parsing If
AnoukRImola marked this conversation as resolved.
Show resolved
Hide resolved
AndlerRL marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+98
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.