-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hyperstructure contributions summary (#1592)
* change fucntion name * added contribution summary
- Loading branch information
1 parent
0beffff
commit 98b2fca
Showing
11 changed files
with
183 additions
and
72 deletions.
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
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
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
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
90 changes: 90 additions & 0 deletions
90
client/src/ui/components/hyperstructures/ContributionSummary.tsx
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useContributions } from "@/hooks/helpers/useContributions"; | ||
import { useRealm } from "@/hooks/helpers/useRealm"; | ||
import { ResourceIcon } from "@/ui/elements/ResourceIcon"; | ||
import { currencyIntlFormat, divideByPrecision } from "@/ui/utils/utils"; | ||
import { ContractAddress, ID, ResourcesIds } from "@bibliothecadao/eternum"; | ||
import { useState } from "react"; | ||
|
||
export const ContributionSummary = ({ | ||
hyperstructureEntityId, | ||
className, | ||
}: { | ||
hyperstructureEntityId: ID; | ||
className?: string; | ||
}) => { | ||
const { getContributions, getContributionsTotalPercentage } = useContributions(); | ||
const { getAddressName } = useRealm(); | ||
|
||
type Resource = { | ||
amount: number; | ||
resourceId: number; | ||
}; | ||
|
||
const contributions = getContributions(hyperstructureEntityId); | ||
const groupedContributions = contributions.reduce<Record<string, Record<number, bigint>>>((acc, contribution) => { | ||
const { player_address, resource_type, amount } = contribution; | ||
const playerAddressString = player_address.toString(); | ||
if (!acc[playerAddressString]) { | ||
acc[playerAddressString] = {}; | ||
} | ||
if (!acc[playerAddressString][resource_type]) { | ||
acc[playerAddressString][resource_type] = 0n; | ||
} | ||
acc[playerAddressString][resource_type] += amount; | ||
return acc; | ||
}, {}); | ||
|
||
const resourceContributions: Record<string, Resource[]> = Object.entries(groupedContributions).reduce( | ||
(acc, [playerAddress, resources]) => { | ||
acc[playerAddress] = Object.entries(resources).map(([resourceType, amount]) => ({ | ||
amount: Number(amount), | ||
resourceId: Number(resourceType), | ||
})); | ||
return acc; | ||
}, | ||
{} as Record<string, Resource[]>, | ||
); | ||
|
||
const [showContributions, setShowContributions] = useState(false); | ||
|
||
// Calculate percentages and sort contributors | ||
const sortedContributors = Object.entries(groupedContributions) | ||
.map(([playerAddress, resources]) => ({ | ||
playerAddress, | ||
resources, | ||
percentage: getContributionsTotalPercentage(resourceContributions[playerAddress]) * 100, | ||
})) | ||
.sort((a, b) => b.percentage - a.percentage); | ||
|
||
return ( | ||
<div className={`space-y-2 ${className || ""}`}> | ||
<div | ||
className="flex items-center cursor-pointer hover:text-white" | ||
onClick={() => setShowContributions(!showContributions)} | ||
> | ||
<span className="mr-2">Contributors</span> | ||
<span className={`transform transition-transform ${showContributions ? "rotate-90" : ""}`}>➤</span> | ||
</div> | ||
{showContributions && ( | ||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-2"> | ||
{sortedContributors.map(({ playerAddress, resources, percentage }) => ( | ||
<div key={playerAddress} className="bg-gold/10 p-1 rounded"> | ||
<div className="flex flex-row mb-1 justify-between mr-1 items-end"> | ||
<div className="text-sm font-bold">{getAddressName(ContractAddress(playerAddress))}</div> | ||
<div className="text-xs">{percentage.toFixed(2)}%</div> | ||
</div> | ||
<div className="flex flex-wrap gap-2"> | ||
{Object.entries(resources).map(([resourceType, amount]) => ( | ||
<div key={resourceType} className="flex items-center"> | ||
<ResourceIcon size="xs" resource={ResourcesIds[Number(resourceType)]} /> | ||
<span className="ml-1 text-xs">{currencyIntlFormat(divideByPrecision(Number(amount)))}</span> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</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
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
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
Oops, something went wrong.