import { useUser } from "@clerk/remix"; 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"; 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 Index() { const { user } = useUser(); let roomsFromDb = useEventSource("/api/room", { event: "roomlist" }); let roomsFromDbParsed = JSON.parse(roomsFromDb!) as RoomsResponse; const [roomName, setRoomName] = useState(""); // const [roomsFromDb, setRoomsFromDb] = useState(undefined); const createRoomHandler = async () => { await fetch("/api/room", { 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 getRoomsHandler = async () => { // const dbRoomsResponse = await fetch("/api/room", { // cache: "no-cache", // method: "GET", // }); // const dbRooms = (await dbRoomsResponse.json()) as RoomsResponse; // setRoomsFromDb(dbRooms); // }; const deleteRoomHandler = async (roomId: string) => { await fetch(`/api/room/${roomId}`, { cache: "no-cache", method: "DELETE", }); }; // useEffect(() => { // void getRoomsHandler(); // }, []); 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}
)} {roomsFromDb === undefined && }
); }