import { FilterListRounded, RadioButtonChecked, RadioButtonUnchecked, } from '@mui/icons-material' import { useMemo, useState } from 'react' import { ButtonPopupProps, FilterFn, TypedOption } from '@dao-dao/types' import { ButtonLink } from '../components' type UseButtonPopupFilterOptions = { data: T[] options: O[] initialIndex?: number } type UseButtonPopupFilterReturn = { buttonPopupProps: Pick< ButtonPopupProps, 'sections' | 'sectionClassName' | 'trigger' | 'ButtonLink' > filteredData: T[] selectedOption: O } // Pass an array of data and sort options, and get `buttonPopupProps` (for // passing to `ButtonPopup`) and memoized `filteredData`. export const useButtonPopupFilter = < T extends unknown, O extends TypedOption>, >({ data, options, initialIndex = 0, }: UseButtonPopupFilterOptions): UseButtonPopupFilterReturn => { const [selectedIndex, setSelectedIndex] = useState(initialIndex) const selectedOption = options[selectedIndex] const filteredData = useMemo( () => (selectedOption ? data.filter(selectedOption.value) : data), [data, selectedOption] ) return { buttonPopupProps: { trigger: { type: 'button', props: { variant: 'ghost', children: ( <>

{selectedOption?.label}

), }, }, sectionClassName: 'gap-1', sections: [ { buttons: options.map(({ label }, index) => ({ Icon: selectedIndex === index ? RadioButtonChecked : RadioButtonUnchecked, pressed: selectedIndex === index, label, onClick: () => setSelectedIndex(index), })), }, ], // No button links, so using the stateless component is fine here. ButtonLink, }, filteredData, selectedOption, } }