hooks.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { useEffect, useState, useRef } from "react";
  2. import {
  3. useGameId,
  4. useCurrentRound,
  5. dispatch,
  6. } from "../../../domain/gameStore";
  7. import { getFirstSubmitter } from "../../../domain/apiMethods";
  8. import {
  9. savePanoPositionToLocalStorage,
  10. savePanoPovToLocalStorage,
  11. } from "../../../domain/localStorageMethods";
  12. /* global google */
  13. export const useFirstSubmitter = (rate, cutoffTime) => {
  14. const [first, setFirst] = useState(null);
  15. const gameId = useGameId();
  16. const round = useCurrentRound();
  17. useEffect(() => {
  18. if (first !== null) {
  19. return;
  20. }
  21. const update = async () => {
  22. const firstRes = await getFirstSubmitter(gameId, round);
  23. if (firstRes !== null) {
  24. dispatch.updateRoundSeconds(r => Math.min(r, cutoffTime));
  25. setFirst(firstRes);
  26. }
  27. };
  28. update();
  29. const interval = setInterval(update, rate);
  30. // eslint-disable-next-line consistent-return
  31. return () => clearInterval(interval);
  32. }, [first, gameId, round, rate, cutoffTime]);
  33. return first;
  34. };
  35. export const useIsFaded = first => {
  36. const [faded, setFaded] = useState(false);
  37. // eslint-disable-next-line consistent-return
  38. useEffect(() => {
  39. if (first !== null) {
  40. const timeout = setTimeout(() => setFaded(true), 2000);
  41. return () => clearTimeout(timeout);
  42. }
  43. }, [first]);
  44. return faded;
  45. };
  46. export const useIsFinished = () => {
  47. const finished = useCurrentRound() === null;
  48. useEffect(() => {
  49. if (finished) {
  50. dispatch.goToSummary();
  51. }
  52. }, [finished]);
  53. return finished;
  54. };
  55. export const usePano = (panoDivRef, { lat, lng }, { heading, pitch }) => {
  56. const panoRef = useRef(null);
  57. useEffect(() => {
  58. const position = { lat, lng };
  59. const pov = { heading, pitch };
  60. if (panoRef.current) {
  61. panoRef.current.setPosition(position);
  62. panoRef.current.setPov(pov);
  63. return;
  64. }
  65. panoRef.current = new google.maps.StreetViewPanorama(panoDivRef.current, {
  66. position,
  67. pov,
  68. fullscreenControl: false,
  69. addressControl: false,
  70. showRoadLabels: false,
  71. clickToGo: true,
  72. visible: true,
  73. motionTracking: false,
  74. motionTrackingControl: false,
  75. });
  76. }, [panoDivRef, lat, lng, heading, pitch]);
  77. return panoRef;
  78. };
  79. export const useSavePanoSettings = panoRef => {
  80. useEffect(() => {
  81. if (panoRef.current) {
  82. panoRef.current.addListener("position_changed", () => {
  83. const { lat, lng } = panoRef.current.getPosition();
  84. savePanoPositionToLocalStorage(lat(), lng());
  85. });
  86. panoRef.current.addListener("pov_changed", () => {
  87. const { heading, pitch } = panoRef.current.getPov();
  88. savePanoPovToLocalStorage(heading, pitch);
  89. });
  90. }
  91. }, [panoRef]);
  92. };