@@ -8,6 +8,7 @@ import { useMemo, useRef, useState } from "react";
88import { toast } from "sonner" ;
99import { useDashboardContext } from "@/app/(org)/dashboard/Contexts" ;
1010import { useEffectMutation } from "@/lib/EffectRuntime" ;
11+ import { useVideosAnalyticsQuery } from "@/lib/Requests/AnalyticsRequest" ;
1112import { Rpc , withRpc } from "@/lib/Rpcs" ;
1213import type { VideoData } from "../../../caps/Caps" ;
1314import { CapCard } from "../../../caps/components/CapCard/CapCard" ;
@@ -108,49 +109,10 @@ export default function FolderVideosSection({
108109 } ) ;
109110 } ;
110111
111- const { data : analyticsData , isLoading : isLoadingAnalytics } = useQuery ( {
112- queryKey : [ "analytics" , initialVideos . map ( ( video ) => video . id ) ] ,
113- queryFn : async ( ) => {
114- if ( ! dubApiKeyEnabled || initialVideos . length === 0 ) {
115- return { } ;
116- }
117-
118- const analyticsPromises = initialVideos . map ( async ( video ) => {
119- try {
120- const response = await fetch ( `/api/analytics?videoId=${ video . id } ` , {
121- method : "GET" ,
122- headers : {
123- "Content-Type" : "application/json" ,
124- } ,
125- } ) ;
126-
127- if ( response . ok ) {
128- const responseData = await response . json ( ) ;
129- return { videoId : video . id , count : responseData . count || 0 } ;
130- }
131- return { videoId : video . id , count : 0 } ;
132- } catch ( error ) {
133- console . warn (
134- `Failed to fetch analytics for video ${ video . id } :` ,
135- error ,
136- ) ;
137- return { videoId : video . id , count : 0 } ;
138- }
139- } ) ;
140-
141- const results = await Promise . allSettled ( analyticsPromises ) ;
142- const analyticsData : Record < string , number > = { } ;
143-
144- results . forEach ( ( result ) => {
145- if ( result . status === "fulfilled" && result . value ) {
146- analyticsData [ result . value . videoId ] = result . value . count ;
147- }
148- } ) ;
149- return analyticsData ;
150- } ,
151- refetchOnWindowFocus : false ,
152- refetchOnMount : true ,
153- } ) ;
112+ const analyticsQuery = useVideosAnalyticsQuery (
113+ initialVideos . map ( ( video ) => video . id ) ,
114+ dubApiKeyEnabled ,
115+ ) ;
154116
155117 const [ isUploading , uploadingCapId ] = useUploadingStatus ( ) ;
156118 const visibleVideos = useMemo (
@@ -161,7 +123,7 @@ export default function FolderVideosSection({
161123 [ initialVideos , isUploading , uploadingCapId ] ,
162124 ) ;
163125
164- const analytics = analyticsData || { } ;
126+ const analytics = analyticsQuery . data || { } ;
165127
166128 return (
167129 < >
@@ -185,7 +147,7 @@ export default function FolderVideosSection({
185147 cap = { video }
186148 analytics = { analytics [ video . id ] || 0 }
187149 userId = { user ?. id }
188- isLoadingAnalytics = { isLoadingAnalytics }
150+ isLoadingAnalytics = { analyticsQuery . isLoading }
189151 isSelected = { selectedCaps . includes ( video . id ) }
190152 anyCapSelected = { selectedCaps . length > 0 }
191153 isDeleting = { isDeletingCaps || isDeletingCap }
0 commit comments