SummarySession.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2017-2020 @polkadot/app-explorer authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { DeriveSessionProgress } from '@polkadot/api-derive/types';
  5. import { Forcing } from '@polkadot/types/interfaces';
  6. import React, { useMemo } from 'react';
  7. import { CardSummary } from '@polkadot/react-components';
  8. import { useApi, useCall } from '@polkadot/react-hooks';
  9. import { formatNumber } from '@polkadot/util';
  10. import { useTranslation } from './translate';
  11. interface Props {
  12. withEra?: boolean;
  13. withSession?: boolean;
  14. }
  15. function SummarySession ({ withEra = true, withSession = true }: Props): React.ReactElement<Props> {
  16. const { t } = useTranslation();
  17. const { api } = useApi();
  18. const sessionInfo = useCall<DeriveSessionProgress>(api.derive.session?.progress, []);
  19. const forcing = useCall<Forcing>(api.query.staking?.forceEra, []);
  20. const eraLabel = useMemo(() =>
  21. t<string>('era')
  22. , [t]);
  23. const sessionLabel = useMemo(() =>
  24. sessionInfo?.isEpoch
  25. ? t<string>('epoch')
  26. : t<string>('session')
  27. , [sessionInfo?.isEpoch, t]);
  28. return (
  29. <>
  30. {sessionInfo && (
  31. <>
  32. {withSession && (
  33. sessionInfo.sessionLength.gtn(1)
  34. ? (
  35. <CardSummary
  36. label={sessionLabel}
  37. progress={{
  38. total: sessionInfo.sessionLength,
  39. value: sessionInfo.sessionProgress,
  40. withTime: true
  41. }}
  42. />
  43. )
  44. : (
  45. <CardSummary label={sessionLabel}>
  46. #{formatNumber(sessionInfo.currentIndex)}
  47. </CardSummary>
  48. )
  49. )}
  50. {forcing && !forcing.isForceNone && withEra && (
  51. sessionInfo.sessionLength.gtn(1)
  52. ? (
  53. <CardSummary
  54. label={eraLabel}
  55. progress={{
  56. total: forcing.isForceAlways ? sessionInfo.sessionLength : sessionInfo.eraLength,
  57. value: forcing.isForceAlways ? sessionInfo.sessionProgress : sessionInfo.eraProgress,
  58. withTime: true
  59. }}
  60. />
  61. )
  62. : (
  63. <CardSummary label={eraLabel}>
  64. #{formatNumber(sessionInfo.activeEra)}
  65. </CardSummary>
  66. )
  67. )}
  68. </>
  69. )}
  70. </>
  71. );
  72. }
  73. export default React.memo(SummarySession);