Low hanging hook fruit
This commit is contained in:
parent
3b0cf502ab
commit
c1d47bf2f7
|
@ -9,7 +9,7 @@ const Context = React.createContext<ServerTime>(Object.seal({ setOffset: () => {
|
||||||
|
|
||||||
export const ServerTimeProvider = (props: React.PropsWithChildren<{}>) => {
|
export const ServerTimeProvider = (props: React.PropsWithChildren<{}>) => {
|
||||||
const [offset, setOffset] = React.useState(0);
|
const [offset, setOffset] = React.useState(0);
|
||||||
const value = React.useMemo(() => Object.seal({ setOffset, now: () => Date.now() + offset }), [offset, setOffset]);
|
const value = React.useMemo(() => Object.seal({ setOffset, now: () => Date.now() + offset }), [offset]);
|
||||||
return <Context.Provider value={value}>{props.children}</Context.Provider>;
|
return <Context.Provider value={value}>{props.children}</Context.Provider>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -84,6 +84,7 @@ const CenterText = ({ winner, timer, turn }: State) => {
|
||||||
const classes = useCenterStyles();
|
const classes = useCenterStyles();
|
||||||
const [countdown, setCountdown] = React.useState<number | undefined>();
|
const [countdown, setCountdown] = React.useState<number | undefined>();
|
||||||
const { now } = useServerTime();
|
const { now } = useServerTime();
|
||||||
|
const deadline = timer?.turnEnd;
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const updateCountdown = () => {
|
const updateCountdown = () => {
|
||||||
|
@ -92,14 +93,13 @@ const CenterText = ({ winner, timer, turn }: State) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isDefined(timer)) {
|
if (deadline === undefined) {
|
||||||
if (countdown !== undefined) {
|
if (countdown !== undefined) {
|
||||||
setCountdown(undefined);
|
setCountdown(undefined);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const deadline = timer.turnEnd;
|
|
||||||
const diff = deadline.getTime() - now();
|
const diff = deadline.getTime() - now();
|
||||||
|
|
||||||
const between = Math.floor(diff / 1000);
|
const between = Math.floor(diff / 1000);
|
||||||
|
@ -120,7 +120,7 @@ const CenterText = ({ winner, timer, turn }: State) => {
|
||||||
}, 200);
|
}, 200);
|
||||||
|
|
||||||
return () => window.clearInterval(interval);
|
return () => window.clearInterval(interval);
|
||||||
}, [countdown, setCountdown, winner, timer, now]);
|
}, [countdown, winner, deadline, now]);
|
||||||
|
|
||||||
const centerText = React.useMemo(() => {
|
const centerText = React.useMemo(() => {
|
||||||
const text = isDefined(winner) ? `${teamSpecs[winner].name} wins!` : `${teamSpecs[turn].name}'s turn`;
|
const text = isDefined(winner) ? `${teamSpecs[winner].name} wins!` : `${teamSpecs[turn].name}'s turn`;
|
||||||
|
|
Loading…
Reference in New Issue