import * as React from "react"; import { Check, ChevronsUpDown, Search } from "lucide-react"; import { cn } from "@/components/ui/utils"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { getAllHsnSacCodes, HsnSacCode } from "@/services/hsnSacCodeApi"; interface HsnSacSelectorProps { value: string; onChange: (value: string) => void; type: "HSN" | "SAC"; placeholder?: string; disabled?: boolean; className?: string; } export function HsnSacSelector({ value, onChange, type, placeholder, disabled, className, }: HsnSacSelectorProps) { const [open, setOpen] = React.useState(false); const [allCodes, setAllCodes] = React.useState([]); React.useEffect(() => { const fetchCodes = async () => { try { const response = await getAllHsnSacCodes(true, 1, 1000); setAllCodes(response.codes); } catch (error) { console.error("Failed to fetch HSN/SAC codes for selector:", error); } }; if (open && allCodes.length === 0) { fetchCodes(); } }, [open, allCodes.length]); const filteredCodes = React.useMemo(() => { return allCodes.filter((c) => c.type === type); }, [allCodes, type]); return (

No {type} code found

Try a different search term

{filteredCodes.map((code) => ( { onChange(currentValue); setOpen(false); }} className="flex flex-col items-start gap-1 p-2.5 rounded-md aria-selected:bg-slate-100 transition-colors cursor-pointer mb-1 last:mb-0" >
{code.code}
{code.description && ( {code.description} )}
))}
); }