import { useState, useEffect, useCallback } from 'react' import * as api from '@/services/api' import type { WorkspaceOverview } from '@/types' interface UseAnalyticsResult { data: WorkspaceOverview | null isLoading: boolean error: string | null refresh: () => Promise } export function useAnalytics(workspaceId: string | null): UseAnalyticsResult { const [data, setData] = useState(null) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const fetch = useCallback(async () => { if (!workspaceId) return setIsLoading(true) setError(null) try { const response = await api.getWorkspaceOverview(workspaceId) setData(response) } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch analytics') setData(null) } finally { setIsLoading(false) } }, [workspaceId]) useEffect(() => { fetch() }, [fetch]) return { data, isLoading, error, refresh: fetch } }