wallet-core/packages/taler-wallet-webextension/src/wallet/Application.tsx

270 lines
9.0 KiB
TypeScript
Raw Normal View History

2022-03-25 20:57:27 +01:00
/*
This file is part of GNU Taler
(C) 2020 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Main entry point for extension pages.
*
* @author sebasjm
*/
import { createHashHistory } from "history";
import { Fragment, h, VNode } from "preact";
import Router, { route, Route } from "preact-router";
import Match from "preact-router/match";
import { useEffect, useState } from "preact/hooks";
2022-03-29 04:41:07 +02:00
import { LogoHeader } from "../components/LogoHeader.js";
import PendingTransactions from "../components/PendingTransactions.js";
import { SuccessBox, WalletBox } from "../components/styled/index.js";
import { DevContextProvider } from "../context/devContext.js";
import { IoCProviderForRuntime } from "../context/iocContext.js";
2022-03-25 20:57:27 +01:00
import {
TranslationProvider,
useTranslationContext,
2022-03-29 04:41:07 +02:00
} from "../context/translation.js";
import { PayPage } from "../cta/Pay.js";
import { RefundPage } from "../cta/Refund.js";
import { TipPage } from "../cta/Tip.js";
import { WithdrawPage } from "../cta/Withdraw.js";
2022-05-03 05:16:03 +02:00
import { DepositPage as DepositPageCTA } from "../cta/Deposit.js";
2022-03-29 04:41:07 +02:00
import { Pages, WalletNavBar } from "../NavigationBar.js";
import { DeveloperPage } from "./DeveloperPage.js";
import { BackupPage } from "./BackupPage.js";
import { DepositPage } from "./DepositPage.js";
import { ExchangeAddPage } from "./ExchangeAddPage.js";
import { HistoryPage } from "./History.js";
import { ManualWithdrawPage } from "./ManualWithdrawPage.js";
import { ProviderAddPage } from "./ProviderAddPage.js";
import { ProviderDetailPage } from "./ProviderDetailPage.js";
import { SettingsPage } from "./Settings.js";
import { TransactionPage } from "./Transaction.js";
import { WelcomePage } from "./Welcome.js";
2022-03-25 20:57:27 +01:00
export function Application(): VNode {
const [globalNotification, setGlobalNotification] = useState<
VNode | undefined
>(undefined);
const hash_history = createHashHistory();
function clearNotification(): void {
setGlobalNotification(undefined);
}
function clearNotificationWhenMovingOut(): void {
// const movingOutFromNotification =
// globalNotification && e.url !== globalNotification.to;
if (globalNotification) {
//&& movingOutFromNotification) {
setGlobalNotification(undefined);
}
}
const { i18n } = useTranslationContext();
return (
<TranslationProvider>
<DevContextProvider>
<IoCProviderForRuntime>
{/* <Match/> won't work in the first render if <Router /> is not called first */}
{/* https://github.com/preactjs/preact-router/issues/415 */}
<Router history={hash_history} />
<Match>
{({ path }: { path: string }) => {
if (path && path.startsWith("/cta")) return;
return (
<Fragment>
<LogoHeader />
<WalletNavBar path={path} />
2022-03-25 21:06:38 +01:00
{shouldShowPendingOperations(path) && (
<div
style={{
backgroundColor: "lightcyan",
display: "flex",
justifyContent: "center",
}}
>
<PendingTransactions
2022-06-02 17:20:36 +02:00
goToTransaction={(tid: string) =>
redirectTo(Pages.balanceTransaction({ tid }))
2022-03-25 21:06:38 +01:00
}
/>
</div>
)}
2022-03-25 20:57:27 +01:00
</Fragment>
);
}}
</Match>
<WalletBox>
{globalNotification && (
<SuccessBox onClick={clearNotification}>
<div>{globalNotification}</div>
</SuccessBox>
)}
<Router
history={hash_history}
onChange={clearNotificationWhenMovingOut}
>
<Route path={Pages.welcome} component={WelcomePage} />
{/**
* BALANCE
*/}
<Route
2022-06-02 17:20:36 +02:00
path={Pages.balanceHistory.pattern}
2022-03-25 20:57:27 +01:00
component={HistoryPage}
goToWalletDeposit={(currency: string) =>
2022-06-02 17:20:36 +02:00
redirectTo(Pages.balanceDeposit({ currency }))
2022-03-25 20:57:27 +01:00
}
goToWalletManualWithdraw={(currency?: string) =>
2022-06-02 17:20:36 +02:00
redirectTo(Pages.balanceManualWithdraw({ currency }))
2022-03-25 20:57:27 +01:00
}
/>
<Route
2022-06-02 17:20:36 +02:00
path={Pages.balanceTransaction.pattern}
2022-03-25 20:57:27 +01:00
component={TransactionPage}
2022-06-01 20:47:47 +02:00
goToWalletHistory={(currency?: string) =>
2022-06-02 17:20:36 +02:00
redirectTo(Pages.balanceHistory({ currency }))
2022-06-01 20:47:47 +02:00
}
2022-03-25 20:57:27 +01:00
/>
<Route
2022-06-02 17:20:36 +02:00
path={Pages.balanceManualWithdraw.pattern}
2022-03-25 20:57:27 +01:00
component={ManualWithdrawPage}
2022-06-01 20:47:47 +02:00
onCancel={() => redirectTo(Pages.balance)}
2022-03-25 20:57:27 +01:00
/>
<Route
2022-06-02 17:20:36 +02:00
path={Pages.balanceDeposit.pattern}
2022-03-25 20:57:27 +01:00
component={DepositPage}
onCancel={(currency: string) => {
2022-06-02 17:20:36 +02:00
redirectTo(Pages.balanceHistory({ currency }));
2022-03-25 20:57:27 +01:00
}}
onSuccess={(currency: string) => {
2022-06-02 17:20:36 +02:00
redirectTo(Pages.balanceHistory({ currency }));
2022-03-25 20:57:27 +01:00
setGlobalNotification(
<i18n.Translate>
All done, your transaction is in progress
</i18n.Translate>,
);
}}
/>
{/**
* PENDING
*/}
<Route path={Pages.settings} component={SettingsPage} />
{/**
* BACKUP
*/}
<Route
path={Pages.backup}
component={BackupPage}
2022-06-02 17:20:36 +02:00
onAddProvider={() => redirectTo(Pages.backupProviderAdd)}
2022-03-25 20:57:27 +01:00
/>
<Route
2022-06-02 17:20:36 +02:00
path={Pages.backupProviderDetail.pattern}
2022-03-25 20:57:27 +01:00
component={ProviderDetailPage}
2022-06-01 20:47:47 +02:00
onBack={() => redirectTo(Pages.backup)}
2022-03-25 20:57:27 +01:00
/>
<Route
2022-06-02 17:20:36 +02:00
path={Pages.backupProviderAdd}
2022-03-25 20:57:27 +01:00
component={ProviderAddPage}
2022-06-01 20:47:47 +02:00
onBack={() => redirectTo(Pages.backup)}
2022-03-25 20:57:27 +01:00
/>
{/**
* SETTINGS
*/}
<Route
2022-06-02 17:20:36 +02:00
path={Pages.settingsExchangeAdd.pattern}
2022-03-25 20:57:27 +01:00
component={ExchangeAddPage}
2022-06-01 20:47:47 +02:00
onBack={() => redirectTo(Pages.balance)}
2022-03-25 20:57:27 +01:00
/>
{/**
* DEV
*/}
<Route path={Pages.dev} component={DeveloperPage} />
{/**
* CALL TO ACTION
*/}
<Route
2022-06-02 17:20:36 +02:00
path={Pages.ctaPay}
2022-03-25 20:57:27 +01:00
component={PayPage}
goToWalletManualWithdraw={(currency?: string) =>
2022-06-02 17:20:36 +02:00
redirectTo(Pages.balanceManualWithdraw({ currency }))
2022-03-25 20:57:27 +01:00
}
2022-06-01 20:47:47 +02:00
goBack={() => redirectTo(Pages.balance)}
2022-03-25 20:57:27 +01:00
/>
2022-06-02 17:20:36 +02:00
<Route path={Pages.ctaRefund} component={RefundPage} />
<Route path={Pages.ctaTips} component={TipPage} />
<Route path={Pages.ctaWithdraw} component={WithdrawPage} />
<Route path={Pages.ctaDeposit} component={DepositPageCTA} />
2022-03-25 20:57:27 +01:00
{/**
* NOT FOUND
* all redirects should be at the end
*/}
<Route
path={Pages.balance}
component={Redirect}
2022-06-02 17:20:36 +02:00
to={Pages.balanceHistory({})}
2022-03-25 20:57:27 +01:00
/>
<Route
default
component={Redirect}
2022-06-02 17:20:36 +02:00
to={Pages.balanceHistory({})}
2022-03-25 20:57:27 +01:00
/>
</Router>
</WalletBox>
</IoCProviderForRuntime>
</DevContextProvider>
</TranslationProvider>
);
}
2022-06-01 20:47:47 +02:00
async function redirectTo(location: string): Promise<void> {
route(location);
}
2022-03-25 20:57:27 +01:00
function Redirect({ to }: { to: string }): null {
useEffect(() => {
route(to, true);
});
return null;
}
2022-03-25 21:06:38 +01:00
2022-06-03 15:19:33 +02:00
function matchesRoute(url: string, route: string): boolean {
type MatcherFunc = (
url: string,
route: string,
opts: any,
) => Record<string, string> | false;
const internalPreactMatcher: MatcherFunc = (Router as any).exec;
const result = internalPreactMatcher(url, route, {});
return !result ? false : true;
}
function shouldShowPendingOperations(url: string): boolean {
return [
Pages.balanceHistory.pattern,
Pages.dev,
Pages.settings,
Pages.backup,
].some((p) => matchesRoute(url, p));
2022-03-25 21:06:38 +01:00
}