/
App.tsx
148 lines (141 loc) · 4.88 KB
/
App.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
import { useCallback } from 'react'
import * as rb from 'react-bootstrap'
import { useTranslation } from 'react-i18next'
import {
createBrowserRouter,
createRoutesFromElements,
Navigate,
Route,
RouterProvider,
Outlet,
} from 'react-router-dom'
import { routes } from '../constants/routes'
import { useSessionConnectionError } from '../context/ServiceInfoContext'
import { useSettings } from '../context/SettingsContext'
import { useCurrentWallet, useSetCurrentWallet } from '../context/WalletContext'
import { clearSession, setSession } from '../session'
import { isDebugFeatureEnabled } from '../constants/debugFeatures'
import CreateWallet from './CreateWallet'
import Earn from './Earn'
import ErrorPage, { ErrorThrowingComponent } from './ErrorPage'
import Footer from './Footer'
import Jam from './Jam'
import Layout from './Layout'
import MainWalletView from './MainWalletView'
import Navbar from './Navbar'
import Onboarding from './Onboarding'
import Receive from './Receive'
import Send from './Send'
import Settings from './Settings'
import Wallets from './Wallets'
export default function App() {
const { t } = useTranslation()
const settings = useSettings()
const currentWallet = useCurrentWallet()
const setCurrentWallet = useSetCurrentWallet()
const sessionConnectionError = useSessionConnectionError()
const startWallet = useCallback(
(name, token) => {
setSession({ name, token })
setCurrentWallet({ name, token })
},
[setCurrentWallet]
)
const stopWallet = useCallback(() => {
clearSession()
setCurrentWallet(null)
}, [setCurrentWallet])
const router = createBrowserRouter(
createRoutesFromElements(
<Route
id="base"
element={
<>
<Navbar />
<rb.Container as="main" className="py-4 py-sm-5">
<Outlet />
</rb.Container>
<Footer />
</>
}
errorElement={<ErrorPage />}
>
<Route
id="error-boundary"
element={
<Layout>
<Outlet />
</Layout>
}
errorElement={
<Layout variant="wide">
<ErrorPage />
</Layout>
}
>
{/**
* This sections defines all routes that can be displayed, even if the connection
* to the backend is down, e.g. "create-wallet" shows the seed quiz and it is important
* that it stays visible in case the backend becomes unavailable.
*/}
<Route id="create-wallet" path={routes.createWallet} element={<CreateWallet startWallet={startWallet} />} />
{sessionConnectionError ? (
<Route
id="404"
path="*"
element={
<rb.Alert variant="danger">
{t('app.alert_no_connection', { connectionError: sessionConnectionError.message })}.
</rb.Alert>
}
/>
) : (
<>
{/**
* This section defines all routes that are displayed only if the backend is reachable.
*/}
<Route
id="wallets"
path={routes.home}
element={<Wallets currentWallet={currentWallet} startWallet={startWallet} stopWallet={stopWallet} />}
/>
{currentWallet && (
<>
<Route id="wallet" path={routes.wallet} element={<MainWalletView wallet={currentWallet} />} />
<Route id="jam" path={routes.jam} element={<Jam wallet={currentWallet} />} />
<Route id="send" path={routes.send} element={<Send wallet={currentWallet} />} />
<Route id="earn" path={routes.earn} element={<Earn wallet={currentWallet} />} />
<Route id="receive" path={routes.receive} element={<Receive wallet={currentWallet} />} />
<Route
id="settings"
path={routes.settings}
element={<Settings wallet={currentWallet} stopWallet={stopWallet} />}
/>
</>
)}
{isDebugFeatureEnabled('errorExamplePage') && (
<Route id="error-example" path={routes.__errorExample} element={<ErrorThrowingComponent />} />
)}
<Route id="404" path="*" element={<Navigate to={routes.home} replace={true} />} />
</>
)}
</Route>
</Route>
),
{
basename: window.JM.PUBLIC_PATH,
}
)
if (settings.showOnboarding === true) {
return (
<rb.Container className="onboarding my-5">
<rb.Row className="justify-content-center mt-md-5">
<rb.Col xs={10} sm={10} md={8} lg={6} xl={4}>
<Onboarding />
</rb.Col>
</rb.Row>
</rb.Container>
)
}
return <RouterProvider router={router} />
}