commit 5a47c08088f7fe0ab9b334eea877d9c83515332e
parent 6adac325f9dc9fe1f94e276f1e1a5c29bf5d9852
Author: Florian Dold <dold@taler.net>
Date: Thu, 26 Nov 2026 04:14:56 +0100
wallet-webui: let users read and accept updated exchange terms
Large terms could only be downloaded, and a new revision could appear to
have been accepted already. Show long documents as paged plain text and
use the acceptance status of the revision the wallet just fetched.
Diffstat:
5 files changed, 164 insertions(+), 50 deletions(-)
diff --git a/packages/taler-wallet-core/src/exchanges.ts b/packages/taler-wallet-core/src/exchanges.ts
@@ -3958,12 +3958,16 @@ export async function getExchangeTos(
checkLogicInvariant(!!tosDownload);
- await wex.runWalletDbTx(async (tx) => {
+ const acceptance = await wex.runWalletDbTx(async (tx) => {
const updateExchangeEntry = await tx.getExchange(exchangeBaseUrl);
- if (
- updateExchangeEntry &&
- updateExchangeEntry.tosCurrentEtag !== tosDownload.tosEtag
- ) {
+ if (!updateExchangeEntry) {
+ throw TalerError.fromDetail(
+ TalerErrorCode.WALLET_EXCHANGE_ENTRY_NOT_FOUND,
+ { exchangeBaseUrl },
+ "no exchange entry for that base URL",
+ );
+ }
+ if (updateExchangeEntry.tosCurrentEtag !== tosDownload.tosEtag) {
const oldExchangeState = getExchangeState(updateExchangeEntry);
updateExchangeEntry.tosCurrentEtag = tosDownload.tosEtag;
wex.ws.exchangeCache.clear();
@@ -3976,15 +3980,18 @@ export async function getExchangeTos(
newExchangeState: getExchangeState(updateExchangeEntry),
});
}
+ return {
+ acceptedEtag: updateExchangeEntry.tosAcceptedEtag,
+ tosStatus: getExchangeTosStatusFromRecord(updateExchangeEntry),
+ };
});
return {
- acceptedEtag: exch.tosAcceptedEtag,
+ ...acceptance,
currentEtag: tosDownload.tosEtag,
content: tosDownload.tosText,
contentType: tosDownload.tosContentType,
contentLanguage: tosDownload.tosContentLanguage,
- tosStatus: exch.tosStatus,
tosAvailableLanguages: tosDownload.tosAvailableLanguages,
};
}
diff --git a/packages/taler-wallet-core/src/observe-transaction.test.ts b/packages/taler-wallet-core/src/observe-transaction.test.ts
@@ -565,7 +565,13 @@ for (const makeRunner of runnerFactories) {
};
},
};
- await getExchangeTos({ ...f.wex, http }, exchangeUrl);
+ const downloaded = await getExchangeTos(
+ { ...f.wex, http },
+ exchangeUrl,
+ );
+ assert.equal(downloaded.currentEtag, "v2");
+ assert.equal(downloaded.acceptedEtag, "v1");
+ assert.equal(downloaded.tosStatus, ExchangeTosStatus.Proposed);
const tos = await next;
assert.equal(tos.revision, 2);
assert.deepEqual(
@@ -619,6 +625,9 @@ for (const makeRunner of runnerFactories) {
(await next).snapshot?.confirmation.unavailableReasons,
[],
);
+ const accepted = await getExchangeTos({ ...f.wex, http }, exchangeUrl);
+ assert.equal(accepted.acceptedEtag, "v2");
+ assert.equal(accepted.tosStatus, ExchangeTosStatus.Accepted);
} finally {
await f.ws.shutdown();
}
diff --git a/packages/wallet-webui/src/routes/ManagementRoutes.tsx b/packages/wallet-webui/src/routes/ManagementRoutes.tsx
@@ -486,13 +486,13 @@ export function ExchangeTermsRoute() {
if (active) setTerms(value);
},
(cause) => {
- if (active && !isProgressRequestCancelled(cause))
- setLoadError(
- errorFromException(
- cause,
- /* Translators: An exchange is a GNU Taler service issuing and redeeming digital cash, not an action that converts one currency into another. */ i18n.str`Exchange terms could not be loaded.`,
- ),
- );
+ if (!active) return;
+ const message = /* Translators: An exchange is a GNU Taler service issuing and redeeming digital cash, not an action that converts one currency into another. */ i18n.str`Exchange terms could not be loaded.`;
+ setLoadError(
+ isProgressRequestCancelled(cause)
+ ? localError(message)
+ : errorFromException(cause, message),
+ );
},
)
.finally(() => {
@@ -508,12 +508,13 @@ export function ExchangeTermsRoute() {
termsRevision,
]);
const accept = async () => {
+ if (!terms || loading || working) return;
setWorking(true);
setError(undefined);
try {
const result = await callMutation(
WalletApiOperation.SetExchangeTosAccepted,
- { exchangeBaseUrl: exchangeUrl, expectedEtag: terms?.currentEtag },
+ { exchangeBaseUrl: exchangeUrl, expectedEtag: terms.currentEtag },
["exchanges"],
);
if (Result.isError(result)) {
@@ -534,6 +535,12 @@ export function ExchangeTermsRoute() {
);
return (
<TermsScreen
+ key={JSON.stringify([
+ exchangeUrl,
+ terms?.currentEtag,
+ terms?.contentLanguage,
+ languagePreference.primary,
+ ])}
exchangeUrl={exchangeUrl}
content={terms?.content}
contentType={terms?.contentType}
@@ -542,7 +549,15 @@ export function ExchangeTermsRoute() {
status={terms?.tosStatus}
loading={loading}
working={working}
- error={routeError ? localError(routeError) : (error ?? loadError)}
+ error={routeError ? localError(routeError) : (loadError ?? error)}
+ onRetryLoad={
+ loadError && !loading
+ ? () => {
+ setError(undefined);
+ setTermsRevision((revision) => revision + 1);
+ }
+ : undefined
+ }
onLanguage={setDialogLanguage}
onOpenLink={(url) => void platform.openExternal(url)}
onAccept={() => void accept()}
diff --git a/packages/wallet-webui/src/screens/TermsScreen.tsx b/packages/wallet-webui/src/screens/TermsScreen.tsx
@@ -1,3 +1,4 @@
+import { useMemo, useRef, useState } from "preact/hooks";
import { Button } from "../ui/Button.js";
import { Card } from "../ui/Card.js";
import { ErrorCard } from "../ui/ErrorCard.js";
@@ -24,6 +25,58 @@ export function exchangeTermsStatusLabel(status: string | undefined): string {
}
}
+/** Keep the text accessible without parsing an unbounded Markdown document. */
+function PagedTerms(props: { content: string }) {
+ const pages = useMemo(() => {
+ const pages: string[] = [];
+ const limit = 8192;
+ for (let start = 0; start < props.content.length; ) {
+ let end = Math.min(start + limit, props.content.length);
+ if (end < props.content.length) {
+ // Prefer a nearby paragraph break and keep UTF-16 surrogate pairs intact.
+ const tailStart = end - 1024;
+ const paragraph = props.content.slice(tailStart, end).lastIndexOf("\n");
+ if (paragraph >= 0) end = tailStart + paragraph + 1;
+ else if (/^[\uDC00-\uDFFF]$/.test(props.content[end])) end--;
+ }
+ pages.push(props.content.slice(start, end));
+ start = end;
+ }
+ return pages;
+ }, [props.content]);
+ const [page, setPage] = useState(0);
+ const reader = useRef<HTMLPreElement>(null);
+ const changePage = (next: number) => {
+ setPage(next);
+ if (reader.current) reader.current.scrollTop = 0;
+ };
+ return (
+ <Card>
+ <p class="mb-3 text-sm text-secondary">{i18n.str`These terms are shown as plain text in pages.`}</p>
+ <pre
+ ref={reader}
+ tabIndex={0}
+ class="max-h-[55vh] overflow-auto whitespace-pre-wrap break-words font-sans text-sm leading-6"
+ >
+ {pages[page]}
+ </pre>
+ <div class="mt-3 flex flex-wrap items-center justify-between gap-3">
+ <Button
+ tone="secondary"
+ disabled={page === 0}
+ onClick={() => changePage(page - 1)}
+ >{i18n.str`Previous page`}</Button>
+ <span role="status">{i18n.str`Page ${page + 1} of ${pages.length}`}</span>
+ <Button
+ tone="secondary"
+ disabled={page + 1 === pages.length}
+ onClick={() => changePage(page + 1)}
+ >{i18n.str`Next page`}</Button>
+ </div>
+ </Card>
+ );
+}
+
export function TermsScreen(props: {
exchangeUrl: string;
content?: string;
@@ -35,16 +88,17 @@ export function TermsScreen(props: {
working: boolean;
error?: ErrorPresentation;
onLanguage: (language: string) => void;
+ onRetryLoad?: () => void;
onOpenLink: (url: string) => void;
onAccept: () => void;
onSave: () => void;
onBack: () => void;
}) {
const missingTerms = props.status === "missing-tos";
- const markdownTooLarge =
- props.content !== undefined &&
- isMarkdownContentType(props.contentType) &&
- !isSafeMarkdownSize(props.content);
+ const paged = useMemo(
+ () => props.content !== undefined && !isSafeMarkdownSize(props.content),
+ [props.content],
+ );
return (
<div class="mx-auto max-w-3xl space-y-6">
<div class="flex items-center gap-4">
@@ -68,7 +122,15 @@ export function TermsScreen(props: {
<p role="status">{i18n.str`Loading terms of service…`}</p>
</Card>
)}
- {props.error && <ErrorCard error={props.error} />}
+ {props.error && (
+ <ErrorCard error={props.error}>
+ {props.onRetryLoad && (
+ <div class="mt-3">
+ <Button onClick={props.onRetryLoad}>{i18n.str`Retry`}</Button>
+ </div>
+ )}
+ </ErrorCard>
+ )}
{missingTerms && (
<Card>
<p>
@@ -89,6 +151,7 @@ export function TermsScreen(props: {
<span class="mr-2 text-sm text-secondary">{i18n.str`Language`}</span>
<select
value={props.language}
+ disabled={props.working}
onChange={(event) =>
props.onLanguage(event.currentTarget.value)
}
@@ -103,19 +166,23 @@ export function TermsScreen(props: {
</label>
)}
</div>
- <Card class="max-h-[55vh] overflow-auto">
- {isMarkdownContentType(props.contentType) ? (
- <SafeMarkdown
- content={props.content}
- tooLargeMessage={i18n.str`These terms are too large to display safely. Save the file to review them.`}
- onOpenLink={props.onOpenLink}
- />
- ) : (
- <pre class="whitespace-pre-wrap font-sans text-sm leading-6">
- {props.content}
- </pre>
- )}
- </Card>
+ {paged ? (
+ <PagedTerms content={props.content} />
+ ) : (
+ <Card class="max-h-[55vh] overflow-auto">
+ {isMarkdownContentType(props.contentType) ? (
+ <SafeMarkdown
+ content={props.content}
+ tooLargeMessage={i18n.str`These terms are too large to display safely. Save the file to review them.`}
+ onOpenLink={props.onOpenLink}
+ />
+ ) : (
+ <pre class="whitespace-pre-wrap font-sans text-sm leading-6">
+ {props.content}
+ </pre>
+ )}
+ </Card>
+ )}
<div class="flex flex-wrap justify-end gap-3">
<Button
tone="secondary"
@@ -124,7 +191,7 @@ export function TermsScreen(props: {
<Button
onClick={props.onAccept}
disabled={
- props.working || props.status === "accepted" || markdownTooLarge
+ props.loading || props.working || props.status === "accepted"
}
>
{props.status === "accepted"
diff --git a/packages/wallet-webui/test/markdown.test.tsx b/packages/wallet-webui/test/markdown.test.tsx
@@ -194,13 +194,18 @@ test("terms render markdown only for the markdown media type", async () => {
await window.happyDOM.abort();
});
-test("oversized markdown can be saved but not accepted", async () => {
+test("large terms are complete paged plain text and can be accepted", async () => {
const window = installDom();
- const { render, cleanup } = await import("@testing-library/preact");
+ const { render, cleanup, act } = await import("@testing-library/preact");
+ const content =
+ "# Unformatted heading\n" +
+ "😀".repeat(MAX_SAFE_MARKDOWN_BYTES / 3) +
+ "\nFinal paragraph.";
+ const actions: string[] = [];
const view = render(
<TermsScreen
exchangeUrl="https://exchange.example/"
- content={"x".repeat(MAX_SAFE_MARKDOWN_BYTES + 1)}
+ content={content}
contentType="text/markdown"
languages={[]}
status="proposed"
@@ -208,22 +213,33 @@ test("oversized markdown can be saved but not accepted", async () => {
working={false}
onLanguage={() => {}}
onOpenLink={() => {}}
- onAccept={() => {}}
- onSave={() => {}}
+ onAccept={() => actions.push("accept")}
+ onSave={() => actions.push("save")}
onBack={() => {}}
/>,
);
- assert.match(view.getByRole("alert").textContent ?? "", /too large/i);
assert.equal(
- (view.getByRole("button", { name: "Accept terms" }) as HTMLButtonElement)
- .disabled,
- true,
- );
- assert.equal(
- (view.getByRole("button", { name: "Save terms" }) as HTMLButtonElement)
- .disabled,
- false,
+ view.queryByRole("heading", { name: "Unformatted heading" }),
+ null,
);
+ const next = view.getByRole("button", {
+ name: "Next page",
+ }) as HTMLButtonElement;
+ const parts: string[] = [];
+ do {
+ const page = view.container.querySelector("pre")!.textContent!;
+ assert(page.length <= 8192);
+ assert(!/^[\uDC00-\uDFFF]|[\uD800-\uDBFF]$/.test(page));
+ parts.push(page);
+ if (next.disabled) break;
+ await act(() => next.click());
+ } while (parts.length < 200);
+ assert.equal(parts.join(""), content);
+ await act(() => view.getByRole("button", { name: "Previous page" }).click());
+ assert.equal(view.container.querySelector("pre")?.textContent, parts.at(-2));
+ await act(() => view.getByRole("button", { name: "Save terms" }).click());
+ await act(() => view.getByRole("button", { name: "Accept terms" }).click());
+ assert.deepEqual(actions, ["save", "accept"]);
cleanup();
await window.happyDOM.abort();
});