-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathgame-over.tsx
65 lines (57 loc) · 1.67 KB
/
game-over.tsx
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
import React from "react";
import { animated, useSpring } from "react-spring";
import styles from "../styles/game-over.module.scss";
import Button from "./button";
import Score from "./score";
interface Props {
highscore: number;
resetGame: () => void;
score: number;
}
const defaultShareText = "Share";
function getMedal(score: number): string {
if (score >= 20) {
return "🥇 ";
} else if (score >= 10) {
return "🥈 ";
} else if (score >= 1) {
return "🥉 ";
}
return "";
}
export default function GameOver(props: Props) {
const { highscore, resetGame, score } = props;
const animProps = useSpring({
opacity: 1,
from: { opacity: 0 },
config: { duration: 500 },
});
const [shareText, setShareText] = React.useState(defaultShareText);
const share = React.useCallback(async () => {
await navigator?.clipboard?.writeText(
`🏛️ wikitrivia.tomjwatson.com\n\n${getMedal(
score
)}Streak: ${score}\n${getMedal(highscore)}Best Streak: ${highscore}`
);
setShareText("Copied");
setTimeout(() => {
setShareText(defaultShareText);
}, 2000);
}, [highscore, score]);
return (
<animated.div style={animProps} className={styles.gameOver}>
<div className={styles.scoresWrapper}>
<div className={styles.score}>
<Score score={score} title="Streak" />
</div>
<div className={styles.score}>
<Score score={highscore} title="Best streak" />
</div>
</div>
<div className={styles.buttons}>
<Button onClick={resetGame} text="Play again" />
<Button onClick={share} text={shareText} minimal />
</div>
</animated.div>
);
}