sim16/simmadome/src/GamePage.tsx

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-01-15 02:26:54 +00:00
import React, {useState, useRef, useLayoutEffect} from 'react';
import twemoji from 'twemoji';
import ReactRouter from 'react-router';
2021-01-08 05:29:39 +00:00
import {GameState, useListener} from './GamesUtil';
import './GamePage.css';
import Game from './Game';
2021-01-15 02:26:54 +00:00
import {getUID} from './util';
2021-01-08 05:29:39 +00:00
function GamePage(props: ReactRouter.RouteComponentProps<{id: string}>) {
2021-01-15 02:26:54 +00:00
let [game, setGame] = useState<[string, GameState]|undefined>(undefined);
let history = useRef<[number, string, string][]>([]);
useListener((newGames) => {
let newGame = newGames.find((gamePair) => gamePair[0] === props.match.params.id);
setGame(newGame);
console.log(newGame);
if (newGame !== undefined && newGame[1].start_delay < 0 && newGame[1].end_delay > 8) {
history.current.unshift([getUID(), newGame[1].update_emoji, newGame[1].update_text]);
if (history.current.length > 8) {
history.current.pop();
}
}
});
if (game === undefined) {
return <div id="game_container">The game you're looking for either doesn't exist or has already ended.</div>
}
2021-01-08 05:29:39 +00:00
return (
<div id="game_container">
2021-01-15 02:26:54 +00:00
<Game gameId={game[0]} state={game[1]}/>
{ history.current.length > 0 ?
<GameHistory history={history.current}/> :
null
2021-01-08 05:29:39 +00:00
}
</div>
);
}
2021-01-15 02:26:54 +00:00
function GameHistory(props: {history: [number, string, string][]}) {
let self = useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
if (self.current) {
twemoji.parse(self.current);
}
})
return (
<div className="history_box" ref={self}>
<div className="history_title">History</div>
{props.history.map((update) => (
<div className="update history_update" key={update[0]}>
<div className="update_emoji">{update[1]}</div>
<div className="update_text">{update[2]}</div>
</div>
))}
</div>
);
}
2021-01-08 05:29:39 +00:00
export default GamePage;