1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Fix complexity warnings in search results

This commit is contained in:
Bill Thornton 2023-04-26 13:13:37 -04:00
parent 9c128b8c10
commit 0e0a30a067

View file

@ -1,7 +1,7 @@
import type { BaseItemDto } from '@jellyfin/sdk/lib/generated-client'; import type { BaseItemDto, BaseItemDtoQueryResult } from '@jellyfin/sdk/lib/generated-client';
import type { ApiClient } from 'jellyfin-apiclient'; import type { ApiClient } from 'jellyfin-apiclient';
import classNames from 'classnames'; import classNames from 'classnames';
import React, { FunctionComponent, useEffect, useState } from 'react'; import React, { FunctionComponent, useCallback, useEffect, useState } from 'react';
import globalize from '../../scripts/globalize'; import globalize from '../../scripts/globalize';
import ServerConnections from '../ServerConnections'; import ServerConnections from '../ServerConnections';
@ -14,6 +14,17 @@ type SearchResultsProps = {
query?: string; query?: string;
} }
const ensureNonNullItems = (result: BaseItemDtoQueryResult) => ({
...result,
Items: result.Items || []
});
const isMovies = (collectionType: string) => collectionType === 'movies';
const isMusic = (collectionType: string) => collectionType === 'music';
const isTVShows = (collectionType: string) => collectionType === 'tvshows' || collectionType === 'tv';
/* /*
* React component to display search result rows for global search and non-live tv library search * React component to display search result rows for global search and non-live tv library search
*/ */
@ -35,8 +46,7 @@ const SearchResults: FunctionComponent<SearchResultsProps> = ({ serverId = windo
const [ people, setPeople ] = useState<BaseItemDto[]>([]); const [ people, setPeople ] = useState<BaseItemDto[]>([]);
const [ collections, setCollections ] = useState<BaseItemDto[]>([]); const [ collections, setCollections ] = useState<BaseItemDto[]>([]);
useEffect(() => { const getDefaultParameters = useCallback(() => ({
const getDefaultParameters = () => ({
ParentId: parentId, ParentId: parentId,
searchTerm: query, searchTerm: query,
Limit: 24, Limit: 24,
@ -49,41 +59,42 @@ const SearchResults: FunctionComponent<SearchResultsProps> = ({ serverId = windo
IncludeGenres: false, IncludeGenres: false,
IncludeStudios: false, IncludeStudios: false,
IncludeArtists: false IncludeArtists: false
}); }), [parentId, query]);
const fetchArtists = (apiClient: ApiClient, params = {}) => apiClient?.getArtists( const fetchArtists = useCallback((apiClient: ApiClient, params = {}) => (
apiClient?.getCurrentUserId(), apiClient?.getArtists(
apiClient.getCurrentUserId(),
{ {
...getDefaultParameters(), ...getDefaultParameters(),
IncludeArtists: true, IncludeArtists: true,
...params ...params
} }
); ).then(ensureNonNullItems)
), [getDefaultParameters]);
const fetchItems = (apiClient: ApiClient, params = {}) => apiClient?.getItems( const fetchItems = useCallback((apiClient: ApiClient, params = {}) => (
apiClient?.getCurrentUserId(), apiClient?.getItems(
apiClient.getCurrentUserId(),
{ {
...getDefaultParameters(), ...getDefaultParameters(),
IncludeMedia: true, IncludeMedia: true,
...params ...params
} }
); ).then(ensureNonNullItems)
), [getDefaultParameters]);
const fetchPeople = (apiClient: ApiClient, params = {}) => apiClient?.getPeople( const fetchPeople = useCallback((apiClient: ApiClient, params = {}) => (
apiClient?.getCurrentUserId(), apiClient?.getPeople(
apiClient.getCurrentUserId(),
{ {
...getDefaultParameters(), ...getDefaultParameters(),
IncludePeople: true, IncludePeople: true,
...params ...params
} }
); ).then(ensureNonNullItems)
), [getDefaultParameters]);
const isMovies = () => collectionType === 'movies';
const isMusic = () => collectionType === 'music';
const isTVShows = () => collectionType === 'tvshows' || collectionType === 'tv';
useEffect(() => {
// Reset state // Reset state
setMovies([]); setMovies([]);
setShows([]); setShows([]);
@ -102,45 +113,48 @@ const SearchResults: FunctionComponent<SearchResultsProps> = ({ serverId = windo
setPeople([]); setPeople([]);
setCollections([]); setCollections([]);
if (query) { if (!query) {
return;
}
const apiClient = ServerConnections.getApiClient(serverId); const apiClient = ServerConnections.getApiClient(serverId);
// Movie libraries // Movie libraries
if (!collectionType || isMovies()) { if (!collectionType || isMovies(collectionType)) {
// Movies row // Movies row
fetchItems(apiClient, { IncludeItemTypes: 'Movie' }) fetchItems(apiClient, { IncludeItemTypes: 'Movie' })
.then(result => setMovies(result.Items || [])); .then(result => setMovies(result.Items));
} }
// TV Show libraries // TV Show libraries
if (!collectionType || isTVShows()) { if (!collectionType || isTVShows(collectionType)) {
// Shows row // Shows row
fetchItems(apiClient, { IncludeItemTypes: 'Series' }) fetchItems(apiClient, { IncludeItemTypes: 'Series' })
.then(result => setShows(result.Items || [])); .then(result => setShows(result.Items));
// Episodes row // Episodes row
fetchItems(apiClient, { IncludeItemTypes: 'Episode' }) fetchItems(apiClient, { IncludeItemTypes: 'Episode' })
.then(result => setEpisodes(result.Items || [])); .then(result => setEpisodes(result.Items));
} }
// People are included for Movies and TV Shows // People are included for Movies and TV Shows
if (!collectionType || isMovies() || isTVShows()) { if (!collectionType || isMovies(collectionType) || isTVShows(collectionType)) {
// People row // People row
fetchPeople(apiClient).then(result => setPeople(result.Items || [])); fetchPeople(apiClient).then(result => setPeople(result.Items));
} }
// Music libraries // Music libraries
if (!collectionType || isMusic()) { if (!collectionType || isMusic(collectionType)) {
// Playlists row // Playlists row
fetchItems(apiClient, { IncludeItemTypes: 'Playlist' }) fetchItems(apiClient, { IncludeItemTypes: 'Playlist' })
.then(results => setPlaylists(results.Items || [])); .then(results => setPlaylists(results.Items));
// Artists row // Artists row
fetchArtists(apiClient).then(result => setArtists(result.Items || [])); fetchArtists(apiClient).then(result => setArtists(result.Items));
// Albums row // Albums row
fetchItems(apiClient, { IncludeItemTypes: 'MusicAlbum' }) fetchItems(apiClient, { IncludeItemTypes: 'MusicAlbum' })
.then(result => setAlbums(result.Items || [])); .then(result => setAlbums(result.Items));
// Songs row // Songs row
fetchItems(apiClient, { IncludeItemTypes: 'Audio' }) fetchItems(apiClient, { IncludeItemTypes: 'Audio' })
.then(result => setSongs(result.Items || [])); .then(result => setSongs(result.Items));
} }
// Other libraries do not support in-library search currently // Other libraries do not support in-library search currently
@ -149,31 +163,30 @@ const SearchResults: FunctionComponent<SearchResultsProps> = ({ serverId = windo
fetchItems(apiClient, { fetchItems(apiClient, {
MediaTypes: 'Video', MediaTypes: 'Video',
ExcludeItemTypes: 'Movie,Episode,TvChannel' ExcludeItemTypes: 'Movie,Episode,TvChannel'
}).then(result => setVideos(result.Items || [])); }).then(result => setVideos(result.Items));
// Programs row // Programs row
fetchItems(apiClient, { IncludeItemTypes: 'LiveTvProgram' }) fetchItems(apiClient, { IncludeItemTypes: 'LiveTvProgram' })
.then(result => setPrograms(result.Items || [])); .then(result => setPrograms(result.Items));
// Channels row // Channels row
fetchItems(apiClient, { IncludeItemTypes: 'TvChannel' }) fetchItems(apiClient, { IncludeItemTypes: 'TvChannel' })
.then(result => setChannels(result.Items || [])); .then(result => setChannels(result.Items));
// Photo Albums row // Photo Albums row
fetchItems(apiClient, { IncludeItemTypes: 'PhotoAlbum' }) fetchItems(apiClient, { IncludeItemTypes: 'PhotoAlbum' })
.then(results => setPhotoAlbums(results.Items || [])); .then(result => setPhotoAlbums(result.Items));
// Photos row // Photos row
fetchItems(apiClient, { IncludeItemTypes: 'Photo' }) fetchItems(apiClient, { IncludeItemTypes: 'Photo' })
.then(results => setPhotos(results.Items || [])); .then(result => setPhotos(result.Items));
// Audio Books row // Audio Books row
fetchItems(apiClient, { IncludeItemTypes: 'AudioBook' }) fetchItems(apiClient, { IncludeItemTypes: 'AudioBook' })
.then(results => setAudioBooks(results.Items || [])); .then(result => setAudioBooks(result.Items));
// Books row // Books row
fetchItems(apiClient, { IncludeItemTypes: 'Book' }) fetchItems(apiClient, { IncludeItemTypes: 'Book' })
.then(results => setBooks(results.Items || [])); .then(result => setBooks(result.Items));
// Collections row // Collections row
fetchItems(apiClient, { IncludeItemTypes: 'BoxSet' }) fetchItems(apiClient, { IncludeItemTypes: 'BoxSet' })
.then(result => setCollections(result.Items || [])); .then(result => setCollections(result.Items));
} }
} }, [collectionType, fetchArtists, fetchItems, fetchPeople, query, serverId]);
}, [collectionType, parentId, query, serverId]);
return ( return (
<div <div