Line data Source code
1 : import type { ManagerState } from '@/platform/customNodes/manager/types/comfyManagerTypes'
2 : import {
3 : ManagerTab,
4 : SortableAlgoliaField
5 : } from '@/platform/customNodes/manager/types/comfyManagerTypes'
6 :
7 2355 : const STORAGE_KEY = 'Comfy.Manager.UI.State'
8 :
9 2355 : export const useManagerStatePersistence = () => {
10 : /**
11 : * Load the UI state from localStorage.
12 : */
13 15 : const loadStoredState = (): ManagerState => {
14 15 : try {
15 15 : const stored = localStorage.getItem(STORAGE_KEY)
16 15 : if (stored) {
17 0 : return JSON.parse(stored)
18 : }
19 : } catch (e) {
20 0 : console.error('Failed to load manager UI state:', e)
21 : }
22 15 : return {
23 : selectedTabId: ManagerTab.All,
24 : searchQuery: '',
25 : searchMode: 'packs',
26 : sortField: SortableAlgoliaField.Downloads
27 : }
28 : }
29 :
30 : /**
31 : * Persist the UI state to localStorage.
32 : */
33 15 : const persistState = (state: ManagerState) => {
34 1 : localStorage.setItem(STORAGE_KEY, JSON.stringify(state))
35 : }
36 :
37 : /**
38 : * Reset the UI state to the default values.
39 : */
40 15 : const reset = () => {
41 0 : persistState({
42 : selectedTabId: ManagerTab.All,
43 : searchQuery: '',
44 : searchMode: 'packs',
45 : sortField: SortableAlgoliaField.Downloads
46 : })
47 : }
48 :
49 15 : return {
50 : loadStoredState,
51 : persistState,
52 : reset
53 : }
54 : }
|