aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Result/index.tsx
blob: 46959a65488ff2a4b4b1a73b22a43d3660ad4889 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import React, { useState } from 'react';

import { Song } from "../../types/song";
import { GuessType } from "../../types/guess";
import { scoreToEmoji } from "../../helpers";
import { appName } from '../../constants';

import { Button } from "../Button";
import { MiniYouTubePlayer } from "../MiniYouTubePlayer";

import * as Styled from "./index.styled";
import { theme } from "../../constants";
import GuessDistributionChart from '../Chart';

interface SolutionProps {
  didGuess: boolean;
  currentTry: number;
  todaysSolution: Song;
  isUnlimited?: boolean;
}

function Solution({
  didGuess,
  todaysSolution,
  currentTry,
  isUnlimited,
}: SolutionProps) {
  return (
    <>
      <Styled.SongTitle>
        {isUnlimited ? "The song was" : "Today's song is"} {todaysSolution.artist} - {todaysSolution.name}
      </Styled.SongTitle>

      {didGuess && (
        <Styled.Tries>
          You guessed it in {currentTry} {currentTry === 1 ? 'try' : 'tries'}.
        </Styled.Tries>
      )}

      <MiniYouTubePlayer id={todaysSolution.youtubeId} />
    </>
  );
}

interface ShareButtonProps {
  guesses: GuessType[];
  variant?: keyof typeof theme;
}

function ShareButton({ guesses, variant }: ShareButtonProps) {
  const [buttonText, setButtonText] = useState('Share Results');
  const [result, setResult] = useState<string>('');

  React.useEffect(() => {
    let cancelled = false;

    scoreToEmoji(guesses).then((text) => {
      if (!cancelled) setResult(text);
    });

    return () => {
      cancelled = true;
    };
  }, [guesses]);


  const handleClick = React.useCallback(async () => {
    const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
    const onWindows =
      windowsPlatforms.indexOf(window.navigator.platform) !== -1;
    const isSecureContext = window.isSecureContext;
    if (navigator.share !== undefined && !onWindows) {
      await navigator.share({ text: result });
    } else if (isSecureContext && navigator.clipboard !== undefined) {
      await navigator.clipboard.writeText(result);
      setButtonText('Copied!');
    } else {
      setButtonText('Clipboard unavailable (requires secure context)');
    }
  }, [result]);

  return (
    <Button onClick={handleClick} variant={variant}>
      {buttonText}
    </Button>
  );
}

interface Props {
  didGuess: boolean;
  currentTry: number;
  todaysSolution: Song;
  guesses: GuessType[];
  mode?: "daily" | "unlimited";
  sessionDate: string;
  onPlayAgain?: () => void;
}

export function Result({
  didGuess,
  todaysSolution,
  guesses,
  currentTry,
  mode = "daily",
  sessionDate,
  onPlayAgain,
}: Props) {
  const [timeLeftStr, setTimeLeftStr] = useState<string>('');
  const now = new Date();
  const nextUtcMidnight = Date.UTC(
    now.getUTCFullYear(),
    now.getUTCMonth(),
    now.getUTCDate() + 1,
    0, 0, 0
  );

  React.useEffect(() => {
    const updateTimeLeft = () => {
      const now = Date.now();
      const nextUtcMidnight = Date.UTC(
        new Date().getUTCFullYear(),
        new Date().getUTCMonth(),
        new Date().getUTCDate() + 1
      );
      const remaining = nextUtcMidnight - now;
      const hours = Math.floor(remaining / 3600000);
      const minutes = Math.floor((remaining % 3600000) / 60000);
      const seconds = Math.floor((remaining % 60000) / 1000);
      setTimeLeftStr(`${hours}h ${minutes}m ${seconds}s`);
    };

    updateTimeLeft();
    const intervalId = setInterval(updateTimeLeft, 1000);

    return () => {
      clearInterval(intervalId);
    };
  }, []);

  const isUnlimited = mode === "unlimited";

  if (didGuess) {
    const textForTry = ["Perfect!", "Wow!", "Super!", "Congrats!", "Nice!"];

    return (
      <>
        <Styled.ResultTitle>{textForTry[currentTry - 1]}</Styled.ResultTitle>

        <Solution
          todaysSolution={todaysSolution}
          didGuess={didGuess}
          currentTry={currentTry}
          isUnlimited={isUnlimited}
        />
        {!isUnlimited && (
          <GuessDistributionChart
            currentTry={currentTry}
            didGuess={didGuess}
            sessionDate={sessionDate}
          />
        )}

        {!isUnlimited && <ShareButton guesses={guesses} variant="green" />}

        {isUnlimited && onPlayAgain ? (
          <Button variant="green" onClick={onPlayAgain}>
            Play Again
          </Button>
        ) : (
          <Styled.TimeToNext>
            The next {appName} will be available in {timeLeftStr}!
          </Styled.TimeToNext>
        )}
      </>
    );
  }

  return (
    <>
      <Styled.ResultTitle>Unfortunately, thats wrong.</Styled.ResultTitle>

      <Solution
        todaysSolution={todaysSolution}
        didGuess={didGuess}
        currentTry={currentTry}
        isUnlimited={isUnlimited}
      />
      {!isUnlimited && (
        <GuessDistributionChart
          currentTry={currentTry}
          didGuess={didGuess}
          sessionDate={sessionDate}
        />
      )}

      {!isUnlimited && <ShareButton guesses={guesses} variant="red" />}

      {isUnlimited && onPlayAgain ? (
        <Button variant="red" onClick={onPlayAgain}>
          Play Again
        </Button>
      ) : (
        <Styled.TimeToNext>
          Try again in {timeLeftStr}.
        </Styled.TimeToNext>
      )}
    </>
  );
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage