import { useEffect, useState } from 'react';
import { Battery, Pane, Text } from '@cybercongress/gravity';
import { Link } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { useQueryClient } from 'src/contexts/queryClient';
import Tooltip from '../tooltip/tooltip';
import { CYBER } from '../../utils/config';
import {
  coinDecimals,
  convertResources,
  formatCurrency,
  reduceBalances,
} from '../../utils/utils';
import { setBandwidth } from '../../redux/actions/bandwidth';
import { useSigningClient } from 'src/contexts/signerClient';
import { Networks } from 'src/types/networks';
import { routes } from 'src/routes';

const PREFIXES = [
  {
    prefix: 't',
    power: 10 ** 12,
  },
  {
    prefix: 'g',
    power: 10 ** 9,
  },
  {
    prefix: 'm',
    power: 10 ** 6,
  },
  {
    prefix: 'k',
    power: 10 ** 3,
  },
];

function ContentTooltip({ bwRemained, bwMaxValue, amounPower, countLink }) {
  let text =
    'Empty battery. You have no power & energy so you cannot submit cyberlinks. ';

  if (bwMaxValue > 0) {
    text = `You have ${formatCurrency(
      amounPower,
      'W',
      2,
      PREFIXES
    )} and can immediately submit ${Math.floor(countLink)} cyberlinks. `;
  }

  return (
    <Pane zIndex={4} paddingX={10} paddingY={10} maxWidth={200}>
      <Pane marginBottom={12}>
        <Text color="#fff" size={400}>
          {text}
          <Link
            to={
              CYBER.CHAIN_ID === Networks.BOSTROM
                ? routes.search.getLink('get BOOT')
                : routes.teleport.path
            }
          >
            Get {CYBER.DENOM_CYBER.toUpperCase()}
          </Link>
        </Text>
      </Pane>
    </Pane>
  );
}

function BandwidthBar({ tooltipPlacement }) // bwRemained = 0,
// bwMaxValue = 0,
// countLink = 0,
// amounPower,
// ...props

{
  const [linkPrice] = useState(4);

  const queryClient = useQueryClient();

  const [countLink, setCountLink] = useState(0);
  const [priceLink, setPriceLink] = useState(0.25);

  const [amounPower, setAmounPower] = useState(0);

  const bandwidth = useSelector((state) => state.bandwidth.bandwidth);
  const { defaultAccount } = useSelector((state) => state.pocket);

  const dispatch = useDispatch();

  const bwRemained = bandwidth.remained;
  const bwMaxValue = bandwidth.maxValue;

  const bwPercent =
    bwMaxValue > 0 ? Math.floor((bwRemained / bwMaxValue) * 100) : 0;

  useEffect(() => {
    const getBandwidth = async () => {
      try {
        const { account } = defaultAccount;
        if (
          account !== null &&
          Object.prototype.hasOwnProperty.call(account, 'cyber') &&
          queryClient
        ) {
          const { bech32: cyberBech32 } = account.cyber;
          const responseAccountBandwidth = await queryClient.accountBandwidth(
            cyberBech32
          );

          if (
            responseAccountBandwidth !== null &&
            responseAccountBandwidth.neuronBandwidth
          ) {
            const { maxValue, remainedValue } =
              responseAccountBandwidth.neuronBandwidth;
            dispatch(setBandwidth(remainedValue, maxValue));
            setCountLink(remainedValue / (priceLink * 1000));
          } else {
            dispatch(setBandwidth(0, 0));
            setCountLink(0);
          }
        } else {
          dispatch(setBandwidth(0, 0));
          setCountLink(0);
        }
      } catch (error) {
        dispatch(setBandwidth(0, 0));
        setCountLink(0);
      }
    };
    getBandwidth();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [defaultAccount, location.pathname, priceLink, queryClient]);

  useEffect(() => {
    const getPrice = async () => {
      if (queryClient) {
        const response = await queryClient.price();
        setPriceLink(coinDecimals(response.price.dec));
      }
    };
    getPrice();
  }, [queryClient]);

  useEffect(() => {
    const getAmounPower = async () => {
      try {
        const { account } = defaultAccount;
        if (
          account !== null &&
          Object.prototype.hasOwnProperty.call(account, 'cyber') &&
          queryClient
        ) {
          const { bech32 } = account.cyber;
          const allBalances = await queryClient.getAllBalances(bech32);
          const reduceallBalances = reduceBalances(allBalances);
          if (reduceallBalances.milliampere && reduceallBalances.millivolt) {
            const { milliampere, millivolt } = reduceallBalances;
            setAmounPower(
              convertResources(milliampere) * convertResources(millivolt)
            );
          }
        } else {
          setAmounPower(0);
        }
      } catch (error) {
        setAmounPower(0);
      }
    };
    getAmounPower();
  }, [queryClient, defaultAccount]);

  return (
    <Tooltip
      placement={tooltipPlacement || 'bottom'}
      // trigger="click"
      tooltip={
        <ContentTooltip
          bwRemained={bwRemained}
          bwMaxValue={bwMaxValue}
          linkPrice={linkPrice}
          countLink={countLink}
          amounPower={amounPower}
        />
      }
    >
      <Battery
        // {...props}
        height="10px"
        // // styleText={{ display: 'none' }}
        fontSize={12}
        maxWidth={75}
        colorText="#000"
        bwPercent={bwPercent}
        bwRemained={bwRemained}
        bwMaxValue={bwMaxValue}
        linkPrice={linkPrice}
      />
    </Tooltip>
  );
}

export default BandwidthBar;

Synonyms

cyb/src/index.tsx
pussy-ts/src/index.tsx
bostrom.network/src/pages/Index.tsx
pussy-landing/src/pages/index.tsx
pussy.meme/src/pages/index.tsx
pussy-ts/src/containers/txs/index.tsx
pussy-ts/src/containers/mint/index.tsx
cyb/src/containers/mint/index.tsx
pussy-ts/src/components/ButtonSwap/index.tsx
pussy-ts/src/components/denom/index.tsx
cyb/src/containers/Objects/index.tsx
cyb/src/components/btnGrd/index.tsx
pussy-ts/src/components/Select/index.tsx
cyb/src/components/actionBar/index.tsx
cyb/src/components/Select/index.tsx
pussy-ts/src/containers/portal/index.tsx
pussy-ts/src/components/btnGrd/index.tsx
cyb/src/components/TextMarkdown/index.tsx
cyb/src/containers/energy/index.tsx
pussy-ts/src/components/MainContainer/index.tsx
pussy-ts/src/components/actionBar/index.tsx
cyb/src/containers/portal/index.tsx
cyb/src/components/ButtonSwap/index.tsx
cyb/src/containers/nebula/index.tsx
pussy-ts/src/components/DonutChart/index.tsx
cyb/src/components/DonutChart/index.tsx
pussy-ts/src/containers/nebula/index.tsx
cyb/src/components/BandwidthBar/index.tsx
pussy-ts/src/containers/energy/index.tsx
cyb/src/components/denom/index.tsx
cyb/src/containers/sigma/index.tsx
cyb/src/components/Input/index.tsx
pussy-ts/src/containers/sigma/index.tsx
pussy-ts/src/containers/taverna/index.tsx
pussy-ts/src/components/Input/index.tsx
cyb/src/containers/blok/index.tsx
cyb/src/components/PDF/index.tsx
cyb/src/containers/txs/index.tsx
cyb/src/containers/taverna/index.tsx
cyb/src/components/MainContainer/index.tsx
pussy-ts/src/components/TextMarkdown/index.tsx
pussy-ts/src/components/PDF/index.tsx
cyb/src/containers/portal/citizenship/index.tsx
pussy-landing/src/components/xp/btnGrd/index.tsx
cyb/src/containers/portal/pasport/index.tsx
pussy-landing/src/components/xp/stars/index.tsx
cyb/src/components/buttons/ButtonIcon/index.tsx
cyb/src/containers/wasm/codes/index.tsx
pussy-ts/src/features/ipfs/Drive/index.tsx
pussy-ts/src/containers/portal/gift/index.tsx
pussy-ts/src/features/ipfs/ipfsSettings/index.tsx
pussy-ts/src/components/buttons/ButtonIcon/index.tsx
pussy-ts/src/containers/portal/release/index.tsx
cyb/src/containers/portal/release/index.tsx
cyb/src/features/ipfs/Drive/index.tsx
pussy-ts/src/containers/portal/pasport/index.tsx
cyb/src/features/ipfs/ipfsSettings/index.tsx
pussy-ts/src/containers/portal/citizenship/index.tsx
cyb/src/containers/portal/gift/index.tsx
cyb/src/components/contentIpfs/component/gateway/index.tsx
pussy-ts/src/containers/portal/components/ReleaseStatus/index.tsx
pussy-ts/src/components/contentIpfs/component/gateway/index.tsx
pussy-ts/src/containers/sigma/components/CardPassport/index.tsx
cyb/src/containers/sigma/components/CardPassport/index.tsx
cyb/src/components/contentIpfs/component/img/index.tsx
pussy-ts/src/components/contentIpfs/component/img/index.tsx
cyb/src/containers/portal/components/ActionBar/index.tsx
pussy-ts/src/components/contentIpfs/component/link/index.tsx
cyb/src/containers/portal/components/ReleaseStatus/index.tsx
cyb/src/containers/portal/components/stars/index.tsx
pussy-ts/src/containers/portal/components/stars/index.tsx
cyb/src/components/contentIpfs/component/link/index.tsx
pussy-ts/src/containers/sigma/components/cardUi/TitleCard/index.tsx
cyb/src/containers/sigma/components/cardUi/TitleCard/index.tsx
cyb/src/containers/sigma/components/cardUi/RowBalancesDetails/index.tsx
pussy-ts/src/containers/sigma/components/cardUi/RowBalancesDetails/index.tsx

Neighbours