ChainOverviewCard.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { Typography } from "@mui/material";
  2. import React, { useEffect, useState } from "react";
  3. import { chainIDStrings } from "../../utils/consts";
  4. import { amountFormatter } from "../../utils/explorer";
  5. import {
  6. NotionalTransferred,
  7. NotionalTransferredToCumulative,
  8. Totals,
  9. } from "./ExplorerStats";
  10. interface ChainOverviewCardProps {
  11. dataKey: keyof typeof chainIDStrings;
  12. totals?: Totals;
  13. notionalTransferred?: NotionalTransferred;
  14. notionalTransferredToCumulative?: NotionalTransferredToCumulative;
  15. }
  16. const ChainOverviewCard: React.FC<ChainOverviewCardProps> = ({
  17. dataKey,
  18. totals,
  19. notionalTransferred,
  20. notionalTransferredToCumulative,
  21. }) => {
  22. const [totalCount, setTotalColunt] = useState<number>();
  23. const [animate, setAnimate] = useState<boolean>(false);
  24. useEffect(() => {
  25. // hold values from props in state, so that we can detect changes and add animation class
  26. setTotalColunt(totals?.TotalCount[dataKey]);
  27. let timeout: NodeJS.Timeout;
  28. if (
  29. totals?.LastDayCount[dataKey] &&
  30. totalCount !== totals?.LastDayCount[dataKey]
  31. ) {
  32. setAnimate(true);
  33. timeout = setTimeout(() => {
  34. setAnimate(false);
  35. }, 2000);
  36. }
  37. return function cleanup() {
  38. if (timeout) {
  39. clearTimeout(timeout);
  40. }
  41. };
  42. }, [
  43. totals?.TotalCount[dataKey],
  44. totals?.LastDayCount[dataKey],
  45. dataKey,
  46. totalCount,
  47. ]);
  48. const centerStyles: any = {
  49. display: "flex",
  50. justifyContent: "flex-start",
  51. alignItems: "center",
  52. flexDirection: "column",
  53. };
  54. return (
  55. <>
  56. <div style={{ ...centerStyles, gap: 8 }}>
  57. {notionalTransferredToCumulative &&
  58. notionalTransferredToCumulative.AllTime && (
  59. <div style={centerStyles}>
  60. <div>
  61. <Typography
  62. variant="h5"
  63. className={animate ? "highlight-new-val" : ""}
  64. >
  65. $
  66. {amountFormatter(
  67. notionalTransferredToCumulative.AllTime[dataKey]["*"]
  68. )}
  69. </Typography>
  70. </div>
  71. <div style={{ marginTop: -10 }}>
  72. <Typography variant="caption">received</Typography>
  73. </div>
  74. </div>
  75. )}
  76. {notionalTransferred &&
  77. notionalTransferred.WithinPeriod &&
  78. dataKey in notionalTransferred.WithinPeriod &&
  79. "*" in notionalTransferred.WithinPeriod[dataKey] &&
  80. "*" in notionalTransferred.WithinPeriod[dataKey]["*"] &&
  81. notionalTransferred.WithinPeriod[dataKey]["*"]["*"] > 0 ? (
  82. <div style={centerStyles}>
  83. <div>
  84. <Typography
  85. variant="h5"
  86. className={animate ? "highlight-new-val" : ""}
  87. >
  88. {notionalTransferred.WithinPeriod[dataKey]["*"]["*"]
  89. ? "$" +
  90. amountFormatter(
  91. notionalTransferred.WithinPeriod[dataKey]["*"]["*"]
  92. )
  93. : "..."}
  94. </Typography>
  95. </div>
  96. <div style={{ marginTop: -10 }}>
  97. <Typography variant="caption">sent</Typography>
  98. </div>
  99. </div>
  100. ) : (
  101. <div style={centerStyles}>
  102. <div style={{ marginTop: -10 }}>
  103. <Typography variant="body1">
  104. amount sent
  105. <br />
  106. coming soon
  107. </Typography>
  108. </div>
  109. </div>
  110. )}
  111. {!!totalCount && (
  112. <div style={centerStyles}>
  113. <div>
  114. <Typography
  115. variant="h5"
  116. className={animate ? "highlight-new-val" : ""}
  117. >
  118. {amountFormatter(totalCount)}
  119. </Typography>
  120. </div>
  121. <div style={{ marginTop: -10 }}>
  122. <Typography variant="caption"> messages </Typography>
  123. </div>
  124. </div>
  125. )}
  126. </div>
  127. {totalCount === 0 && <Typography variant="h6">coming soon</Typography>}
  128. </>
  129. );
  130. };
  131. export default ChainOverviewCard;