ai-wpa/hooks/useProfileData.ts

58 lines
1.5 KiB
TypeScript

'use client'
import { useQuery } from '@tanstack/react-query'
import { useAuth } from '@/contexts/AuthContext'
interface ProfileStats {
totalTopics: number
publishedTopics: number
draftTopics: number
totalViews: number
balance: number
activeServices: number
}
export function useProfileData() {
const { user } = useAuth()
const { data: profileStats, isLoading, error, refetch } = useQuery<ProfileStats>({
queryKey: ['profile-stats', user?.id],
queryFn: async () => {
const response = await fetch('/api/dashboard', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('authToken') || ''}`,
},
})
if (!response.ok) {
throw new Error('Failed to fetch profile data')
}
const result = await response.json()
if (!result.success) {
throw new Error(result.error?.message || 'Failed to fetch profile data')
}
// Transform dashboard data into profile stats
return {
totalTopics: result.data.stats.totalTopics,
publishedTopics: result.data.stats.publishedTopics,
draftTopics: result.data.stats.draftTopics,
totalViews: result.data.stats.totalViews,
balance: 1250.00, // TODO: Get from user balance API
activeServices: 3, // TODO: Get from services API
}
},
enabled: !!user?.id,
staleTime: 5 * 60 * 1000, // 5 minutes
refetchOnWindowFocus: false,
})
return {
profileStats,
isLoading,
error,
refetch,
}
}