import { Button } from "@/components/ui/button" import { selectedCompanyAtom, useCurrentCompany } from "@/hooks/useCurrentCompany" import { useSetAtom } from "jotai" import { Building2, Check, ChevronDown } from "lucide-react" import { useState } from "react" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { cn } from "@/lib/utils" import _ from "@/lib/translate" import { selectedBankAccountAtom } from "./bankRecAtoms" import { useFrappeGetDocList } from "frappe-react-sdk" import ErrorBanner from "@/components/ui/error-banner" const CompanySelector = ({ onChange }: { onChange?: (company: string) => void }) => { const [open, setOpen] = useState(false) const [searchQuery, setSearchQuery] = useState("") const { data: companies, error } = useFrappeGetDocList("Company", { limit: 0, fields: ["name"], }, 'company_list', { revalidateOnFocus: false, revalidateOnReconnect: false, }) const options = companies?.map((company: { name: string }) => company.name) || [] const setSelectedCompany = useSetAtom(selectedCompanyAtom) const setSelectedBankAccount = useSetAtom(selectedBankAccountAtom) const selectedCompany = useCurrentCompany() const handleSelectCompany = (company: string) => { setSelectedCompany(company) setSearchQuery("") setOpen(false) // Only reset bank account if the company is changed if (selectedCompany !== company) { setSelectedBankAccount(null) onChange?.(company) } } if (error) { return } return ( {options.length > 5 && } {_("No company found.")} {options.map((option: string) => ( { handleSelectCompany(currentValue) }} > {option} ))} ) } export default CompanySelector