From 537176b77d5ca6f0be2551ee3e6ad334ed258e5e Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Wed, 22 Nov 2023 17:34:44 +0530 Subject: [PATCH] Fix libraryPage --- src/main.jsx | 1 - src/routes/library/index.jsx | 2262 +++++++++++++++++----------------- 2 files changed, 1151 insertions(+), 1112 deletions(-) diff --git a/src/main.jsx b/src/main.jsx index 0d606be..999afad 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -31,7 +31,6 @@ const init = async () => { const defaultServerInfo = await getServer(defaultServerOnDisk); createApi(defaultServerInfo.address); const userOnDisk = await getUser(); - console.log(userOnDisk); if (userOnDisk) { try { const auth = await useApi diff --git a/src/routes/library/index.jsx b/src/routes/library/index.jsx index c4fc54f..cc01c5e 100644 --- a/src/routes/library/index.jsx +++ b/src/routes/library/index.jsx @@ -52,6 +52,7 @@ import { useBackdropStore } from "../../utils/store/backdrop"; import "./library.module.scss"; import { useApi } from "../../utils/store/api"; +import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api"; const LibraryView = () => { const [api] = useApi((state) => [state.api]); @@ -85,19 +86,18 @@ const LibraryView = () => { setSortBy(e.target.value); }; - const fetchLib = async (libraryId) => { - const result = await getItemsApi(api).getItems({ - userId: user.data.Id, - ids: [libraryId], - fields: [ItemFields.RecursiveItemCount], - }); - return result.data; - }; const currentLib = useQuery({ queryKey: ["libraryView", "currentLib", id], - queryFn: () => fetchLib(id), + queryFn: async () => { + const result = await getUserLibraryApi(api).getItem({ + userId: user.data.Id, + itemId: [id], + }); + return result.data; + }, enabled: !!user.data, networkMode: "always", + gcTime: 0, }); const [isPlayed, setIsPlayed] = useState(false); @@ -127,15 +127,20 @@ const LibraryView = () => { const [viewType, setViewType] = useState([]); useEffect(() => { + if (currentLib.isError) { + console.error(currentLib.error); + } + console.log(currentLib.isSuccess); if (currentLib.isSuccess) { - if (currentLib.data.Items[0].CollectionType == "movies") { + setViewType(null); + if (currentLib.data.CollectionType == "movies") { setCurrentViewType(BaseItemKind.Movie); setViewType([ { title: "Movies", value: BaseItemKind.Movie }, { title: "Genres", value: BaseItemKind.Genre }, { title: "Studios", value: BaseItemKind.Studio }, ]); - } else if (currentLib.data.Items[0].CollectionType == "music") { + } else if (currentLib.data.CollectionType == "music") { setCurrentViewType(BaseItemKind.MusicAlbum); setViewType([ { title: "Albums", value: BaseItemKind.MusicAlbum }, @@ -143,18 +148,14 @@ const LibraryView = () => { { title: "Artists", value: BaseItemKind.MusicArtist }, { title: "Genres", value: BaseItemKind.MusicGenre }, ]); - } else if ( - currentLib.data.Items[0].CollectionType == "tvshows" - ) { + } else if (currentLib.data.CollectionType == "tvshows") { setCurrentViewType(BaseItemKind.Series); setViewType([ { title: "Series", value: BaseItemKind.Series }, { title: "Genres", value: BaseItemKind.Genre }, { title: "Networks", value: BaseItemKind.Studio }, ]); - } else if ( - currentLib.data.Items[0].CollectionType == "playlists" - ) { + } else if (currentLib.data.CollectionType == "playlists") { setCurrentViewType(BaseItemKind.PlaylistsFolder); setViewType([ { @@ -167,8 +168,7 @@ const LibraryView = () => { setCurrentViewType("none"); setViewType([]); } - - if (currentLib.data.Items[0].CollectionType == "movies") { + if (currentLib.data.CollectionType == "movies") { setSortBy("SortName"); setSortByData([ { title: "Name", value: "SortName" }, @@ -180,9 +180,7 @@ const LibraryView = () => { { title: "Runtime", value: "Runtime" }, { title: "Random", value: "Random" }, ]); - } else if ( - currentLib.data.Items[0].CollectionType == "tvshows" - ) { + } else if (currentLib.data.CollectionType == "tvshows") { setSortBy("SortName"); setSortByData([ { title: "Name", value: "SortName" }, @@ -193,7 +191,7 @@ const LibraryView = () => { { title: "Release Date", value: "PremiereDate" }, { title: "Random", value: "Random" }, ]); - } else if (currentLib.data.Items[0].CollectionType == "music") { + } else if (currentLib.data.CollectionType == "music") { setSortBy("Name"); setSortByData([ { title: "Name", value: "Name" }, @@ -293,10 +291,9 @@ const LibraryView = () => { } else { result = await getItemsApi(api).getItems({ userId: user.data.Id, - parentId: currentLib.data.Items[0].Id, + parentId: currentLib.data.Id, recursive: - currentLib.data.Items[0].CollectionType == - "boxsets" + currentLib.data.CollectionType == "boxsets" ? undefined : true, includeItemTypes: [currentViewType], @@ -325,8 +322,13 @@ const LibraryView = () => { } return result.data; }, - enabled: user.isSuccess && Boolean(currentViewType), + // enabled: false, + enabled: + user.isSuccess && + Boolean(currentViewType) && + currentLib.isSuccess, networkMode: "always", + // gcTime: 0, }); const disabledSortViews = [ @@ -347,1167 +349,1205 @@ const LibraryView = () => { useState(() => { // Remove App backdrop in library page setAppBackdrop("", ""); - console.log(items); + console.log(items.isSuccess); }, []); - return ( -
+ if (items.isError) { + console.error(items.error); + } + if (currentLib.isPending) { + return (
+ +
+ ); + } else if (currentLib.isSuccess) { + return ( +
- - {currentLib.isPending ? ( - - ) : ( - currentLib.data.Items[0].Name - )} - - + + {currentLib.isPending ? ( + ) : ( - items.data.TotalRecordCount - ) - } - /> + currentLib.data.Name + )} + + + ) : ( + items.data.TotalRecordCount + ) + } + /> +
- -
-
-
- {disabledSortViews.includes(currentViewType) ? ( - <> - ) : ( - <> - - setSortAscending( - !sortAscending, - ) - } - > - {sortAscending ? ( - - ) : ( - - )} - +
+
+
+ {disabledSortViews.includes( + currentViewType, + ) ? ( + <> + ) : ( + <> + + setSortAscending( + !sortAscending, + ) + } + > + {sortAscending ? ( + + ) : ( + + )} + - - {sortByData.map( - (option, index) => ( - - {option.title} - - ), - )} - - - )} + + {sortByData.map( + (option, index) => ( + + {option.title} + + ), + )} + + + )} - {viewType.length > 0 && ( - <> - - { - setCurrentViewType( - e.target.value, - ); - }} - size="small" - > - {viewType.map((option, index) => ( - - {option.title} - - ))} - - - )} - - {currentLib.isSuccess && - allowedFilterViews.includes( - currentLib.data.Items[0].CollectionType, - ) && ( + {viewType.length > 0 && ( <> - + {viewType.map( + (option, index) => ( + + {option.title} + + ), + )} + )} - - {({ TransitionProps }) => ( - -
- - - } - > - - Filters - - - - { - setIsPlayed( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setFilterArray( - ( - state, - ) => [ - ...state, - "isPlayed", - ], + + {currentLib.isSuccess && + allowedFilterViews.includes( + currentLib.data.CollectionType, + ) && ( + <> + + + + )} + + {({ TransitionProps }) => ( + +
+ + + } + > + + Filters + + + + { + setIsPlayed( + e + .target + .checked, ); - } else { - setFilterArray( - filterArray.filter( + if ( + e + .target + .checked + ) { + setFilterArray( ( - val, - ) => - val != + state, + ) => [ + ...state, "isPlayed", - ), - ); - } - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + ], + ); + } else { + setFilterArray( + filterArray.filter( + ( + val, + ) => + val != + "isPlayed", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setIsUnPlayed( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setFilterArray( - ( - state, - ) => [ - ...state, - "IsUnplayed", - ], + }} + /> + { + setIsUnPlayed( + e + .target + .checked, ); - } else { - setFilterArray( - filterArray.filter( + if ( + e + .target + .checked + ) { + setFilterArray( ( - val, - ) => - val != + state, + ) => [ + ...state, "IsUnplayed", - ), - ); - } - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + ], + ); + } else { + setFilterArray( + filterArray.filter( + ( + val, + ) => + val != + "IsUnplayed", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setIsResumable( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setFilterArray( - ( - state, - ) => [ - ...state, - "isResumable", - ], + }} + /> + { + setIsResumable( + e + .target + .checked, ); - } else { - setFilterArray( - filterArray.filter( + if ( + e + .target + .checked + ) { + setFilterArray( ( - val, - ) => - val != + state, + ) => [ + ...state, "isResumable", - ), - ); - } - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + ], + ); + } else { + setFilterArray( + filterArray.filter( + ( + val, + ) => + val != + "isResumable", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setisFavorite( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setFilterArray( - ( - state, - ) => [ - ...state, - "isFavorite", - ], + }} + /> + { + setisFavorite( + e + .target + .checked, ); - } else { - setFilterArray( - filterArray.filter( + if ( + e + .target + .checked + ) { + setFilterArray( ( - val, - ) => - val != + state, + ) => [ + ...state, "isFavorite", - ), - ); - } - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + ], + ); + } else { + setFilterArray( + filterArray.filter( + ( + val, + ) => + val != + "isFavorite", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setIsLiked( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setFilterArray( - ( - state, - ) => [ - ...state, - "isLiked", - ], + }} + /> + { + setIsLiked( + e + .target + .checked, ); - } else { - setFilterArray( - filterArray.filter( + if ( + e + .target + .checked + ) { + setFilterArray( ( - val, - ) => - val != + state, + ) => [ + ...state, "isLiked", - ), - ); - } - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + ], + ); + } else { + setFilterArray( + filterArray.filter( + ( + val, + ) => + val != + "isLiked", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setIsUnliked( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setFilterArray( - ( - state, - ) => [ - ...state, - "isUnliked", - ], + }} + /> + { + setIsUnliked( + e + .target + .checked, ); - } else { - setFilterArray( - filterArray.filter( + if ( + e + .target + .checked + ) { + setFilterArray( ( - val, - ) => - val != + state, + ) => [ + ...state, "isUnliked", - ), - ); - } + ], + ); + } else { + setFilterArray( + filterArray.filter( + ( + val, + ) => + val != + "isUnliked", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, + }, + }} + /> + + + {currentLib.data.Items[0] + .CollectionType == + "movies" && ( + + + } + > + + Quality + + + + { + setIsBluRay( + e + .target + .checked, + ); + if ( + e + .target + .checked + ) { + setVideoTypes( + ( + state, + ) => [ + ...state, + "BluRay", + ], + ); + } else { + setVideoTypes( + filterArray.filter( + ( + val, + ) => + val != + "BluRay", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, + }, }} /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", - }, - }, - }} - /> - - - {currentLib.data.Items[0] - .CollectionType == - "movies" && ( - - - } - > - - Quality - - - - { - setIsBluRay( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setVideoTypes( - ( - state, - ) => [ - ...state, - "BluRay", - ], - ); - } else { - setVideoTypes( - filterArray.filter( - ( - val, - ) => - val != - "BluRay", - ), - ); + - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", - }, - }, - }} - /> - { - setIsDVD( - e - .target - .checked, - ); - if ( - e - .target - .checked - ) { - setVideoTypes( - ( - state, - ) => [ - ...state, - "Dvd", - ], + onChange={( + e, + ) => { + setIsDVD( + e + .target + .checked, ); - } else { - setVideoTypes( - filterArray.filter( + if ( + e + .target + .checked + ) { + setVideoTypes( ( - val, - ) => - val != + state, + ) => [ + ...state, "Dvd", - ), - ); + ], + ); + } else { + setVideoTypes( + filterArray.filter( + ( + val, + ) => + val != + "Dvd", + ), + ); + } + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, + }, + }} + /> + - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + onChange={( + e, + ) => { + setIsHD( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setIsHD( - e - .target - .checked, - ); - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + }} + /> + { + setIs4K( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setIs4K( - e - .target - .checked, - ); - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + }} + /> + { + setIs3D( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setIs3D( - e - .target - .checked, - ); - }} - /> + }} + /> + + + )} + {!onlyStatusFilterViews.includes( + currentLib.data + .Items[0] + .CollectionType, + ) && ( + + } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", - }, - }, + > + + Features + + + - - - )} - {!onlyStatusFilterViews.includes( - currentLib.data.Items[0] - .CollectionType, - ) && ( - - - } - > - - Features - - - - { - setHasSubtitles( - e - .target - .checked, - ); - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + > + { + setHasSubtitles( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setHasTrailer( - e - .target - .checked, - ); - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + }} + /> + { + setHasTrailer( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setHasSpecialFeature( - e - .target - .checked, - ); - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + }} + /> + { + setHasSpecialFeature( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setHasThemeSong( - e - .target - .checked, - ); - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + }} + /> + { + setHasThemeSong( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - { - setHasThemeVideo( - e - .target - .checked, - ); - }} - /> - } - labelPlacement="start" - componentsProps={{ - typography: - { - style: { - justifySelf: - "start", + }} + /> + { + setHasThemeVideo( + e + .target + .checked, + ); + }} + /> + } + labelPlacement="start" + componentsProps={{ + typography: + { + style: { + justifySelf: + "start", + }, }, - }, - }} - /> - - - )} -
-
- )} -
+ }} + /> +
+
+ )} +
+
+ )} +
+
-
- {items.isPending ? ( - - - - ) : items.data.TotalRecordCount == 0 ? ( - - ) : currentViewType == BaseItemKind.Genre ? ( - items.data.Items.map((item) => { - return ( - - ); - }) - ) : currentViewType != BaseItemKind.Audio ? ( -
- {items.data.Items.map((item) => { - return ( - - - - ); - })} -
- ) : ( -
- {items.data.Items.map((item) => ( - - ))} -
- )} - - {items.isSuccess && - items.data.TotalRecordCount > maxDisplayItems && ( - - - {maxDisplayItems * (page - 1)} -{" "} - {items.data.TotalRecordCount <= - maxDisplayItems * page - ? items.data.TotalRecordCount - : maxDisplayItems * page}{" "} - of {items.data.TotalRecordCount} - - { - setPage(val); - }} - count={Math.ceil( - items.data.TotalRecordCount / - maxDisplayItems, - )} + - + + ) : items.data.TotalRecordCount == 0 ? ( + + ) : currentViewType == BaseItemKind.Genre ? ( + items.data.Items.map((item) => { + return ( + + ); + }) + ) : currentViewType != BaseItemKind.Audio ? ( +
+ {items.data.Items.map((item) => { + return ( + + + + ); + })} +
+ ) : ( +
+ {items.data.Items.map((item) => ( + + ))} +
)} -
- {items.isError && } -
- ); + {items.isSuccess && + items.data.TotalRecordCount > maxDisplayItems && ( + + + {maxDisplayItems * (page - 1)} -{" "} + {items.data.TotalRecordCount <= + maxDisplayItems * page + ? items.data.TotalRecordCount + : maxDisplayItems * page}{" "} + of {items.data.TotalRecordCount} + + { + setPage(val); + }} + count={Math.ceil( + items.data.TotalRecordCount / + maxDisplayItems, + )} + sx={{ + width: "fit-content", + }} + /> + + )} + + + {items.isError && } +
+ ); + } else if (currentLib.isError) { + return
{currentLib.error}
; + } }; export default LibraryView;