Updating workspace structure
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user