refactor: use bundled /behavior endpoint via useBehavior SWR hook

Replaces 4 separate frustration API calls with single useBehavior hook.
Removes manual fetchData, loading/error state, and refresh interval—SWR
handles caching, revalidation, and error state automatically.
This commit is contained in:
Usman Baig
2026-03-12 20:24:28 +01:00
parent eb17e8e8d6
commit 03e3f41e48
3 changed files with 47 additions and 58 deletions

View File

@@ -15,6 +15,7 @@ import {
getRealtime,
getStats,
getDailyStats,
getBehavior,
} from '@/lib/api/stats'
import { listAnnotations } from '@/lib/api/annotations'
import type { Annotation } from '@/lib/api/annotations'
@@ -32,6 +33,7 @@ import type {
DashboardReferrersData,
DashboardPerformanceData,
DashboardGoalsData,
BehaviorData,
} from '@/lib/api/stats'
// * SWR fetcher functions
@@ -52,6 +54,7 @@ const fetchers = {
campaigns: (siteId: string, start: string, end: string, limit: number) =>
getCampaigns(siteId, start, end, limit),
annotations: (siteId: string, start: string, end: string) => listAnnotations(siteId, start, end),
behavior: (siteId: string, start: string, end: string) => getBehavior(siteId, start, end),
}
// * Standard SWR config for dashboard data
@@ -265,5 +268,18 @@ export function useAnnotations(siteId: string, startDate: string, endDate: strin
)
}
// * Hook for bundled behavior data (all frustration signals in one request)
export function useBehavior(siteId: string, start: string, end: string) {
return useSWR<BehaviorData>(
siteId && start && end ? ['behavior', siteId, start, end] : null,
() => fetchers.behavior(siteId, start, end),
{
...dashboardSWRConfig,
refreshInterval: 60 * 1000,
dedupingInterval: 10 * 1000,
}
)
}
// * Re-export for convenience
export { fetchers }