"use client"; import { CaretSortIcon, CheckIcon } from "@radix-ui/react-icons"; import { cn } from "@/lib/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 { FC, useEffect, useState } from "react"; import Geonames from "geonames.js"; import { FormControl } from "./ui/form"; const frameworks = [ { value: "next.js", label: "Next.js", }, { value: "sveltekit", label: "SvelteKit", }, { value: "nuxt.js", label: "Nuxt.js", }, { value: "remix", label: "Remix", }, { value: "astro", label: "Astro", }, ]; // @ts-ignore const geonames = new Geonames({ username: "thalesandrade", lan: "en", encoding: "JSON", }); interface Props { geoId: number | null; onChange: (country: string, geoId: number) => void; isCountry: boolean; placeholderText: string; searchPlaceholder: string; emptyPlaceholder: string; isDisabled: boolean; } const Geolocation: FC = ({ geoId, onChange, isCountry, placeholderText, searchPlaceholder, emptyPlaceholder, isDisabled, }) => { const [open, setOpen] = useState(false); const [currentValue, setCurrentValue] = useState(null); const [options, setOptions] = useState< { countryName: string; geonameId: number; name: string }[] >([]); useEffect(() => { try { const data = () => { isCountry ? geonames.countryInfo({}).then((res: any) => { console.log(res); setOptions(res.geonames); }) : geonames.children({ geonameId: geoId }).then((res: any) => { if (res.totalResultsCount) setOptions(res.geonames); }); }; data(); } catch (err) { console.error(err); } }, [geoId, isCountry]); const currentPlaceholderText = currentValue && isCountry ? options.find((option: any) => option.geonameId === currentValue) ?.countryName : currentValue ? options.find((option: any) => option.geonameId === currentValue)?.name : placeholderText; return ( {emptyPlaceholder} {options.map((v: any, index) => ( { setCurrentValue( v.geonameId === currentValue ? null : v.geonameId, ); const name = isCountry ? v.countryName : v.name; onChange( v.geonameId === currentValue ? "" : name, v.geonameId === currentValue ? null : v.geonameId, ); setOpen(false); }} > {isCountry ? v.countryName : v.name} ))} ); }; export default Geolocation;