|
@@ -0,0 +1,43 @@
|
|
|
+import ms from 'pretty-ms';
|
|
|
+import React, { useEffect, useState } from 'react';
|
|
|
+import { useGameId, useCurrentRound, dispatch } from '../../../domain/gameStore';
|
|
|
+import { getFirstSubmitter } from '../../../domain/apiMethods';
|
|
|
+import styles from './GamePanel.module.css';
|
|
|
+
|
|
|
+export default ({ rate, cutoffTime }) => {
|
|
|
+ const [ first, setFirst ] = useState(null);
|
|
|
+ const gameId = useGameId();
|
|
|
+ const round = useCurrentRound();
|
|
|
+ useEffect(() => {
|
|
|
+ if (first !== null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const update = async () => {
|
|
|
+ const firstRes = await getFirstSubmitter(gameId, round);
|
|
|
+ if (firstRes !== null) {
|
|
|
+ dispatch.updateRoundSeconds(r => Math.min(r, cutoffTime));
|
|
|
+ setFirst(firstRes)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ update();
|
|
|
+ const interval = setInterval(update, rate);
|
|
|
+ return () => clearInterval(interval);
|
|
|
+ }, [first, gameId, round, rate, cutoffTime]);
|
|
|
+ const [faded, setFaded] = useState(false);
|
|
|
+ useEffect(() => {
|
|
|
+ if (first !== null) {
|
|
|
+ const timeout = setTimeout(() => setFaded(true), 1000);
|
|
|
+ return () => clearTimeout(timeout);
|
|
|
+ }
|
|
|
+ }, [first]);
|
|
|
+
|
|
|
+ if (first === null) {
|
|
|
+ return (<></>);
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div className={`${styles.cutoff} ${faded ? styles.hidden : ''}`}>
|
|
|
+ You were cut off by {first}! Only {ms(cutoffTime * 1000)} left!
|
|
|
+ </div>
|
|
|
+ )
|
|
|
+};
|