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

324 lines
11 KiB
TypeScript
Raw Normal View History

2022-03-25 20:57:27 +01:00
/*
This file is part of GNU Taler
2022-06-06 17:05:26 +02:00
(C) 2022 Taler Systems S.A.
2022-03-25 20:57:27 +01:00
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";
2022-07-31 01:55:41 +02:00
import { PaymentPage } from "../cta/Payment/index.js";
import { RefundPage } from "../cta/Refund/index.js";
import { TipPage } from "../cta/Tip/index.js";
2022-08-29 16:32:07 +02:00
import {
WithdrawPageFromParams,
WithdrawPageFromURI,
} from "../cta/Withdraw/index.js";
2022-07-31 01:55:41 +02:00
import { DepositPage as DepositPageCTA } from "../cta/Deposit/index.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-08-11 17:28:02 +02:00
import { QrReaderPage } from "./QrReader.js";
import { platform } from "../platform/api.js";
2022-08-14 15:10:37 +02:00
import {
DestinationSelectionGetCash,
DestinationSelectionSendCash,
} from "./DestinationSelection.js";
2022-08-16 02:18:39 +02:00
import { Amounts } from "@gnu-taler/taler-util";
2022-08-26 06:08:51 +02:00
import { ExchangeSelectionPage } from "./ExchangeSelection/index.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 */}
2022-08-10 16:50:46 +02:00
<Router history={hash_history}>
<Match default>
{({ path }: { path: string }) => {
if (path && path.startsWith("/cta")) return;
return (
<Fragment>
<LogoHeader />
<WalletNavBar path={path} />
{shouldShowPendingOperations(path) && (
<div
style={{
backgroundColor: "lightcyan",
display: "flex",
justifyContent: "center",
}}
>
<PendingTransactions
goToTransaction={(tid: string) =>
redirectTo(Pages.balanceTransaction({ tid }))
}
/>
</div>
)}
</Fragment>
);
}}
</Match>
</Router>
2022-03-25 20:57:27 +01:00
<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-08-16 02:18:39 +02:00
redirectTo(Pages.sendCash({ amount: `${currency}:0` }))
2022-03-25 20:57:27 +01:00
}
goToWalletManualWithdraw={(currency?: string) =>
2022-08-16 02:18:39 +02:00
redirectTo(
Pages.receiveCash({
amount: !currency ? undefined : `${currency}:0`,
}),
)
2022-03-25 20:57:27 +01:00
}
/>
2022-08-26 06:08:51 +02:00
<Route path={Pages.exchanges} component={ExchangeSelectionPage} />
2022-08-14 15:10:37 +02:00
<Route
path={Pages.sendCash.pattern}
component={DestinationSelectionSendCash}
/>
<Route
path={Pages.receiveCash.pattern}
component={DestinationSelectionGetCash}
2022-08-16 02:18:39 +02:00
goToWalletManualWithdraw={(amount?: string) =>
2022-08-29 16:32:07 +02:00
redirectTo(Pages.ctaWithdrawManual({ amount }))
}
goToWalletWalletInvoice={(amount?: string) =>
redirectTo(Pages.ctaWithdrawManual({ amount }))
2022-08-16 02:18:39 +02:00
}
2022-08-14 15:10:37 +02:00
/>
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.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
*/}
2022-08-11 17:28:02 +02:00
<Route
path={Pages.qr}
component={QrReaderPage}
onDetected={(talerActionUrl: string) => {
platform.openWalletURIFromPopup(talerActionUrl);
}}
/>
2022-03-25 20:57:27 +01:00
<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-07-31 01:55:41 +02:00
component={PaymentPage}
2022-08-16 02:18:39 +02:00
goToWalletManualWithdraw={(amount?: string) =>
2022-08-29 16:32:07 +02:00
redirectTo(Pages.ctaWithdrawManual({ amount }))
2022-03-25 20:57:27 +01:00
}
2022-08-10 16:50:46 +02:00
cancel={() => redirectTo(Pages.balance)}
/>
<Route
path={Pages.ctaRefund}
component={RefundPage}
cancel={() => redirectTo(Pages.balance)}
/>
<Route
path={Pages.ctaTips}
component={TipPage}
cancel={() => redirectTo(Pages.balance)}
/>
<Route
path={Pages.ctaWithdraw}
2022-08-29 16:32:07 +02:00
component={WithdrawPageFromURI}
cancel={() => redirectTo(Pages.balance)}
/>
<Route
path={Pages.ctaWithdrawManual.pattern}
component={WithdrawPageFromParams}
2022-08-10 16:50:46 +02:00
cancel={() => redirectTo(Pages.balance)}
/>
<Route
path={Pages.ctaDeposit}
component={DepositPageCTA}
cancel={() => redirectTo(Pages.balance)}
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
}