Testing functionality of the workspace storing and receiving
This commit is contained in:
+26
-14
@@ -5,25 +5,37 @@ import {
|
||||
} from "@/lib/workspace";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useWorkspace(workspaceId: string) {
|
||||
const [workspace, setWorkspace] = useState<Workspace>(() =>
|
||||
loadWorkspaceFromLocalStorage(workspaceId),
|
||||
);
|
||||
export function useWorkspace() {
|
||||
const [workspace, setWorkspace] = useState<Workspace>();
|
||||
|
||||
const updateEntity = (entityId: EntityId, updater: (e: Entity) => Entity) => {
|
||||
setWorkspace((prev) => ({
|
||||
...prev,
|
||||
entities: {
|
||||
...prev.entities,
|
||||
[entityId]: updater(prev.entities[entityId]),
|
||||
},
|
||||
updatedAt: Date.now(),
|
||||
}));
|
||||
setWorkspace((prev) =>
|
||||
prev
|
||||
? {
|
||||
...prev,
|
||||
entities: {
|
||||
...prev.entities,
|
||||
[entityId]: updater(prev.entities[entityId]),
|
||||
},
|
||||
updatedAt: Date.now(),
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
};
|
||||
|
||||
const loadWorkspace = (workspaceId: string | undefined) => {
|
||||
if (!workspaceId) {
|
||||
return setWorkspace(undefined);
|
||||
}
|
||||
|
||||
const w = loadWorkspaceFromLocalStorage(workspaceId);
|
||||
setWorkspace(w);
|
||||
return w;
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
saveWorkspaceToLocalStorage(workspace);
|
||||
if (workspace) saveWorkspaceToLocalStorage(workspace);
|
||||
}, [workspace]);
|
||||
|
||||
return { workspace, updateEntity };
|
||||
return { workspace, loadWorkspace, updateEntity };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user