Duffer Derek

Current Path : /var/www/sitesecurity.bitkit.dk/httpdocs/src/hooks/
Upload File :
Current File : /var/www/sitesecurity.bitkit.dk/httpdocs/src/hooks/useHealthCheck.ts

// Health Check Hook - Manages notification subscription health checks
import { useEffect, useCallback } from 'react';
import { useNotifications } from './useNotifications';

export const useHealthCheck = (isLoggedIn: boolean) => {
  const { healthCheck, isSubscribed } = useNotifications();

  const performHealthCheck = useCallback(async () => {
    if (!isLoggedIn || !isSubscribed) {
      return;
    }

    try {
      // console.log('Performing notification health check...');
      const isHealthy = await healthCheck();
      
      if (isHealthy) {
        // console.log('Notification subscription is healthy');
      } else {
        // console.log('Notification subscription health check failed');
      }
    } catch (error) {
      console.error('Health check error:', error);
    }
  }, [isLoggedIn, isSubscribed, healthCheck]);

  // Perform health check on app initialization
  useEffect(() => {
    if (isLoggedIn) {
      // Small delay to ensure app is fully loaded
      const timer = setTimeout(() => {
        performHealthCheck();
      }, 1000);

      return () => clearTimeout(timer);
    }
  }, [isLoggedIn, performHealthCheck]);

  // Perform health check when app becomes visible (user returns to app)
  useEffect(() => {
    if (!isLoggedIn) return;

    const handleVisibilityChange = () => {
      if (!document.hidden) {
        performHealthCheck();
      }
    };

    document.addEventListener('visibilitychange', handleVisibilityChange);

    return () => {
      document.removeEventListener('visibilitychange', handleVisibilityChange);
    };
  }, [isLoggedIn, performHealthCheck]);

  // Perform health check when app regains focus
  useEffect(() => {
    if (!isLoggedIn) return;

    const handleFocus = () => {
      performHealthCheck();
    };

    window.addEventListener('focus', handleFocus);

    return () => {
      window.removeEventListener('focus', handleFocus);
    };
  }, [isLoggedIn, performHealthCheck]);

  return {
    performHealthCheck
  };
};

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists