diff --git a/app/src/components/VideoPlayer.js b/app/src/components/VideoPlayer.js index 1903ccd9..e75642cf 100644 --- a/app/src/components/VideoPlayer.js +++ b/app/src/components/VideoPlayer.js @@ -1,7 +1,7 @@ import React, {useEffect, useState} from 'react'; import {useHotkeys} from 'react-hotkeys-hook'; import {deleteFileGroups, downloadVideoMetadata, tagFileGroup, untagFileGroup} from "../api"; -import {Link, useNavigate, useParams} from "react-router"; +import {Link, useNavigate} from "react-router"; import _ from "lodash"; import { APIButton, @@ -19,7 +19,7 @@ import { useTitle } from "./Common"; import {VideoPlaceholder} from "./Placeholder"; -import {useChannel, useOneQuery, useVideoCaptions, useVideoExtras} from "../hooks/customHooks"; +import {useOneQuery, useVideoCaptions, useVideoExtras} from "../hooks/customHooks"; import {useMediaProgress} from "../hooks/useFileProgress"; import {Button, Grid, Header, Icon, Label, Loading, Panel, Placeholder, Tabs, TextInput} from "./ui"; import {VideoCard} from "./Videos"; @@ -242,9 +242,9 @@ function VideoPage({videoFile, prevFile, nextFile, fetchVideo, ...props}) { videoRef.current.currentTime = seconds; } - // Get the Video's channel, fallback to the URL's channel id. - const {channelId} = useParams(); - const {channel} = useChannel(video && video.channel_id ? video.channel_id : channelId); + // The video response carries its Channel's id and name. Never fetch the whole Channel here, it computes + // the Channel's statistics, which is slow on a large Channel. + const channel = video?.channel || null; const {comments, description: videoDescription, fetchDescription} = useVideoExtras(videoFile?.id); const {captions, captionsLoading, fetchCaptions} = useVideoCaptions(videoFile?.id); diff --git a/app/src/components/VideoPlayerChannel.test.js b/app/src/components/VideoPlayerChannel.test.js new file mode 100644 index 00000000..6fa6d4e5 --- /dev/null +++ b/app/src/components/VideoPlayerChannel.test.js @@ -0,0 +1,60 @@ +import React from 'react'; +import {render, screen, waitFor} from '../test-utils'; +import VideoPage from './VideoPlayer'; +import {getChannel} from '../api'; + +// The video response carries the Video's Channel (id and name); fetching the whole Channel only to +// show its name costs a statistics query that takes seconds on a large, cold Channel. + +jest.mock('../api', () => { + const actual = jest.requireActual('../api'); + return { + ...actual, + getChannel: jest.fn(), + getVideoComments: jest.fn().mockResolvedValue([]), + getVideoDescription: jest.fn().mockResolvedValue(''), + getVideoCaptions: jest.fn().mockResolvedValue([]), + }; +}); + +const videoFile = (channel) => ({ + id: 3, + name: 'video.mp4', + title: 'A Video', + primary_path: 'channel/video.mp4', + files: [{path: 'channel/video.mp4', mimetype: 'video/mp4'}], + tags: [], + video: { + id: 3, + channel, + channel_id: channel ? channel.id : null, + video_path: 'channel/video.mp4', + caption_files: [], + codec_names: [], + codec_types: [], + }, +}); + +describe('VideoPage channel', () => { + beforeEach(() => getChannel.mockReset()); + + test("shows the Video's Channel without fetching the Channel", async () => { + render( null}/>); + + const link = await screen.findByRole('link', {name: 'Some Channel'}); + expect(link).toHaveAttribute('href', '/videos/channel/5/video'); + // Let any effects that would fetch settle. + await waitFor(() => expect(screen.getByText('A Video')).toBeInTheDocument()); + expect(getChannel).not.toHaveBeenCalled(); + }); + + test('a Video without a Channel shows no Channel link', async () => { + render( null}/>); + + await waitFor(() => expect(screen.getByText('A Video')).toBeInTheDocument()); + const channelLinks = screen.queryAllByRole('link') + .filter(i => (i.getAttribute('href') || '').startsWith('/videos/channel/')); + expect(channelLinks).toHaveLength(0); + expect(getChannel).not.toHaveBeenCalled(); + }); +});