SummaryMap.jsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React, { useEffect, useRef, useState } from 'react';
  2. import useMap from '../../../../hooks/useMap';
  3. import useMarkersFromGuesses from '../../../../hooks/useMarkersFromGuesses';
  4. import styles from './SummaryMap.module.css';
  5. const RoundSelect = ({ rounds, selected, onSelect }) => (
  6. <div className={styles.tabs}>
  7. {// fun fact: es6 doesn't have a range(x) function
  8. Array.from(Array(rounds).keys()).map((r) => (r + 1).toString()).map((r) => (
  9. <div
  10. className={`${styles.tab} ${r === selected ? styles['tab--active'] : ''}`}
  11. key={r}
  12. onClick={() => onSelect(r)}
  13. >
  14. {r}
  15. </div>
  16. ))}
  17. <div className={styles.tab} onClick={() => onSelect('0')}>
  18. X
  19. </div>
  20. </div>
  21. );
  22. export default ({ players, coords }) => {
  23. const rounds = Object.keys(coords).length;
  24. const singleRound = rounds === 1;
  25. // create the map
  26. const mapDivRef = useRef(null);
  27. const mapRef = useMap(mapDivRef, 0, 0, 1);
  28. // set up round number selection
  29. const [ roundNum, setRoundNum ] = useState('0');
  30. const roundActive = singleRound ? '1' : roundNum;
  31. // get the target point
  32. const targetPoint = coords?.[roundActive] ?? null;
  33. // put the markers on the map
  34. useMarkersFromGuesses(mapRef, players, roundActive, targetPoint);
  35. // scroll the map to the target point
  36. useEffect(
  37. () => {
  38. mapRef.current && mapRef.current.panTo(targetPoint ?? { lat: 0, lng: 0 });
  39. },
  40. [ mapRef, targetPoint ]
  41. );
  42. return (
  43. <div className={styles.container}>
  44. <div className={styles.map} ref={mapDivRef} />
  45. {singleRound || <RoundSelect rounds={rounds} selected={roundNum} onSelect={setRoundNum} />}
  46. </div>
  47. );
  48. };