import { useToggle } from 'ahooks' import Fuse from 'fuse.js' import { ArrowDownAZIcon, ArrowUpAZIcon, BookIcon, CheckIcon, ChevronsUpDownIcon } from 'lucide-react' import { forwardRef, use, useMemo, useState, type InputHTMLAttributes } from 'react' import { Button } from '@repo/ui/components/ui/button' import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@repo/ui/components/ui/command' import { InputGroup, InputGroupAddon, InputGroupInput } from '@repo/ui/components/ui/input-group' import { Popover, PopoverContent, PopoverTrigger } from '@repo/ui/components/ui/popover' import { cn } from '@repo/ui/lib/utils' import { Abbr } from '@repo/ui/components/abbr' import { Kbd } from '@repo/ui/components/ui/kbd' import { type Course } from './data' interface CoursePickerProps extends Omit< InputHTMLAttributes, 'value' | 'onChange' > { value?: Course options: Promise<{ hits: any[] }> onChange?: (value: any) => void error?: any } const normalize = (value: string) => value .toLowerCase() .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .replace(/[^a-z0-9]/g, '') export const CoursePicker = forwardRef( ({ value, onChange, options, error, ...props }, ref) => { const { hits } = use(options) const [search, setSearch] = useState('') const [open, { set }] = useToggle() const [sort, { toggle }] = useToggle('a-z', 'z-a') const fuse = useMemo(() => { return new Fuse(hits, { keys: ['name'], threshold: 0.3, includeMatches: true, getFn: (obj, path) => { const value = obj[path as keyof typeof obj] return typeof value === 'string' ? normalize(value) : value } }) }, [hits]) const filtered = useMemo(() => { if (!search) { return [...hits].sort((a, b) => { const comparison = a.name.localeCompare(b.name) return sort === 'a-z' ? comparison : -comparison }) } return fuse.search(search).map(({ item, matches }) => ({ ...item, matches })) }, [search, fuse, hits, sort]) return (
{/* Force rerender to reset the scroll position */} Nenhum resultado encontrado. {filtered .filter( ({ metadata__unit_price = 0 }) => metadata__unit_price > 0 ) .map( ({ id, name, access_period, metadata__unit_price: unit_price, quantity = null }) => { return ( { onChange?.({ id, name, access_period: Number(access_period), unit_price: Number(unit_price) }) set(false) }} > {name} {quantity && ( {quantity}x )} ) } )}
) } )