All files / src/store ui-store.ts

100% Statements 44/44
100% Branches 2/2
100% Functions 25/25
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                                                                                                          11x   11x                                                 12x 12x       4x 4x       5x 5x       12x 12x           3x 3x           1x 1x                     4x 4x       8x 8x       5x 5x           19x 19x         19x         19x 19x 2x 2x 2x 2x             2x 2x 1x         1x 1x       63x 63x                                 63x         141x              
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { startTransition } from 'react';
import { DASHBOARD_ROUTES } from '@/constants/route';
 
export interface UIState {
  // Global UI state
  isSidebarOpen: boolean;
  isMobileMenuOpen: boolean;
  isLoading: boolean;
 
  // Modal states
  modals: {
    createTransaction: boolean;
    editTransaction: boolean;
    deleteConfirmation: boolean;
    profileSettings: boolean;
  };
 
  // Current selection/context
  selectedTransactionId: string | null;
  currentPage: string;
 
  // Form states
  forms: {
    hasUnsavedChanges: boolean;
    isDirty: boolean;
  };
 
  // Toast/notification display
  toasts: Array<{
    id: string;
    type: 'success' | 'error' | 'warning' | 'info';
    message: string;
    duration?: number;
  }>;
 
  // Actions
  setSidebarOpen: (open: boolean) => void;
  setMobileMenuOpen: (open: boolean) => void;
  setLoading: (loading: boolean) => void;
  openModal: (modalName: keyof UIState['modals']) => void;
  closeModal: (modalName: keyof UIState['modals']) => void;
  closeAllModals: () => void;
  setSelectedTransactionId: (id: string | null) => void;
  setCurrentPage: (page: string) => void;
  setFormState: (state: Partial<UIState['forms']>) => void;
  addToast: (toast: Omit<UIState['toasts'][0], 'id'>) => void;
  removeToast: (id: string) => void;
  clearToasts: () => void;
  resetUI: () => void;
}
 
export const useUIStore = create<UIState>()(
  persist(
    (set, get) => ({
      // Initial state
      isSidebarOpen: true,
      isMobileMenuOpen: false,
      isLoading: false,
 
      modals: {
        createTransaction: false,
        editTransaction: false,
        deleteConfirmation: false,
        profileSettings: false,
      },
 
      selectedTransactionId: null,
      currentPage: DASHBOARD_ROUTES.OVERVIEW,
 
      forms: {
        hasUnsavedChanges: false,
        isDirty: false,
      },
 
      toasts: [],
 
      // Actions
      setSidebarOpen: open => {
        console.log('🎨 [UIStore] Setting sidebar open:', open);
        set({ isSidebarOpen: open });
      },
 
      setMobileMenuOpen: open => {
        console.log('🎨 [UIStore] Setting mobile menu open:', open);
        set({ isMobileMenuOpen: open });
      },
 
      setLoading: loading => {
        console.log('🎨 [UIStore] Setting loading:', loading);
        set({ isLoading: loading });
      },
 
      openModal: modalName => {
        console.log('🎨 [UIStore] Opening modal:', modalName);
        set(state => ({
          modals: { ...state.modals, [modalName]: true },
        }));
      },
 
      closeModal: modalName => {
        console.log('🎨 [UIStore] Closing modal:', modalName);
        set(state => ({
          modals: { ...state.modals, [modalName]: false },
        }));
      },
 
      closeAllModals: () => {
        console.log('🎨 [UIStore] Closing all modals');
        set({
          modals: {
            createTransaction: false,
            editTransaction: false,
            deleteConfirmation: false,
            profileSettings: false,
          },
        });
      },
 
      setSelectedTransactionId: id => {
        console.log('🎨 [UIStore] Setting selected transaction ID:', id);
        set({ selectedTransactionId: id });
      },
 
      setCurrentPage: page => {
        console.log('🎨 [UIStore] Setting current page:', page);
        set({ currentPage: page });
      },
 
      setFormState: formState => {
        console.log('🎨 [UIStore] Setting form state:', formState);
        set(state => ({
          forms: { ...state.forms, ...formState },
        }));
      },
 
      addToast: toast => {
        const id = Date.now().toString();
        console.log('🎨 [UIStore] Adding toast:', {
          id,
          type: toast.type,
          message: toast.message,
        });
        set(state => ({
          toasts: [...state.toasts, { ...toast, id }],
        }));
 
        // Auto remove toast after duration
        const duration = toast.duration || 5000;
        setTimeout(() => {
          console.log('🎨 [UIStore] Auto-removing toast:', id);
          startTransition(() => {
            set(state => ({
              toasts: state.toasts.filter(t => t.id !== id),
            }));
          });
        }, duration);
      },
 
      removeToast: id => {
        console.log('🎨 [UIStore] Removing toast:', id);
        set(state => ({
          toasts: state.toasts.filter(t => t.id !== id),
        }));
      },
 
      clearToasts: () => {
        console.log('🎨 [UIStore] Clearing all toasts');
        set({ toasts: [] });
      },
 
      resetUI: () => {
        console.log('🎨 [UIStore] Resetting UI state');
        set({
          isSidebarOpen: true,
          isMobileMenuOpen: false,
          isLoading: false,
          modals: {
            createTransaction: false,
            editTransaction: false,
            deleteConfirmation: false,
            profileSettings: false,
          },
          selectedTransactionId: null,
          forms: {
            hasUnsavedChanges: false,
            isDirty: false,
          },
          toasts: [],
        });
        console.log('🎨 [UIStore] UI state reset complete');
      },
    }),
    {
      name: 'ui-storage',
      partialize: state => ({
        isSidebarOpen: state.isSidebarOpen,
        currentPage: state.currentPage,
      }),
    }
  )
);