even more changes
parent
2ad01a11a9
commit
043ac80784
@ -1,17 +1,54 @@
|
||||
import { requestJSON } from '@/client/utils'
|
||||
import type { Room } from '@/db/model'
|
||||
import { useEffect, useState } from 'preact/hooks'
|
||||
import { Leaderboard } from './Leaderboard'
|
||||
import { type Dispatch, type StateUpdater } from 'preact/hooks'
|
||||
import { computeScoreboardState } from '@/ggwp'
|
||||
|
||||
type Props = {
|
||||
room: Room
|
||||
setRoom: Dispatch<StateUpdater<Room>>
|
||||
roomId: string
|
||||
}
|
||||
|
||||
const useEventSource = (roomId: string, onMessage: (data: any) => void) => {
|
||||
useEffect(() => {
|
||||
const es = new EventSource(`/api/room/${roomId}?sse`)
|
||||
es.onmessage = e => {
|
||||
onMessage(JSON.parse(e.data))
|
||||
}
|
||||
|
||||
selectTeamQuestion?: ({ team, question }: { team: string; question: string }) => void
|
||||
return () => {
|
||||
es.close()
|
||||
}
|
||||
}, [roomId])
|
||||
}
|
||||
|
||||
export const LiveLeaderboard = ({ room, selectTeamQuestion }: Props) => {
|
||||
export const LiveLeaderboard = ({ roomId }: Props) => {
|
||||
const [room, setRoom] = useState<Room | null>(null)
|
||||
const fetchRoom = async () => {
|
||||
await requestJSON(`/api/room/${roomId}`).then(room => {
|
||||
setRoom({ id: roomId, ...room })
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchRoom()
|
||||
}, [])
|
||||
|
||||
useEventSource(roomId, data => {
|
||||
console.log('event', data)
|
||||
fetchRoom()
|
||||
})
|
||||
|
||||
if (!room) {
|
||||
return <div>Loading...</div>
|
||||
}
|
||||
|
||||
const scoreboard = computeScoreboardState(
|
||||
{
|
||||
teamIds: room.teams,
|
||||
questions: room.questions,
|
||||
},
|
||||
room.actions
|
||||
)
|
||||
|
||||
return <Leaderboard questions={room.questions} scoreboard={scoreboard} />
|
||||
}
|
||||
|
Loading…
Reference in New Issue