RoundSummary.jsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import React, { useRef } from "react";
  2. import styled from "styled-components";
  3. import { useLastRound } from "../../../domain/gameStore";
  4. import useMarkersFromGuesses from "../../../hooks/useMarkersFromGuesses";
  5. import useMap from "../../../hooks/useMap";
  6. import NextRoundButton from "./NextRoundButton";
  7. import useClickToCheckScore from "./useClickToCheckScore";
  8. import { usePlayers } from "../../../hooks/useGameInfo";
  9. import usePreventNavigation from "../../../hooks/usePreventNavigation";
  10. const SummaryDiv = styled.div`
  11. position: absolute;
  12. display: flex;
  13. flex-flow: column nowrap;
  14. background-color: #333;
  15. z-index: 1;
  16. bottom: 16px;
  17. right: 8px;
  18. padding: 8px;
  19. `
  20. const ScoreSpan = styled.span`
  21. padding-bottom: 2px;
  22. `;
  23. const MapDiv = styled.div`
  24. position: absolute;
  25. height: 100%;
  26. width: 100%;
  27. `
  28. export default () => {
  29. // get the info about the last round
  30. const { roundNum, score, totalScore, targetPoint } = useLastRound();
  31. // create the map
  32. const mapDivRef = useRef(null);
  33. const mapRef = useMap(mapDivRef, targetPoint.lat, targetPoint.lng, 4);
  34. // live update with all the scores
  35. const players = usePlayers();
  36. // add the player guess markers
  37. useMarkersFromGuesses(mapRef, players, roundNum, targetPoint);
  38. // let the current player click the map to see a possible score
  39. useClickToCheckScore(mapRef, targetPoint);
  40. // warn the user if they navigate away
  41. usePreventNavigation();
  42. return (
  43. <div>
  44. <MapDiv ref={mapDivRef} />
  45. <SummaryDiv>
  46. <ScoreSpan>Score for Round {roundNum}: {score}</ScoreSpan>
  47. <ScoreSpan>Running Total: {totalScore}</ScoreSpan>
  48. <NextRoundButton/>
  49. </SummaryDiv>
  50. </div>
  51. );
  52. };