Entity export and stuff
This commit is contained in:
@@ -15,26 +15,91 @@ import {
|
||||
|
||||
export default function Workspace() {
|
||||
const [currentWorkspace, setCurrentWorkspace] = useState<string>();
|
||||
const [isInitialized, setIsInitialized] = useState(false);
|
||||
|
||||
const { workspace, loadWorkspace, updateEntity, setWorkspace } =
|
||||
useWorkspace();
|
||||
|
||||
const [route, setRoute] = useState<Route>({});
|
||||
|
||||
/**
|
||||
* Initialize workspace from cookie on mount
|
||||
*/
|
||||
useEffect(() => {
|
||||
setCurrentWorkspace(Cookies.get("workspace"));
|
||||
const workspaceId = Cookies.get("workspace");
|
||||
setCurrentWorkspace(workspaceId);
|
||||
setIsInitialized(true);
|
||||
}, []);
|
||||
|
||||
/**
|
||||
* 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(() => {
|
||||
if (!isInitialized) return;
|
||||
|
||||
loadWorkspace(currentWorkspace);
|
||||
if (currentWorkspace) Cookies.set("workspace", currentWorkspace);
|
||||
else Cookies.remove("workspace");
|
||||
}, [currentWorkspace]);
|
||||
if (currentWorkspace) {
|
||||
Cookies.set("workspace", currentWorkspace);
|
||||
} else {
|
||||
Cookies.remove("workspace");
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [currentWorkspace, isInitialized]);
|
||||
|
||||
/**
|
||||
* 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 (
|
||||
<main
|
||||
className="flex h-svh items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100 dark:from-slate-900 dark:to-slate-800"
|
||||
className="flex h-svh items-center justify-center bg-linear-to-br from-blue-50 to-indigo-100 dark:from-slate-900 dark:to-slate-800"
|
||||
onClick={() => {
|
||||
const w = createDefaultWorkspace();
|
||||
saveWorkspaceToLocalStorage(w);
|
||||
|
||||
Reference in New Issue
Block a user