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:
@@ -433,6 +433,25 @@ export function getEventPropertyValues(siteId: string, eventName: string, propNa
|
||||
|
||||
// ─── Frustration Signals ────────────────────────────────────────────
|
||||
|
||||
export interface BehaviorData {
|
||||
summary: FrustrationSummary
|
||||
rage_clicks: { items: FrustrationElement[]; total: number }
|
||||
dead_clicks: { items: FrustrationElement[]; total: number }
|
||||
by_page: FrustrationByPage[]
|
||||
}
|
||||
|
||||
const emptyBehavior: BehaviorData = {
|
||||
summary: { rage_clicks: 0, rage_unique_elements: 0, rage_top_page: '', dead_clicks: 0, dead_unique_elements: 0, dead_top_page: '', prev_rage_clicks: 0, prev_dead_clicks: 0 },
|
||||
rage_clicks: { items: [], total: 0 },
|
||||
dead_clicks: { items: [], total: 0 },
|
||||
by_page: [],
|
||||
}
|
||||
|
||||
export function getBehavior(siteId: string, startDate?: string, endDate?: string, limit = 7): Promise<BehaviorData> {
|
||||
return apiRequest<BehaviorData>(`/sites/${siteId}/behavior${buildQuery({ startDate, endDate, limit })}`)
|
||||
.then(r => r ?? emptyBehavior)
|
||||
}
|
||||
|
||||
export function getFrustrationSummary(siteId: string, startDate?: string, endDate?: string): Promise<FrustrationSummary> {
|
||||
return apiRequest<FrustrationSummary>(`/sites/${siteId}/frustration/summary${buildQuery({ startDate, endDate })}`)
|
||||
.then(r => r ?? { rage_clicks: 0, rage_unique_elements: 0, rage_top_page: '', dead_clicks: 0, dead_unique_elements: 0, dead_top_page: '', prev_rage_clicks: 0, prev_dead_clicks: 0 })
|
||||
|
||||
@@ -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