Repository navigation
Expand file tree
/
Copy pathsymbol-selector.tsx
More file actions
72 lines (66 loc) · 1.97 KB
/
Copy pathsymbol-selector.tsx
File metadata and controls
72 lines (66 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use client';
import { useMemo } from 'react';
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import type { ActiveSymbol } from '@deriv/core';
import { getSubmarketDisplayName } from '@/lib/active-symbols-display-names';
interface SymbolSelectorProps {
symbols: ActiveSymbol[];
activeSymbol: ActiveSymbol | null;
onSymbolChange: (symbol: string) => void;
}
type SubmarketGroup = { displayName: string; symbols: ActiveSymbol[] };
function groupBySubmarket(symbols: ActiveSymbol[]): Map<string, SubmarketGroup> {
const groups = new Map<string, SubmarketGroup>();
for (const symbol of symbols) {
const key = symbol.submarket;
const existing = groups.get(key);
if (existing) {
existing.symbols.push(symbol);
} else {
const displayName =
symbol.submarket_display_name ?? getSubmarketDisplayName(symbol.submarket);
groups.set(key, { displayName, symbols: [symbol] });
}
}
return groups;
}
export function SymbolSelector({
symbols,
activeSymbol,
onSymbolChange,
}: SymbolSelectorProps) {
const grouped = useMemo(() => groupBySubmarket(symbols), [symbols]);
return (
<Select
value={activeSymbol?.underlying_symbol ?? ''}
onValueChange={onSymbolChange}
>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select a symbol" />
</SelectTrigger>
<SelectContent>
{Array.from(grouped.entries()).map(([submarket, { displayName, symbols: group }]) => (
<SelectGroup key={submarket}>
<SelectLabel>{displayName}</SelectLabel>
{group.map((symbol) => (
<SelectItem
key={symbol.underlying_symbol}
value={symbol.underlying_symbol}
>
{symbol.underlying_symbol_name}
</SelectItem>
))}
</SelectGroup>
))}
</SelectContent>
</Select>
);
}