"use client"; import Router from "@/components/Router"; import AppSidebar, { SidebarTrigger } from "@/components/sidebar/AppSidebar"; import { useWorkspace } from "@/hooks/workspace"; import { ChevronRight } from "lucide-react"; import Cookies from "js-cookie"; import { useEffect, useState } from "react"; import { Route } from "@/lib/types"; import { Button } from "@/components/ui/button"; import { createDefaultWorkspace, saveWorkspaceToLocalStorage, } from "@/lib/workspace"; export default function Workspace() { const { workspace, loadWorkspace, updateEntity, setWorkspace, addEntity, addGroup, renameEntity, deleteEntity, removeNode, renameGroup, } = useWorkspace(); const [route, setRoute] = useState({}); /** * Initialize workspace from cookie on mount */ useEffect(() => { const routeString = Cookies.get("route"); if (!routeString) return; const route = JSON.parse(routeString) as Route; setRoute(route); }, []); /** * Load workspace when currentWorkspace changes. * * `loadWorkspace` is intentionally omitted from the dependency array * because it is recreated on every render by the `useWorkspace` hook. * Including it would cause this effect to run repeatedly and trigger * the "maximum update depth exceeded" error. ESLint warning is disabled * for this case. */ useEffect(() => { loadWorkspace(route.workspace); if (route.workspace) { Cookies.set("route", JSON.stringify(route)); } else { Cookies.remove("route"); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [route]); /** * Load route (entity) from localStorage when workspace loads */ useEffect(() => { if (!workspace) return; try { const savedRoute = localStorage.getItem(`route_${workspace.id}`); if (savedRoute) { const parsedRoute: Route = JSON.parse(savedRoute); setRoute(parsedRoute); } else { // Default to first entity if no route saved const firstEntityId = Object.keys(workspace.entities)[0]; if (firstEntityId) { setRoute({ workspace: workspace.id, entity: firstEntityId }); } } } catch (err) { console.error("Failed to load route:", err); const firstEntityId = Object.keys(workspace.entities)[0]; if (firstEntityId) { setRoute({ workspace: workspace.id, entity: firstEntityId }); } } }, [workspace?.id]); /** * Save route whenever it changes */ useEffect(() => { if (!workspace || !route.entity) return; try { localStorage.setItem( `route_${workspace.id}`, JSON.stringify({ workspace: workspace.id, entity: route.entity }), ); } catch (err) { console.error("Failed to save route:", err); } }, [route, workspace?.id]); if (!workspace) { return (
{ const w = createDefaultWorkspace(); saveWorkspaceToLocalStorage(w); setRoute({ workspace: w.id }); }} >

Welcome to Drift

Your personal productivity workspace

); } return (
{workspace.name} {workspace.entities[route.entity || ""]?.name || "Select an item"}
); }