| 1 | import React from "react"; |
| 2 | import { cn } |
| 3 | import { currencies as |
| 4 | import { |
| 5 | Select, |
| 6 | SelectContent, |
| 7 | SelectGroup, |
| 8 | SelectItem, |
| 9 | SelectTrigger, |
| 10 | SelectValue, |
| 11 | } from "@/components/ui/select"; |
| 12 | import { type SelectProps |
| 13 | import { allCurrencies } |
| 14 | import { useVirtualizer } |
| 15 | |
| 16 | export interface Currency |
| 17 | code: string; |
| 18 | decimals: number; |
| 19 | name: string; |
| 20 | number: string; |
| 21 | symbol?: string |
| 22 | } |
| 23 | |
| 24 | interface CurrencySelectProps extends |
| 25 | onValueChange?: (value |
| 26 | onCurrencySelect?: (currency |
| 27 | name: string; |
| 28 | placeholder?: string |
| 29 | currencies?: "custom |
| 30 | variant?: "default |
| 31 | valid?: boolean |
| 32 | } |
| 33 | |
| 34 | const CurrencySelect = React |
| 35 | ( |
| 36 | { |
| 37 | value, |
| 38 | onValueChange, |
| 39 | onCurrencySelect, |
| 40 | name, |
| 41 | placeholder = "Select currency" |
| 42 | currencies = "withdrawal" |
| 43 | variant = "default" |
| 44 | valid = true, |
| 45 | ...props |
| 46 | }, |
| 47 | ref, |
| 48 | ) => { |
| 49 | const [selectedCurrency, |
| 50 | React.useState<Currency |
| 51 | |
| 52 | const uniqueCurrencies = |
| 53 | const currencyMap = |
| 54 | |
| 55 | AllCurrencies.all.forEach |
| 56 | if (currency.code |
| 57 | let shouldInclude = |
| 58 | |
| 59 | switch (currencies) { |
| 60 | case "custom": |
| 61 | shouldInclude = allCurrencies. |
| 62 | break; |
| 63 | case "all": |
| 64 | shouldInclude = !allCurrencies |
| 65 | break; |
| 66 | default: |
| 67 | shouldInclude = !allCurrencies |
| 68 | } |
| 69 | |
| 70 | if (shouldInclude) { |
| 71 | // Special handling for Euro |
| 72 | if (currency.code |
| 73 | currencyMap.set(currency |
| 74 | code: currency.code |
| 75 | name: "Euro", |
| 76 | symbol: currency.symbol |
| 77 | decimals: currency.decimals |
| 78 | number: currency.number |
| 79 | }); |
| 80 | } else { |
| 81 | currencyMap.set(currency |
| 82 | code: currency.code |
| 83 | name: currency.name |
| 84 | symbol: currency.symbol |
| 85 | decimals: currency.decimals |
| 86 | number: currency.number |
| 87 | }); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | }); |
| 92 | |
| 93 | // Convert the map to an array and sort by currency name |
| 94 | return Array.from |
| 95 | a.name.localeCompare |
| 96 | ); |
| 97 | }, [currencies]); |
| 98 | |
| 99 | const handleValueChange = |
| 100 | const fullCurrencyData = |
| 101 | (curr) => |
| 102 | ); |
| 103 | if (fullCurrencyData) { |
| 104 | setSelectedCurrency(fullCurrencyData); |
| 105 | if (onValueChange) { |
| 106 | onValueChange(newValue); |
| 107 | } |
| 108 | if (onCurrencySelect) { |
| 109 | onCurrencySelect(fullCurrencyData); |
| 110 | } |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | void selectedCurrency; |
| 115 | |
| 116 | return ( |
| 117 | <Select |
| 118 | value={value} |
| 119 | onValueChange={handleValueChange} |
| 120 | {...props} |
| 121 | name={name} |
| 122 | data-valid={valid} |
| 123 | > |
| 124 | <SelectTrigger |
| 125 | className={cn(" |
| 126 | data-valid={valid} |
| 127 | ref={ref} |
| 128 | > |
| 129 | {value && variant |
| 130 | <SelectValue placeholder={ |
| 131 | <span>{value}</ |
| 132 | </SelectValue> |
| 133 | ) : ( |
| 134 | <SelectValue placeholder={ |
| 135 | )} |
| 136 | </SelectTrigger> |
| 137 | <SelectContent className=" |
| 138 | <SelectGroup> |
| 139 | <VirtualizedCurrencyList |
| 140 | currencies={uniqueCurrencies} |
| 141 | selectedValue={value} |
| 142 | /> |
| 143 | </SelectGroup> |
| 144 | </SelectContent> |
| 145 | </Select> |
| 146 | ); |
| 147 | }, |
| 148 | ); |
| 149 | |
| 150 | // Virtualized currency list component |
| 151 | const VirtualizedCurrencyList = React |
| 152 | currencies: Currency |
| 153 | selectedValue: string |
| 154 | }>(({ currencies, |
| 155 | const parentRef = |
| 156 | |
| 157 | // Separate selected currency from the list |
| 158 | const selectedCurrency = |
| 159 | ? currencies.find( |
| 160 | : null; |
| 161 | |
| 162 | const nonSelectedCurrencies = |
| 163 | (curr) => |
| 164 | ); |
| 165 | |
| 166 | const virtualizer = |
| 167 | count: nonSelectedCurrencies.length, |
| 168 | getScrollElement: () => |
| 169 | estimateSize: () => |
| 170 | overscan: 5, // |
| 171 | }); |
| 172 | return ( |
| 173 | <div |
| 174 | ref={parentRef} |
| 175 | className="max-h-[300px] overflow-auto w-full" |
| 176 | style={{ |
| 177 | height: `${Math. |
| 178 | }} |
| 179 | > |
| 180 | {/* Force render selected item at the top */ |
| 181 | {selectedCurrency && ( |
| 182 | <div className="sticky top-0 z-10 bg-background border-b w-full |
| 183 | <SelectItem value={ |
| 184 | <div className="flex items-center w-full gap-2 |
| 185 | <span className="text-sm text-muted-foreground w-8 text-left |
| 186 | {selectedCurrency.code} |
| 187 | </span> |
| 188 | <span className="hidden |
| 189 | <span>{selectedCurrency.name |
| 190 | </div> |
| 191 | </SelectItem> |
| 192 | </div> |
| 193 | )} |
| 194 | |
| 195 | {/* Virtualized list for non-selected items */ |
| 196 | <div |
| 197 | style={{ |
| 198 | height: `${virtualizer. |
| 199 | width: "100%", |
| 200 | position: "relative", |
| 201 | }} |
| 202 | > |
| 203 | {virtualizer.getVirtualItems(). |
| 204 | const currency = |
| 205 | return ( |
| 206 | <div |
| 207 | key={virtualItem.key |
| 208 | style={{ |
| 209 | position: "absolute", |
| 210 | top: 0, |
| 211 | left: 0, |
| 212 | width: "100%", |
| 213 | height: `${virtualItem. |
| 214 | transform: `translateY(${virtualItem |
| 215 | }} |
| 216 | > |
| 217 | <SelectItem value={ |
| 218 | <div className="flex items-center w-full gap-2 |
| 219 | <span className="text-sm text-muted-foreground w-8 text-left |
| 220 | {currency?.code} |
| 221 | </span> |
| 222 | <span className="hidden |
| 223 | <span>{currency?.name |
| 224 | </div> |
| 225 | </SelectItem> |
| 226 | </div> |
| 227 | ); |
| 228 | })} |
| 229 | </div> |
| 230 | </div> |
| 231 | ); |
| 232 | }); |
| 233 | |
| 234 | VirtualizedCurrencyList.displayName = " |
| 235 | |
| 236 | CurrencySelect.displayName = " |
| 237 | |
| 238 | export { CurrencySelect }; |