Merge branch 'fix-zap-split-identical-id' into 'main'
Some instances have identical IDs Closes #1742 See merge request soapbox-pub/soapbox!3122
This commit is contained in:
commit
d4ef0129f8
|
@ -1,7 +1,7 @@
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
|
|
||||||
import { useApi } from 'soapbox/hooks';
|
import { useApi } from 'soapbox/hooks';
|
||||||
import { type ZapSplitData } from 'soapbox/schemas/zap-split';
|
import { baseZapAccountSchema, type ZapSplitData } from 'soapbox/schemas/zap-split';
|
||||||
|
|
||||||
import type { Account as AccountEntity, Status as StatusEntity } from 'soapbox/types/entities';
|
import type { Account as AccountEntity, Status as StatusEntity } from 'soapbox/types/entities';
|
||||||
|
|
||||||
|
@ -37,7 +37,10 @@ const useZapSplit = (status: StatusEntity | undefined, account: AccountEntity) =
|
||||||
const loadZapSplitData = async () => {
|
const loadZapSplitData = async () => {
|
||||||
if (status) {
|
if (status) {
|
||||||
const data = (await fetchZapSplit(status.id)).data;
|
const data = (await fetchZapSplit(status.id)).data;
|
||||||
setZapArrays(data);
|
if (data) {
|
||||||
|
const normalizedData = data.map((dataSplit: ZapSplitData) => baseZapAccountSchema.parse(dataSplit));
|
||||||
|
setZapArrays(normalizedData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -50,13 +53,18 @@ const useZapSplit = (status: StatusEntity | undefined, account: AccountEntity) =
|
||||||
const receiveAmount = (zapAmount: number) => {
|
const receiveAmount = (zapAmount: number) => {
|
||||||
if (zapArrays.length > 0) {
|
if (zapArrays.length > 0) {
|
||||||
const zapAmountPrincipal = zapArrays.find((zapSplit: ZapSplitData) => zapSplit.account.id === account.id);
|
const zapAmountPrincipal = zapArrays.find((zapSplit: ZapSplitData) => zapSplit.account.id === account.id);
|
||||||
|
const formattedZapAmountPrincipal = {
|
||||||
|
account: zapAmountPrincipal?.account,
|
||||||
|
message: zapAmountPrincipal?.message,
|
||||||
|
weight: zapArrays.filter((zapSplit: ZapSplitData) => zapSplit.account.id === account.id).reduce((acc:number, zapData: ZapSplitData) => acc + zapData.weight, 0),
|
||||||
|
};
|
||||||
const zapAmountOthers = zapArrays.filter((zapSplit: ZapSplitData) => zapSplit.account.id !== account.id);
|
const zapAmountOthers = zapArrays.filter((zapSplit: ZapSplitData) => zapSplit.account.id !== account.id);
|
||||||
|
|
||||||
const totalWeightSplit = zapAmountOthers.reduce((e: number, b: ZapSplitData) => e + b.weight, 0);
|
const totalWeightSplit = zapAmountOthers.reduce((e: number, b: ZapSplitData) => e + b.weight, 0);
|
||||||
const totalWeight = zapArrays.reduce((e: number, b: ZapSplitData) => e + b.weight, 0);
|
const totalWeight = zapArrays.reduce((e: number, b: ZapSplitData) => e + b.weight, 0);
|
||||||
|
|
||||||
if (zapAmountPrincipal) {
|
if (zapAmountPrincipal) {
|
||||||
const receiveZapAmount = Math.floor(zapAmountPrincipal.weight * (zapAmount / totalWeight));
|
const receiveZapAmount = Math.floor(formattedZapAmountPrincipal.weight * (zapAmount / totalWeight));
|
||||||
const splitResult = zapAmount - receiveZapAmount;
|
const splitResult = zapAmount - receiveZapAmount;
|
||||||
|
|
||||||
let totalRoundedSplit = 0;
|
let totalRoundedSplit = 0;
|
||||||
|
|
Loading…
Reference in New Issue