"use client"; import { useMemo, useState } from "react"; import { Check, ChevronDown } from "lucide-react"; import { POPULAR_CODES } from "@/lib/assets"; import { RateAsset } from "@/lib/rates"; import { cn } from "@/lib/utils"; import { CurrencyIcon } from "@/components/converter/currency-icon"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; interface CurrencySelectProps { value: string; onChange: (nextCode: string) => void; assets: RateAsset[]; label: string; disabled?: boolean; } function AssetLabel({ asset }: { asset?: RateAsset }) { if (!asset) { return ( Choose currency ); } return ( {asset.code} - {asset.name} {asset.type === "fiat" ? "Fiat" : "Crypto"} {asset.symbol ? ` | ${asset.symbol}` : ""} ); } export function CurrencySelect({ value, onChange, assets, label, disabled }: CurrencySelectProps) { const [open, setOpen] = useState(false); const selectedAsset = useMemo( () => assets.find((asset) => asset.code === value), [assets, value] ); const popularAssets = useMemo( () => POPULAR_CODES.map((code) => assets.find((asset) => asset.code === code)).filter( (asset): asset is RateAsset => Boolean(asset) ), [assets] ); const allAssets = useMemo(() => { const popularSet = new Set(popularAssets.map((asset) => asset.code)); return assets .filter((asset) => !popularSet.has(asset.code)) .sort((a, b) => { if (a.type !== b.type) { return a.type === "fiat" ? -1 : 1; } return a.code.localeCompare(b.code); }); }, [assets, popularAssets]); return (

{label}

No assets found. {popularAssets.length > 0 ? ( {popularAssets.map((asset) => ( { onChange(asset.code); setOpen(false); }} >
{asset.code} - {asset.name} {asset.type === "fiat" ? "Fiat" : "Crypto"} {asset.symbol ? ` | ${asset.symbol}` : ""}
))}
) : null} {allAssets.length > 0 ? : null} {allAssets.map((asset) => ( { onChange(asset.code); setOpen(false); }} >
{asset.code} - {asset.name} {asset.type === "fiat" ? "Fiat" : "Crypto"} {asset.symbol ? ` | ${asset.symbol}` : ""}
))}
); }