ElectionStatus.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React from "react";
  2. import { Status } from "../../types";
  3. const timeLeft = (blocks: number) => {
  4. const seconds = blocks * 6;
  5. const days = Math.floor(seconds / 86400);
  6. const hours = Math.floor((seconds - days * 86400) / 3600);
  7. const minutes = Math.floor(seconds / 60);
  8. return days ? `${days}d` : hours ? `${hours}h` : `${minutes}min`;
  9. };
  10. const ElectionStage = (props: {
  11. block: number;
  12. council: { stage; termEndsAt: number };
  13. domain: string;
  14. }) => {
  15. const { block, council, domain } = props;
  16. if (!council) return <span>Loading..</span>;
  17. const { stage, termEndsAt } = council;
  18. if (!stage) {
  19. if (!block || !termEndsAt) return <span />;
  20. const left = timeLeft(termEndsAt - block);
  21. return <span>election starts in {left}</span>;
  22. }
  23. let stageString = Object.keys(stage)[0];
  24. const left = timeLeft(stage[stageString] - block);
  25. if (stageString === "announcing")
  26. return <a style={{color: "#fff"}} href={`${domain}/#/council/applicants`}>{left} to apply</a>;
  27. if (stageString === "voting")
  28. return <a style={{color: "#fff"}} href={`${domain}/#/council/applicants`}>{left} to vote</a>;
  29. if (stageString === "revealing")
  30. return <a style={{color: "#fff"}} href={`${domain}/#/council/votes`}>{left} to reveal votes</a>;
  31. return <span>{JSON.stringify(stage)}</span>;
  32. };
  33. const ElectionStatus = (props: { block; domain: string; status: Status }) => {
  34. const { domain, status } = props;
  35. if (!status.block || !status.council) return <div />;
  36. return (
  37. <div className="text-center text-white">
  38. <ElectionStage
  39. block={status.block.id}
  40. council={status.council}
  41. domain={domain}
  42. />
  43. </div>
  44. );
  45. };
  46. export default ElectionStatus;