-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathAppNavbar.tsx
More file actions
255 lines (241 loc) · 8.92 KB
/
AppNavbar.tsx
File metadata and controls
255 lines (241 loc) · 8.92 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
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
import { useState, type PropsWithChildren } from 'react'
import type { SessionResponse } from '@joinmarket-webui/joinmarket-api-ts/jm'
import type { TFunction } from 'i18next'
import { LockKeyholeIcon, LogOutIcon, PackageSearchIcon, SettingsIcon, ShuffleIcon, WalletIcon } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { Link, useNavigate, type NavigateFunction } from 'react-router-dom'
import { DevBadge } from '@/components/dev/DevBadge'
import { Button } from '@/components/ui/button'
import { ThemeToggleButton } from '@/components/ui/jam/ThemeToggleButton'
import { Skeleton } from '@/components/ui/skeleton'
import type { SidebarContextProps } from '@/components/ui/use-sidebar'
import { isDevMode } from '@/constants/debugFeatures'
import { routes } from '@/constants/routes'
import type { RescanInfo } from '@/context/JamSessionInfoContext'
import { cn, shortenStringMiddle } from '@/lib/utils'
import type { AmountSats } from '@/types/global'
import { Spinner } from '../ui/spinner'
const WithActivityIndicator = ({ active, children }: PropsWithChildren<{ active: boolean }>) => {
return (
<span className="relative">
{children}
<span
className={cn('absolute -top-1 -right-2 text-[8px]', {
'light:text-green-600 text-green-300 motion-safe:animate-pulse': active,
})}
>
●
</span>
</span>
)
}
type WalletPreviewProps = {
isLoading?: boolean
walletName: string | null
formatAmount: (AmountSats: number) => string
currencySymbol: (size: 'sm' | 'lg') => React.ReactNode
totalBalance: AmountSats
rescanInfo?: RescanInfo
}
const WalletPreview = ({
walletName,
formatAmount,
currencySymbol,
totalBalance,
isLoading = false,
rescanInfo,
}: WalletPreviewProps) => {
const { t } = useTranslation()
const walletNameTitle = shortenStringMiddle(walletName ?? '...', 12)
return (
<div className="flex flex-1 items-center">
<Link to={routes.home} className="flex items-center gap-2">
<div className="flex h-8 w-8 items-center">
{isLoading ? (
<Spinner className="text-muted-foreground size-6 motion-reduce:hidden" strokeWidth={3} />
) : (
<WalletIcon strokeWidth={1} />
)}
</div>
<div className="flex flex-col gap-0.25 leading-none">
<div className="flex items-center gap-2">
<div
className="font-semibold tracking-tight"
title={walletName && walletName.length !== walletNameTitle.length ? walletName : undefined}
>
{walletNameTitle}
</div>
{isDevMode() && <DevBadge />}
</div>
<div className="flex min-h-5 min-w-[150px] items-center">
{rescanInfo?.rescanning === true ? (
<div className="cursor-wait motion-safe:animate-pulse">
{rescanInfo.progress !== undefined
? t('navbar.text_rescan_in_progress_with_progress', {
progress: Math.floor(rescanInfo.progress * 100),
})
: t('navbar.text_rescan_in_progress')}
</div>
) : (
<>
{isLoading ? (
<Skeleton className="h-4 w-full bg-neutral-200 dark:bg-neutral-600" />
) : (
<>
<span className="tabular-nums">{formatAmount(totalBalance)}</span>
{currencySymbol('sm')}
</>
)}
</>
)}
</div>
</div>
</Link>
</div>
)
}
type SessionInfo = Pick<SessionResponse, 'maker_running' | 'coinjoin_in_process' | 'schedule'>
type SidebarInfo = Pick<SidebarContextProps, 'isMobile' | 'open' | 'openMobile'>
type AppNavbarProps = WalletPreviewProps & {
theme: string
toggleTheme: () => void
onLogout: (navigate: NavigateFunction) => Promise<void>
onLockWallet: (navigate: NavigateFunction, t: TFunction<'translation', undefined>) => Promise<void>
sidebarTrigger?: React.ReactNode
sessionInfo?: SessionInfo
sidebarInfo?: SidebarInfo
rescanInfo?: RescanInfo
}
export function AppNavbar({
isLoading = false,
walletName,
totalBalance,
theme,
formatAmount,
currencySymbol,
toggleTheme,
onLogout,
onLockWallet,
sidebarTrigger,
sessionInfo,
sidebarInfo,
rescanInfo,
}: AppNavbarProps) {
const { t } = useTranslation()
const navigate = useNavigate()
const isSidebarOpen =
sidebarInfo === undefined ? false : sidebarInfo.isMobile ? sidebarInfo.openMobile : sidebarInfo.open
const makerRunning = sessionInfo?.maker_running === true
const singleCoinJoinRunning = sessionInfo?.coinjoin_in_process === true && !sessionInfo?.schedule
const schedulerRunning = sessionInfo?.coinjoin_in_process === true && !!sessionInfo?.schedule
const joiningRoute = (() => {
if (schedulerRunning) return routes.sweep
if (singleCoinJoinRunning) return routes.send
if (makerRunning) return routes.earn
return undefined
})()
const rescanningRoute = rescanInfo?.rescanning !== true ? undefined : routes.rescan
const [isLockingWallet, setIsLockingWallet] = useState(false)
const doOnLockWallet = async () => {
try {
setIsLockingWallet(true)
await onLockWallet(navigate, t)
} finally {
setIsLockingWallet(false)
}
}
return (
<header className="light:bg-gray-100 light:text-black flex items-center justify-between bg-[#23262b] px-4 py-2 text-white transition-colors duration-300">
<WalletPreview
isLoading={isLoading}
walletName={walletName}
totalBalance={totalBalance}
formatAmount={formatAmount}
currencySymbol={currencySymbol}
rescanInfo={rescanInfo}
/>
<div
className={cn(
'hidden min-w-0 flex-1 items-center justify-center text-sm font-semibold md:gap-6 lg:gap-12 lg:text-base',
{
'md:flex': !isSidebarOpen,
'lg:flex': isSidebarOpen,
'blur-[1px]': rescanInfo?.rescanning === true,
},
)}
>
<Link to={routes.receive} className="text-muted-foreground hover:text-foreground">
{t('navbar.tab_receive')}
</Link>
<Link to={routes.send} className="text-muted-foreground hover:text-foreground relative">
<WithActivityIndicator active={singleCoinJoinRunning}>{t('navbar.tab_send')}</WithActivityIndicator>
</Link>
<Link to={routes.earn} className="text-muted-foreground hover:text-foreground relative">
<WithActivityIndicator active={makerRunning}>{t('navbar.tab_earn')}</WithActivityIndicator>
</Link>
<span className="text-gray-400 dark:text-gray-600">|</span>
<Link to={routes.sweep} className="text-muted-foreground hover:text-foreground relative">
<WithActivityIndicator active={schedulerRunning}>{t('navbar.tab_sweep')}</WithActivityIndicator>
</Link>
</div>
<div className="flex min-w-0 flex-1 items-center justify-end gap-1 sm:gap-2">
{rescanningRoute && (
<Button
className="light:text-green-600 text-green-300"
variant="ghost-navbar"
size="icon"
onClick={() => navigate(rescanningRoute)}
aria-label={t('navbar.text_rescan_in_progress')}
title={t('navbar.text_rescan_in_progress')}
>
<PackageSearchIcon className="motion-safe:animate-pulse" />
</Button>
)}
{joiningRoute && (
<Button
className="light:text-green-600 text-green-300"
variant="ghost-navbar"
size="icon"
onClick={() => navigate(joiningRoute)}
aria-label={t('navbar.joining_in_progress')}
title={t('navbar.joining_in_progress')}
>
<ShuffleIcon className="motion-safe:animate-pulse" />
</Button>
)}
<ThemeToggleButton className="hidden sm:flex" variant="ghost-navbar" theme={theme} onClick={toggleTheme} />
<Button
variant="ghost-navbar"
size="icon"
onClick={() => navigate(routes.settings)}
aria-label={t('navbar.menu_mobile_settings')}
title={t('navbar.menu_mobile_settings')}
>
<SettingsIcon />
</Button>
<Button
className="hidden sm:flex"
variant="ghost-navbar"
size="icon"
onClick={doOnLockWallet}
aria-label={t('settings.button_lock_wallet')}
title={t('settings.button_lock_wallet')}
disabled={isLockingWallet}
>
<LockKeyholeIcon />
</Button>
<Button
className="hidden sm:flex"
variant="ghost-navbar"
size="icon"
onClick={async () => await onLogout(navigate)}
aria-label={/* TODO: i18n */ 'Logout'}
title={/* TODO: i18n */ 'Logout'}
>
<LogOutIcon />
</Button>
{sidebarTrigger}
</div>
</header>
)
}