import { getAuth } from "@clerk/remix/ssr.server"; import { type LoaderFunction, redirect } from "@remix-run/node"; import { Link } from "@remix-run/react"; import { LogInIcon, ShieldIcon, StarIcon, TrashIcon } from "lucide-react"; import { useState } from "react"; import LoadingIndicator from "~/components/LoadingIndicator"; import { useEventSource } from "remix-utils/sse/react"; import { ClerkLoaded, ClerkLoading, useUser } from "@clerk/remix"; import { isAdmin, isVIP } from "~/services/helpers.client"; export const loader: LoaderFunction = async (args) => { const { userId } = await getAuth(args); if (!userId) { return redirect("/sign-in"); } if (userId) return {}; }; type RoomsResponse = | { id: string; createdAt: Date; roomName: string; }[] | { roomName: string | null; id: string; created_at: Date | null; userId: string; topicName: string | null; visible: boolean; scale: string; }[] | null | undefined; export default function Dashboard() { return ( <> > ); } function DashboardContent() { const { user, isLoaded } = useUser(); let roomsFromDb = useEventSource("/api/room/get/all", { event: user?.id }); let roomsFromDbParsed = JSON.parse(roomsFromDb!) as RoomsResponse; const [roomName, setRoomName] = useState(""); const createRoomHandler = async () => { await fetch("/api/room/create", { cache: "no-cache", method: "POST", body: JSON.stringify({ name: roomName }), }); setRoomName(""); (document.querySelector("#roomNameInput") as HTMLInputElement).value = ""; (document.querySelector("#new-room-modal") as HTMLInputElement).checked = false; }; const deleteRoomHandler = async (roomId: string) => { await fetch(`/api/room/delete/${roomId}`, { cache: "no-cache", method: "DELETE", }); }; return ( {/* Modal for Adding Rooms */} ✕ Create a new room! Room Name { setRoomName(event.target.value); }} /> {roomName.length > 0 && ( void createRoomHandler()} > Submit )} Hi, {user?.firstName ?? user?.username}!{" "} {isAdmin(user?.publicMetadata) && ( )} {isVIP(user?.publicMetadata) && ( )} {roomsFromDbParsed && roomsFromDbParsed.length > 0 && ( {/* head */} Room Name Actions {roomsFromDbParsed?.map((room) => { return ( {room.roomName} void deleteRoomHandler(room.id)} > ); })} )} New Room {(!roomsFromDbParsed || !isLoaded) && } ); }