Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve pasting #496

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ const dict = {
broadcasting_claim: "Broadcasting claim transaction...",
open_swap: "Open Swap",
swap_in_progress: "This swap is still in progress.",
paste_invalid:
"Clipboard contains invalid characters or maximum amount is exceeded",
},
de: {
language: "Deutsch",
Expand Down Expand Up @@ -351,6 +353,8 @@ const dict = {
broadcasting_claim: "Sende claim transaction...",
open_swap: "Swap öffnen",
swap_in_progress: "Dieser Swap ist noch nicht abgeschlossen.",
paste_invalid:
"Zwischenablage enthält ungültige Zeichen oder der maximale Betrag wurde überschritten",
},
es: {
language: "Español",
Expand Down Expand Up @@ -532,6 +536,8 @@ const dict = {
broadcasting_claim: "Enviando transacción de reclamación...",
open_swap: "Abrir intercambio",
swap_in_progress: "Este intercambio aún está en curso.",
paste_invalid:
"El portapapeles contiene caracteres no válidos o se ha excedido el monto máximo",
},
zh: {
language: "中文",
Expand Down Expand Up @@ -696,6 +702,7 @@ const dict = {
broadcasting_claim: "正在发送索赔交易...",
open_swap: "打开交换",
swap_in_progress: "此交换仍在进行中。",
paste_invalid: "剪贴板包含无效字符或超出最大金额",
},
};

Expand Down
6 changes: 3 additions & 3 deletions src/pages/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Create = () => {
let receiveAmountRef: HTMLInputElement | undefined = undefined;
let sendAmountRef: HTMLInputElement | undefined = undefined;

const { setDenomination, denomination, wasmSupported, webln, t } =
const { setDenomination, denomination, wasmSupported, webln, t, notify } =
useGlobalContext();
const {
reverse,
Expand Down Expand Up @@ -134,10 +134,10 @@ const Create = () => {
const validatePaste = (evt: ClipboardEvent) => {
const clipboardData = evt.clipboardData || globalThis.clipboardData;
const pastedData = clipboardData.getData("Text").trim();
changeDenomination(pastedData);
if (!getValidationRegex(maximum(), denomination()).test(pastedData)) {
if (!getValidationRegex(maximum()).test(pastedData)) {
evt.stopPropagation();
evt.preventDefault();
notify("error", t("paste_invalid"));
}
};

Expand Down
13 changes: 5 additions & 8 deletions src/utils/denomination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ export const denominations = {
btc: "btc",
};

export const getValidationRegex = (
maximum: number,
denomination: string,
): RegExp => {
const regex =
denomination === denominations.sat
? `^[0-9]{1,${maximum.toString().length}}$`
: `^[0-9](.[0-9]{1,10}){0,1}$`;
export const getValidationRegex = (maximum: number): RegExp => {
const digits = maximum.toString().length;
const firstDigit = BigNumber(maximum).div(satFactor).toString().charAt(0);
const firstDigitRegex = firstDigit === "0" ? `0` : `0-${firstDigit}`;
const regex = `^[0-9]{1,${digits}}$|^[${firstDigitRegex}](\\.[0-9]{1,8}){0,1}$`;
return new RegExp(regex);
};

Expand Down
38 changes: 16 additions & 22 deletions tests/utils/denomination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,23 @@ describe("denomination utils", () => {
});

describe("check paste validation regex", () => {
const max = 100000000;
const max = 40_000_000;

test.each`
denomination | amount | valid
${denominations.sat} | ${"123123"} | ${true}
${denominations.sat} | ${max} | ${true}
${denominations.sat} | ${max * 10} | ${false}
${denominations.sat} | ${"lol"} | ${false}
${denominations.btc} | ${"lol"} | ${false}
${denominations.btc} | ${"123123"} | ${true}
${denominations.btc} | ${"0.123123"} | ${true}
${denominations.btc} | ${"0.1231.23"} | ${false}
${denominations.btc} | ${"1.12321"} | ${true}
${denominations.btc} | ${"10.12300011"} | ${false}
${denominations.btc} | ${max / 10 ** 8} | ${true}
${denominations.btc} | ${(max / 10 ** 8) * 10} | ${false}
${denominations.btc} | ${"0.12312313123131"} | ${false}
`(
"validating regex for $amount in $denomination",
({ denomination, amount, valid }) => {
let regex = getValidationRegex(max, denomination);
expect(regex.test(amount)).toEqual(valid);
},
);
amount | valid
${"123123"} | ${true}
${max} | ${true}
${max * 10} | ${false}
${"lol"} | ${false}
${"0.12312333"} | ${true}
${"0.1231.23"} | ${false}
${"1.12321"} | ${false}
${"10.12300011"} | ${false}
${"0.123123131"} | ${false}
${"0,123"} | ${false}
`("validating regex for $amount", ({ amount, valid }) => {
let regex = getValidationRegex(max);
expect(regex.test(amount)).toEqual(valid);
});
});
});