import { FC, useMemo } from "react"; import { assetInfoStore } from "../../store/assetInfoStore"; import { swapStore } from "../../store/swapStore"; import { AssetChainLogos } from "../../common/AssetChainLogos"; import { IOType } from "../../constants/constants"; type AssetSelectorProps = { isFromAsset: boolean; openDropdown: string | null; setOpenDropdown: (id: string | null) => void; dropdownId: string; }; export const AssetSelector: FC = ({ isFromAsset, openDropdown, setOpenDropdown, dropdownId, }) => { const { assets, allChains } = assetInfoStore(); const { inputAsset, outputAsset, setAsset } = swapStore(); const currentAsset = isFromAsset ? inputAsset : outputAsset; const assetOptions = useMemo(() => { if (!assets) return []; const otherAsset = isFromAsset ? outputAsset : inputAsset; return Object.values(assets) .filter((asset) => { if (otherAsset && asset.tokenAddress === otherAsset.tokenAddress && asset.chain === otherAsset.chain) { return false; } return true; }) .map((asset) => { const chainData = allChains ? allChains[asset.chain] : null; return { value: asset.tokenAddress, label: asset.symbol, asset, chainLogo: chainData ? chainData.networkLogo : "", }; }); }, [assets, allChains, isFromAsset, inputAsset, outputAsset]); const handleSelect = (option: any) => { setOpenDropdown(null); if (isFromAsset) { setAsset(IOType.input, option.asset); } else { setAsset(IOType.output, option.asset); } }; return (
setOpenDropdown(openDropdown === dropdownId ? null : dropdownId)} > {currentAsset ? (
{currentAsset.symbol}
) : ( Select asset... )}
{openDropdown === dropdownId && (
{assetOptions.map((option) => (
handleSelect(option)} className="flex items-center gap-2 px-4 py-2 cursor-pointer hover:bg-gray-100" > {option.label}
))}
)}
); };