RoundInfoPane.jsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import React from "react";
  2. import ClickToCopy from "../../util/ClickToCopy";
  3. import Button from "../../util/Button";
  4. import usePlayerScores from "../../../hooks/usePlayerScores";
  5. const useTopScorer = (gameId, roundNum) => {
  6. const scores = usePlayerScores(gameId);
  7. const top = { topPlayer: null, topScore: null };
  8. if (scores === null) {
  9. return top;
  10. }
  11. scores
  12. .filter(({ guesses }) => guesses[roundNum] && guesses[roundNum].score)
  13. .forEach(({ name, guesses }) => {
  14. const score = guesses[roundNum].score;
  15. if (top.topScore === null || score > top.topScore) {
  16. top.topPlayer = name;
  17. top.topScore = score;
  18. }
  19. });
  20. return top;
  21. }
  22. export default ({ gameId, selectedPoint, targetPoint, roundNum, score, totalScore, onNext }) => {
  23. const { topPlayer, topScore } = useTopScorer(gameId, roundNum);
  24. return (
  25. <div className="round-summary__info-pane">
  26. {selectedPoint
  27. ? <span className="round-summary__guess">
  28. You selected: <ClickToCopy text={`${selectedPoint.lat}, ${selectedPoint.lng}`}/>
  29. </span>
  30. : <span className="round-summary__guess round-summary__guess--timed-out">
  31. Timed out!
  32. </span>
  33. }
  34. <span className="round-summary__target-point">Target: <ClickToCopy text={`${targetPoint.lat}, ${targetPoint.lng}`}/></span>
  35. <span className="round-summary__round-score">Score For Round {roundNum}: {score}</span>
  36. <span className="round-summary__total-score">Running Total Score: {totalScore}</span>
  37. {topPlayer ? <span className="round-summary__top-score">Current best is {topPlayer} with {topScore}</span> : null}
  38. <Button className="round-summary__button" onClick={onNext}>{roundNum === "5" ? "View Summary" : "Next Round"}</Button>
  39. </div>
  40. );
  41. }