From b2798a64ac6eaa4583326a078a5879485d703b79 Mon Sep 17 00:00:00 2001 From: Dennis Wang <66754085+hellolol2016@users.noreply.github.com> Date: Sun, 27 Oct 2024 16:56:34 -0400 Subject: [PATCH] sandbox localstorage is good --- .../frontend/components/Sidebar/Sidebar.tsx | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/packages/frontend/components/Sidebar/Sidebar.tsx b/packages/frontend/components/Sidebar/Sidebar.tsx index af5d7459..e1ff8ff6 100644 --- a/packages/frontend/components/Sidebar/Sidebar.tsx +++ b/packages/frontend/components/Sidebar/Sidebar.tsx @@ -338,7 +338,7 @@ interface SidebarContainerProps { renderCoopBlock?: boolean; renderBetaMajorBlock?: boolean; renderDropdownWarning?: boolean; - planId: string | number; + planId?: string | number; } export const NoPlanSidebar: React.FC = () => { @@ -358,18 +358,23 @@ const SidebarContainer: React.FC> = ({ }) => { const [notes, setNotes] = useState(""); const handleNewNotes = (e: React.ChangeEvent) => { + if (!planId) return; setNotes(e.target.value); + // Retrieve existing notes from localStorage + const storedNotes = localStorage.getItem("notes"); + const notesObject = storedNotes ? JSON.parse(storedNotes) : {}; + notesObject[planId] = notes; // have a notes object plan_id (number | string) -> note (string) - localStorage.setItem(planId.toString(), e.target.value); + localStorage.setItem("notes", JSON.stringify(notesObject)); console.log("New notes: ", e.target.value); }; useEffect(() => { if (!planId) return; - - const storedNotes = localStorage.getItem(planId.toString()); + const storedNotes = localStorage.getItem("notes"); + const notesObject = storedNotes ? JSON.parse(storedNotes) : {}; if (storedNotes) { - setNotes(storedNotes); + setNotes(notesObject[planId]); } }, [planId]); @@ -434,30 +439,36 @@ const SidebarContainer: React.FC> = ({ {children} - - - - sandbox logo + {planId && ( + + + + sandbox logo + + Sandbox Area {planId} + + - Sandbox Area {planId} + Notes - - - Notes - - - - + + + + )} ); };