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

Use minSendable and maxSendable values from LNURLp response to guide user input #1697

Closed
wants to merge 9 commits into from
62 changes: 33 additions & 29 deletions src/app/components/SatButtons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,43 @@ import Button from "../Button";
type Props = {
onClick: (amount: string) => void;
disabled?: boolean;
min?: number;
max?: number;
};

function SatButtons({ onClick, disabled }: Props) {
function roundThousands(number: number) {
return number < 1000 ? Math.round(number) : Math.floor(number / 1000) * 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to round here but take into account whether this is the min / max value.

Let's say a service sets 1.4 as their minimum value, we would currently round (which would set the minimum to 1) which will then fail because it is lower than the min amount.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it would make sense to add some unit tests for this behavior as well.

cc @escapedcat

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to help with unit-tests if wanted once this is fixed.

}

function stringifyThousands(number: number) {
return number < 1000
? `${Math.round(number)}`
: `${Math.floor(number / 1000)}K`;
}

function amountsArray(min: number, max: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this is leaving afraid and confused :P
Not sure how others feel. Is there a nice way to makes this easier to digest?

const distance = Math.min(max - min, 10000);
const safeMin = max < 100 ? min : Math.max(min, 100);
return [safeMin, min + distance / 10, min + distance / 2, min + distance].map(
(amount) => roundThousands(amount)
);
}

function SatButtons({ onClick, disabled, min = 100, max = 10000 }: Props) {
// we use Set to dedup the array, useful if the min-max range is tight
const amounts = [...new Set(amountsArray(min, max))];
return (
<div className="flex gap-2 mt-2">
<Button
icon={<SatoshiV2Icon className="w-4 h-4" />}
label="100 ⚡"
onClick={() => onClick("100")}
fullWidth
disabled={disabled}
/>
<Button
icon={<SatoshiV2Icon className="w-4 h-4" />}
label="1K ⚡"
onClick={() => onClick("1000")}
fullWidth
disabled={disabled}
/>
<Button
icon={<SatoshiV2Icon className="w-4 h-4" />}
label="5K ⚡"
onClick={() => onClick("5000")}
fullWidth
disabled={disabled}
/>
<Button
icon={<SatoshiV2Icon className="w-4 h-4" />}
label="10K ⚡"
onClick={() => onClick("10000")}
fullWidth
disabled={disabled}
/>
{amounts.map((amount, index) => (
<Button
key={index}
icon={<SatoshiV2Icon className="w-4 h-4" />}
label={`${stringifyThousands(amount)} ⚡`}
onClick={() => onClick(`${amount}`)}
fullWidth
disabled={disabled}
/>
))}
</div>
);
}
Expand Down
11 changes: 9 additions & 2 deletions src/app/screens/LNURLPay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SatButtons from "@components/SatButtons";
import DualCurrencyField from "@components/form/DualCurrencyField";
import TextField from "@components/form/TextField";
import axios from "axios";
import React, { Fragment, useState, useEffect } from "react";
import React, { Fragment, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { toast } from "react-toastify";
Expand Down Expand Up @@ -392,6 +392,8 @@ function LNURLPay() {
<SatButtons
onClick={setValueSat}
disabled={loadingConfirm}
min={+details.minSendable / 1000}
max={+details.maxSendable / 1000}
/>
</div>
)}
Expand Down Expand Up @@ -441,7 +443,12 @@ function LNURLPay() {
isFocused={false}
label={tCommon("actions.confirm")}
loading={loadingConfirm}
disabled={loadingConfirm || !valueSat}
disabled={
loadingConfirm ||
!valueSat ||
+valueSat < +details.minSendable / 1000 ||
+valueSat > +details.maxSendable / 1000
}
onCancel={reject}
/>
</div>
Expand Down