// Copyright 2017-2020 @polkadot/react-components authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { DeriveDemocracyLock } from '@polkadot/api-derive/types'; import BN from 'bn.js'; import { TFunction } from 'i18next'; import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import { useApi, useCall } from '@polkadot/react-hooks'; import { BlockToTime, FormatBalance } from '@polkadot/react-query'; import { BN_ZERO, bnMax, formatBalance, formatNumber } from '@polkadot/util'; import { useTranslation } from './translate'; import Icon from './Icon'; import Tooltip from './Tooltip'; interface Props { className?: string; value?: DeriveDemocracyLock[]; } interface Entry { details: React.ReactNode; headers: React.ReactNode[]; isCountdown: boolean; isFinished: boolean; } interface State { maxBalance: BN; sorted: Entry[]; } let id = 0; // group by header & details // - all unlockable together // - all ongoing together // - unlocks are displayed individually function groupLocks (t: TFunction, bestNumber: BN, locks: DeriveDemocracyLock[] = []): State { return { maxBalance: bnMax(...locks.map(({ balance }) => balance)), sorted: locks .map((info): [DeriveDemocracyLock, BN] => [info, info.unlockAt.gt(bestNumber) ? info.unlockAt.sub(bestNumber) : BN_ZERO]) .sort((a, b) => a[0].referendumId.cmp(b[0].referendumId)) .sort((a, b) => a[1].cmp(b[1])) .sort((a, b) => a[0].isFinished === b[0].isFinished ? 0 : (a[0].isFinished ? -1 : 1)) .reduce((sorted: Entry[], [{ balance, isDelegated, isFinished, referendumId, vote }, blocks]): Entry[] => { const isCountdown = blocks.gt(BN_ZERO); const header =