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 (
<>
{isUnlimited ? "The song was" : "Today's song is"} {todaysSolution.artist} - {todaysSolution.name}
{didGuess && (
You guessed it in {currentTry} {currentTry === 1 ? 'try' : 'tries'}.
)}
>
);
}
interface ShareButtonProps {
guesses: GuessType[];
variant?: keyof typeof theme;
}
function ShareButton({ guesses, variant }: ShareButtonProps) {
const [buttonText, setButtonText] = useState('Share Results');
const [result, setResult] = useState('');
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 (
);
}
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('');
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 (
<>
{textForTry[currentTry - 1]}
{!isUnlimited && (
)}
{!isUnlimited && }
{isUnlimited && onPlayAgain ? (
) : (
The next {appName} will be available in {timeLeftStr}!
)}
>
);
}
return (
<>
Unfortunately, thats wrong.
{!isUnlimited && (
)}
{!isUnlimited && }
{isUnlimited && onPlayAgain ? (
) : (
Try again in {timeLeftStr}.
)}
>
);
}