Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 1m1s
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
// 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,
|
|
};
|
|
};
|