aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/UnlimitedPage.tsx
blob: 761d2b92cc9cb302a85fb5154654fd6608ee9097 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React from "react";

import { Song } from "../types/song";
import { getSelectSolution } from "../helpers/fetchSolution";
import { useGameState } from "../hooks/useGameState";

import { Header, InfoPopUp, Game, Footer } from "../components";

import * as Styled from "../app.styled";

export function UnlimitedPage() {
  const [solution, setSolution] = React.useState<Song | null>(null);

  const firstRun = localStorage.getItem("firstRun") === null;

  function fetchNewSong() {
    setSolution(null);
    getSelectSolution().then((s) => setSolution(s));
  }

  React.useEffect(() => {
    fetchNewSong();
  }, []);

  const {
    guesses,
    currentTry,
    setSelectedSong,
    didGuess,
    skip,
    guess,
    reset,
  } = useGameState({ solution, persist: false });

  const playAgain = React.useCallback(() => {
    reset();
    fetchNewSong();
  }, [reset]);

  const [isInfoPopUpOpen, setIsInfoPopUpOpen] =
    React.useState<boolean>(firstRun);

  const openInfoPopUp = React.useCallback(() => {
    setIsInfoPopUpOpen(true);
  }, []);

  const closeInfoPopUp = React.useCallback(() => {
    if (firstRun) {
      localStorage.setItem("firstRun", "false");
    }
    setIsInfoPopUpOpen(false);
  }, [localStorage.getItem("firstRun")]);

  if (solution === null) {
    return null;
  }

  return (
    <main>
      <Header openInfoPopUp={openInfoPopUp} />
      {isInfoPopUpOpen && <InfoPopUp onClose={closeInfoPopUp} />}
      <Styled.Container>
        <Game
          guesses={guesses}
          didGuess={didGuess}
          todaysSolution={solution}
          currentTry={currentTry}
          setSelectedSong={setSelectedSong}
          skip={skip}
          guess={guess}
          mode="unlimited"
          onPlayAgain={playAgain}
        />
      </Styled.Container>
      <Footer />
    </main>
  );
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage