mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 02:31:12 -05:00
Refactor: Simplify auth and profile handling
This commit is contained in:
50
src/hooks/useProfile.tsx
Normal file
50
src/hooks/useProfile.tsx
Normal file
@@ -0,0 +1,50 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { Profile } from '@/types/database';
|
||||
|
||||
export function useProfile(userId: string | undefined) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: ['profile', userId],
|
||||
queryFn: async () => {
|
||||
if (!userId) return null;
|
||||
|
||||
console.log('[useProfile] Fetching profile for userId:', userId);
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('profiles')
|
||||
.select('*, location:locations(*)')
|
||||
.eq('user_id', userId)
|
||||
.maybeSingle();
|
||||
|
||||
if (error) {
|
||||
console.error('[useProfile] Error:', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log('[useProfile] Profile loaded:', {
|
||||
username: data?.username,
|
||||
avatar_url: data?.avatar_url
|
||||
});
|
||||
|
||||
return data as Profile;
|
||||
},
|
||||
enabled: !!userId,
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
refetchOnWindowFocus: false,
|
||||
retry: 2,
|
||||
});
|
||||
|
||||
const refreshProfile = () => {
|
||||
if (userId) {
|
||||
console.log('[useProfile] Invalidating profile cache for userId:', userId);
|
||||
queryClient.invalidateQueries({ queryKey: ['profile', userId] });
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
...query,
|
||||
refreshProfile,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user