import { useState, useEffect } from "react"; import { type LoaderFunctionArgs } from "@remix-run/server-runtime"; import { requireUserId } from "~/services/session.server"; import { useTypedLoaderData } from "remix-typedjson"; import { GraphVisualizationClient } from "~/components/graph/graph-client"; import { LoaderCircle } from "lucide-react"; import { PageHeader } from "~/components/common/page-header"; export async function loader({ request }: LoaderFunctionArgs) { // Only return userId, not the heavy nodeLinks const userId = await requireUserId(request); return { userId }; } export default function Dashboard() { const { userId } = useTypedLoaderData(); // State for nodeLinks and loading const [nodeLinks, setNodeLinks] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { let cancelled = false; async function fetchNodeLinks() { setLoading(true); try { const res = await fetch( "/node-links?userId=" + encodeURIComponent(userId), ); if (!res.ok) throw new Error("Failed to fetch node links"); const data = await res.json(); if (!cancelled) { setNodeLinks(data); setLoading(false); } } catch (e) { if (!cancelled) { setNodeLinks([]); setLoading(false); } } } fetchNodeLinks(); return () => { cancelled = true; }; }, [userId]); return ( <>
{loading ? (
Loading graph...
) : ( typeof window !== "undefined" && nodeLinks && )}
); }