All files / src/hooks useAppInitialization.ts

95.74% Statements 45/47
95.83% Branches 23/24
71.42% Functions 5/7
95.74% Lines 45/47

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                1x 17x 17x 17x 17x   17x 17x     17x 10x 10x   10x 2x 2x     2x     10x 10x 1x 1x     1x         17x 10x 10x 10x 10x 10x 9x 9x           17x   10x   1x     9x 9x 9x   10x 10x   10x 1x         17x 10x 1x 1x   9x 9x 3x         17x    
// src/hooks/useAppInitialization.ts
import { useState, useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useAuth } from './useAuth';
import { useModal } from './useModal';
import { logger } from '../services/logger.client';
import config from '../config';
 
export const useAppInitialization = () => {
  const { userProfile, login } = useAuth();
  const { openModal } = useModal();
  const location = useLocation();
  const navigate = useNavigate();
 
  const [isDarkMode, setIsDarkMode] = useState(false);
  const [unitSystem, setUnitSystem] = useState<'metric' | 'imperial'>('imperial');
 
  // Effect to handle the token from Google/GitHub OAuth redirect
  useEffect(() => {
    const urlParams = new URLSearchParams(location.search);
    const googleToken = urlParams.get('googleAuthToken');
 
    if (googleToken) {
      logger.info('Received Google Auth token from URL. Authenticating...');
      login(googleToken).catch((err) =>
        logger.error('Failed to log in with Google token', { error: err }),
      );
      navigate(location.pathname, { replace: true });
    }
 
    const githubToken = urlParams.get('githubAuthToken');
    if (githubToken) {
      logger.info('Received GitHub Auth token from URL. Authenticating...');
      login(githubToken).catch((err) => {
        logger.error('Failed to log in with GitHub token', { error: err });
      });
      navigate(location.pathname, { replace: true });
    }
  }, [login, location.search, navigate, location.pathname]);
 
  // Effect to handle "What's New" modal
  useEffect(() => {
    const appVersion = config.app.version;
    Eif (appVersion) {
      logger.info(`Application version: ${appVersion}`);
      const lastSeenVersion = localStorage.getItem('lastSeenVersion');
      if (appVersion !== lastSeenVersion) {
        openModal('whatsNew');
        localStorage.setItem('lastSeenVersion', appVersion);
      }
    }
  }, [openModal]);
 
  // Effect to set initial theme based on user profile, local storage, or system preference
  useEffect(() => {
    let darkModeValue: boolean;
    if (userProfile && userProfile.preferences?.darkMode !== undefined) {
      // Preference from DB
      darkModeValue = userProfile.preferences.darkMode;
    } else {
      // Fallback to local storage or system preference
      const savedMode = localStorage.getItem('darkMode');
      const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
      darkModeValue = savedMode !== null ? savedMode === 'true' : prefersDark;
    }
    setIsDarkMode(darkModeValue);
    document.documentElement.classList.toggle('dark', darkModeValue);
    // Also save to local storage if coming from profile, to persist on logout
    if (userProfile && userProfile.preferences?.darkMode !== undefined) {
      localStorage.setItem('darkMode', String(userProfile.preferences.darkMode));
    }
  }, [userProfile]);
 
  // Effect to set initial unit system based on user profile or local storage
  useEffect(() => {
    if (userProfile && userProfile.preferences?.unitSystem) {
      setUnitSystem(userProfile.preferences.unitSystem);
      localStorage.setItem('unitSystem', userProfile.preferences.unitSystem);
    } else {
      const savedSystem = localStorage.getItem('unitSystem') as 'metric' | 'imperial' | null;
      if (savedSystem) {
        setUnitSystem(savedSystem);
      }
    }
  }, [userProfile?.preferences?.unitSystem, userProfile?.user.user_id]);
 
  return { isDarkMode, unitSystem };
};