"use client"; import { useMemo, useState } from "react"; import { AnimatePresence, motion, useReducedMotion } from "framer-motion"; import { Check, ChevronDown } from "lucide-react"; import { POPULAR_CODES } from "@/lib/assets"; import { getDisplaySymbol } from "@/lib/currency-display"; 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 ); } const displaySymbol = getDisplaySymbol(asset); return ( {asset.code} - {asset.name} {asset.type === "fiat" ? "Fiat" : "Crypto"} {displaySymbol ? ` | ${displaySymbol}` : ""} ); } export function CurrencySelect({ value, onChange, assets, label, disabled }: CurrencySelectProps) { const [open, setOpen] = useState(false); const shouldReduceMotion = useReducedMotion(); 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) => { const displaySymbol = getDisplaySymbol(asset); return ( { onChange(asset.code); setOpen(false); }} >
{asset.code} - {asset.name} {asset.type === "fiat" ? "Fiat" : "Crypto"} {displaySymbol ? ` | ${displaySymbol}` : ""}
); })}
) : null} {allAssets.length > 0 ? : null} {allAssets.map((asset) => { const displaySymbol = getDisplaySymbol(asset); return ( { onChange(asset.code); setOpen(false); }} >
{asset.code} - {asset.name} {asset.type === "fiat" ? "Fiat" : "Crypto"} {displaySymbol ? ` | ${displaySymbol}` : ""}
); })}
); }