-
Notifications
You must be signed in to change notification settings - Fork 195
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
Closed
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf49652
feat: use minSendable and maxSendable amounts for input suggestions
69481cc
Merge branch 'getAlby:master' into master
fanismichalakis d1daf68
feat: round sub-1k numbers and dedup suggestions when range is tight
9884913
fix: typo in comment
88459bc
Merge branch 'getAlby:master' into master
fanismichalakis b542ecd
Merge remote-tracking branch 'upstream/master'
b61290d
Merge branch 'getAlby:master' into master
fanismichalakis 9efa98d
fix: better rounding and amount suggestion
007ff0f
Merge branch 'getAlby:master' into master
fanismichalakis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
||
function stringifyThousands(number: number) { | ||
return number < 1000 | ||
? `${Math.round(number)}` | ||
: `${Math.floor(number / 1000)}K`; | ||
} | ||
|
||
function amountsArray(min: number, max: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this is leaving afraid and confused :P |
||
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> | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 to1
) which will then fail because it is lower than themin
amount.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.