hooks#useBlocks TypeScript Examples

The following examples show how to use hooks#useBlocks. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlocksList.tsx    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
BlockList = () => {
  const blocks = useBlocks();

  const showMoreClick = () => {
    const list = document.querySelector('.programs-list--short-list');
    list?.classList.remove('programs-list--short-list');
    const showMoreBtn = document.querySelector('.block-list__button');
    if (showMoreBtn !== null) {
      showMoreBtn.classList.add('block-list__button--hidden');
    }
  };

  return (
    <div className="block-list">
      <h3 className="block-list__header">Recent blocks: {blocks.length && blocks[0].number}</h3>
      {(blocks && blocks.length && (
        <>
          <ul className="programs-list programs-list--short-list">
            {blocks &&
              blocks.length &&
              blocks.map((block) => (
                <li className="programs-list__item" key={block.number}>
                  <span className="programs-list__number">{block.number}</span>
                  <span className="programs-list__name">{block.hash}</span>
                  <span className="programs-list__time">{block.time}</span>
                </li>
              ))}
          </ul>
          <button className="block-list__button" type="button" onClick={showMoreClick}>
            Show more
          </button>
        </>
      )) || <div className="no-message">There are no blocks</div>}
    </div>
  );
}
Example #2
Source File: BlocksSummary.tsx    From gear-js with GNU General Public License v3.0 5 votes vote down vote up
BlocksSummary = () => {
  const { api } = useApi();
  const blocks = useBlocks();

  const [timeInstance, setTimeInstance] = useState(0);
  const [totalIssuance, setTotalIssuance] = useState('');
  const [prevBlockHash, setPrevBlockHash] = useState('');

  const seconds = timeInstance.toFixed(1).slice(0, 1);
  const milliseconds = timeInstance.toFixed(1).slice(-1);
  const time = `${seconds}.${milliseconds}`;

  useEffect(() => {
    const intervalId = setInterval(() => {
      const decreasedTime = timeInstance + 0.1;
      setTimeInstance(decreasedTime);
    }, 100);

    if (blocks && blocks.length) {
      if (blocks[0].hash !== prevBlockHash) {
        setTimeInstance(0);
      }
      setPrevBlockHash(blocks[0].hash);
    }

    return () => {
      clearInterval(intervalId);
    };
  }, [setTimeInstance, timeInstance, setPrevBlockHash, prevBlockHash, blocks]);

  useEffect(() => {
    const getTotal = async () => {
      if (api) {
        const totalBalance = await api.totalIssuance();
        setTotalIssuance(totalBalance);
      }
    };
    getTotal();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [totalIssuance]);

  return (
    <div className={styles.summary}>
      <div className={styles.section}>
        <p>Last block</p>
        <p className={styles.data}>
          <span className={styles.number}>{time}</span> s
        </p>
      </div>
      <div className={styles.section}>
        <p>Total issuance</p>
        <p className={styles.data}>
          <span className={styles.number}>{totalIssuance.slice(0, 5)}</span> Munit
        </p>
      </div>
    </div>
  );
}