-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLayerMenu.tsx
More file actions
157 lines (146 loc) · 5.04 KB
/
Copy pathLayerMenu.tsx
File metadata and controls
157 lines (146 loc) · 5.04 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { FC, PropsWithChildren, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Typography from '@mui/material/Typography'
import KeyboardArrowRightIcon from '@mui/icons-material/KeyboardArrowRight'
import { Tooltip } from '@oasisprotocol/ui-library/src/components/tooltip'
import { COLORS } from '../../../styles/theme/colors'
import { Layer } from '../../../oasis-nexus/api'
import { getLayerLabels } from '../../utils/content'
import { isScopeHidden, mergeNetworksInLayerSelector, RouteUtils } from '../../utils/route-utils'
import { Network } from '../../../types/network'
import { orderByLayer } from '../../../types/layers'
import { useScreenSize } from '../../hooks/useScreensize'
import { useScopeParam } from '../../hooks/useScopeParam'
import { SearchScope } from '../../../types/searchScope'
import { MenuItem } from '../LayerPicker/MenuItem'
type BaseLayerMenuItemProps = {
divider: boolean
targetScope: SearchScope
}
const LayerMenuItemCaption: FC<PropsWithChildren> = ({ children }) => (
<Typography
component="span"
sx={{ fontSize: '10px', fontStyle: 'italic', color: COLORS.grayMedium, ml: 2 }}
>
{children}
</Typography>
)
export const DisabledLayerMenuItem: FC<BaseLayerMenuItemProps> = ({ divider, targetScope }) => {
const { isTablet } = useScreenSize()
const { t } = useTranslation()
const labels = getLayerLabels(t)
return (
<Tooltip title={t('layerPicker.comingSoonTitle')}>
{/* Div is needed because we need an element with enabled pointer-events to make Tooltip work */}
<div>
<MenuItem disabled divider={divider}>
<div className="flex-auto">
{labels[targetScope.layer]}
{isTablet && <LayerMenuItemCaption>{t('layerPicker.comingSoonLabel')}</LayerMenuItemCaption>}
</div>
</MenuItem>
</div>
</Tooltip>
)
}
type LayerMenuItemProps = LayerMenuProps &
BaseLayerMenuItemProps & {
hoveredScope?: SearchScope
setHoveredScope: (scope?: SearchScope) => void
}
export const LayerMenuItem: FC<LayerMenuItemProps> = ({
divider,
targetScope,
selectedScope,
setHoveredScope,
setSelectedScope,
}) => {
const { t } = useTranslation()
const labels = getLayerLabels(t)
const activeScope = useScopeParam()!
const isSelected =
targetScope.network === selectedScope?.network && targetScope.layer === selectedScope?.layer
const isActive = targetScope.network === activeScope.network && targetScope.layer === activeScope.layer
return (
<MenuItem
divider={divider}
onMouseEnter={() => {
setHoveredScope(targetScope)
}}
onMouseLeave={() => {
setHoveredScope()
}}
onClick={() => {
setSelectedScope(targetScope)
}}
selected={isSelected}
>
<div className="flex-auto">
{labels[targetScope.layer]}
{isActive && <LayerMenuItemCaption>{t('layerPicker.active')}</LayerMenuItemCaption>}
</div>
{isSelected && <KeyboardArrowRightIcon />}
</MenuItem>
)
}
type LayerMenuProps = {
selectedNetwork?: Network
selectedScope?: SearchScope
setSelectedScope: (scope: SearchScope) => void
}
type LayerMenuOption = {
scope: SearchScope
enabled: boolean
}
const getOptionsForNetwork = (network: Network, activeScope?: SearchScope | undefined): LayerMenuOption[] =>
Object.values(Layer)
.sort(orderByLayer)
// Don't show hidden layers, unless we are already viewing them.
.filter(
layer =>
!isScopeHidden({ network, layer }) ||
(layer === activeScope?.layer && network === activeScope?.network),
)
.map(layer => ({
scope: {
network,
layer,
},
enabled: RouteUtils.getAllLayersForNetwork(network).enabled.includes(layer),
}))
export const LayerMenu: FC<LayerMenuProps> = ({ selectedNetwork, selectedScope, setSelectedScope }) => {
const activeScope = useScopeParam()!
const [hoveredScope, setHoveredScope] = useState<undefined | SearchScope>()
const options = mergeNetworksInLayerSelector
? RouteUtils.getVisibleScopes(activeScope).map(scope => ({ scope, enabled: true }))
: getOptionsForNetwork(selectedNetwork ?? activeScope.network, activeScope)
return (
<ul role="menu">
{options.map((option, index) => {
if (!option.enabled) {
if (selectedNetwork === 'localnet') return null
return (
<DisabledLayerMenuItem
divider={index !== options.length - 1}
key={option.scope.network + option.scope.layer}
targetScope={option.scope}
/>
)
} else {
return (
<LayerMenuItem
divider={index !== options.length - 1}
hoveredScope={hoveredScope}
key={option.scope.network + option.scope.layer}
targetScope={option.scope}
selectedScope={selectedScope}
selectedNetwork={selectedNetwork}
setHoveredScope={setHoveredScope}
setSelectedScope={setSelectedScope}
/>
)
}
})}
</ul>
)
}