mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
Initial clean state
This commit is contained in:
46
packages/web/src/app/(auth)/page.tsx
Normal file
46
packages/web/src/app/(auth)/page.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
"use client"
|
||||
|
||||
import Room from "@rahoot/web/components/game/join/Room"
|
||||
import Username from "@rahoot/web/components/game/join/Username"
|
||||
import { useEvent, useSocket } from "@rahoot/web/contexts/socketProvider"
|
||||
import { usePlayerStore } from "@rahoot/web/stores/player"
|
||||
import { useEffect } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
import toast from "react-hot-toast"
|
||||
|
||||
const Home = () => {
|
||||
const { isConnected, connect, socket } = useSocket()
|
||||
const { player } = usePlayerStore()
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
if (!isConnected) {
|
||||
connect()
|
||||
}
|
||||
}, [connect, isConnected])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isConnected) return
|
||||
try {
|
||||
const storedGameId = localStorage.getItem("last_game_id")
|
||||
if (storedGameId) {
|
||||
socket?.emit("player:reconnect", { gameId: storedGameId })
|
||||
router.replace(`/game/${storedGameId}`)
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}, [isConnected, socket, router])
|
||||
|
||||
useEvent("game:errorMessage", (message) => {
|
||||
toast.error(message)
|
||||
})
|
||||
|
||||
if (player) {
|
||||
return <Username />
|
||||
}
|
||||
|
||||
return <Room />
|
||||
}
|
||||
|
||||
export default Home
|
||||
Reference in New Issue
Block a user