Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/components/VideoFeed.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
mockSetVideosForFullscreen,
mockUpdateVideos,
mockUseVideoPrefetch,
mockInfiniteScroll,
mockTrackEvent,
} = vi.hoisted(() => ({
mockTrackEvent: vi.fn(),
Expand All @@ -22,6 +23,7 @@ const {
mockSetVideosForFullscreen: vi.fn(),
mockUpdateVideos: vi.fn(),
mockUseVideoPrefetch: vi.fn(),
mockInfiniteScroll: vi.fn(),
}));

vi.mock('@/hooks/useVideoProvider', () => ({
Expand Down Expand Up @@ -111,7 +113,10 @@ vi.mock('@/components/ui/card', () => ({
}));

vi.mock('react-infinite-scroll-component', () => ({
default: ({ children }: { children: ReactNode }) => <div>{children}</div>,
default: ({ children, dataLength }: { children: ReactNode; dataLength: number }) => {
mockInfiniteScroll({ dataLength });
return <div>{children}</div>;
},
}));

describe('VideoFeed', () => {
Expand Down Expand Up @@ -143,6 +148,7 @@ describe('VideoFeed', () => {
isLoading: false,
error: null,
refetch: vi.fn(),
fetchedCount: 1,
dataSource: 'funnelcake',
});
});
Expand Down Expand Up @@ -205,6 +211,7 @@ describe('VideoFeed', () => {
isLoading: false,
error: null,
refetch: vi.fn(),
fetchedCount: 1,
dataSource: 'funnelcake',
});

Expand All @@ -227,6 +234,7 @@ describe('VideoFeed', () => {
isLoading: false,
error: null,
refetch: vi.fn(),
fetchedCount: 3,
dataSource: 'funnelcake',
});

Expand All @@ -252,4 +260,45 @@ describe('VideoFeed', () => {
{ prefetchVideos: false },
);
});

it('keys infinite scroll from fetched count instead of filtered rendered count', () => {
mockUseVideoProvider.mockReturnValue({
data: {
pages: [{
videos: [
{
id: 'video-1',
pubkey: 'a'.repeat(64),
kind: 34236,
vineId: 'same-address',
videoUrl: 'https://example.com/video-1.mp4',
},
{
id: 'video-2',
pubkey: 'a'.repeat(64),
kind: 34236,
vineId: 'same-address',
videoUrl: 'https://example.com/video-2.mp4',
},
],
}],
},
fetchNextPage: vi.fn(),
hasNextPage: true,
isLoading: false,
error: null,
refetch: vi.fn(),
fetchedCount: 2,
dataSource: 'funnelcake',
});

render(
<MemoryRouter initialEntries={['/discovery']}>
<VideoFeed feedType="discovery" />
</MemoryRouter>
);

expect(screen.getAllByTestId('video-card')).toHaveLength(1);
expect(mockInfiniteScroll).toHaveBeenCalledWith({ dataLength: 2 });
});
});
5 changes: 3 additions & 2 deletions src/components/VideoFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function VideoFeed({
isLoading,
error,
refetch,
fetchedCount,
dataSource,
} = useVideoProvider({
feedType,
Expand Down Expand Up @@ -478,7 +479,7 @@ export function VideoFeed({
>
{compilationLauncher}
<InfiniteScroll
dataLength={filteredVideos.length}
dataLength={fetchedCount}
next={fetchNextPage}
hasMore={hasNextPage ?? false}
loader={
Expand Down Expand Up @@ -532,7 +533,7 @@ export function VideoFeed({
>
{compilationLauncher}
<InfiniteScroll
dataLength={filteredVideos.length}
dataLength={fetchedCount}
next={fetchNextPage}
hasMore={hasNextPage ?? false}
loader={
Expand Down
9 changes: 8 additions & 1 deletion src/hooks/useInfiniteSearchVideos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function useInfiniteSearchVideos({
const debounceMs = isTest ? 0 : 300;
const debouncedQuery = useDebouncedValue(query, debounceMs);

return useInfiniteQuery<VideoPage, Error>({
const queryResult = useInfiniteQuery<VideoPage, Error>({
queryKey: ['infinite-search-videos', debouncedQuery, searchType, sortMode, pageSize],
queryFn: async ({ pageParam, signal }) => {
if (!debouncedQuery.trim()) {
Expand Down Expand Up @@ -311,4 +311,11 @@ export function useInfiniteSearchVideos({
staleTime: 60_000,
gcTime: 300_000,
});

const fetchedCount = queryResult.data?.pages.reduce((sum, page) => sum + page.videos.length, 0) ?? 0;

return {
...queryResult,
fetchedCount,
};
}
Loading
Loading