From 5d9390bb3437171353db99e7afd8cbcca432ba5c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 2 Jun 2022 12:20:36 -0300 Subject: [PATCH] typechecked dynamic path --- .../src/NavigationBar.tsx | 117 +++++++++++++----- .../src/components/PendingTransactions.tsx | 4 +- .../src/components/TransactionItem.tsx | 2 +- .../src/popup/Application.tsx | 39 +++--- .../src/wallet/Application.tsx | 68 ++++------ .../src/wallet/BackupPage.tsx | 7 +- .../src/wallet/CreateManualWithdraw.tsx | 9 +- .../src/wallet/DeveloperPage.tsx | 3 - .../src/wallet/Settings.tsx | 4 +- .../src/wallet/Transaction.tsx | 14 +-- 10 files changed, 145 insertions(+), 122 deletions(-) diff --git a/packages/taler-wallet-webextension/src/NavigationBar.tsx b/packages/taler-wallet-webextension/src/NavigationBar.tsx index 58783a087..9c8e1ea29 100644 --- a/packages/taler-wallet-webextension/src/NavigationBar.tsx +++ b/packages/taler-wallet-webextension/src/NavigationBar.tsx @@ -40,43 +40,94 @@ import settingsIcon from "./svg/settings_black_24dp.svg"; * @author sebasjm */ -export enum Pages { - welcome = "/welcome", +type PageLocation = { + pattern: string; + (params: DynamicPart): string; +}; - balance = "/balance", - balance_history = "/balance/history/:currency?", - balance_manual_withdraw = "/balance/manual-withdraw/:currency?", - balance_deposit = "/balance/deposit/:currency", - balance_transaction = "/balance/transaction/:tid", - - dev = "/dev", - - backup = "/backup", - backup_provider_detail = "/backup/provider/:pid", - backup_provider_add = "/backup/provider/add", - - settings = "/settings", - settings_exchange_add = "/settings/exchange/add/:currency?", - - cta = "/cta/:action", - cta_pay = "/cta/pay", - cta_refund = "/cta/refund", - cta_tips = "/cta/tip", - cta_withdraw = "/cta/withdraw", - cta_deposit = "/cta/deposit", +function replaceAll( + pattern: string, + vars: Record, + values: Record, +): string { + let result = pattern; + for (const v in vars) { + result = result.replace(vars[v], values[v]); + } + return result; } +function pageDefinition(pattern: string): PageLocation { + 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); + + 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", +}; + export function PopupNavBar({ path = "" }: { path?: string }): VNode { const { i18n } = useTranslationContext(); return ( - + Balance - + Backup - + - + Balance - + Backup - + Dev Settings diff --git a/packages/taler-wallet-webextension/src/components/PendingTransactions.tsx b/packages/taler-wallet-webextension/src/components/PendingTransactions.tsx index 2fc2d2d38..55287903a 100644 --- a/packages/taler-wallet-webextension/src/components/PendingTransactions.tsx +++ b/packages/taler-wallet-webextension/src/components/PendingTransactions.tsx @@ -14,7 +14,7 @@ import Banner from "./Banner.js"; import { Time } from "./Time.js"; interface Props extends JSX.HTMLAttributes { - goToTransaction: (id: string) => void; + goToTransaction: (id: string) => Promise; } export function PendingTransactions({ goToTransaction }: Props): VNode { @@ -46,7 +46,7 @@ export function PendingTransactionsView({ transactions, goToTransaction, }: { - goToTransaction: (id: string) => void; + goToTransaction: (id: string) => Promise; transactions: Transaction[]; }): VNode { return ( diff --git a/packages/taler-wallet-webextension/src/components/TransactionItem.tsx b/packages/taler-wallet-webextension/src/components/TransactionItem.tsx index bfffa3267..127d0411a 100644 --- a/packages/taler-wallet-webextension/src/components/TransactionItem.tsx +++ b/packages/taler-wallet-webextension/src/components/TransactionItem.tsx @@ -120,7 +120,7 @@ function TransactionLayout(props: TransactionLayoutProps): VNode { const { i18n } = useTranslationContext(); return ( { - if (talerActionUrl) - route(Pages.cta.replace(":action", encodeURIComponent(talerActionUrl))); + if (talerActionUrl) { + route(Pages.cta({ action: encodeURIComponent(talerActionUrl) })); + } }, [talerActionUrl]); return ; @@ -60,8 +61,8 @@ export function Application(): VNode { {({ devMode }: { devMode: boolean }) => ( - redirectTo(Pages.balance_transaction.replace(":tid", txId)) + goToTransaction={(tid: string) => + redirectTo(Pages.balanceTransaction({ tid })) } /> @@ -74,24 +75,18 @@ export function Application(): VNode { path={Pages.balance} component={BalancePage} goToWalletManualWithdraw={() => - redirectTo( - Pages.balance_manual_withdraw.replace(":currency?", ""), - ) + redirectTo(Pages.balanceManualWithdraw({})) } goToWalletDeposit={(currency: string) => - redirectTo( - Pages.balance_deposit.replace(":currency", currency), - ) + redirectTo(Pages.balanceDeposit({ currency })) } goToWalletHistory={(currency: string) => - redirectTo( - Pages.balance_history.replace(":currency?", currency), - ) + redirectTo(Pages.balanceHistory({ currency })) } /> redirectTo(Pages.backup_provider_add)} + onAddProvider={() => redirectTo(Pages.backupProviderAdd)} /> redirectTo(Pages.backup)} /> diff --git a/packages/taler-wallet-webextension/src/wallet/Application.tsx b/packages/taler-wallet-webextension/src/wallet/Application.tsx index 37ea80d96..84aa0dd84 100644 --- a/packages/taler-wallet-webextension/src/wallet/Application.tsx +++ b/packages/taler-wallet-webextension/src/wallet/Application.tsx @@ -93,10 +93,8 @@ export function Application(): VNode { }} > - redirectTo( - Pages.balance_transaction.replace(":tid", txId), - ) + goToTransaction={(tid: string) => + redirectTo(Pages.balanceTransaction({ tid })) } /> @@ -122,50 +120,37 @@ export function Application(): VNode { */} - redirectTo( - Pages.balance_deposit.replace(":currency", currency), - ) + redirectTo(Pages.balanceDeposit({ currency })) } goToWalletManualWithdraw={(currency?: string) => - redirectTo( - Pages.balance_manual_withdraw.replace( - ":currency?", - currency || "", - ), - ) + redirectTo(Pages.balanceManualWithdraw({ currency })) } /> - redirectTo( - Pages.balance_history.replace(":currency?", currency || ""), - ) + redirectTo(Pages.balanceHistory({ currency })) } /> redirectTo(Pages.balance)} /> { - redirectTo( - Pages.balance_history.replace(":currency?", currency), - ); + redirectTo(Pages.balanceHistory({ currency })); }} onSuccess={(currency: string) => { - redirectTo( - Pages.balance_history.replace(":currency?", currency), - ); + redirectTo(Pages.balanceHistory({ currency })); setGlobalNotification( All done, your transaction is in progress @@ -184,15 +169,15 @@ export function Application(): VNode { redirectTo(Pages.backup_provider_add)} + onAddProvider={() => redirectTo(Pages.backupProviderAdd)} /> redirectTo(Pages.backup)} /> redirectTo(Pages.backup)} /> @@ -201,7 +186,7 @@ export function Application(): VNode { * SETTINGS */} redirectTo(Pages.balance)} /> @@ -216,22 +201,17 @@ export function Application(): VNode { * CALL TO ACTION */} - redirectTo( - Pages.balance_manual_withdraw.replace( - ":currency?", - currency || "", - ), - ) + redirectTo(Pages.balanceManualWithdraw({ currency })) } goBack={() => redirectTo(Pages.balance)} /> - - - - + + + + {/** * NOT FOUND @@ -240,13 +220,13 @@ export function Application(): VNode { @@ -268,5 +248,7 @@ function Redirect({ to }: { to: string }): null { } function shouldShowPendingOperations(path: string): boolean { + // FIXME: replace includes with a match API like preact router does + // [Pages.balanceHistory, Pages.dev, Pages.settings, Pages.backup] return ["/balance/history/", "/dev", "/settings", "/backup"].includes(path); } diff --git a/packages/taler-wallet-webextension/src/wallet/BackupPage.tsx b/packages/taler-wallet-webextension/src/wallet/BackupPage.tsx index 1f23be856..b1c34c213 100644 --- a/packages/taler-wallet-webextension/src/wallet/BackupPage.tsx +++ b/packages/taler-wallet-webextension/src/wallet/BackupPage.tsx @@ -167,10 +167,9 @@ function BackupLayout(props: TransactionLayoutProps): VNode {
{props.title} diff --git a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx b/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx index 502e9f8ad..d2a9c1366 100644 --- a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx +++ b/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx @@ -165,10 +165,7 @@ export function CreateManualWithdraw({ Add Exchange @@ -194,7 +191,7 @@ export function CreateManualWithdraw({ No exchange configured Add Exchange @@ -242,7 +239,7 @@ export function CreateManualWithdraw({
Add Exchange diff --git a/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx b/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx index e71ea48f0..074b0d356 100644 --- a/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx +++ b/packages/taler-wallet-webextension/src/wallet/DeveloperPage.tsx @@ -156,9 +156,6 @@ export function View({ [exchange_name: string]: CalculatedCoinfInfo[]; }, ); - function Item({ children }: any) { - return
{children}
; - } return (

diff --git a/packages/taler-wallet-webextension/src/wallet/Settings.tsx b/packages/taler-wallet-webextension/src/wallet/Settings.tsx index be75c1eac..55f046259 100644 --- a/packages/taler-wallet-webextension/src/wallet/Settings.tsx +++ b/packages/taler-wallet-webextension/src/wallet/Settings.tsx @@ -178,9 +178,7 @@ export function SettingsView({ )}

- + Add an exchange
diff --git a/packages/taler-wallet-webextension/src/wallet/Transaction.tsx b/packages/taler-wallet-webextension/src/wallet/Transaction.tsx index b78039ddd..89f7064c5 100644 --- a/packages/taler-wallet-webextension/src/wallet/Transaction.tsx +++ b/packages/taler-wallet-webextension/src/wallet/Transaction.tsx @@ -352,10 +352,9 @@ export function TransactionView({ {}{" "} was refunded {" "} @@ -556,10 +555,9 @@ export function TransactionView({ title={Original order ID} text={ {transaction.info.orderId}