Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Fixes to web tipping
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Nov 30, 2023
1 parent 904b61b commit 1615547
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions frontend/apps/site/src/web-tipping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function SplitRow({
<Text color="$gray10">{Math.round(percentage * 1000) / 10}%</Text>
</XStack>
<XStack gap="$2">
{dispatchSplit ? (
{dispatchSplit && canReceive ? (
<XGroup opacity={displayIncrementButton ? 1 : 0} size="$2">
<XGroup.Item>
<Button
Expand Down Expand Up @@ -253,6 +253,7 @@ type SplitAction = {
let editorsOverallPercentage = 0.99

function splitReducer(state: InvoiceSplit, action: SplitAction): InvoiceSplit {
const availRecipients = state.filter((s) => s.canReceive)
if (action.type === 'incrementPercentage') {
const prevTarget = state.find((s) => s.id === action.id)
if (!prevTarget) return state
Expand All @@ -270,16 +271,18 @@ function splitReducer(state: InvoiceSplit, action: SplitAction): InvoiceSplit {
!state.filter((s) => s.id !== action.id).find((s) => s.percentage !== 0)
) {
return state.map((s) => {
if (!s.canReceive) return s
if (s.id === action.id) {
return {id: s.id, percentage: nextPercentage}
return {...s, id: s.id, percentage: nextPercentage}
}
const percentage = nextRemainderPercentage / (state.length - 1)
return {id: s.id, percentage}
const percentage =
nextRemainderPercentage / (availRecipients.length - 1)
return {...s, id: s.id, percentage}
})
}
return state.map((s) => {
if (s.id === action.id) {
return {id: s.id, percentage: nextPercentage}
return {...s, id: s.id, percentage: nextPercentage}
}

// these split the remainder based on their ratio from the previous remainder
Expand All @@ -288,7 +291,7 @@ function splitReducer(state: InvoiceSplit, action: SplitAction): InvoiceSplit {
? 0
: s.percentage / prevRemainderPercentage
const percentage = prevRatioExcludingTarget * nextRemainderPercentage
return {id: s.id, percentage}
return {...s, id: s.id, percentage}
})
}
return state
Expand Down Expand Up @@ -327,22 +330,24 @@ function CreateInvoiceStep({
})
.filter((editorId) => !!editorId)
let equalPercentage = validRecipients.length
? editorsOverallPercentage / editorIds.length
? editorsOverallPercentage / validRecipients.length
: editorsOverallPercentage
return editorIds.map((id) => {
const canReceive = !!id && validRecipients.indexOf(id) !== -1
if (!canReceive) return {id, canReceive, percentage: 0}
return {id, canReceive, percentage: equalPercentage}
})
}, [editors, validRecipients])
let [split, dispatchSplit] = useReducer(splitReducer, initialSplit)
console.log({initialSplit, split})
let computed = useMemo(() => {
let remainderSats = amount
let remainderPercent = 1
let computedSplit = split.map((splitRow) => {
let sats = Math.floor(amount * splitRow.percentage)
remainderSats -= sats
remainderPercent -= splitRow.percentage
return {...splitRow, sats}
return {...splitRow, sats, percentage: sats / amount}
})
// TO DO: handle edge case where remainderSats is 0 but the actual service fee should be at least 1 sat
return {
Expand Down

0 comments on commit 1615547

Please sign in to comment.