import { TextInput, ActionMenu, ActionList, Button } from '@primer/react' import { SearchIcon } from '@primer/octicons-react' import { useRef, useEffect, useState } from 'react' import { ArticleCardItems } from '@/landings/types' import { useTranslation } from '@/languages/components/useTranslation' import styles from './CookBookFilter.module.scss' type Props = { tokens: ArticleCardItems onSearch: (query: string) => void isSearchOpen?: boolean handleFilter: (option: string, type: 'category' | 'complexity') => void handleResetFilter: () => void } export const CookBookFilter = ({ onSearch, isSearchOpen, tokens, handleFilter, handleResetFilter, }: Props) => { const categories: string[] = ['All', ...new Set(tokens.flatMap((item) => item.category || []))] const complexities: string[] = [ 'All', ...new Set(tokens.flatMap((item) => item.complexity || [])), ] const { t } = useTranslation('cookbook_landing') const [selectedCategory, setSelectedCategory] = useState(0) const [selectedComplexity, setSelectedComplexity] = useState(0) const inputRef = useRef(null) const onFilter = (option: string, type: 'category' | 'complexity', index: number) => { if (type === 'category') { setSelectedCategory(index) } else if (type === 'complexity') { setSelectedComplexity(index) } handleFilter(option, type) } const onResetFilter = () => { setSelectedCategory(0) setSelectedComplexity(0) handleResetFilter() if (inputRef.current) { inputRef.current.value = '' } } useEffect(() => { if (isSearchOpen) { inputRef.current?.focus() } }, [isSearchOpen]) return (
e.preventDefault()}> { const query = e.target.value || '' onSearch(query) }} />
{t('category')}:{' '} {categories[selectedCategory]} {categories.map((category, index) => ( onFilter(category, 'category', index)} > {category} ))} {t('complexity')}:{' '} {complexities[selectedComplexity]} {complexities.map((complexity, index) => ( onFilter(complexity, 'complexity', index)} > {complexity} ))}
) }