forked from SigNoz/signoz-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseExpandedCategories.ts
35 lines (28 loc) · 1.02 KB
/
useExpandedCategories.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use client'
import { useState, useEffect } from 'react'
export const useExpandedCategories = () => {
const [expandedCategories, setExpandedCategories] = useState({})
useEffect(() => {
const storedState = localStorage.getItem('expandedCategories')
if (storedState) {
setExpandedCategories(JSON.parse(storedState))
}
}, [])
const toggleCategory = (label: string) => {
// Update local storage first
const updatedCategories = {
...expandedCategories,
[label]: !expandedCategories[label], // Toggle the state for the specified category label
}
localStorage.setItem('expandedCategories', JSON.stringify(updatedCategories))
// Update state
setExpandedCategories(updatedCategories)
}
const getExpandedCategoriesFromLocalStorage = () => {
const storedState = localStorage.getItem('expandedCategories')
if (storedState) {
setExpandedCategories(JSON.parse(storedState))
}
}
return { expandedCategories, toggleCategory, getExpandedCategoriesFromLocalStorage }
}