-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathDisplaySettings.tsx
281 lines (268 loc) · 9.33 KB
/
DisplaySettings.tsx
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import ButtonGroup from '@components/forms/ButtonGroup'
import Select from '@components/forms/Select'
import ChartMiddleOBLeft from '@components/icons/ChartMiddleOBLeft'
import ChartMiddleOBRight from '@components/icons/ChartMiddleOBRight'
import ChartOnLeft from '@components/icons/ChartOnLeft'
import ChartOnRight from '@components/icons/ChartOnRight'
import Tooltip from '@components/shared/Tooltip'
import { TradeLayout } from '@components/trade/TradeAdvancedPage'
// import dayjs from 'dayjs'
import { ReactNode, useEffect, useState } from 'react'
// import { useRouter } from 'next/router'
// import { useCallback } from 'react'
import dayjs from 'dayjs'
import useLocalStorageState from 'hooks/useLocalStorageState'
import { useTranslation } from 'next-i18next'
import { useTheme } from 'next-themes'
import { useCallback } from 'react'
import { useRouter } from 'next/router'
import {
NOTIFICATION_POSITION_KEY,
SHOW_ANNOUNCEMENTS_KEY,
SIZE_INPUT_UI_KEY,
TRADE_CHART_UI_KEY,
TRADE_LAYOUT_KEY,
} from 'utils/constants'
import mangoStore from '@store/mangoStore'
import { CUSTOM_SKINS } from 'utils/theme'
import { SETTINGS_BUTTON_TITLE_CLASSES } from './AccountSettings'
import Switch from '@components/forms/Switch'
const NOTIFICATION_POSITIONS = [
'bottom-left',
'bottom-right',
'top-left',
'top-right',
]
const TRADING_CHARTS = ['trading-view']
const TRADE_FORM_UI = ['slider', 'buttons']
const LANGS = [
{ locale: 'en', name: 'english', description: 'english' },
{ locale: 'pt', name: 'Português', description: 'portuguese' },
// { locale: 'ru', name: 'russian', description: 'russian' },
// { locale: 'es', name: 'spanish', description: 'spanish' },
{
locale: 'zh_tw',
name: 'chinese-traditional',
description: 'traditional chinese',
},
// { locale: 'zh', name: 'chinese', description: 'simplified chinese' },
]
const DEFAULT_THEMES = [
'light',
'medium',
'dark',
'high-contrast',
'mango-classic',
'avocado',
'banana',
'blueberry',
'lychee',
'olive',
]
const DisplaySettings = () => {
const { t } = useTranslation(['common', 'settings'])
const { theme, setTheme } = useTheme()
const [themes, setThemes] = useState(DEFAULT_THEMES)
const nfts = mangoStore((s) => s.wallet.nfts.data)
const [savedLanguage, setSavedLanguage] = useLocalStorageState(
'language',
'en',
)
const router = useRouter()
const { pathname, asPath, query } = router
const [notificationPosition, setNotificationPosition] = useLocalStorageState(
NOTIFICATION_POSITION_KEY,
'bottom-left',
)
const [tradeFormUi, setTradeFormUi] = useLocalStorageState(
SIZE_INPUT_UI_KEY,
'slider',
)
const [tradeChartUi, setTradeChartUi] = useLocalStorageState(
TRADE_CHART_UI_KEY,
'trading-view',
)
const [, setTradeLayout] = useLocalStorageState(TRADE_LAYOUT_KEY, 'chartLeft')
const [showAnnouncements, setShowAnnouncements] = useLocalStorageState(
SHOW_ANNOUNCEMENTS_KEY,
true,
)
// add nft skins to theme selection list
useEffect(() => {
if (nfts.length) {
const customThemes = []
const ownedCollections = [
...new Set(nfts.map((x) => x.collectionAddress)),
]
for (const collection of ownedCollections) {
for (const themeKey in CUSTOM_SKINS) {
if (CUSTOM_SKINS[themeKey] === collection) {
customThemes.push(themeKey)
}
}
}
setThemes(
customThemes.length
? [...customThemes, ...DEFAULT_THEMES]
: [...DEFAULT_THEMES],
)
} else {
setThemes([...DEFAULT_THEMES])
}
}, [nfts])
const handleLangChange = useCallback(
(l: string) => {
setSavedLanguage(l)
router.push({ pathname, query }, asPath, { locale: l })
dayjs.locale(l == 'zh_tw' ? 'zh-tw' : l)
},
[router, pathname, query, asPath, setSavedLanguage],
)
return (
<div className="border-b border-th-bkg-3">
<div className="flex flex-col border-t border-th-bkg-3 py-4 md:flex-row md:items-center md:justify-between md:px-4">
<p className={`mb-2 md:mb-0 ${SETTINGS_BUTTON_TITLE_CLASSES}`}>
{t('settings:theme')}
</p>
<div className="w-full min-w-[140px] md:w-auto">
<Select
value={theme || DEFAULT_THEMES[0]}
onChange={(t: string) => {
setTheme(t)
}}
className="w-full"
>
{themes.map((theme) => (
<Select.Option key={theme} value={t(`settings:${theme}`)}>
{t(`settings:${theme}`)}
</Select.Option>
))}
</Select>
</div>
</div>
<div className="flex flex-col border-t border-th-bkg-3 py-4 md:flex-row md:items-center md:justify-between md:px-4">
<p className={`mb-2 md:mb-0 ${SETTINGS_BUTTON_TITLE_CLASSES}`}>
{t('settings:language')}
</p>
<div className="w-full min-w-[280px] md:w-auto md:pl-4">
<ButtonGroup
activeValue={savedLanguage}
onChange={(l) => handleLangChange(l)}
values={LANGS.map((val) => val.locale)}
names={LANGS.map((val) => t(`settings:${val.name}`))}
/>
</div>
</div>
<div className="hidden border-t border-th-bkg-3 py-4 md:flex md:flex-row md:items-center md:justify-between md:px-4">
<p className={`mb-2 md:mb-0 ${SETTINGS_BUTTON_TITLE_CLASSES}`}>
{t('settings:notification-position')}
</p>
<div className="w-full min-w-[140px] md:w-auto">
<Select
value={t(`settings:${notificationPosition}`)}
onChange={(p) => setNotificationPosition(p)}
className="w-full"
>
{NOTIFICATION_POSITIONS.map((val) => (
<Select.Option key={val} value={t(`settings:${val}`)}>
{t(`settings:${val}`)}
</Select.Option>
))}
</Select>
</div>
</div>
<div className="hidden border-t border-th-bkg-3 py-4 md:px-4 lg:flex lg:items-center lg:justify-between">
<p className={`mb-2 md:mb-0 ${SETTINGS_BUTTON_TITLE_CLASSES}`}>
{t('settings:trade-layout')}
</p>
<div className="flex space-x-3">
<Tooltip content={t('settings:chart-left')}>
<ChartLayoutButton
icon={<ChartOnLeft className="h-auto w-32" />}
position="chartLeft"
onClick={() => setTradeLayout('chartLeft')}
/>
</Tooltip>
<Tooltip content={t('settings:chart-middle-ob-right')}>
<ChartLayoutButton
icon={<ChartMiddleOBRight className="h-auto w-32" />}
position="chartMiddleOBRight"
onClick={() => setTradeLayout('chartMiddleOBRight')}
/>
</Tooltip>
<Tooltip content={t('settings:chart-middle-ob-left')}>
<ChartLayoutButton
icon={<ChartMiddleOBLeft className="h-auto w-32" />}
position="chartMiddleOBLeft"
onClick={() => setTradeLayout('chartMiddleOBLeft')}
/>
</Tooltip>
<Tooltip content={t('settings:chart-right')}>
<ChartLayoutButton
icon={<ChartOnRight className="h-auto w-32" />}
position="chartRight"
onClick={() => setTradeLayout('chartRight')}
/>
</Tooltip>
</div>
</div>
<div className="flex flex-col border-t border-th-bkg-3 py-4 md:flex-row md:items-center md:justify-between md:px-4">
<p className={`mb-2 md:mb-0 ${SETTINGS_BUTTON_TITLE_CLASSES}`}>
{t('settings:swap-trade-size-selector')}
</p>
<div className="w-full min-w-[160px] md:w-auto">
<ButtonGroup
activeValue={tradeFormUi}
onChange={(v) => setTradeFormUi(v)}
values={TRADE_FORM_UI}
names={TRADE_FORM_UI.map((val) => t(`settings:${val}`))}
/>
</div>
</div>
<div className="flex flex-col border-t border-th-bkg-3 py-4 md:flex-row md:items-center md:justify-between md:px-4">
<p className={`mb-2 md:mb-0 ${SETTINGS_BUTTON_TITLE_CLASSES}`}>
{t('settings:trade-chart')}
</p>
<div className="w-full min-w-[220px] md:w-auto">
<ButtonGroup
activeValue={tradeChartUi}
onChange={(v) => setTradeChartUi(v)}
values={TRADING_CHARTS}
names={TRADING_CHARTS.map((val) => t(`settings:${val}`))}
/>
</div>
</div>
<div className="flex items-center justify-between border-t border-th-bkg-3 p-4">
<p className={SETTINGS_BUTTON_TITLE_CLASSES}>{t('announcements')}</p>
<Switch
checked={showAnnouncements}
onChange={() => setShowAnnouncements(!showAnnouncements)}
/>
</div>
</div>
)
}
export default DisplaySettings
const ChartLayoutButton = ({
onClick,
icon,
position,
}: {
onClick: (l: TradeLayout) => void
icon: ReactNode
position: TradeLayout
}) => {
const [tradeLayout] = useLocalStorageState(TRADE_LAYOUT_KEY, 'chartLeft')
return (
<button
className={`flex h-max items-center justify-center rounded border ${
tradeLayout === position
? 'border-th-active'
: 'border-th-bkg-4 md:hover:border-th-fgd-4'
} p-0.5 focus-visible:border-th-fgd-4`}
onClick={() => onClick(position)}
>
{icon}
</button>
)
}