60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { authOptions } from "../api/auth/[...nextauth]"
|
|
import { getAnyRunningGame } from "../api/game/running"
|
|
import { GetServerSideProps } from "next"
|
|
import { Session, getServerSession } from "next-auth"
|
|
import { useRouter } from "next/router"
|
|
import React, { useEffect } from "react"
|
|
import { toast } from "react-toastify"
|
|
|
|
interface Props {
|
|
gameId: string
|
|
session: Session | null
|
|
}
|
|
|
|
export default function Lobby({ gameId, session }: Props) {
|
|
const router = useRouter()
|
|
|
|
useEffect(() => {
|
|
const path = gameId ? "/lobby/" + gameId : "/start"
|
|
toast.promise(router.push(path), {
|
|
pending: {
|
|
render: "Wird weitergeleitet...",
|
|
toastId: "redirect",
|
|
},
|
|
success: {
|
|
render: gameId
|
|
? "Spiel gefunden!"
|
|
: session?.user
|
|
? "Kein laufendes Spiel."
|
|
: "Kein laufendes Spiel. Bitte anmelden.",
|
|
toastId: session?.user ? "postRedirect" : "user",
|
|
theme: session?.user ? "dark" : undefined,
|
|
type: gameId ? "success" : "info",
|
|
},
|
|
error: {
|
|
render: "Es ist ein Fehler aufgetreten 🤯",
|
|
type: "error",
|
|
theme: "colored",
|
|
},
|
|
})
|
|
})
|
|
|
|
return (
|
|
<div className="h-full bg-theme">
|
|
<div className="mx-auto flex h-full max-w-screen-md flex-col items-center justify-evenly"></div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export const getServerSideProps: GetServerSideProps<Props> = async (
|
|
context
|
|
) => {
|
|
const session = await getServerSession(context.req, context.res, authOptions)
|
|
let gameId = ""
|
|
if (session?.user.id) {
|
|
const game = await getAnyRunningGame(session?.user.id)
|
|
if (game && game.state === "launching") gameId = game?.id
|
|
}
|
|
|
|
return { props: { gameId, session } }
|
|
}
|