All files / src/store debug.ts

98.59% Statements 70/71
75% Branches 15/20
100% Functions 12/12
98.59% Lines 70/71

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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242                        4x         4x   4x 4x 4x                 4x   4x 4x 4x                     4x   4x 4x 4x                 4x   4x 4x 4x                                 4x   4x 4x 4x             4x   4x             4x     4x 118x 118x                                   4x 135x 135x                                   4x 136x 48x               4x 100x         100x 8x                     1x   1x 1x 1x 1x   1x             4x     4x 4x 4x                 4x 4x 1x 1x         4x 4x 4x 4x 4x 2x 2x         4x 4x 4x           4x         4x 4x 4x 4x 4x 4x 4x 4x        
/**
 * Zustand Store Debug Utilities
 * Provides logging and debugging helpers for all stores
 */
 
import { startTransition } from 'react';
import { useAuthStore } from './auth-store';
import { useUserStore } from './user-store';
import { useUIStore } from './ui-store';
import { useWalletStore } from './wallet-store';
import { useNotificationStore } from './notification-store';
 
export const StoreDebugger = {
  /**
   * Log current state of all stores
   */
  logAllStores: () => {
    console.group('๐Ÿ” [Store Debugger] Current Store States');
 
    console.group('๐Ÿ” Auth Store');
    const authState = useAuthStore.getState();
    console.log('State:', {
      hasUser: !!authState.user,
      username: authState.user?.username,
      isAuthenticated: authState.isAuthenticated,
      isSigningIn: authState.isSigningIn,
      isSigningUp: authState.isSigningUp,
      isLoggingOut: authState.isLoggingOut,
      hasToken: !!authState.token,
    });
    console.groupEnd();
 
    console.group('๐Ÿ‘ค User Store');
    const userState = useUserStore.getState();
    console.log('State:', {
      hasProfile: !!userState.profile,
      profileName: userState.profile?.name,
      hasPreferences: !!userState.preferences,
      theme: userState.preferences?.theme,
      isLoading: userState.isProfileLoading || userState.isPreferencesLoading,
      errors: {
        profile: userState.profileError,
        preferences: userState.preferencesError,
      },
    });
    console.groupEnd();
 
    console.group('๐ŸŽจ UI Store');
    const uiState = useUIStore.getState();
    console.log('State:', {
      sidebar: uiState.isSidebarOpen,
      mobileMenu: uiState.isMobileMenuOpen,
      modals: uiState.modals,
      selectedTransaction: uiState.selectedTransactionId,
      currentPage: uiState.currentPage,
      toastCount: uiState.toasts.length,
      forms: uiState.forms,
    });
    console.groupEnd();
 
    console.group('๐Ÿ’ฐ Wallet Store');
    const walletState = useWalletStore.getState();
    console.log('State:', {
      hasWalletData: !!walletState.walletData,
      balance: walletState.walletData?.balance,
      transactionCount: walletState.transactions.length,
      loading: {
        wallet: walletState.isWalletLoading,
        transactions: walletState.isTransactionsLoading,
        withdrawing: walletState.isWithdrawing,
        depositing: walletState.isDepositing,
      },
      errors: {
        wallet: walletState.walletError,
        transactions: walletState.transactionsError,
        withdraw: walletState.withdrawError,
        deposit: walletState.depositError,
      },
    });
    console.groupEnd();
 
    console.group('๐Ÿ”” Notification Store');
    const notificationState = useNotificationStore.getState();
    console.log('State:', {
      notificationCount: notificationState.notifications.length,
      counts: notificationState.counts,
      isOpen: notificationState.isOpen,
      isLoading: notificationState.isLoading,
      error: notificationState.error,
    });
    console.groupEnd();
 
    console.groupEnd();
  },
 
  /**
   * Subscribe to all store changes and log them
   */
  subscribeToChanges: () => {
    console.log('๐Ÿ” [Store Debugger] Subscribing to store changes...');
 
    // Auth store subscription
    useAuthStore.subscribe((state, prevState) => {
      Eif (state !== prevState) {
        console.log('๐Ÿ” [Auth Store Changed]', {
          prev: {
            isAuthenticated: prevState.isAuthenticated,
            hasUser: !!prevState.user,
            isSigningIn: prevState.isSigningIn,
            isSigningUp: prevState.isSigningUp,
          },
          new: {
            isAuthenticated: state.isAuthenticated,
            hasUser: !!state.user,
            isSigningIn: state.isSigningIn,
            isSigningUp: state.isSigningUp,
          },
        });
      }
    });
 
    // UI store subscription
    useUIStore.subscribe((state, prevState) => {
      Eif (state !== prevState) {
        console.log('๐ŸŽจ [UI Store Changed]', {
          sidebar:
            state.isSidebarOpen !== prevState.isSidebarOpen
              ? `${prevState.isSidebarOpen} โ†’ ${state.isSidebarOpen}`
              : 'unchanged',
          toasts:
            state.toasts.length !== prevState.toasts.length
              ? `${prevState.toasts.length} โ†’ ${state.toasts.length}`
              : 'unchanged',
          modals:
            JSON.stringify(state.modals) !== JSON.stringify(prevState.modals)
              ? 'changed'
              : 'unchanged',
        });
      }
    });
 
    // Notification store subscription
    useNotificationStore.subscribe((state, prevState) => {
      if (state.notifications.length !== prevState.notifications.length) {
        console.log('๐Ÿ”” [Notification Store Changed]', {
          notificationCount: `${prevState.notifications.length} โ†’ ${state.notifications.length}`,
          unreadCount: `${prevState.counts.unread} โ†’ ${state.counts.unread}`,
        });
      }
    });
 
    // Wallet store subscription
    useWalletStore.subscribe((state, prevState) => {
      Iif (state.transactions.length !== prevState.transactions.length) {
        console.log('๐Ÿ’ฐ [Wallet Store Changed]', {
          transactionCount: `${prevState.transactions.length} โ†’ ${state.transactions.length}`,
        });
      }
      if (state.walletData?.balance !== prevState.walletData?.balance) {
        console.log('๐Ÿ’ฐ [Wallet Balance Changed]', {
          balance: `${prevState.walletData?.balance} โ†’ ${state.walletData?.balance}`,
        });
      }
    });
  },
 
  /**
   * Clear all store data (useful for testing)
   */
  clearAllStores: () => {
    console.log('๐Ÿงน [Store Debugger] Clearing all stores...');
 
    useAuthStore.getState().logout();
    useUserStore.getState().clearUserData();
    useWalletStore.getState().clearWalletData();
    useUIStore.getState().resetUI();
 
    console.log('๐Ÿงน [Store Debugger] All stores cleared');
  },
 
  /**
   * Test store functionality
   */
  testStores: () => {
    console.group('๐Ÿงช [Store Debugger] Testing Store Functionality');
 
    // Test Auth Store
    console.log('Testing Auth Store...');
    const { login, logout } = useAuthStore.getState();
    const testUser = {
      id: 999,
      username: 'test-user',
      email: 'test@example.com',
      confirmed: true,
      blocked: false,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    };
    login(testUser, 'test-token');
    setTimeout(() => {
      startTransition(() => {
        logout();
      });
    }, 1000);
 
    // Test UI Store
    console.log('Testing UI Store...');
    const { addToast, setSidebarOpen } = useUIStore.getState();
    addToast({ type: 'success', message: 'Test toast from debugger' });
    setSidebarOpen(false);
    setTimeout(() => {
      startTransition(() => {
        setSidebarOpen(true);
      });
    }, 500);
 
    // Test Notification Store
    console.log('Testing Notification Store...');
    const { addNotification } = useNotificationStore.getState();
    addNotification({
      type: 'info',
      title: 'Test Notification',
      message: 'This is a test notification from the debugger',
    });
 
    console.groupEnd();
  },
};
 
// Global debugging helpers (available in browser console)
Eif (typeof window !== 'undefined') {
  (window as any).storeDebugger = StoreDebugger;
  console.log('๐Ÿ” Store debugger available globally as `window.storeDebugger`');
  console.log('Available methods:');
  console.log('- storeDebugger.logAllStores()');
  console.log('- storeDebugger.subscribeToChanges()');
  console.log('- storeDebugger.clearAllStores()');
  console.log('- storeDebugger.testStores()');
}
 
export default StoreDebugger;