mirror of
https://github.com/zhigang1992/wallet.git
synced 2026-04-30 21:52:28 +08:00
151 lines
5.4 KiB
TypeScript
151 lines
5.4 KiB
TypeScript
import { Suspense, useEffect } from 'react';
|
|
import { Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom';
|
|
|
|
import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
|
|
import { Container } from '@app/components/container/container';
|
|
import { LoadingSpinner } from '@app/components/loading-spinner';
|
|
import { MagicRecoveryCode } from '@app/pages/onboarding/magic-recovery-code/magic-recovery-code';
|
|
import { ChooseAccount } from '@app/pages/choose-account/choose-account';
|
|
import { SignTransaction } from '@app/pages/sign-transaction/sign-transaction';
|
|
import { SignIn } from '@app/pages/onboarding/sign-in/sign-in';
|
|
import { ReceiveTokens } from '@app/pages/receive-tokens/receive-tokens';
|
|
import { AddNetwork } from '@app/pages/add-network/add-network';
|
|
import { SetPasswordPage } from '@app/pages/onboarding/set-password/set-password';
|
|
import { SendTokensForm } from '@app/pages/send-tokens/send-tokens';
|
|
import { ViewSecretKey } from '@app/pages/view-secret-key/view-secret-key';
|
|
import { useSaveAuthRequest } from '@app/common/hooks/auth/use-save-auth-request-callback';
|
|
import { AccountGate } from '@app/routes/account-gate';
|
|
import { Unlock } from '@app/pages/unlock';
|
|
import { Home } from '@app/pages/home/home';
|
|
import { SignOutConfirmDrawer } from '@app/pages/sign-out-confirm/sign-out-confirm';
|
|
import { AllowDiagnosticsPage } from '@app/pages/allow-diagnostics/allow-diagnostics';
|
|
import { BuyPage } from '@app/pages/buy/buy';
|
|
import { BackUpSecretKeyPage } from '@app/pages/onboarding/back-up-secret-key/back-up-secret-key';
|
|
import { WelcomePage } from '@app/pages/onboarding/welcome/welcome';
|
|
import { RouteUrls } from '@shared/route-urls';
|
|
import { useHasStateRehydrated } from '@app/store/root-reducer';
|
|
|
|
import { useCurrentKey } from '@app/store/keys/key.slice';
|
|
|
|
import { useOnWalletLock } from './hooks/use-on-wallet-lock';
|
|
import { useOnSignOut } from './hooks/use-on-sign-out';
|
|
|
|
export function AppRoutes(): JSX.Element | null {
|
|
const { pathname } = useLocation();
|
|
const navigate = useNavigate();
|
|
const analytics = useAnalytics();
|
|
|
|
useSaveAuthRequest();
|
|
|
|
useOnWalletLock(() => navigate(RouteUrls.Unlock));
|
|
useOnSignOut(() => navigate(RouteUrls.Onboarding));
|
|
|
|
useEffect(() => {
|
|
void analytics.page('view', `${pathname}`);
|
|
}, [analytics, pathname]);
|
|
|
|
const hasStateRehydrated = useHasStateRehydrated();
|
|
const currentKey = useCurrentKey();
|
|
|
|
useEffect(() => {
|
|
// This ensures the route is correct bc the VaultLoader is slow to set wallet state
|
|
if (pathname === RouteUrls.Home && !currentKey?.hasSetPassword) navigate(RouteUrls.Onboarding);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [currentKey?.hasSetPassword]);
|
|
|
|
// check to prevent renders before the state has rehydrated
|
|
if (!hasStateRehydrated) return <>rehdryating state</>;
|
|
|
|
return (
|
|
<Suspense fallback="loading from app route">
|
|
<Routes>
|
|
<Route path={RouteUrls.Container} element={<Container />}>
|
|
<Route
|
|
path={RouteUrls.Home}
|
|
element={
|
|
<AccountGate>
|
|
<Home />
|
|
</AccountGate>
|
|
}
|
|
>
|
|
<Route path={RouteUrls.SignOutConfirm} element={<SignOutConfirmDrawer />} />
|
|
</Route>
|
|
</Route>
|
|
<Route path={RouteUrls.Onboarding} element={<WelcomePage />} />
|
|
<Route path={RouteUrls.BackUpSecretKey} element={<BackUpSecretKeyPage />} />
|
|
<Route path={RouteUrls.RequestDiagnostics} element={<AllowDiagnosticsPage />} />
|
|
<Route path={RouteUrls.SetPassword} element={<SetPasswordPage />} />
|
|
<Route path={RouteUrls.SignIn} element={<SignIn />} />
|
|
<Route path={RouteUrls.RecoveryCode} element={<MagicRecoveryCode />} />
|
|
<Route
|
|
path={RouteUrls.AddNetwork}
|
|
element={
|
|
<AccountGate>
|
|
<AddNetwork />
|
|
</AccountGate>
|
|
}
|
|
/>
|
|
<Route
|
|
path={RouteUrls.Buy}
|
|
element={
|
|
<AccountGate>
|
|
<Suspense fallback={<></>}>
|
|
<BuyPage />
|
|
</Suspense>
|
|
</AccountGate>
|
|
}
|
|
/>
|
|
<Route
|
|
path={RouteUrls.ChooseAccount}
|
|
element={
|
|
<AccountGate>
|
|
<Suspense fallback={<></>}>
|
|
<ChooseAccount />
|
|
</Suspense>
|
|
</AccountGate>
|
|
}
|
|
/>
|
|
<Route
|
|
path={RouteUrls.Receive}
|
|
element={
|
|
<AccountGate>
|
|
<ReceiveTokens />
|
|
</AccountGate>
|
|
}
|
|
/>
|
|
<Route
|
|
path={RouteUrls.Send}
|
|
element={
|
|
<AccountGate>
|
|
<Suspense fallback={<LoadingSpinner />}>
|
|
<SendTokensForm />
|
|
</Suspense>
|
|
</AccountGate>
|
|
}
|
|
/>
|
|
<Route
|
|
path={RouteUrls.Transaction}
|
|
element={
|
|
<AccountGate>
|
|
<Suspense fallback={<LoadingSpinner height="600px" />}>
|
|
<SignTransaction />
|
|
</Suspense>
|
|
</AccountGate>
|
|
}
|
|
/>
|
|
<Route
|
|
path={RouteUrls.ViewSecretKey}
|
|
element={
|
|
<AccountGate>
|
|
<ViewSecretKey />
|
|
</AccountGate>
|
|
}
|
|
/>
|
|
<Route path={RouteUrls.Unlock} element={<Unlock />} />
|
|
{/* Catch-all route redirects to onboarding */}
|
|
<Route path="*" element={<Navigate replace to={RouteUrls.Onboarding} />} />
|
|
</Routes>
|
|
</Suspense>
|
|
);
|
|
}
|