feat: add new library page with performance improvements
This commit is contained in:
29 files changed
+2020
-645
No files matched your search
@@ -173,14 +173,14 @@ export const SORT_BY_OPTIONS: Array<{
|
||||
title: "Random",
|
||||
value: ItemSortBy.Random,
|
||||
compatibleViewTypes: [
|
||||
BaseItemKind.MusicAlbum,
|
||||
BaseItemKind.Audio,
|
||||
BaseItemKind.Playlist,
|
||||
// BaseItemKind.MusicAlbum,
|
||||
// BaseItemKind.Audio,
|
||||
// BaseItemKind.Playlist,
|
||||
],
|
||||
compatibleCollectionTypes: [
|
||||
CollectionType.Movies,
|
||||
CollectionType.Tvshows,
|
||||
CollectionType.Boxsets,
|
||||
// CollectionType.Movies,
|
||||
// CollectionType.Tvshows,
|
||||
// CollectionType.Boxsets,
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -242,6 +242,18 @@ export const SORT_BY_OPTIONS: Array<{
|
||||
},
|
||||
];
|
||||
|
||||
export const getDefaultSortByForCollectionType = (
|
||||
collectionType: CollectionType,
|
||||
): ItemSortBy => {
|
||||
const option = SORT_BY_OPTIONS.find((opt) =>
|
||||
opt.compatibleCollectionTypes?.includes(collectionType),
|
||||
);
|
||||
return Array.isArray(option?.value)
|
||||
? option.value[0]
|
||||
: option?.value || ItemSortBy.SortName;
|
||||
};
|
||||
|
||||
|
||||
export type FILTERS =
|
||||
| "isPlayed"
|
||||
| "isUnPlayed"
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// Deprecated: navigation helpers for search-param based library state were removed.
|
||||
// This file will be removed in a future commit. No exports remain.
|
||||
|
||||
export {}; // keep as empty module
|
||||
@@ -23,4 +23,5 @@ export const getLibraryQueryOptions = (
|
||||
});
|
||||
return result.data;
|
||||
},
|
||||
enabled: !!api && !!userId,
|
||||
});
|
||||
@@ -0,0 +1,174 @@
|
||||
import type { Api } from "@jellyfin/sdk";
|
||||
import type {
|
||||
BaseItemDtoQueryResult,
|
||||
BaseItemKind,
|
||||
ItemSortBy,
|
||||
} from "@jellyfin/sdk/lib/generated-client";
|
||||
import {
|
||||
ItemFilter,
|
||||
SortOrder,
|
||||
VideoType,
|
||||
} from "@jellyfin/sdk/lib/generated-client";
|
||||
import { getArtistsApi } from "@jellyfin/sdk/lib/utils/api/artists-api";
|
||||
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
|
||||
import { getPersonsApi } from "@jellyfin/sdk/lib/utils/api/persons-api";
|
||||
import { getStudiosApi } from "@jellyfin/sdk/lib/utils/api/studios-api";
|
||||
import { queryOptions } from "@tanstack/react-query";
|
||||
|
||||
export interface LibraryItemsParams {
|
||||
currentViewType?:
|
||||
| BaseItemKind
|
||||
| "Artist"
|
||||
| "Person"
|
||||
| "Studio"
|
||||
| "MusicArtist"
|
||||
| "BoxSet";
|
||||
sortAscending: boolean;
|
||||
sortBy: string; // comma separated
|
||||
filters?: Record<string, boolean | undefined>;
|
||||
videoTypesState?: {
|
||||
BluRay?: boolean;
|
||||
Dvd?: boolean;
|
||||
Iso?: boolean;
|
||||
VideoFile?: boolean;
|
||||
};
|
||||
nameStartsWith?: string;
|
||||
genreIds?: string[];
|
||||
collectionType?: string; // from library metadata (e.g. boxsets)
|
||||
limit?: number; // optional (e.g. 0 for count-only)
|
||||
}
|
||||
|
||||
/** Build TanStack Query options for fetching library items with unified branching logic. */
|
||||
export const getLibraryItemsQueryOptions = (
|
||||
api: Api | null | undefined,
|
||||
userId: string | null | undefined,
|
||||
libraryId: string | null | undefined,
|
||||
params: LibraryItemsParams,
|
||||
) => {
|
||||
return queryOptions<BaseItemDtoQueryResult>({
|
||||
queryKey: [
|
||||
"library",
|
||||
"items",
|
||||
libraryId,
|
||||
{
|
||||
currentViewType: params.currentViewType,
|
||||
sortAscending: params.sortAscending,
|
||||
sortBy: params.sortBy,
|
||||
filters: params.filters,
|
||||
videoTypesState: params.videoTypesState,
|
||||
nameStartsWith: params.nameStartsWith,
|
||||
genreIds: params.genreIds,
|
||||
collectionType: params.collectionType,
|
||||
limit: params.limit,
|
||||
},
|
||||
],
|
||||
queryFn: async () => {
|
||||
if (!api || !userId || !libraryId || !params.currentViewType) {
|
||||
return { Items: [], TotalRecordCount: 0 } as BaseItemDtoQueryResult;
|
||||
}
|
||||
const { currentViewType, videoTypesState, filters } = params;
|
||||
const buildVideoTypes = (state: typeof videoTypesState) => {
|
||||
const videoTypes: VideoType[] = [];
|
||||
if (state?.BluRay) videoTypes.push(VideoType.BluRay);
|
||||
if (state?.Dvd) videoTypes.push(VideoType.Dvd);
|
||||
if (state?.Iso) videoTypes.push(VideoType.Iso);
|
||||
if (state?.VideoFile) videoTypes.push(VideoType.VideoFile);
|
||||
return videoTypes;
|
||||
};
|
||||
const buildFilters = (f: typeof filters) => {
|
||||
const filtersArray: ItemFilter[] = [];
|
||||
if (f?.isPlayed) filtersArray.push(ItemFilter.IsPlayed);
|
||||
if (f?.isUnPlayed) filtersArray.push(ItemFilter.IsUnplayed);
|
||||
if (f?.isResumable) filtersArray.push(ItemFilter.IsResumable);
|
||||
if (f?.isFavorite) filtersArray.push(ItemFilter.IsFavorite);
|
||||
return filtersArray;
|
||||
};
|
||||
let result: { data: BaseItemDtoQueryResult };
|
||||
if (currentViewType === "MusicArtist") {
|
||||
result = await getArtistsApi(api).getAlbumArtists({
|
||||
userId: userId,
|
||||
parentId: libraryId,
|
||||
limit: params.limit,
|
||||
});
|
||||
} else if (currentViewType === "Artist") {
|
||||
result = await getArtistsApi(api).getArtists({
|
||||
userId: userId,
|
||||
parentId: libraryId,
|
||||
limit: params.limit,
|
||||
});
|
||||
} else if (currentViewType === "Person") {
|
||||
result = await getPersonsApi(api).getPersons({
|
||||
userId: userId,
|
||||
personTypes: ["Actor"],
|
||||
limit: params.limit,
|
||||
});
|
||||
} else if (currentViewType === "Studio") {
|
||||
result = await getStudiosApi(api).getStudios({
|
||||
userId: userId,
|
||||
parentId: libraryId,
|
||||
limit: params.limit,
|
||||
});
|
||||
} else if (currentViewType === "BoxSet") {
|
||||
result = await getItemsApi(api).getItems({
|
||||
userId: userId,
|
||||
parentId: libraryId,
|
||||
recursive: params.collectionType === "boxsets" ? undefined : true,
|
||||
sortOrder: [
|
||||
params.sortAscending ? SortOrder.Ascending : SortOrder.Descending,
|
||||
],
|
||||
sortBy: params.sortBy.split(",") as ItemSortBy[],
|
||||
filters: buildFilters(filters),
|
||||
hasSubtitles: filters?.hasSubtitles ? true : undefined,
|
||||
hasTrailer: filters?.hasTrailer ? true : undefined,
|
||||
hasSpecialFeature: filters?.hasSpecialFeature ? true : undefined,
|
||||
hasThemeSong: filters?.hasThemeSong ? true : undefined,
|
||||
hasThemeVideo: filters?.hasThemeVideo ? true : undefined,
|
||||
videoTypes: buildVideoTypes(videoTypesState),
|
||||
isHd: filters?.isSD ? true : filters?.isHD || undefined,
|
||||
is4K: filters?.is4K || undefined,
|
||||
is3D: filters?.is3D || undefined,
|
||||
enableUserData: true,
|
||||
nameStartsWith: params.nameStartsWith || undefined,
|
||||
genreIds:
|
||||
params.genreIds && params.genreIds.length > 0
|
||||
? params.genreIds
|
||||
: undefined,
|
||||
limit: params.limit,
|
||||
});
|
||||
} else {
|
||||
result = await getItemsApi(api).getItems({
|
||||
userId: userId,
|
||||
parentId: libraryId,
|
||||
recursive: true,
|
||||
includeItemTypes: [currentViewType],
|
||||
sortOrder: [
|
||||
params.sortAscending ? SortOrder.Ascending : SortOrder.Descending,
|
||||
],
|
||||
sortBy: params.sortBy.split(",") as ItemSortBy[],
|
||||
filters: buildFilters(filters),
|
||||
hasSubtitles: filters?.hasSubtitles ? true : undefined,
|
||||
hasTrailer: filters?.hasTrailer ? true : undefined,
|
||||
hasSpecialFeature: filters?.hasSpecialFeature ? true : undefined,
|
||||
hasThemeSong: filters?.hasThemeSong ? true : undefined,
|
||||
hasThemeVideo: filters?.hasThemeVideo ? true : undefined,
|
||||
videoTypes: buildVideoTypes(videoTypesState),
|
||||
isHd: filters?.isSD ? true : filters?.isHD || undefined,
|
||||
is4K: filters?.is4K || undefined,
|
||||
is3D: filters?.is3D || undefined,
|
||||
enableUserData: true,
|
||||
nameStartsWith: params.nameStartsWith || undefined,
|
||||
genreIds:
|
||||
params.genreIds && params.genreIds.length > 0
|
||||
? params.genreIds
|
||||
: undefined,
|
||||
limit: params.limit,
|
||||
});
|
||||
}
|
||||
return result.data;
|
||||
},
|
||||
staleTime: 5_000,
|
||||
gcTime: 5 * 60_000,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
// Deprecated: Router search schema removed in favor of Zustand store.
|
||||
// This file will be removed in a future commit. No exports remain.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { create } from "zustand";
|
||||
|
||||
type HeaderStoreType = {
|
||||
pageTitle: string | null;
|
||||
setPageTitle: (title: string | null) => void;
|
||||
};
|
||||
|
||||
const useHeaderStore = create<HeaderStoreType>((set) => ({
|
||||
pageTitle: null,
|
||||
setPageTitle: (title: string | null) => set(() => ({ pageTitle: title })),
|
||||
}));
|
||||
|
||||
export default useHeaderStore;
|
||||
@@ -0,0 +1,4 @@
|
||||
// Deprecated: Draft store replaced by unified library state store in Zustand.
|
||||
// This file will be removed in a future commit. No exports remain.
|
||||
|
||||
export {}; // ensure this is a module with no exports
|
||||
@@ -0,0 +1,87 @@
|
||||
import type { BaseItemKind } from "@jellyfin/sdk/lib/generated-client";
|
||||
import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
|
||||
export type VideoTypesState = {
|
||||
BluRay?: boolean;
|
||||
Dvd?: boolean;
|
||||
Iso?: boolean;
|
||||
VideoFile?: boolean;
|
||||
};
|
||||
|
||||
export type FiltersState = Record<string, boolean | undefined>;
|
||||
|
||||
export interface LibraryStateSlice {
|
||||
currentViewType?: BaseItemKind | "Artist";
|
||||
sortBy: string; // comma joined for multi-key
|
||||
sortAscending: boolean;
|
||||
nameStartsWith?: string;
|
||||
genreIds: string[];
|
||||
filters: FiltersState;
|
||||
videoTypesState: VideoTypesState;
|
||||
libraryName?: string;
|
||||
itemsTotalCount?: number;
|
||||
}
|
||||
|
||||
export interface LibraryStateStore {
|
||||
libraries: Record<string, LibraryStateSlice>;
|
||||
initLibrary: (
|
||||
libraryId: string,
|
||||
defaults: Partial<LibraryStateSlice>,
|
||||
) => void;
|
||||
updateLibrary: (
|
||||
libraryId: string,
|
||||
partial: Partial<LibraryStateSlice>,
|
||||
) => void;
|
||||
resetLibrary: (libraryId: string) => void;
|
||||
}
|
||||
|
||||
const defaultSlice: LibraryStateSlice = {
|
||||
currentViewType: undefined,
|
||||
sortBy: "Name",
|
||||
sortAscending: true,
|
||||
nameStartsWith: undefined,
|
||||
genreIds: [],
|
||||
filters: {},
|
||||
videoTypesState: {},
|
||||
libraryName: undefined,
|
||||
itemsTotalCount: undefined,
|
||||
};
|
||||
|
||||
export const useLibraryStateStore = create<LibraryStateStore>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
libraries: {},
|
||||
initLibrary: (libraryId, defaults) =>
|
||||
set((s) => {
|
||||
if (s.libraries[libraryId]) return s; // already initialized
|
||||
return {
|
||||
libraries: {
|
||||
...s.libraries,
|
||||
[libraryId]: { ...defaultSlice, ...defaults },
|
||||
},
|
||||
};
|
||||
}),
|
||||
updateLibrary: (libraryId, partial) =>
|
||||
set((s) => ({
|
||||
libraries: {
|
||||
...s.libraries,
|
||||
[libraryId]: {
|
||||
...(s.libraries[libraryId] || defaultSlice),
|
||||
...partial,
|
||||
},
|
||||
},
|
||||
})),
|
||||
resetLibrary: (libraryId) =>
|
||||
set((s) => ({
|
||||
libraries: { ...s.libraries, [libraryId]: { ...defaultSlice } },
|
||||
})),
|
||||
}),
|
||||
{
|
||||
name: "blink-library-state",
|
||||
storage: createJSONStorage(() => sessionStorage),
|
||||
partialize: (state) => ({ libraries: state.libraries }),
|
||||
version: 1,
|
||||
},
|
||||
),
|
||||
);
|
||||
Reference in new issue
Block a user