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

253 lines
7.5 KiB
TypeScript
Raw Normal View History

/*
2022-06-06 17:05:26 +02:00
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
2022-06-06 17:05:26 +02: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.
2022-06-06 17:05:26 +02:00
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
2022-06-06 17:05:26 +02:00
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
2016-03-01 19:46:20 +01:00
/**
* Popup shown to the user when they click
* the Taler browser action button.
*
2022-02-23 19:18:37 +01:00
* @author sebasjm
2016-03-01 19:46:20 +01:00
*/
2017-05-29 15:18:48 +02:00
/**
* Imports.
*/
import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
2022-11-25 03:16:01 +01:00
import { Fragment, h, VNode } from "preact";
import { JustInDevMode } from "./components/JustInDevMode.js";
2022-03-24 20:02:38 +01:00
import {
NavigationHeader,
NavigationHeaderHolder,
SvgIcon,
2022-03-29 04:41:07 +02:00
} from "./components/styled/index.js";
import { useBackendContext } from "./context/backend.js";
2022-03-29 04:41:07 +02:00
import { useTranslationContext } from "./context/translation.js";
import { useAsyncAsHook } from "./hooks/useAsyncAsHook.js";
2022-08-11 17:28:02 +02:00
import qrIcon from "./svg/qr_code_24px.svg";
import settingsIcon from "./svg/settings_black_24dp.svg";
2022-11-25 03:16:01 +01:00
import warningIcon from "./svg/warning_24px.svg";
2016-10-10 00:37:08 +02:00
2022-02-23 19:18:37 +01:00
/**
* List of pages used by the wallet
*
* @author sebasjm
*/
2023-01-04 15:24:58 +01:00
// eslint-disable-next-line @typescript-eslint/ban-types
2022-06-02 17:20:36 +02:00
type PageLocation<DynamicPart extends object> = {
pattern: string;
(params: DynamicPart): string;
};
function replaceAll(
pattern: string,
vars: Record<string, string>,
values: Record<string, any>,
): string {
let result = pattern;
for (const v in vars) {
2022-06-09 15:04:30 +02:00
result = result.replace(vars[v], !values[v] ? "" : values[v]);
2022-06-02 17:20:36 +02:00
}
return result;
2021-06-16 22:01:06 +02:00
}
2023-01-04 15:24:58 +01:00
// eslint-disable-next-line @typescript-eslint/ban-types
2022-06-02 17:20:36 +02:00
function pageDefinition<T extends object>(pattern: string): PageLocation<T> {
const patternParams = pattern.match(/(:[\w?]*)/g);
if (!patternParams)
throw Error(
`page definition pattern ${pattern} doesn't have any parameter`,
);
const vars = patternParams.reduce((prev, cur) => {
const pName = cur.match(/(\w+)/g);
//skip things like :? in the path pattern
if (!pName || !pName[0]) return prev;
const name = pName[0];
return { ...prev, [name]: cur };
}, {} as Record<string, string>);
const f = (values: T): string => replaceAll(pattern, vars, values);
f.pattern = pattern;
return f;
}
export const Pages = {
welcome: "/welcome",
balance: "/balance",
balanceHistory: pageDefinition<{ currency?: string }>(
"/balance/history/:currency?",
),
balanceDeposit: pageDefinition<{ amount: string }>(
"/balance/deposit/:amount",
2022-06-02 17:20:36 +02:00
),
balanceTransaction: pageDefinition<{ tid: string }>(
"/balance/transaction/:tid",
),
2022-08-16 02:18:39 +02:00
sendCash: pageDefinition<{ amount?: string }>("/destination/send/:amount"),
receiveCash: pageDefinition<{ amount?: string }>("/destination/get/:amount?"),
2022-06-02 17:20:36 +02:00
dev: "/dev",
exchanges: "/exchanges",
2022-06-02 17:20:36 +02:00
backup: "/backup",
backupProviderDetail: pageDefinition<{ pid: string }>(
"/backup/provider/:pid",
),
backupProviderAdd: "/backup/provider/add",
2022-08-11 17:28:02 +02:00
qr: "/qr",
2022-11-25 03:16:01 +01:00
notifications: "/notifications",
2022-06-02 17:20:36 +02:00
settings: "/settings",
settingsExchangeAdd: pageDefinition<{ currency?: string }>(
"/settings/exchange/add/:currency?",
),
cta: pageDefinition<{ action: string }>("/cta/:action"),
ctaPay: "/cta/pay",
2023-03-10 05:27:31 +01:00
ctaPayTemplate: "/cta/pay/template",
ctaRecovery: "/cta/recovery",
2022-06-02 17:20:36 +02:00
ctaRefund: "/cta/refund",
ctaTips: "/cta/tip",
ctaWithdraw: "/cta/withdraw",
ctaDeposit: "/cta/deposit",
2022-08-31 05:20:35 +02:00
ctaInvoiceCreate: pageDefinition<{ amount?: string }>(
"/cta/invoice/create/:amount?",
),
ctaTransferCreate: pageDefinition<{ amount?: string }>(
"/cta/transfer/create/:amount?",
),
ctaInvoicePay: "/cta/invoice/pay",
ctaTransferPickup: "/cta/transfer/pickup",
2022-08-29 16:32:07 +02:00
ctaWithdrawManual: pageDefinition<{ amount?: string }>(
"/cta/manual-withdraw/:amount?",
),
2022-06-02 17:20:36 +02:00
};
2023-01-04 15:24:58 +01:00
export type PopupNavBarOptions = "balance" | "backup" | "dev";
export function PopupNavBar({ path }: { path?: PopupNavBarOptions }): VNode {
const api = useBackendContext();
2022-11-25 03:16:01 +01:00
const hook = useAsyncAsHook(async () => {
return await api.wallet.call(
WalletApiOperation.GetUserAttentionUnreadCount,
{},
);
});
const attentionCount = !hook || hook.hasError ? 0 : hook.response.total;
2022-03-14 16:15:41 +01:00
const { i18n } = useTranslationContext();
2016-10-10 00:37:08 +02:00
return (
2022-02-16 19:15:47 +01:00
<NavigationHeader>
2023-01-04 15:24:58 +01:00
<a href={Pages.balance} class={path === "balance" ? "active" : ""}>
<i18n.Translate>Balance</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
2023-01-18 16:06:37 +01:00
<JustInDevMode>
<a href={Pages.backup} class={path === "backup" ? "active" : ""}>
<i18n.Translate>Backup</i18n.Translate>
</a>
</JustInDevMode>
2022-08-11 17:28:02 +02:00
<div style={{ display: "flex", paddingTop: 4, justifyContent: "right" }}>
2022-11-25 03:16:01 +01:00
{attentionCount > 0 ? (
<a href={Pages.notifications}>
<SvgIcon
title={i18n.str`Notifications`}
dangerouslySetInnerHTML={{ __html: warningIcon }}
color="yellow"
/>
</a>
) : (
<Fragment />
)}
2022-08-11 17:28:02 +02:00
<a href={Pages.qr}>
<SvgIcon
2022-09-14 13:14:00 +02:00
title={i18n.str`QR Reader and Taler URI`}
2022-08-11 17:28:02 +02:00
dangerouslySetInnerHTML={{ __html: qrIcon }}
color="white"
/>
</a>
<a href={Pages.settings}>
<SvgIcon
title={i18n.str`Settings`}
dangerouslySetInnerHTML={{ __html: settingsIcon }}
color="white"
/>
</a>
</div>
2022-02-16 19:15:47 +01:00
</NavigationHeader>
2016-10-10 00:37:08 +02:00
);
}
2023-01-04 15:24:58 +01:00
export type WalletNavBarOptions = "balance" | "backup" | "dev";
export function WalletNavBar({ path }: { path?: WalletNavBarOptions }): VNode {
2022-03-14 16:15:41 +01:00
const { i18n } = useTranslationContext();
2022-11-28 19:33:45 +01:00
const api = useBackendContext();
2022-11-28 19:33:45 +01:00
const hook = useAsyncAsHook(async () => {
return await api.wallet.call(
WalletApiOperation.GetUserAttentionUnreadCount,
{},
);
});
2023-01-04 15:24:58 +01:00
const attentionCount =
(!hook || hook.hasError ? 0 : hook.response?.total) ?? 0;
2022-11-28 19:33:45 +01:00
2021-11-15 15:18:58 +01:00
return (
2022-02-16 19:15:47 +01:00
<NavigationHeaderHolder>
<NavigationHeader>
2023-01-04 15:24:58 +01:00
<a href={Pages.balance} class={path === "balance" ? "active" : ""}>
<i18n.Translate>Balance</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
2023-01-18 16:06:37 +01:00
<JustInDevMode>
<a href={Pages.backup} class={path === "backup" ? "active" : ""}>
<i18n.Translate>Backup</i18n.Translate>
</a>
</JustInDevMode>
2022-11-28 19:33:45 +01:00
{attentionCount > 0 ? (
<a href={Pages.notifications}>
<i18n.Translate>Notifications</i18n.Translate>
</a>
) : (
<Fragment />
)}
2022-02-16 19:15:47 +01:00
2022-11-25 03:16:01 +01:00
<JustInDevMode>
2023-01-04 15:24:58 +01:00
<a href={Pages.dev} class={path === "dev" ? "active" : ""}>
2022-11-25 03:16:01 +01:00
<i18n.Translate>Dev</i18n.Translate>
</a>
</JustInDevMode>
2022-08-11 17:28:02 +02:00
<div
style={{ display: "flex", paddingTop: 4, justifyContent: "right" }}
2022-02-16 19:15:47 +01:00
>
2022-08-11 17:28:02 +02:00
<a href={Pages.qr}>
<SvgIcon
2022-09-14 13:14:00 +02:00
title={i18n.str`QR Reader and Taler URI`}
2022-08-11 17:28:02 +02:00
dangerouslySetInnerHTML={{ __html: qrIcon }}
color="white"
/>
</a>
<a href={Pages.settings}>
<SvgIcon
title={i18n.str`Settings`}
dangerouslySetInnerHTML={{ __html: settingsIcon }}
color="white"
/>
</a>
</div>
2022-02-16 19:15:47 +01:00
</NavigationHeader>
</NavigationHeaderHolder>
2021-11-15 15:18:58 +01:00
);
2021-07-16 17:00:39 +02:00
}