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:
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user