Updating workspace structure

This commit is contained in:
2026-02-27 21:16:02 +01:00
parent 629f7d7a6d
commit 1a51dc8467
3 changed files with 153 additions and 100 deletions
+50
View File
@@ -0,0 +1,50 @@
import { Workspace } from "./types";
export function createDefaultWorkspace(): Workspace {
const now = Date.now();
return {
id: crypto.randomUUID(),
name: "New Workspace",
entities: {},
layout: [],
createdAt: now,
updatedAt: now,
};
}
export function loadWorkspaceFromLocalStorage(id: string): Workspace {
try {
const raw = localStorage.getItem(id);
if (!raw) {
return createDefaultWorkspace();
}
const parsed = JSON.parse(raw);
// Basic shape validation
if (
typeof parsed !== "object" ||
parsed === null ||
typeof parsed.version !== "number"
) {
console.warn("Invalid workspace shape. Resetting.");
return createDefaultWorkspace();
}
return parsed as Workspace;
} catch (err) {
console.error("Failed to load workspace:", err);
return createDefaultWorkspace();
}
}
export function saveWorkspaceToLocalStorage(workspace: Workspace): void {
try {
const serialized = JSON.stringify(workspace);
localStorage.setItem(workspace.id, serialized);
} catch (err) {
console.error("Failed to save workspace:", err);
}
}