11import type { DateRange } from '@databuddy/shared' ;
2- import {
3- useMutation ,
4- useQueries ,
5- useQuery ,
6- useQueryClient ,
7- } from '@tanstack/react-query' ;
8- import { useCallback , useMemo } from 'react' ;
2+ import { useQueries , useQueryClient } from '@tanstack/react-query' ;
3+ import { useMemo } from 'react' ;
94import { trpc } from '@/lib/trpc' ;
105
11- // Types
126export interface FunnelStep {
137 type : 'PAGE_VIEW' | 'EVENT' | 'CUSTOM' ;
148 target : string ;
159 name : string ;
16- conditions ?: Record < string , any > ;
10+ conditions ?: Record < string , unknown > ;
1711}
1812
1913export interface FunnelFilter {
@@ -64,7 +58,6 @@ export interface CreateFunnelData {
6458 filters ?: FunnelFilter [ ] ;
6559}
6660
67- // Simple autocomplete data types
6861export interface AutocompleteData {
6962 customEvents : string [ ] ;
7063 pagePaths : string [ ] ;
@@ -88,7 +81,6 @@ export interface FunnelAnalyticsByReferrerResult {
8881 conversion_rate : number ;
8982}
9083
91- // Hook for managing funnels (CRUD operations)
9284export function useFunnels ( websiteId : string , enabled = true ) {
9385 const queryClient = useQueryClient ( ) ;
9486
@@ -107,14 +99,12 @@ export function useFunnels(websiteId: string, enabled = true) {
10799 [ query . data ]
108100 ) ;
109101
110- // Create funnel mutation
111102 const createMutation = trpc . funnels . create . useMutation ( {
112103 onSuccess : ( ) => {
113104 queryClient . invalidateQueries ( { queryKey : [ [ 'funnels' , 'list' ] ] } ) ;
114105 } ,
115106 } ) ;
116107
117- // Update funnel mutation
118108 const updateMutation = trpc . funnels . update . useMutation ( {
119109 onSuccess : ( ) => {
120110 queryClient . invalidateQueries ( { queryKey : [ [ 'funnels' , 'list' ] ] } ) ;
@@ -124,7 +114,6 @@ export function useFunnels(websiteId: string, enabled = true) {
124114 } ,
125115 } ) ;
126116
127- // Delete funnel mutation
128117 const deleteMutation = trpc . funnels . delete . useMutation ( {
129118 onSuccess : ( ) => {
130119 queryClient . invalidateQueries ( { queryKey : [ [ 'funnels' , 'list' ] ] } ) ;
@@ -140,14 +129,13 @@ export function useFunnels(websiteId: string, enabled = true) {
140129 error : query . error ,
141130 refetch : query . refetch ,
142131
143- // Mutations
144- createFunnel : async ( funnelData : CreateFunnelData ) => {
132+ createFunnel : ( funnelData : CreateFunnelData ) => {
145133 return createMutation . mutateAsync ( {
146134 websiteId,
147135 ...funnelData ,
148136 } ) ;
149137 } ,
150- updateFunnel : async ( {
138+ updateFunnel : ( {
151139 funnelId,
152140 updates,
153141 } : {
@@ -159,11 +147,10 @@ export function useFunnels(websiteId: string, enabled = true) {
159147 ...updates ,
160148 } ) ;
161149 } ,
162- deleteFunnel : async ( funnelId : string ) => {
150+ deleteFunnel : ( funnelId : string ) => {
163151 return deleteMutation . mutateAsync ( { id : funnelId } ) ;
164152 } ,
165153
166- // Mutation states
167154 isCreating : createMutation . isPending ,
168155 isUpdating : updateMutation . isPending ,
169156 isDeleting : deleteMutation . isPending ,
@@ -174,15 +161,13 @@ export function useFunnels(websiteId: string, enabled = true) {
174161 } ;
175162}
176163
177- // Hook for single funnel details
178164export function useFunnel ( websiteId : string , funnelId : string , enabled = true ) {
179165 return trpc . funnels . getById . useQuery (
180166 { id : funnelId , websiteId } ,
181167 { enabled : enabled && ! ! websiteId && ! ! funnelId }
182168 ) ;
183169}
184170
185- // Hook for funnel analytics data
186171export function useFunnelAnalytics (
187172 websiteId : string ,
188173 funnelId : string ,
@@ -200,7 +185,6 @@ export function useFunnelAnalytics(
200185 ) ;
201186}
202187
203- // Hook for funnel analytics grouped by referrer
204188export function useFunnelAnalyticsByReferrer (
205189 websiteId : string ,
206190 funnelId : string ,
@@ -218,22 +202,18 @@ export function useFunnelAnalyticsByReferrer(
218202 ) ;
219203}
220204
221- // Hook for comprehensive funnel analytics using direct endpoints
222205export function useEnhancedFunnelAnalytics (
223206 websiteId : string ,
224207 funnelId : string ,
225208 dateRange : DateRange ,
226209 enabled = true
227210) {
228- // Get funnel definition
229211 const funnelQuery = useFunnel ( websiteId , funnelId , enabled ) ;
230212
231- // Get analytics data using direct endpoint
232213 const analyticsQuery = useFunnelAnalytics ( websiteId , funnelId , dateRange , {
233214 enabled,
234215 } ) ;
235216
236- // Process and structure the enhanced data
237217 const enhancedData = useMemo ( ( ) => {
238218 const analytics = analyticsQuery . data ;
239219
@@ -260,7 +240,6 @@ export function useEnhancedFunnelAnalytics(
260240 } ;
261241 } , [ analyticsQuery . data ] ) ;
262242
263- // Calculate loading and error states
264243 const isLoading = funnelQuery . isLoading || analyticsQuery . isLoading ;
265244 const error = funnelQuery . error || analyticsQuery . error ;
266245
@@ -275,7 +254,7 @@ export function useEnhancedFunnelAnalytics(
275254 } ,
276255 } ;
277256}
278- // Hook for funnel comparison
257+
279258export function useFunnelComparison (
280259 websiteId : string ,
281260 funnelIds : string [ ] ,
@@ -314,7 +293,6 @@ export function useFunnelComparison(
314293 } ;
315294}
316295
317- // Hook to get funnel performance metrics (for overview cards)
318296export function useFunnelPerformance (
319297 websiteId : string ,
320298 dateRange : DateRange ,
@@ -346,7 +324,9 @@ export function useFunnelPerformance(
346324 const performanceData = useMemo ( ( ) => {
347325 return results
348326 . map ( ( result , index ) => {
349- if ( ! result . data ) return null ;
327+ if ( ! result . data ) {
328+ return null ;
329+ }
350330 return {
351331 funnelId : funnels [ index ] . id ,
352332 funnelName : funnels [ index ] . name ,
0 commit comments