aboutsummaryrefslogtreecommitdiff
path: root/components/secret.js
blob: 782fcf508945faf72c3b23b98474cd2f98b868a5 (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
import { useEffect, useState } from "react";

export default function SecretPage({ cheatCode, onCheatCodeEntered }) {
  const [typedCode, setTypedCode] = useState("");
  const [timer, setTimer] = useState(null);

  const handleKeyPress = (e) => {
    const newTypedCode = typedCode + e.key;

    if (newTypedCode === cheatCode) {
      onCheatCodeEntered();
      setTypedCode("");
    } else {
      setTypedCode(newTypedCode);

      // Reset the timer if the user stops typing for 2 seconds
      clearTimeout(timer);
      const newTimer = setTimeout(() => {
        setTypedCode("");
      }, 2000);
      setTimer(newTimer);
    }
  };

  useEffect(() => {
    window.addEventListener("keydown", handleKeyPress);

    return () => {
      window.removeEventListener("keydown", handleKeyPress);
    };

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [typedCode]);

  return;
}