1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/apps/stable/features/search/api/useSearchItems.ts

85 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-06-11 00:23:57 +03:00
import type {
BaseItemDto,
BaseItemKind
2024-06-11 00:23:57 +03:00
} from '@jellyfin/sdk/lib/generated-client';
import { CollectionType } from '@jellyfin/sdk/lib/generated-client/models/collection-type';
import { useQuery } from '@tanstack/react-query';
import { useApi } from '../../../../../hooks/useApi';
import { addSection, getCardOptionsFromType, getItemTypesFromCollectionType, getTitleFromType, isLivetv, isMovies, isMusic, isTVShows, sortSections } from '../utils/search';
import { useArtistsSearch } from './useArtistsSearch';
import { usePeopleSearch } from './usePeopleSearch';
import { useVideoSearch } from './useVideoSearch';
import { Section } from '../types';
import { useLiveTvSearch } from './useLiveTvSearch';
import { fetchItemsByType } from './fetchItemsByType';
2024-06-11 00:23:57 +03:00
export const useSearchItems = (
parentId?: string,
collectionType?: CollectionType,
2024-06-11 00:23:57 +03:00
searchTerm?: string
) => {
const { data: artists, isPending: isArtistsPending } = useArtistsSearch(parentId, collectionType, searchTerm);
const { data: people, isPending: isPeoplePending } = usePeopleSearch(parentId, collectionType, searchTerm);
const { data: videos, isPending: isVideosPending } = useVideoSearch(parentId, collectionType, searchTerm);
const { data: liveTvSections, isPending: isLiveTvPending } = useLiveTvSearch(parentId, collectionType, searchTerm);
2024-06-11 00:23:57 +03:00
const { api, user } = useApi();
const userId = user?.Id;
const isArtistsEnabled = !isArtistsPending || (collectionType && !isMusic(collectionType));
const isPeopleEnabled = !isPeoplePending || (collectionType && !isMovies(collectionType) && !isTVShows(collectionType));
const isVideosEnabled = !isVideosPending || collectionType;
const isLiveTvEnabled = !isLiveTvPending || !collectionType || !isLivetv(collectionType);
2024-06-11 00:23:57 +03:00
return useQuery({
queryKey: ['SearchItems', collectionType, parentId, searchTerm],
2024-06-11 00:23:57 +03:00
queryFn: async ({ signal }) => {
const sections: Section[] = [];
addSection(sections, 'Artists', artists?.Items, {
coverImage: true
});
2024-06-11 00:23:57 +03:00
addSection(sections, 'People', people?.Items, {
coverImage: true
});
2024-06-11 00:23:57 +03:00
addSection(sections, 'HeaderVideos', videos?.Items, {
showParentTitle: true
});
2024-06-11 00:23:57 +03:00
if (liveTvSections) {
sections.push(...liveTvSections);
2024-06-11 00:23:57 +03:00
}
const itemTypes: BaseItemKind[] = getItemTypesFromCollectionType(collectionType);
const searchData = await fetchItemsByType(
api!,
userId,
{
includeItemTypes: itemTypes,
parentId: parentId,
searchTerm: searchTerm,
limit: 800
},
{ signal }
);
if (searchData.Items) {
for (const itemType of itemTypes) {
const items: BaseItemDto[] = [];
for (const searchItem of searchData.Items) {
if (searchItem.Type === itemType) {
items.push(searchItem);
}
}
addSection(sections, getTitleFromType(itemType), items, getCardOptionsFromType(itemType));
}
2024-06-11 00:23:57 +03:00
}
return sortSections(sections);
2024-06-11 00:23:57 +03:00
},
enabled: !!api && !!userId && !!isArtistsEnabled && !!isPeopleEnabled && !!isVideosEnabled && !!isLiveTvEnabled
2024-06-11 00:23:57 +03:00
});
};