import clsx from 'clsx' import uniq from 'lodash.uniq' import { ComponentType, Fragment } from 'react' import { LoadingData, StatefulEntityDisplayProps } from '@dao-dao/types' import { DISTRIBUTION_COLORS_ORDERED, formatPercentOf100 } from '@dao-dao/utils' const NUM_VERTICAL_BARS = 10 export type VotingPowerDistributionEntry = { address?: string label?: string votingPowerPercent: number | LoadingData // Group by section. Undefined means no section, which is first. section?: number // Override default bar color. color?: string } export type VotingPowerDistributionProps = { data: VotingPowerDistributionEntry[] EntityDisplay: ComponentType className?: string } export const VotingPowerDistribution = ({ data, EntityDisplay, className, }: VotingPowerDistributionProps) => { const sections = uniq(data.map(({ section }) => section)).map((section) => data.filter((d) => d.section === section) ) return (
{sections.map((data, index) => ( {data.map(({ address, label, votingPowerPercent, color }, index) => { const percent = typeof votingPowerPercent === 'number' ? votingPowerPercent : votingPowerPercent.loading ? 0 : votingPowerPercent.data return ( {address ? ( ) : ( label && (

{label}

) )}

{typeof votingPowerPercent !== 'number' && votingPowerPercent.loading ? '...' : formatPercentOf100(percent)}

) })} {/* Space row */} {index < sections.length - 1 && ( <>
)}
))}
) } type BarProps = { color: string percent: number } const Bar = ({ color, percent }: BarProps) => (
{/* Bar color */}
{/* Vertical bars */} {[...Array(NUM_VERTICAL_BARS)].map((_, index) => (
))}
)