import { type GetServerSideProps, type NextPage } from "next"; import Head from "next/head"; import { AiOutlineClear } from "react-icons/ai"; import { FaShieldAlt } from "react-icons/fa"; import { IoTrashBinOutline } from "react-icons/io5"; import { SiGithub, SiGoogle } from "react-icons/si"; import { GiStarFormation } from "react-icons/gi"; import { api } from "~/utils/api"; import { getServerAuthSession } from "../../server/auth"; export const getServerSideProps: GetServerSideProps = async (ctx) => { const session = await getServerAuthSession(ctx); // Redirect to login if not signed in if (!session) { return { redirect: { destination: `/api/auth/signin?callbackUrl=${ctx.resolvedUrl}`, permanent: false, }, }; } if (!session.user.isAdmin) { ctx.res.statusCode = 403; return { redirect: { destination: "/", permanent: false, }, }; } // Return session if logged in return { props: { session }, }; }; const Admin: NextPage = () => { return ( <> Sprint Padawan - Admin
); }; export default Admin; const AdminBody: React.FC = () => { const { data: usersCount, isLoading: usersCountLoading, isFetching: usersCountFetching, refetch: refetchUsersCount, } = api.user.countAll.useQuery(); const { data: users, isLoading: usersLoading, isFetching: usersFetching, refetch: refetchUsers, } = api.user.getAll.useQuery(); const { data: roomsCount, isLoading: roomsCountLoading, isFetching: roomsCountFetching, refetch: refetchRoomsCount, } = api.room.countAll.useQuery(); const { data: votesCount, isLoading: votesCountLoading, isFetching: votesCountFetching, refetch: refetchVotesCount, } = api.vote.countAll.useQuery(); const getProviders = (user: { createdAt: Date; accounts: { provider: string; }[]; sessions: { id: string; }[]; id: string; isAdmin: boolean; isVIP: boolean; name: string | null; email: string | null; }) => { return user.accounts.map((account) => { return account.provider; }); }; const deleteUserMutation = api.user.delete.useMutation({ onSuccess: async () => { await refetchData(); }, }); const clearSessionsByUserMutation = api.session.deleteAllByUserId.useMutation( { onSuccess: async () => { await refetchData(); }, } ); const clearSessionsMutation = api.session.deleteAll.useMutation({ onSuccess: async () => { await refetchData(); }, }); const setAdminMutation = api.user.setAdmin.useMutation({ onSuccess: async () => { await refetchData(); }, }); const setVIPMutation = api.user.setVIP.useMutation({ onSuccess: async () => { await refetchData(); }, }); const deleteUserHandler = async (userId: string) => { await deleteUserMutation.mutateAsync({ userId }); }; const clearSessionsByUserHandler = async (userId: string) => { await clearSessionsByUserMutation.mutateAsync({ userId }); }; const clearSessionsHandler = async () => { await clearSessionsMutation.mutateAsync(); }; const setAdmin = async (userId: string, value: boolean) => { await setAdminMutation.mutateAsync({ userId, value }); }; const setVIP = async (userId: string, value: boolean) => { await setVIPMutation.mutateAsync({ userId, value }); }; const refetchData = async () => { await Promise.all([ refetchUsers(), refetchUsersCount(), refetchRoomsCount(), refetchVotesCount(), ]); }; return ( <>

Admin Panel

Users
{usersCountLoading || usersCountFetching ? ( ) : ( <>{usersCount ? usersCount : "0"} )}
Rooms
{roomsCountLoading || roomsCountFetching ? ( ) : ( <>{roomsCount ? roomsCount : "0"} )}
Votes
{votesCountLoading || votesCountFetching ? ( ) : ( <>{votesCount ? votesCount : "0"} )}
{usersCountFetching || usersFetching || roomsCountFetching || votesCountFetching ? ( ) : (
)}

Users:

{usersLoading || usersFetching ? ( ) : (
{/* head */} {users ?.sort((user1, user2) => user2.createdAt > user1.createdAt ? 1 : -1 ) .map((user) => { return ( ); })}
ID Name Created At # Sessions Providers Actions
{user.id} {user.name} {user.createdAt.toLocaleDateString()} {user.sessions.length} {getProviders(user).includes("google") && ( )} {getProviders(user).includes("github") && ( )}
)}
); };