All files / src/hooks useUserProfileData.ts

0% Statements 0/10
0% Branches 0/10
0% Functions 0/3
0% Lines 0/9

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                                                                                       
// src/hooks/useUserProfileData.ts
import { useCallback } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { useUserProfileDataQuery } from './queries/useUserProfileDataQuery';
import type { UserProfile } from '../types';
 
/**
 * A custom hook to access the authenticated user's profile and achievements.
 *
 * Refactored to use TanStack Query (ADR-0005 Phase 8).
 *
 * @returns An object containing profile, achievements, loading state, error, and setProfile function.
 */
export const useUserProfileData = () => {
  const queryClient = useQueryClient();
  const { data, isLoading, error } = useUserProfileDataQuery();
 
  // Provide a setProfile function for backward compatibility
  // This updates the query cache directly
  const setProfile = useCallback(
    (updater: UserProfile | ((prev: UserProfile | null) => UserProfile | null)) => {
      queryClient.setQueryData(['user-profile-data'], (oldData: typeof data) => {
        if (!oldData) return oldData;
 
        const newProfile = typeof updater === 'function' ? updater(oldData.profile) : updater;
 
        return {
          ...oldData,
          profile: newProfile,
        };
      });
    },
    [queryClient],
  );
 
  return {
    profile: data?.profile ?? null,
    setProfile,
    achievements: data?.achievements ?? [],
    isLoading,
    error: error?.message ?? null,
  };
};