2025-03-06 18:53:18 +03:00
|
|
|
import { BaseItemKind } from '@jellyfin/sdk/lib/generated-client/models/base-item-kind';
|
|
|
|
import type { BaseItemDto } from '@jellyfin/sdk/lib/generated-client/models/base-item-dto';
|
2024-06-11 00:23:57 +03:00
|
|
|
import { CollectionType } from '@jellyfin/sdk/lib/generated-client/models/collection-type';
|
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2025-02-27 15:53:20 +03:00
|
|
|
import { useApi } from '../../../../../hooks/useApi';
|
2025-03-03 18:18:39 +03:00
|
|
|
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,
|
2025-03-03 18:18:39 +03:00
|
|
|
collectionType?: CollectionType,
|
2024-06-11 00:23:57 +03:00
|
|
|
searchTerm?: string
|
|
|
|
) => {
|
2025-03-03 18:18:39 +03:00
|
|
|
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;
|
|
|
|
|
2025-03-03 18:18:39 +03:00
|
|
|
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({
|
2025-03-07 12:29:04 +03:00
|
|
|
queryKey: ['Search', 'Items', collectionType, parentId, searchTerm],
|
2024-06-11 00:23:57 +03:00
|
|
|
queryFn: async ({ signal }) => {
|
2025-03-06 13:14:35 +03:00
|
|
|
if (liveTvSections && collectionType && isLivetv(collectionType)) {
|
2025-03-06 10:44:03 +03:00
|
|
|
return sortSections(liveTvSections);
|
|
|
|
}
|
|
|
|
|
2024-06-11 00:23:57 +03:00
|
|
|
const sections: Section[] = [];
|
|
|
|
|
2025-03-03 18:18:39 +03:00
|
|
|
addSection(sections, 'Artists', artists?.Items, {
|
|
|
|
coverImage: true
|
|
|
|
});
|
2024-06-11 00:23:57 +03:00
|
|
|
|
2025-03-03 18:18:39 +03:00
|
|
|
addSection(sections, 'People', people?.Items, {
|
|
|
|
coverImage: true
|
|
|
|
});
|
2024-06-11 00:23:57 +03:00
|
|
|
|
2025-03-03 18:18:39 +03:00
|
|
|
addSection(sections, 'HeaderVideos', videos?.Items, {
|
|
|
|
showParentTitle: true
|
|
|
|
});
|
2024-06-11 00:23:57 +03:00
|
|
|
|
2025-03-03 18:18:39 +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
|
|
|
}
|
|
|
|
|
2025-03-03 18:18:39 +03:00
|
|
|
return sortSections(sections);
|
2024-06-11 00:23:57 +03:00
|
|
|
},
|
2025-03-03 18:18:39 +03:00
|
|
|
enabled: !!api && !!userId && !!isArtistsEnabled && !!isPeopleEnabled && !!isVideosEnabled && !!isLiveTvEnabled
|
2024-06-11 00:23:57 +03:00
|
|
|
});
|
|
|
|
};
|