All files / apps/host/src/hooks/notifications usePostLoginNotifications.ts

90.9% Statements 20/22
83.33% Branches 10/12
100% Functions 4/4
100% Lines 18/18

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                        2x 4x   4x 4x     3x   3x 3x 3x 3x   2x 1x   1x 1x   1x 1x                 3x   3x 3x        
import { useEffect } from 'react';
 
import { useAuth } from '@/contexts/AuthContext';
 
import {
  createTokenRegistrationPayload,
  registerNotificationToken,
} from '@/services/notification/notificationApi';
import { getNotificationPreference } from '@/services/notification/notificationPreference';
import { notificationService } from '@/services/notification/notificationService';
import { sentryService } from '@/services/sentryService';
 
export const usePostLoginNotifications = () => {
  const { user } = useAuth();
 
  useEffect(() => {
    if (!user) return;
 
    // Avoid running async steps after unmount or when user changes
    let cancelled = false;
 
    const setup = async () => {
      try {
        const isNotificationEnabled = await getNotificationPreference();
        if (!isNotificationEnabled || cancelled) return;
 
        const fcmToken = await notificationService.setupAfterLogin();
        Iif (!fcmToken || cancelled) return;
 
        const payload = createTokenRegistrationPayload(fcmToken);
        await registerNotificationToken(payload);
      } catch (error) {
        Iif (cancelled) return;
        sentryService.captureException(error, {
          tags: {
            component: 'notification',
            action: 'post_login_setup',
          },
        });
      }
    };
 
    setup();
 
    return () => {
      cancelled = true;
    };
  }, [user]);
};