Add logic
This commit is contained in:
parent
019ef38145
commit
535cff8612
@ -56,4 +56,8 @@ span {
|
|||||||
}
|
}
|
||||||
.voting {
|
.voting {
|
||||||
margin: 15%;
|
margin: 15%;
|
||||||
|
}
|
||||||
|
|
||||||
|
pre {
|
||||||
|
color: white;
|
||||||
}
|
}
|
@ -1,23 +1,12 @@
|
|||||||
import { CSSProperties, useEffect, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
import './TVMode.css';
|
|
||||||
import './Client.css';
|
import './Client.css';
|
||||||
|
import './TVMode.css';
|
||||||
import { ButtonType, buttonTypeList, WGPPState } from './types';
|
import { ButtonType, buttonTypeList, WGPPState } from './types';
|
||||||
|
|
||||||
const TVMode = () => {
|
const TVMode = () => {
|
||||||
|
|
||||||
const [state, setState] = useState<Partial<WGPPState>>({});
|
const [state, setState] = useState<Partial<WGPPState>>({});
|
||||||
const [socket, setSocket] = useState<WebSocket>();
|
const [socket, setSocket] = useState<WebSocket>();
|
||||||
const [endTime, setEndTime] = useState(0);
|
|
||||||
const [secondsRemaining, setSecondsRemaining] =useState(0);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setEndTime(Date.now() + (state?.timeUntilNextMode??15) * 1000);
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
const remaining = Math.round((endTime - Date.now())/1000);
|
|
||||||
setSecondsRemaining(remaining < 0 ? 0 : remaining);
|
|
||||||
}, 500);
|
|
||||||
return () => clearInterval(interval);
|
|
||||||
}, [endTime, state?.timeUntilNextMode]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const url = new URL(`api/client`, window.location.href);
|
const url = new URL(`api/client`, window.location.href);
|
||||||
@ -37,8 +26,8 @@ const TVMode = () => {
|
|||||||
};
|
};
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const totalCount = buttonTypeList.reduce((acc, v) => (state?.votes?.[v]??0) + acc, 0);
|
const totalCount = useMemo(() => buttonTypeList.reduce((acc, v) => (state?.votes?.[v] ?? 0) + acc, 0), [state]);
|
||||||
const voteList = buttonTypeList.map(b => [b, state?.votes?.[b] ?? 0] as [ButtonType, number]);
|
const voteList = useMemo(() => buttonTypeList.map(b => [b, state?.votes?.[b] ?? 0] as [ButtonType, number]).sort((a, b) => a[1] < b[1] ? -1 : 1), [state]);
|
||||||
|
|
||||||
return <div className="stream">
|
return <div className="stream">
|
||||||
{state === undefined ?
|
{state === undefined ?
|
||||||
@ -50,10 +39,13 @@ const TVMode = () => {
|
|||||||
<span className="timer">{Math.round(state.timeUntilNextMode ?? 0)}s ⏱️</span>
|
<span className="timer">{Math.round(state.timeUntilNextMode ?? 0)}s ⏱️</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="voting">
|
<div className="voting">
|
||||||
{voteList.map(([b, numVotes], index) =>
|
{voteList.flatMap(([b, numVotes], index) => numVotes === 0
|
||||||
<div className="vote" key={index}>
|
? []
|
||||||
<span>{b}</span><span> {numVotes ? Math.round(numVotes / totalCount * 100) + "%" : ""} </span>
|
: [
|
||||||
</div>
|
<div className="vote" key={index}>
|
||||||
|
<span>{b}</span><span> {Math.round(numVotes / totalCount * 100) + "%"} </span>
|
||||||
|
</div>
|
||||||
|
]
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
|
Loading…
Reference in New Issue
Block a user