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" const CompanySelector = ({ onChange }: { onChange?: (company: string) => void }) => { const [open, setOpen] = useState(false) const [searchQuery, setSearchQuery] = useState("") // eslint-disable-next-line @typescript-eslint/no-explicit-any const options = window.frappe?.boot?.docs?.filter((doc: Record) => doc.doctype === ":Company").map((company: Record) => 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) } } return ( {options.length > 5 && } {_("No company found.")} {options.map((option: string) => ( { handleSelectCompany(currentValue) }} > {option} ))} ) } export default CompanySelector