commit 9c70bb8c746c773830c5f98841f5e6b37e324354
parent 1badd237f1c254dcb4b4244f1f7cb400eb712451
Author: Florian Dold <dold@taler.net>
Date: Thu, 26 Nov 2026 04:12:24 +0100
wallet-webui: report clipboard success only after the copy finishes
The wallet could say it had copied a transfer reference even when the
browser denied access to the clipboard. Wait for the write to finish and
offer selectable text if it fails.
Diffstat:
7 files changed, 127 insertions(+), 50 deletions(-)
diff --git a/packages/wallet-webui/src/routes/App.tsx b/packages/wallet-webui/src/routes/App.tsx
@@ -2312,9 +2312,7 @@ function PeerShareRoute() {
error={error}
retryable={retryable}
retrying={retrying}
- onCopy={() => {
- if (uri) void navigator.clipboard.writeText(uri);
- }}
+ onCopy={(value) => navigator.clipboard.writeText(value)}
onDone={() => navigate(`/transaction/${transactionId}`)}
onRetry={() => void retry()}
recoveryActionLabel={recovery?.actionLabel}
@@ -4550,7 +4548,7 @@ function PaymentRoute({
if (safeWebUrl(fulfillmentUrl))
void platform.openExternal(fulfillmentUrl, fulfillmentTarget);
}}
- onCopyPosConfirmation={(code) => void navigator.clipboard.writeText(code)}
+ onCopyPosConfirmation={(code) => navigator.clipboard.writeText(code)}
/>
);
}
@@ -4858,7 +4856,7 @@ function TransactionDetailView({
amountEffective={observed.amountEffective}
exchange={observed.exchangeBaseUrl ?? i18n.str`Exchange`}
options={transactionWireOptions(observed, [language])}
- onCopy={(value) => void navigator.clipboard.writeText(value)}
+ onCopy={(value) => navigator.clipboard.writeText(value)}
onOpenExternal={(uri) => void platform.openExternal(uri)}
onDone={() => setContinuingTransfer(false)}
/>
@@ -4882,7 +4880,7 @@ function TransactionDetailView({
debitPaytoUri={observed.kycAuthTransferInfo.debitPaytoUri}
accountPub={observed.kycAuthTransferInfo.accountPub}
options={depositKycAuthWireOptions(observed, [language])}
- onCopy={(value) => void navigator.clipboard.writeText(value)}
+ onCopy={(value) => navigator.clipboard.writeText(value)}
onOpenExternal={(uri) => void platform.openExternal(uri)}
onDone={() => setContinuingTransfer(false)}
/>
diff --git a/packages/wallet-webui/src/screens/PaymentScreen.tsx b/packages/wallet-webui/src/screens/PaymentScreen.tsx
@@ -1,3 +1,4 @@
+import { CopyButton } from "../ui/ClipboardFeedback.js";
import { Button } from "../ui/Button.js";
import { Card } from "../ui/Card.js";
import { OrderSummary, type OrderSummaryView } from "../ui/OrderSummary.js";
@@ -500,11 +501,11 @@ export function PaymentHandoff(props: {
/>
</div>
<div class="mt-5 flex flex-wrap justify-end gap-3">
- <Button
- tone="secondary"
- onClick={() => void navigator.clipboard.writeText(props.talerPayUri)}
+ <CopyButton
+ text={props.talerPayUri}
+ label={i18n.str`Copy payment link`}
disabled={props.reclaiming}
- >{i18n.str`Copy payment link`}</Button>
+ />
{props.onReclaim && (
<Button
tone="secondary"
@@ -598,7 +599,7 @@ export function PaymentScreen(props: {
onKeepHere?: () => void;
onReclaim?: () => void;
onOpenFulfillment: () => void;
- onCopyPosConfirmation?: (code: string) => void;
+ onCopyPosConfirmation?: (code: string) => unknown;
}) {
const [confirmUnclaim, setConfirmUnclaim] = useState(false);
if (props.state === "loading")
@@ -912,12 +913,11 @@ export function PaymentScreen(props: {
{props.posConfirmation}
</p>
{props.onCopyPosConfirmation && (
- <Button
- tone="secondary"
- onClick={() =>
- props.onCopyPosConfirmation!(props.posConfirmation!)
- }
- >{i18n.str`Copy code`}</Button>
+ <CopyButton
+ text={props.posConfirmation}
+ label={i18n.str`Copy code`}
+ write={props.onCopyPosConfirmation}
+ />
)}
</Card>
)}
diff --git a/packages/wallet-webui/src/screens/PeerShareScreen.tsx b/packages/wallet-webui/src/screens/PeerShareScreen.tsx
@@ -1,3 +1,4 @@
+import { CopyButton } from "../ui/ClipboardFeedback.js";
import { Button } from "../ui/Button.js";
import { Card } from "../ui/Card.js";
import { ErrorCard } from "../ui/ErrorCard.js";
@@ -19,7 +20,7 @@ export function PeerShareScreen(props: {
onRetryObservation?: () => void;
retryable?: boolean;
retrying?: boolean;
- onCopy: () => void;
+ onCopy: (uri: string) => unknown;
onDone: () => void;
onRetry?: () => void;
recoveryActionLabel?: string;
@@ -115,10 +116,11 @@ export function PeerShareScreen(props: {
<p class="break-all text-sm">{props.uri}</p>
</Card>
<div class="flex flex-wrap justify-end gap-3">
- <Button
- tone="secondary"
- onClick={props.onCopy}
- >{i18n.str`Copy link`}</Button>
+ <CopyButton
+ text={props.uri}
+ write={props.onCopy}
+ label={i18n.str`Copy link`}
+ />
<Button onClick={props.onDone}>{i18n.str`Done`}</Button>
</div>
</>
diff --git a/packages/wallet-webui/src/screens/TransactionDetailScreen.tsx b/packages/wallet-webui/src/screens/TransactionDetailScreen.tsx
@@ -4,6 +4,7 @@ import type {
TransactionDetailView,
TransactionUiAction,
} from "../routes/transaction-model.js";
+import { CopyButton } from "../ui/ClipboardFeedback.js";
import { Button } from "../ui/Button.js";
import { Card } from "../ui/Card.js";
import { ErrorCard } from "../ui/ErrorCard.js";
@@ -282,12 +283,10 @@ export function TransactionDetailScreen(props: {
/>
</div>
<div class="mt-4 flex justify-end">
- <Button
- tone="secondary"
- onClick={() =>
- void navigator.clipboard.writeText(transaction.peerShareUri!)
- }
- >{i18n.str`Copy link`}</Button>
+ <CopyButton
+ text={transaction.peerShareUri}
+ label={i18n.str`Copy link`}
+ />
</div>
</Card>
)}
@@ -388,15 +387,10 @@ export function TransactionDetailScreen(props: {
<dd class={field.breakAll ? "break-all" : ""}>
{field.value}
{field.copyable && (
- <button
- type="button"
- class="ml-2 rounded px-2 py-1 font-semibold text-primary hover:bg-secondaryContainer"
- onClick={() =>
- void navigator.clipboard.writeText(field.value)
- }
- >
- {i18n.str`Copy`}
- </button>
+ <CopyButton
+ text={field.value}
+ label={i18n.str`Copy ${field.label}`}
+ />
)}
</dd>
</div>
diff --git a/packages/wallet-webui/src/screens/WireTransferScreen.tsx b/packages/wallet-webui/src/screens/WireTransferScreen.tsx
@@ -1,3 +1,7 @@
+import {
+ ClipboardFallback,
+ useClipboardFeedback,
+} from "../ui/ClipboardFeedback.js";
import { useEffect, useState } from "preact/hooks";
import type {
WireFieldView,
@@ -14,7 +18,7 @@ function WireInstructions(props: {
transferAmount: string;
isKycAuth: boolean;
copiedValue?: string;
- onCopy: (value: string) => void;
+ onCopy: (value: string) => unknown;
}) {
const { fields, reference } = props.option;
const showSteps = !props.isKycAuth && !!reference;
@@ -87,12 +91,12 @@ export function WireTransferScreen(props: {
purpose?: "withdrawal" | "deposit-kyc-auth";
debitPaytoUri?: string;
accountPub?: string;
- onCopy: (value: string) => void;
+ onCopy: (value: string) => unknown;
onOpenExternal: (uri: string) => void;
onDone: () => void;
}) {
const isKycAuth = props.purpose === "deposit-kyc-auth";
- const [copied, setCopied] = useState<{ optionId: string; value: string }>();
+ const { copy, result } = useClipboardFeedback(props.onCopy);
const [selectedOptionId, setSelectedOptionId] = useState(
props.options[0]?.id,
);
@@ -118,12 +122,8 @@ export function WireTransferScreen(props: {
);
const activeOption = props.options[activeOptionIndex];
const transferAmount = activeOption?.transferAmount ?? props.amountRaw;
- const copy = (optionId: string, value: string) => {
- props.onCopy(value);
- setCopied({ optionId, value });
- };
const wasCopied = (optionId: string, value: string) =>
- copied?.optionId === optionId && copied.value === value;
+ result?.ok && result.key === optionId && result.text === value;
return (
<div class="mx-auto max-w-2xl space-y-6">
@@ -368,8 +368,8 @@ export function WireTransferScreen(props: {
transferAmount={transferAmount}
isKycAuth={isKycAuth}
copiedValue={
- copied?.optionId === option.id
- ? copied.value
+ result?.ok && result.key === option.id
+ ? result.text
: undefined
}
onCopy={(value) => copy(option.id, value)}
@@ -381,6 +381,9 @@ export function WireTransferScreen(props: {
})}
</section>
)}
+ {result && !result.ok && result.key === activeOption?.id && (
+ <ClipboardFallback text={result.text} />
+ )}
<div class="flex justify-end">
<Button onClick={props.onDone}>
{isKycAuth
diff --git a/packages/wallet-webui/src/ui/ClipboardFeedback.tsx b/packages/wallet-webui/src/ui/ClipboardFeedback.tsx
@@ -0,0 +1,68 @@
+import { useRef, useState } from "preact/hooks";
+import { Button } from "./Button.js";
+import { i18n } from "../i18n/runtime.js";
+
+export function useClipboardFeedback(write: (text: string) => unknown) {
+ const [result, setResult] = useState<{
+ key: string;
+ text: string;
+ ok: boolean;
+ }>();
+ const latest = useRef(0);
+ async function copy(key: string, text: string) {
+ const attempt = ++latest.current;
+ setResult(undefined);
+ try {
+ await write(text);
+ if (latest.current === attempt) setResult({ key, text, ok: true });
+ } catch {
+ if (latest.current === attempt) setResult({ key, text, ok: false });
+ }
+ }
+ return { copy, result };
+}
+
+export function ClipboardFallback({ text }: { text: string }) {
+ const hint = i18n.str`Could not copy. Select the text and copy it manually.`;
+ return (
+ <label class="block rounded-xl bg-warningContainer p-3">
+ <span role="status">{hint}</span>
+ <textarea
+ aria-label={hint}
+ readOnly
+ value={text}
+ onFocus={(event) => event.currentTarget.select()}
+ class="mt-2 block w-full rounded border bg-surface p-2 text-onSurface"
+ />
+ </label>
+ );
+}
+
+/** A copy action that reports the actual outcome and preserves a manual fallback. */
+export function CopyButton(props: {
+ text: string;
+ label: string;
+ disabled?: boolean;
+ write?: (text: string) => unknown;
+}) {
+ const { copy, result } = useClipboardFeedback(
+ props.write ?? ((text) => navigator.clipboard.writeText(text)),
+ );
+ return (
+ <span class="inline-flex max-w-full flex-col items-start gap-2">
+ <Button
+ tone="secondary"
+ disabled={props.disabled}
+ onClick={() => void copy(props.text, props.text)}
+ >
+ {props.label}
+ </Button>
+ {result?.text === props.text &&
+ (result.ok ? (
+ <span role="status">{i18n.str`Copied to clipboard.`}</span>
+ ) : (
+ <ClipboardFallback text={props.text} />
+ ))}
+ </span>
+ );
+}
diff --git a/packages/wallet-webui/test/screens.test.tsx b/packages/wallet-webui/test/screens.test.tsx
@@ -2202,6 +2202,7 @@ test("completed POS payments display and copy the merchant confirmation code", a
run(context: Element): Promise<{ violations: unknown[] }>;
};
let copied = "";
+ let clipboardDenied = false;
const view = render(
<main>
<PaymentScreen
@@ -2214,7 +2215,8 @@ test("completed POS payments display and copy the merchant confirmation code", a
onBack={() => {}}
onCancel={() => {}}
onOpenFulfillment={() => {}}
- onCopyPosConfirmation={(code) => {
+ onCopyPosConfirmation={async (code) => {
+ if (clipboardDenied) throw Error("Write permission denied");
copied = code;
}}
/>
@@ -2228,6 +2230,17 @@ test("completed POS payments display and copy the merchant confirmation code", a
.setup({ document: window.document as unknown as Document })
.click(view.getByRole("button", { name: "Copy code" }));
assert.equal(copied, "POS-4Q7R-9K2M");
+ assert(view.getByText("Copied to clipboard."));
+ clipboardDenied = true;
+ await userEvent
+ .setup({ document: window.document as unknown as Document })
+ .click(view.getByRole("button", { name: "Copy code" }));
+ const fallback = view.getByRole("textbox", {
+ name: /Could not copy/,
+ }) as HTMLTextAreaElement;
+ assert.equal(fallback.value, "POS-4Q7R-9K2M");
+ assert.equal(fallback.readOnly, true);
+ assert.equal(view.queryByText("Copied to clipboard."), null);
assert.deepEqual(
(await axe.run(window.document.body as unknown as Element)).violations,
[],
@@ -3538,8 +3551,7 @@ test("wire instructions tab between options and copy the mandatory subject and p
run(context: Element): Promise<{ violations: unknown[] }>;
};
let copied = "";
- const paytoUri =
- "payto://iban/CH680857?amount=EUR%3A12&message=RESERVE123";
+ const paytoUri = "payto://iban/CH680857?amount=EUR%3A12&message=RESERVE123";
const view = render(
<main>
<WireTransferScreen