Skip to content

Commit 0034bb6

Browse files
Nakshatra SharmaNakshatra Sharma
authored andcommitted
feat: persist sidebar state in local storage
1 parent 974ad0e commit 0034bb6

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

pages/tools/components/Sidebar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export default function Sidebar({
129129
label={label}
130130
icon={<IconComponent />}
131131
count={checkedValues.length}
132+
id={accessorKey}
132133
>
133134
{filterCriteria[accessorKey as FilterCriteriaFields]
134135
?.map(String)

pages/tools/components/ui/DropdownMenu.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable linebreak-style */
22
/* eslint-disable react-hooks/rules-of-hooks */
33
/* eslint-disable linebreak-style */
4-
import { useRouter } from 'next/router';
54
import React, {
65
type ReactElement,
76
type ReactNode,
87
useEffect,
98
useState,
9+
useRef,
1010
} from 'react';
1111
import {
1212
Collapsible,
@@ -20,21 +20,35 @@ interface DropdownMenuProps {
2020
icon: ReactElement;
2121
count?: number;
2222
testMode?: boolean;
23+
id?: string;
2324
}
2425

2526
export default function DropdownMenu({
2627
children,
2728
label,
2829
icon,
2930
count = 0,
31+
id,
3032
}: DropdownMenuProps) {
3133
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
32-
const router = useRouter();
33-
34+
const isFirstRun = useRef(true);
3435
useEffect(() => {
35-
setIsDropdownOpen(false);
36-
}, [router]);
37-
36+
if (id) {
37+
const storedState = localStorage.getItem(`sidebar_open_${id}`);
38+
if (storedState) {
39+
setIsDropdownOpen(storedState === 'true');
40+
}
41+
}
42+
}, [id]);
43+
useEffect(() => {
44+
if (id) {
45+
if (isFirstRun.current) {
46+
isFirstRun.current = false;
47+
return;
48+
}
49+
localStorage.setItem(`sidebar_open_${id}`, String(isDropdownOpen));
50+
}
51+
}, [id, isDropdownOpen]);
3852
return (
3953
<div className='my-2 bg-slate-200 dark:bg-slate-900 p-2 rounded cursor-pointer transition-all duration-200 group'>
4054
<Collapsible open={isDropdownOpen} onOpenChange={setIsDropdownOpen}>

0 commit comments

Comments
 (0)