diff --git a/client/src/pages/home.tsx b/client/src/pages/home.tsx index af9c65e..f3a5246 100644 --- a/client/src/pages/home.tsx +++ b/client/src/pages/home.tsx @@ -44,6 +44,7 @@ export default function Home() { // Update videos when new data comes in useEffect(() => { if (videosResponse) { + console.log('Videos response:', videosResponse); if (offset === 0) { setAllVideos(videosResponse.videos); } else { diff --git a/server/storage.ts b/server/storage.ts index 1472709..a0c1499 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -177,13 +177,15 @@ class BunnyStorage implements IStorage { async getVideos(limit = 20, offset = 0, search?: string): Promise { try { - // For search filtering, we need to get more videos than the requested limit - // because we'll filter client-side and then slice to the requested amount - const fetchLimit = search ? 100 : limit; - const page = Math.floor(offset / fetchLimit) + 1; + console.log(`Fetching videos: limit=${limit}, offset=${offset}, search=${search}`); - // Get videos from Bunny API - const { videos } = await this.bunnyService.getVideos(page, fetchLimit); + // For simple pagination, get a larger batch and slice on our side + // This is more reliable than complex page calculations + const batchSize = 100; // Get more videos to handle pagination properly + const page = Math.floor(offset / batchSize) + 1; + + const { videos } = await this.bunnyService.getVideos(page, batchSize); + console.log(`Bunny API returned ${videos.length} videos`); // Apply client-side filtering let filteredVideos = videos; @@ -195,6 +197,7 @@ class BunnyStorage implements IStorage { video.title.toLowerCase().includes(searchLower) || (video.description && video.description.toLowerCase().includes(searchLower)) ); + console.log(`After search filtering: ${filteredVideos.length} videos`); } // Apply cached view counts @@ -204,8 +207,14 @@ class BunnyStorage implements IStorage { } }); - // Apply pagination after filtering - return filteredVideos.slice(offset, offset + limit); + // Simple offset/limit slicing + const startIndex = offset % batchSize; + const endIndex = startIndex + limit; + const result = filteredVideos.slice(startIndex, endIndex); + + console.log(`Returning ${result.length} videos (slice ${startIndex}-${endIndex} from ${filteredVideos.length})`); + + return result; } catch (error) { console.error('Error fetching videos from Bunny:', error); // Fallback to empty array on error @@ -250,9 +259,24 @@ class BunnyStorage implements IStorage { async getVideoCount(search?: string): Promise { try { - // For accurate count with client-side filtering, we need to get all videos and filter them - const allVideos = await this.getVideos(1000, 0, search); - return allVideos.length; + // Get the total from Bunny API directly for better performance + const { total } = await this.bunnyService.getVideos(1, 1); + + // If no search, return total count + if (!search) { + return total; + } + + // For search, we need to get videos and count filtered results + // This is expensive but needed for accurate search counts + const { videos } = await this.bunnyService.getVideos(1, 1000); + const searchLower = search.toLowerCase(); + const filteredCount = videos.filter(video => + video.title.toLowerCase().includes(searchLower) || + (video.description && video.description.toLowerCase().includes(searchLower)) + ).length; + + return filteredCount; } catch (error) { console.error('Error getting video count from Bunny:', error); return 0;