sessions, or something
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"use client";
|
||||
import Link from "next/link";
|
||||
import { useAuth } from "react-oidc-context";
|
||||
import { useState, useEffect, use } from "react";
|
||||
import { useRouter, usePathname } from "next/navigation";
|
||||
import { api, Session } from "muzak/data/fetcher";
|
||||
import { getUser } from "muzak/data/fetcher";
|
||||
import { useSession } from "muzak/contexts/SessionContext";
|
||||
|
||||
export default function SessionChecker() {
|
||||
const auth = useAuth();
|
||||
const router = useRouter();
|
||||
|
||||
const [isSession, setIsSession] = useState(false);
|
||||
const [link, setLink] = useState("/lobby");
|
||||
const { sessionId, setSessionId } = useSession();
|
||||
|
||||
async function getSessions() {
|
||||
if (!auth.isLoading && !auth.error && auth.isAuthenticated) {
|
||||
const { data: sessions } = await api?.sessions?.list({});
|
||||
console.log(sessions);
|
||||
if (sessions?.length > 0) {
|
||||
setIsSession(true);
|
||||
const user = getUser();
|
||||
setSessionId(sessions[0].session_id);
|
||||
if (sessions[0].host === user?.profile?.email) {
|
||||
setLink("/lobby");
|
||||
} else {
|
||||
setLink("/session");
|
||||
}
|
||||
} else {
|
||||
setIsSession(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
getSessions();
|
||||
}, [auth]);
|
||||
|
||||
if (isSession) {
|
||||
return (
|
||||
<Link
|
||||
href={link}
|
||||
className="bold text-l absolute top-2 left-2 rounded-[50%] bg-red-500 p-3 font-bold text-white"
|
||||
>
|
||||
Session available!
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
return <> </>;
|
||||
}
|
||||
Reference in New Issue
Block a user