Files
F-r-ldraledigplannerare/frontend/src/hooks/useAnalytics.ts
T
2026-06-01 16:48:21 +02:00

38 lines
1.0 KiB
TypeScript

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<void>
}
export function useAnalytics(workspaceId: string | null): UseAnalyticsResult {
const [data, setData] = useState<WorkspaceOverview | null>(null)
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState<string | null>(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 }
}