import { getAuth } from "@clerk/remix/ssr.server"; import { LoaderFunction, redirect } from "@remix-run/node"; import { Link } from "@remix-run/react"; import { LogInIcon, TrashIcon } from "lucide-react"; import { useState } from "react"; import LoadingIndicator from "~/components/LoadingIndicator"; import { useEventSource } from "remix-utils/sse/react"; import { useAuth } from "@clerk/remix"; export const loader: LoaderFunction = async (args) => { const { userId } = await getAuth(args); if (!userId) { return redirect("/sign-in"); } return {}; }; type RoomsResponse = | { id: string; createdAt: Date; roomName: string; }[] | { roomName: string | null; id: string; created_at: Date | null; userId: string; storyName: string | null; visible: boolean; scale: string; }[] | null | undefined; export default function Dashboard() { const { userId } = useAuth(); let roomsFromDb = useEventSource("/api/room/get/all", { event: userId! }); 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!

{ setRoomName(event.target.value); }} />
{roomName.length > 0 && ( )}
{roomsFromDbParsed && roomsFromDbParsed.length > 0 && (
{/* head */} {roomsFromDbParsed?.map((room) => { return ( ); })}
Room Name Actions
{room.roomName}
)} {!roomsFromDbParsed && }
); }