|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { CaretDownIcon } from '@phosphor-icons/react'; |
| 4 | +import { usePathname } from 'next/navigation'; |
| 5 | +import { useMemo } from 'react'; |
| 6 | +import { Button } from '@/components/ui/button'; |
| 7 | +import { |
| 8 | + DropdownMenu, |
| 9 | + DropdownMenuContent, |
| 10 | + DropdownMenuItem, |
| 11 | + DropdownMenuTrigger, |
| 12 | +} from '@/components/ui/dropdown-menu'; |
| 13 | +import { useWebsites } from '@/hooks/use-websites'; |
| 14 | +import { cn } from '@/lib/utils'; |
| 15 | +import { |
| 16 | + getDefaultCategory, |
| 17 | + getNavigationWithWebsites, |
| 18 | +} from './navigation-config'; |
| 19 | + |
| 20 | +interface MobileCategorySelectorProps { |
| 21 | + onCategoryChange?: (categoryId: string) => void; |
| 22 | + selectedCategory?: string; |
| 23 | +} |
| 24 | + |
| 25 | +export function MobileCategorySelector({ |
| 26 | + onCategoryChange, |
| 27 | + selectedCategory, |
| 28 | +}: MobileCategorySelectorProps) { |
| 29 | + const pathname = usePathname(); |
| 30 | + const { websites, isLoading: isLoadingWebsites } = useWebsites(); |
| 31 | + |
| 32 | + const { categories, defaultCategory } = useMemo(() => { |
| 33 | + const config = getNavigationWithWebsites( |
| 34 | + pathname, |
| 35 | + websites, |
| 36 | + isLoadingWebsites |
| 37 | + ); |
| 38 | + const defaultCat = getDefaultCategory(pathname); |
| 39 | + return { categories: config.categories, defaultCategory: defaultCat }; |
| 40 | + }, [pathname, websites, isLoadingWebsites]); |
| 41 | + |
| 42 | + const activeCategory = selectedCategory || defaultCategory; |
| 43 | + const currentCategory = categories.find((cat) => cat.id === activeCategory); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="border-border border-b p-3 md:hidden"> |
| 47 | + <DropdownMenu> |
| 48 | + <DropdownMenuTrigger asChild> |
| 49 | + <Button |
| 50 | + className="flex h-10 w-full items-center justify-between px-3" |
| 51 | + type="button" |
| 52 | + variant="outline" |
| 53 | + > |
| 54 | + <div className="flex items-center gap-2"> |
| 55 | + {currentCategory?.icon && ( |
| 56 | + <currentCategory.icon className="h-4 w-4" weight="duotone" /> |
| 57 | + )} |
| 58 | + <span>{currentCategory?.name || 'Select Category'}</span> |
| 59 | + </div> |
| 60 | + <CaretDownIcon className="h-4 w-4" weight="fill" /> |
| 61 | + </Button> |
| 62 | + </DropdownMenuTrigger> |
| 63 | + <DropdownMenuContent className="w-full min-w-[var(--radix-dropdown-menu-trigger-width)]"> |
| 64 | + {categories.map((category) => { |
| 65 | + const Icon = category.icon; |
| 66 | + const isActive = activeCategory === category.id; |
| 67 | + return ( |
| 68 | + <DropdownMenuItem |
| 69 | + className={cn( |
| 70 | + 'flex cursor-pointer items-center gap-2', |
| 71 | + isActive && 'bg-muted' |
| 72 | + )} |
| 73 | + key={category.id} |
| 74 | + onClick={() => onCategoryChange?.(category.id)} |
| 75 | + > |
| 76 | + <Icon |
| 77 | + className={cn( |
| 78 | + 'h-4 w-4', |
| 79 | + isActive ? 'text-primary' : 'text-muted-foreground' |
| 80 | + )} |
| 81 | + weight={isActive ? 'fill' : 'duotone'} |
| 82 | + /> |
| 83 | + <span>{category.name}</span> |
| 84 | + </DropdownMenuItem> |
| 85 | + ); |
| 86 | + })} |
| 87 | + </DropdownMenuContent> |
| 88 | + </DropdownMenu> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +} |
0 commit comments