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

175 lines
4.7 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.
*/
2022-03-24 20:02:38 +01:00
import { h, VNode } from "preact";
2022-03-29 04:41:07 +02:00
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 { useTranslationContext } from "./context/translation.js";
2022-03-24 20:02:38 +01:00
import settingsIcon from "./svg/settings_black_24dp.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
*/
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
}
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?",
),
balanceManualWithdraw: pageDefinition<{ currency?: string }>(
"/balance/manual-withdraw/:currency?",
),
balanceDeposit: pageDefinition<{ currency: string }>(
"/balance/deposit/:currency",
),
balanceTransaction: pageDefinition<{ tid: string }>(
"/balance/transaction/:tid",
),
dev: "/dev",
backup: "/backup",
backupProviderDetail: pageDefinition<{ pid: string }>(
"/backup/provider/:pid",
),
backupProviderAdd: "/backup/provider/add",
settings: "/settings",
settingsExchangeAdd: pageDefinition<{ currency?: string }>(
"/settings/exchange/add/:currency?",
),
cta: pageDefinition<{ action: string }>("/cta/:action"),
ctaPay: "/cta/pay",
ctaRefund: "/cta/refund",
ctaTips: "/cta/tip",
ctaWithdraw: "/cta/withdraw",
ctaDeposit: "/cta/deposit",
};
2022-02-16 19:15:47 +01:00
export function PopupNavBar({ path = "" }: { path?: string }): VNode {
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>
2022-06-02 17:20:36 +02:00
<a
href={Pages.balance}
class={path.startsWith("/balance") ? "active" : ""}
>
<i18n.Translate>Balance</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
2022-06-02 17:20:36 +02:00
<a href={Pages.backup} class={path.startsWith("/backup") ? "active" : ""}>
<i18n.Translate>Backup</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
2022-06-02 17:20:36 +02:00
<a href={Pages.settings}>
2022-03-24 20:02:38 +01:00
<SvgIcon
title={i18n.str`Settings`}
dangerouslySetInnerHTML={{ __html: settingsIcon }}
color="white"
/>
2022-02-16 19:15:47 +01:00
</a>
</NavigationHeader>
2016-10-10 00:37:08 +02:00
);
}
2022-02-16 19:15:47 +01:00
export function WalletNavBar({ path = "" }: { path?: string }): VNode {
2022-03-14 16:15:41 +01:00
const { i18n } = useTranslationContext();
2021-11-15 15:18:58 +01:00
return (
2022-02-16 19:15:47 +01:00
<NavigationHeaderHolder>
<NavigationHeader>
2022-06-02 17:20:36 +02:00
<a
href={Pages.balance}
class={path.startsWith("/balance") ? "active" : ""}
>
<i18n.Translate>Balance</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
2022-06-02 17:20:36 +02:00
<a
href={Pages.backup}
class={path.startsWith("/backup") ? "active" : ""}
>
<i18n.Translate>Backup</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
2022-02-16 19:15:47 +01:00
<JustInDevMode>
2022-06-02 17:20:36 +02:00
<a href={Pages.dev} class={path.startsWith("/dev") ? "active" : ""}>
<i18n.Translate>Dev</i18n.Translate>
2022-02-23 19:18:37 +01:00
</a>
2022-02-16 19:15:47 +01:00
</JustInDevMode>
<a
2022-06-02 17:20:36 +02:00
href={Pages.settings}
2022-02-16 19:15:47 +01:00
class={path.startsWith("/settings") ? "active" : ""}
>
<i18n.Translate>Settings</i18n.Translate>
2022-02-16 19:15:47 +01:00
</a>
</NavigationHeader>
</NavigationHeaderHolder>
2021-11-15 15:18:58 +01:00
);
2021-07-16 17:00:39 +02:00
}