import { ChevronDown } from 'lucide-react' import { CategoryIcon } from '@/app/groups/[groupId]/expenses/category-icon' import { Button } from '@/components/ui/button' import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, } from '@/components/ui/command' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' import { Category } from '@prisma/client' import { useState } from 'react' type Props = { categories: Category[] onValueChange: (categoryId: Category['id']) => void defaultValue: Category['id'] } export function CategorySelector({ categories, onValueChange, defaultValue, }: Props) { const [open, setOpen] = useState(false) const [value, setValue] = useState(defaultValue) const categoriesByGroup = categories.reduce>( (acc, category) => ({ ...acc, [category.grouping]: [...(acc[category.grouping] ?? []), category], }), {}, ) const selectedCategory = categories.find((category) => category.id === value) ?? categories[0] return ( No category found.
{Object.entries(categoriesByGroup).map( ([group, groupCategories], index) => ( {groupCategories.map((category) => ( { const id = Number(currentValue.split(' ')[0]) setValue(id) onValueChange(id) setOpen(false) }} >
{category.name}
))}
), )}
) }