All files / src/hooks use-optimistic-profile.ts

100% Statements 12/12
71.42% Branches 5/7
100% Functions 2/2
100% Lines 12/12

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                  3x 17x 17x 17x     17x 17x 12x             5x 2x           3x     17x   17x          
import { useMemo } from 'react';
import { useUserStore } from '@/store/user-store';
import { useAuthStore } from '@/store/auth-store';
import { useUserProfile } from './use-user-profile';
 
/**
 * Hook for getting user profile data with optimistic updates support
 * This provides immediate UI feedback during profile updates
 */
export const useOptimisticProfile = () => {
  useUserProfile(); // Ensure profile is loaded
  const { user } = useAuthStore();
  const { profile, isProfileLoading } = useUserStore();
 
  // Progressive profile data loading - show immediate fallback, enhance when data loads
  const userProfile = useMemo(() => {
    if (profile) {
      return {
        name: profile.name,
        avatar: profile.avatar,
      };
    }
 
    // Fallback to auth user data while profile loads
    if (user) {
      return {
        name: user.username || user.email || '',
        avatar: null, // Auth user doesn't have avatar data
      };
    }
 
    return null;
  }, [profile, user]);
 
  console.log('useOptimisticProfile', { user, profile, isProfileLoading });
 
  return {
    userProfile,
    isProfileLoading,
  };
};