From 257ff8e552d7f8bc01285a37810d060178f0769b Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Sat, 2 Aug 2025 17:53:12 +0530 Subject: [PATCH] refactor: start using seach params in library route --- package.json | 1 + pnpm-lock.yaml | 8 + src/components/libraryItemsGrid/index.tsx | 240 ++++ src/components/libraryMenu/index.tsx | 126 ++ src/routes/__root.tsx | 3 +- src/routes/_api/library/$id.tsx | 1546 ++++----------------- src/utils/constants/library.ts | 258 ++++ 7 files changed, 922 insertions(+), 1260 deletions(-) create mode 100644 src/components/libraryItemsGrid/index.tsx create mode 100644 src/components/libraryMenu/index.tsx create mode 100644 src/utils/constants/library.ts diff --git a/package.json b/package.json index 3db3346..60554ad 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "typescript": "^5.8.3", "vite-plugin-svgr": "^4.3.0", "wavesurfer.js": "^7.9.9", + "zod": "^4.0.5", "zustand": "5.0.6" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fdd3af7..35ca026 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -185,6 +185,9 @@ importers: wavesurfer.js: specifier: ^7.9.9 version: 7.9.9 + zod: + specifier: ^4.0.5 + version: 4.0.5 zustand: specifier: 5.0.6 version: 5.0.6(@types/react@19.1.8)(immer@10.1.1)(react@19.1.0)(use-sync-external-store@1.5.0(react@19.1.0)) @@ -3051,6 +3054,9 @@ packages: zod@3.25.67: resolution: {integrity: sha512-idA2YXwpCdqUSKRCACDE6ItZD9TZzy3OZMtpfLoh6oPR47lipysRrJfjzMqFxQ3uJuUPyUeWe1r9vLH33xO/Qw==} + zod@4.0.5: + resolution: {integrity: sha512-/5UuuRPStvHXu7RS+gmvRf4NXrNxpSllGwDnCBcJZtQsKrviYXm54yDGV2KYNLT5kq0lHGcl7lqWJLgSaG+tgA==} + zustand@5.0.6: resolution: {integrity: sha512-ihAqNeUVhe0MAD+X8M5UzqyZ9k3FFZLBTtqo6JLPwV53cbRB/mJwBI0PxcIgqhBBHlEs8G45OTDTMq3gNcLq3A==} engines: {node: '>=12.20.0'} @@ -6036,6 +6042,8 @@ snapshots: zod@3.25.67: {} + zod@4.0.5: {} + zustand@5.0.6(@types/react@19.1.8)(immer@10.1.1)(react@19.1.0)(use-sync-external-store@1.5.0(react@19.1.0)): optionalDependencies: '@types/react': 19.1.8 diff --git a/src/components/libraryItemsGrid/index.tsx b/src/components/libraryItemsGrid/index.tsx new file mode 100644 index 0000000..9a2237d --- /dev/null +++ b/src/components/libraryItemsGrid/index.tsx @@ -0,0 +1,240 @@ +import { + type BaseItemDtoQueryResult, + BaseItemKind, + ItemFilter, + type ItemSortBy, + 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 { useSuspenseQuery } from "@tanstack/react-query"; +import { getRouteApi } from "@tanstack/react-router"; +import type { AxiosResponse } from "axios"; +import React, { useEffect, useMemo, useState } from "react"; +import { useShallow } from "zustand/shallow"; +import { getLibraryQueryOptions } from "@/utils/queries/library"; +import { useApiInContext } from "@/utils/store/api"; +import { useBackdropStore } from "@/utils/store/backdrop"; +import { useCentralStore } from "@/utils/store/central"; +import { Card } from "../card/card"; + +const libraryRoute = getRouteApi("/_api/library/$id"); + +const LibraryItemsGrid = () => { + const api = useApiInContext((s) => s.api); + const user = useCentralStore((s) => s.currentUser); + const { + currentViewType, + sortAscending = true, + sortBy = "SortName", + filters = {}, + videoTypesState = {}, + } = libraryRoute.useSearch(); + const currentLibraryId = libraryRoute.useParams().id; + const currentLibrary = useSuspenseQuery( + getLibraryQueryOptions(api, user?.Id, currentLibraryId), + ); + const items = useSuspenseQuery({ + queryKey: [ + "library", + "items", + currentLibraryId, + { + currentViewType: currentViewType, + sortAscending: sortAscending, + sortBy: sortBy, + filters, + videoTypesState, + // genreFilter, + }, + ], + queryFn: async () => { + if (!api) return null; + let result: AxiosResponse; + if (currentViewType === "MusicArtist") { + result = await getArtistsApi(api).getAlbumArtists({ + userId: user?.Id, + parentId: currentLibraryId, + }); + } else if (currentViewType === "Artist") { + result = await getArtistsApi(api).getArtists({ + userId: user?.Id, + parentId: currentLibraryId, + }); + } else if (currentViewType === "Person") { + result = await getPersonsApi(api).getPersons({ + userId: user?.Id, + personTypes: ["Actor"], + }); + } else if (currentViewType === "Studio") { + result = await getStudiosApi(api).getStudios({ + userId: user?.Id, + parentId: currentLibraryId, + }); + } else if (currentViewType === "BoxSet") { + const videoTypes: VideoType[] = []; + if (videoTypesState.BluRay) videoTypes.push(VideoType.BluRay); + if (videoTypesState.Dvd) videoTypes.push(VideoType.Dvd); + if (videoTypesState.Iso) videoTypes.push(VideoType.Iso); + if (videoTypesState.VideoFile) videoTypes.push(VideoType.VideoFile); + + const filtersArray: ItemFilter[] = []; + if (filters.isPlayed) filtersArray.push(ItemFilter.IsPlayed); + if (filters.isUnPlayed) filtersArray.push(ItemFilter.IsUnplayed); + if (filters.isResumable) filtersArray.push(ItemFilter.IsResumable); + if (filters.isFavorite) filtersArray.push(ItemFilter.IsFavorite); + + result = await getItemsApi(api).getItems({ + userId: user?.Id, + parentId: currentLibraryId, + recursive: + currentLibrary.data.CollectionType === "boxsets" ? undefined : true, + // includeItemTypes: [currentViewType], + sortOrder: [ + sortAscending ? SortOrder.Ascending : SortOrder.Descending, + ], + sortBy: sortBy.split(",") as ItemSortBy[], + filters: filtersArray, + 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: videoTypes, + isHd: filters.isSD ? true : filters.isHD || undefined, + is4K: filters.is4K || undefined, + is3D: filters.is3D || undefined, + enableUserData: true, + // genreIds: genreFilter, + }); + } else { + const videoTypes: VideoType[] = []; + if (videoTypesState.BluRay) videoTypes.push(VideoType.BluRay); + if (videoTypesState.Dvd) videoTypes.push(VideoType.Dvd); + if (videoTypesState.Iso) videoTypes.push(VideoType.Iso); + if (videoTypesState.VideoFile) videoTypes.push(VideoType.VideoFile); + + const filtersArray: ItemFilter[] = []; + if (filters.isPlayed) filtersArray.push(ItemFilter.IsPlayed); + if (filters.isUnPlayed) filtersArray.push(ItemFilter.IsUnplayed); + if (filters.isResumable) filtersArray.push(ItemFilter.IsResumable); + if (filters.isFavorite) filtersArray.push(ItemFilter.IsFavorite); + if (!currentViewType) { + throw new Error("currentViewType is required"); + } + result = await getItemsApi(api).getItems({ + userId: user?.Id, + parentId: currentLibraryId, + recursive: true, + includeItemTypes: [currentViewType], + sortOrder: [ + sortAscending ? SortOrder.Ascending : SortOrder.Descending, + ], + sortBy: sortBy.split(",") as ItemSortBy[], + filters: filtersArray, + 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: videoTypes, + isHd: filters.isSD ? true : filters.isHD || undefined, + is4K: filters.is4K || undefined, + is3D: filters.is3D || undefined, + enableUserData: true, + // genreIds: genreFilter, + }); + } + return result.data; + }, + networkMode: "always", + }); + // --- Backdrop --- + const setBackdrop = useBackdropStore(useShallow((s) => s.setBackdrop)); + const [currentBackdropIndex, setCurrentBackdropIndex] = useState(1); + const backdropItems = useMemo(() => { + if (items.isSuccess) { + const temp = items.data?.Items?.filter( + (item) => Object.keys(item.ImageBlurHashes?.Backdrop ?? {}).length > 0, + ); + const backdropItem = temp?.[0]; + + const currentBackdropItemHash = + backdropItem?.ImageBlurHashes?.Backdrop?.[ + Object.keys(backdropItem?.ImageBlurHashes?.Backdrop ?? {})[0] + ]; + setBackdrop(currentBackdropItemHash); + + return temp; + } + }, [items]); + useEffect(() => { + if (backdropItems?.length === 0 || !api || !backdropItems) { + return; // No backdrop items available + } + const intervalId = setInterval(() => { + const currentBackdropItemHash = + backdropItems[currentBackdropIndex].ImageBlurHashes?.Backdrop?.[ + Object.keys( + backdropItems[currentBackdropIndex].ImageBlurHashes?.Backdrop ?? {}, + )[0] + ]; + if (!currentBackdropItemHash) return; // No backdrop image available + setBackdrop(currentBackdropItemHash); + setCurrentBackdropIndex( + (prevIndex) => (prevIndex + 1) % backdropItems.length, + ); + }, 10_000); // Update backdrop every 10s + return () => clearInterval(intervalId); + }, [backdropItems]); + return ( +
+ {items.data?.Items?.map((item) => ( + + ))} +
+ ); +}; +export default LibraryItemsGrid; diff --git a/src/components/libraryMenu/index.tsx b/src/components/libraryMenu/index.tsx new file mode 100644 index 0000000..81d55b0 --- /dev/null +++ b/src/components/libraryMenu/index.tsx @@ -0,0 +1,126 @@ +import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client"; +import { useScrollTrigger } from "@mui/material"; +import { useSuspenseQuery } from "@tanstack/react-query"; +import { getRouteApi, useNavigate } from "@tanstack/react-router"; +import React, { type ChangeEvent, useMemo } from "react"; +import { + AVAILABLE_VIEWS, + type FILTERS, + SORT_BY_OPTIONS, +} from "@/utils/constants/library"; +import { getLibraryQueryOptions } from "@/utils/queries/library"; + +const route = getRouteApi("/_api/library/$id"); + +const LibraryMenu = () => { + const { currentViewType, sortAscending, filters, sortBy } = route.useSearch(); + const { id: currentLibraryId } = route.useParams(); + const { api, user } = route.useRouteContext(); + const currentLibrary = useSuspenseQuery( + getLibraryQueryOptions(api, user?.Id, currentLibraryId), + ); + const scrollTrigger = useScrollTrigger({ + disableHysteresis: true, + threshold: 20, + }); + const navigate = useNavigate(); + const _handleSortAsecendingToggle = () => { + navigate({ + search: { + sortAscending: !sortAscending, + sortBy, + }, + to: "/library/$id", + params: { + id: currentLibraryId ?? "", + }, + }); + }; + const _handleChangeSortBy = () => { + navigate({ + search: { + sortBy, + }, + to: "/library/$id", + params: { + id: currentLibraryId ?? "", + }, + }); + }; + const _handleChangeCurrentViewType = (viewType: BaseItemKind) => { + navigate({ + search: { + currentViewType: viewType, + }, + to: "/library/$id", + params: { + id: currentLibraryId ?? "", + }, + }); + }; + + const [filterButtonAnchorEl, setFilterButtonAnchorEl] = + React.useState(null); + const _filterMenuOpen = useMemo( + () => Boolean(filterButtonAnchorEl), + [filterButtonAnchorEl], + ); + const _handleFilterDropdown = (event: React.MouseEvent) => { + setFilterButtonAnchorEl(event.currentTarget); + }; + + const _handleFilterChange = (key: FILTERS, value: boolean) => { + // Update filter state and navigate + navigate({ + search: { + filters: { ...filters, [key]: value }, + }, + to: "/library/$id", + params: { + id: currentLibraryId ?? "", + }, + }); + // Close the filter menu + setFilterButtonAnchorEl(null); + }; + + const _disableSortOption = useMemo(() => { + return ( + !currentViewType || + !SORT_BY_OPTIONS.some( + (option) => + option.compatibleViewTypes?.includes(currentViewType as any) || + option.compatibleCollectionTypes?.includes( + currentLibrary.data.CollectionType as any, + ), + ) + ); + }, [currentViewType, currentLibrary.data.CollectionType]); + const _disableFilters = useMemo(() => { + switch (currentViewType) { + case BaseItemKind.Movie: + case BaseItemKind.Audio: + case BaseItemKind.Series: + case BaseItemKind.BoxSet: + return false; + default: + return true; + } + }, [currentViewType]); + + if (!filters) { + return null; + } + + return ( +
+ ); +}; + +export default LibraryMenu; diff --git a/src/routes/__root.tsx b/src/routes/__root.tsx index 0c64365..a5b0521 100644 --- a/src/routes/__root.tsx +++ b/src/routes/__root.tsx @@ -22,6 +22,7 @@ import type { Api, Jellyfin } from "@jellyfin/sdk"; import type { UserDto } from "@jellyfin/sdk/lib/generated-client"; import type { QueryClient } from "@tanstack/react-query"; import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; +import { TanStackRouterDevtools } from "@tanstack/react-router-devtools"; // import { TanStackRouterDevtools } from "@tanstack/react-router-devtools"; import { DndProvider } from "react-dnd"; import { HTML5Backend } from "react-dnd-html5-backend"; @@ -59,7 +60,7 @@ export const Route = createRootRouteWithContext()({ - {/* */} + diff --git a/src/routes/_api/library/$id.tsx b/src/routes/_api/library/$id.tsx index 9ab35f2..f8d6689 100644 --- a/src/routes/_api/library/$id.tsx +++ b/src/routes/_api/library/$id.tsx @@ -1,66 +1,124 @@ -import { - type BaseItemDto, - type BaseItemDtoQueryResult, - BaseItemKind, - CollectionType, - ItemFilter, - ItemSortBy, - SortOrder, - VideoType, -} from "@jellyfin/sdk/lib/generated-client"; -import { getArtistsApi } from "@jellyfin/sdk/lib/utils/api/artists-api"; -import { getGenresApi } from "@jellyfin/sdk/lib/utils/api/genres-api"; -import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api"; -import { getMusicGenresApi } from "@jellyfin/sdk/lib/utils/api/music-genres-api"; -import { getPersonsApi } from "@jellyfin/sdk/lib/utils/api/persons-api"; -import { getStudiosApi } from "@jellyfin/sdk/lib/utils/api/studios-api"; -import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api"; -import { FormControl, Menu, Stack, useScrollTrigger } from "@mui/material"; -import Button from "@mui/material/Button"; -import Checkbox from "@mui/material/Checkbox"; -import Chip from "@mui/material/Chip"; +import { BaseItemKind, ItemSortBy } from "@jellyfin/sdk/lib/generated-client"; import CircularProgress from "@mui/material/CircularProgress"; -import Divider from "@mui/material/Divider"; -import FormControlLabel from "@mui/material/FormControlLabel"; -import IconButton from "@mui/material/IconButton"; -import MenuItem from "@mui/material/MenuItem"; -import TextField from "@mui/material/TextField"; import Typography from "@mui/material/Typography"; -import { useQuery, useSuspenseQuery } from "@tanstack/react-query"; -import { useWindowVirtualizer } from "@tanstack/react-virtual"; -import type { AxiosResponse } from "axios"; -import React, { - type ChangeEvent, - type MouseEvent, - useCallback, - useEffect, - useMemo, - useState, -} from "react"; -import { Card } from "@/components/card/card"; -import MusicTrack from "@/components/musicTrack"; -import { EmptyNotice } from "@/components/notices/emptyNotice/emptyNotice"; -import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice"; -import { useBackdropStore } from "@/utils/store/backdrop"; +import { useSuspenseQuery } from "@tanstack/react-query"; +import React, { Suspense } from "react"; import "./library.scss"; -import { createFileRoute } from "@tanstack/react-router"; +import { createFileRoute, redirect } from "@tanstack/react-router"; +import { z } from "zod"; +import LibraryItemsGrid from "@/components/libraryItemsGrid"; +import LibraryMenu from "@/components/libraryMenu"; import LibraryItemsSkeleton from "@/components/skeleton/libraryItems"; -import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; import { getLibraryQueryOptions } from "@/utils/queries/library"; import { useApiInContext } from "@/utils/store/api"; import { useCentralStore } from "@/utils/store/central"; -type SortByObject = { title: string; value: string }; -type ViewObject = { title: string; value: BaseItemKind | "Artist" }; +// type SortByObject = { title: string; value: string }; +// type ViewObject = { title: string; value: BaseItemKind | "Artist" }; // type allowedFilters = ["movies", "tvshows", "music", "books"]; export const Route = createFileRoute("/_api/library/$id")({ component: Library, - loader: async ({ context: { queryClient, api, user }, params }) => { + validateSearch: z.object({ + sortBy: z.optional(z.enum(ItemSortBy)), + sortAscending: z.optional(z.boolean()), + currentViewType: z.optional( + z.union([z.enum(BaseItemKind), z.literal("Artist")]), + ), + filters: z.optional( + z.object({ + isPlayed: z.optional(z.boolean()), + isUnPlayed: z.optional(z.boolean()), + isResumable: z.optional(z.boolean()), + isFavorite: z.optional(z.boolean()), + hasSubtitles: z.optional(z.boolean()), + hasTrailer: z.optional(z.boolean()), + hasSpecialFeature: z.optional(z.boolean()), + hasThemeSong: z.optional(z.boolean()), + hasThemeVideo: z.optional(z.boolean()), + isSD: z.optional(z.boolean()), + isHD: z.optional(z.boolean()), + is4K: z.optional(z.boolean()), + is3D: z.optional(z.boolean()), + }), + ), + videoTypesState: z.optional( + z.object({ + BluRay: z.optional(z.boolean()), + Dvd: z.optional(z.boolean()), + Iso: z.optional(z.boolean()), + VideoFile: z.optional(z.boolean()), + }), + ), + }), + loaderDeps: ({ search }) => ({ + ...search, + }), + loader: async ({ + context: { queryClient, api, user }, + params, + deps: { ...search }, + }) => { if (!api || !user?.Id) return null; - await queryClient.ensureQueryData( + + if (Object.keys(search).length > 0) { + console.log("Using search params:", search); + // If search is not empty, ensure the library data is fetched + return await queryClient.ensureQueryData( + getLibraryQueryOptions(api, user.Id, params.id), + ); + } + const cachedSearch = sessionStorage.getItem(`library-${params.id}-config`); + if (cachedSearch) { + console.log("Using cached search params:", cachedSearch); + throw redirect({ + to: "/library/$id", + params: { id: params.id }, + search: JSON.parse(cachedSearch), + replace: true, + }); + } + + // Priority 3: No params anywhere, fetch the initial type to create them + const initialData = await queryClient.ensureQueryData( getLibraryQueryOptions(api, user.Id, params.id), ); + + // Now, redirect to the URL with the new initial params + throw redirect({ + to: "/library/$id", + search: { + sortAscending: true, + sortBy: ItemSortBy.Name, + currentViewType: + initialData?.CollectionType === "music" + ? BaseItemKind.MusicAlbum + : BaseItemKind.Movie, + filters: { + isPlayed: false, + isUnPlayed: false, + isResumable: false, + isFavorite: false, + hasSubtitles: false, + hasTrailer: false, + hasSpecialFeature: false, + hasThemeSong: false, + hasThemeVideo: false, + isSD: false, + isHD: undefined, + is4K: false, + is3D: false, + }, + videoTypesState: { + BluRay: false, + Dvd: false, + Iso: false, + VideoFile: false, + }, + }, + params: { id: params.id }, + replace: true, + }); }, }); @@ -75,694 +133,6 @@ function Library() { getLibraryQueryOptions(api, user?.Id, id), ); - const genres = useQuery({ - queryKey: ["library", "genreItem", id], - queryFn: async () => { - if (!api) return null; - if (currentLib.data?.CollectionType === "music") { - const result = await getMusicGenresApi(api).getMusicGenres({ - parentId: id, - userId: user?.Id, - }); - return result.data; - } - const result = await getGenresApi(api).getGenres({ - parentId: id, - userId: user?.Id, - }); - return result.data; - }, - }); - - const [genreFilter, setGenreFilter] = useState(() => { - const cachedVal = sessionStorage.getItem( - `library-${currentLib.data?.Id}-config_genreFilter`, - ); - if (cachedVal) { - return JSON.parse(cachedVal); - } - return []; - }); - const handleGenreFilter = useCallback( - (e: ChangeEvent, genre: BaseItemDto) => { - if (e.target.checked) { - const genreFilterTemp = [...genreFilter, genre.Id]; - setGenreFilter((s) => (genre.Id ? [...s, genre.Id] : s)); - sessionStorage.setItem( - `library-${currentLib.data?.Id}-config_genreFilter`, - JSON.stringify(genreFilterTemp), - ); - } else { - const genreFilterTemp = genreFilter.filter((id) => id !== genre.Id); - setGenreFilter((s) => s.filter((id) => id !== genre.Id)); - sessionStorage.setItem( - `library-${currentLib.data?.Id}-config_genreFilter`, - JSON.stringify(genreFilterTemp), - ); - } - }, - [genres.data?.Items?.length, id], - ); - - const [currentViewType, setCurrentViewType] = useState< - BaseItemKind | "Artist" - >(undefined!); - const availableViewTypes: ViewObject[] = useMemo(() => { - let result: ViewObject[] = undefined!; - switch (currentLib.data?.CollectionType) { - case CollectionType.Movies: - result = [ - { - title: "Movies", - value: BaseItemKind.Movie, - }, - { - title: "Trailers", - value: BaseItemKind.Trailer, - }, - { - title: "Collections", - value: BaseItemKind.CollectionFolder, - }, - { - title: "Genres", - value: BaseItemKind.Genre, - }, - ]; - break; - case CollectionType.Tvshows: - result = [ - { title: "Shows", value: BaseItemKind.Series }, - { title: "Genre", value: BaseItemKind.Genre }, - { title: "TV Networks", value: BaseItemKind.Studio }, - ]; - break; - case CollectionType.Boxsets: - result = [{ title: "Collection", value: BaseItemKind.BoxSet }]; - break; - case CollectionType.Music: - result = [ - { - title: "Albums", - value: BaseItemKind.MusicAlbum, - }, - { - title: "Album Artist", - value: BaseItemKind.MusicArtist, - }, - { - title: "Artist", - value: "Artist", - }, - { - title: "Playlist", - value: BaseItemKind.Playlist, - }, - { - title: "Songs", - value: BaseItemKind.Audio, - }, - { - title: "Genre", - value: BaseItemKind.Genre, - }, - ]; - break; - case CollectionType.Playlists: - result = [{ title: "Playlist", value: BaseItemKind.Playlist }]; - break; - case CollectionType.Books: - result = [{ title: "Books", value: BaseItemKind.Book }]; - break; - default: - result = []; - } - if ( - sessionStorage.getItem( - `library-${currentLib.data?.Id}-config_currentViewType`, - ) - ) { - setCurrentViewType( - sessionStorage.getItem( - `library-${currentLib.data?.Id}-config_currentViewType`, - ) as BaseItemKind | "Artist", - ); - } else { - setCurrentViewType(result?.[0]?.value); - } - return result; - }, [currentLib.data?.CollectionType]); - - const [videoTypesState, setVideoTypesState] = useState< - Record - >({ - VideoFile: undefined, - Iso: undefined, - BluRay: undefined, - Dvd: undefined, - }); - - const [sortBy, setSortBy] = useState("Name"); - const [sortAscending, setSortAscending] = useState( - () => - JSON.parse( - sessionStorage.getItem( - `library-${currentLib.data?.Id}-config_sort`, - ) as string, - )?.sortAscending ?? true, - ); - useEffect(() => { - // I don't know why this works, don't ask... - const cachedVal = sessionStorage.getItem( - `library-${currentLib.data?.Id}-config_sort`, - ) as unknown as { - sortAscending?: boolean; - sortBy?: ItemSortBy; - }; - if (cachedVal?.sortAscending) { - console.log(cachedVal.sortAscending); - setSortAscending(cachedVal.sortAscending); - } - }, [currentLib.data?.Id]); - const sortByOptions: SortByObject[] = useMemo(() => { - let result: SortByObject[] = undefined!; - switch (currentViewType) { - case BaseItemKind.Audio: - result = [ - { - title: "Track Name", - value: ItemSortBy.Name, - }, - { - title: "Album", - value: ItemSortBy.Album, - }, - { - title: "Album Artist", - value: ItemSortBy.AlbumArtist, - }, - { - title: "Artist", - value: ItemSortBy.Artist, - }, - { - title: "Date Added", - value: ItemSortBy.DateCreated, - }, - { - title: "Date Played", - value: ItemSortBy.DatePlayed, - }, - { - title: "Play Count", - value: ItemSortBy.PlayCount, - }, - { - title: "Release Date", - value: ItemSortBy.PremiereDate, - }, - { - title: "Runtime", - value: ItemSortBy.Runtime, - }, - { - title: "Random", - value: ItemSortBy.Random, - }, - ]; - break; - case BaseItemKind.MusicAlbum: - result = [ - { - title: "Name", - value: ItemSortBy.SortName, - }, - { - title: "Album Artist", - value: ItemSortBy.AlbumArtist, - }, - { - title: "Community Rating", - value: ItemSortBy.CommunityRating, - }, - { - title: "Critcs Rating", - value: ItemSortBy.CriticRating, - }, - { - title: "Date Added", - value: ItemSortBy.DateCreated, - }, - { - title: "Release Date", - value: ItemSortBy.PremiereDate, - }, - { - title: "Random", - value: ItemSortBy.Random, - }, - ]; - break; - case BaseItemKind.Playlist: - result = [ - { - title: "Name", - value: ItemSortBy.SortName, - }, - { - title: "Community Rating", - value: ItemSortBy.CommunityRating, - }, - { - title: "Critics Rating", - value: ItemSortBy.CriticRating, - }, - { - title: "Date Added", - value: ItemSortBy.DateCreated, - }, - { - title: "Date Played", - value: ItemSortBy.DatePlayed, - }, - { - title: "Folder", - value: `${ItemSortBy.IsFolder},${ItemSortBy.SortName}`, - }, - { title: "Parenal Rating", value: ItemSortBy.OfficialRating }, - { - title: "Play Count", - value: ItemSortBy.PlayCount, - }, - { - title: "Release Date", - value: `${ItemSortBy.ProductionYear},${ItemSortBy.PremiereDate},${ItemSortBy.SortName}`, - }, - { - title: "Random", - value: ItemSortBy.Random, - }, - ]; - break; - default: - result = []; - break; - } - switch (currentLib.data?.CollectionType) { - case CollectionType.Movies: - result = [ - { - title: "Name", - value: ItemSortBy.SortName, - }, - { - title: "Random", - value: ItemSortBy.Random, - }, - { - title: "IMDb Rating", - value: ItemSortBy.CommunityRating, - }, - { - title: "Critics Rating", - value: ItemSortBy.CriticRating, - }, - { - title: "Date Added", - value: ItemSortBy.DateCreated, - }, - { - title: "Date Played", - value: ItemSortBy.DatePlayed, - }, - { - title: "Parental Rating", - value: ItemSortBy.OfficialRating, - }, - { - title: "Play Count", - value: ItemSortBy.PlayCount, - }, - { - title: "Premiere Date", - value: ItemSortBy.PremiereDate, - }, - { - title: "Runtime", - value: ItemSortBy.Runtime, - }, - ]; - break; - case CollectionType.Tvshows: - result = [ - { - title: "Name", - value: ItemSortBy.SortName, - }, - { - title: "Random", - value: ItemSortBy.Random, - }, - { - title: "IMDb Rating", - value: ItemSortBy.CommunityRating, - }, - { - title: "Date Show Added", - value: ItemSortBy.DateCreated, - }, - { - title: "Date Episode Added", - value: ItemSortBy.DateLastContentAdded, - }, - { - title: "Date Played", - value: ItemSortBy.DatePlayed, - }, - { - title: "Parental Rating", - value: ItemSortBy.OfficialRating, - }, - { - title: "Premiere Date", - value: ItemSortBy.PremiereDate, - }, - ]; - break; - case CollectionType.Boxsets: - result = [ - { - title: "Name", - value: ItemSortBy.SortName, - }, - { - title: "Random", - value: ItemSortBy.Random, - }, - { - title: "IMDb Rating", - value: ItemSortBy.CommunityRating, - }, - { - title: "Critics Rating", - value: ItemSortBy.CriticRating, - }, - { - title: "Date Added", - value: ItemSortBy.DateCreated, - }, - { - title: "Date Played", - value: ItemSortBy.DatePlayed, - }, - { - title: "Parental Rating", - value: ItemSortBy.OfficialRating, - }, - { - title: "Play Count", - value: ItemSortBy.PlayCount, - }, - { - title: "Premiere Date", - value: ItemSortBy.PremiereDate, - }, - { - title: "Runtime", - value: ItemSortBy.Runtime, - }, - ]; - break; - case CollectionType.Books: - result = [ - { - title: "Name", - value: ItemSortBy.SortName, - }, - ]; - break; - } - // And they say typescript makes your code better :| - const cachedVal = JSON.parse( - sessionStorage.getItem( - `library-${currentLib.data?.Id}-config_sort`, - ) as string, - ); - // as { - // sortAscending: boolean; - // sortBy: ItemSortBy; - // } - if (cachedVal) { - setSortBy(cachedVal.sortBy); - } else { - setSortBy(result?.[0]?.value); - } - return result; - }, [currentLib.data?.CollectionType, currentViewType]); - - const [filters, setFilters] = useState>( - () => { - const cachedVal = sessionStorage.getItem( - `library-${currentLib.data?.Id}-config_fliters`, - ); - if (cachedVal !== null) { - return JSON.parse(cachedVal); - } - return { - isPlayed: false, - isUnPlayed: false, - isResumable: false, - isFavorite: false, - isLiked: false, - isUnliked: false, - hasSubtitles: false, - hasTrailer: false, - hasSpecialFeature: false, - hasThemeSong: false, - hasThemeVideo: false, - isBluRay: false, - isDVD: false, - isHD: undefined, - isSD: false, - is4K: false, - is3D: false, - }; - }, - ); - useEffect(() => { - sessionStorage.setItem( - `library-${currentLib.data?.Id}-config_fliters`, - JSON.stringify(filters), - ); - }, [filters, currentLib.data?.Id]); - - const items = useQuery({ - queryKey: [ - "library", - "currentLibItems", - id, - { - currentViewType: currentViewType, - sortAscending: sortAscending, - sortBy: sortBy, - filters, - videoTypesState, - genreFilter, - }, - ], - queryFn: async () => { - if (!api) return null; - let result: AxiosResponse; - if (currentViewType === "MusicArtist") { - result = await getArtistsApi(api).getAlbumArtists({ - userId: user?.Id, - parentId: id, - }); - } else if (currentViewType === "Artist") { - result = await getArtistsApi(api).getArtists({ - userId: user?.Id, - parentId: id, - }); - } else if (currentViewType === "Person") { - result = await getPersonsApi(api).getPersons({ - userId: user?.Id, - personTypes: ["Actor"], - }); - } else if (currentViewType === "Studio") { - result = await getStudiosApi(api).getStudios({ - userId: user?.Id, - parentId: id, - }); - } else if (currentViewType === "BoxSet") { - const videoTypes: VideoType[] = []; - if (videoTypesState.BluRay) videoTypes.push(VideoType.BluRay); - if (videoTypesState.Dvd) videoTypes.push(VideoType.Dvd); - if (videoTypesState.Iso) videoTypes.push(VideoType.Iso); - if (videoTypesState.VideoFile) videoTypes.push(VideoType.VideoFile); - - const filtersArray: ItemFilter[] = []; - if (filters.isPlayed) filtersArray.push(ItemFilter.IsPlayed); - if (filters.isUnPlayed) filtersArray.push(ItemFilter.IsUnplayed); - if (filters.isResumable) filtersArray.push(ItemFilter.IsResumable); - if (filters.isFavorite) filtersArray.push(ItemFilter.IsFavorite); - - result = await getItemsApi(api).getItems({ - userId: user?.Id, - parentId: currentLib.data?.Id, - recursive: - currentLib.data?.CollectionType === "boxsets" ? undefined : true, - // includeItemTypes: [currentViewType], - sortOrder: [ - sortAscending ? SortOrder.Ascending : SortOrder.Descending, - ], - sortBy: sortBy.split(",") as ItemSortBy[], - filters: filtersArray, - 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: videoTypes, - isHd: filters.isSD ? true : filters.isHD || undefined, - is4K: filters.is4K || undefined, - is3D: filters.is3D || undefined, - enableUserData: true, - genreIds: genreFilter, - }); - } else { - const videoTypes: VideoType[] = []; - if (videoTypesState.BluRay) videoTypes.push(VideoType.BluRay); - if (videoTypesState.Dvd) videoTypes.push(VideoType.Dvd); - if (videoTypesState.Iso) videoTypes.push(VideoType.Iso); - if (videoTypesState.VideoFile) videoTypes.push(VideoType.VideoFile); - - const filtersArray: ItemFilter[] = []; - if (filters.isPlayed) filtersArray.push(ItemFilter.IsPlayed); - if (filters.isUnPlayed) filtersArray.push(ItemFilter.IsUnplayed); - if (filters.isResumable) filtersArray.push(ItemFilter.IsResumable); - if (filters.isFavorite) filtersArray.push(ItemFilter.IsFavorite); - - result = await getItemsApi(api).getItems({ - userId: user?.Id, - parentId: currentLib.data?.Id, - recursive: true, - includeItemTypes: [currentViewType], - sortOrder: [ - sortAscending ? SortOrder.Ascending : SortOrder.Descending, - ], - sortBy: sortBy.split(",") as ItemSortBy[], - filters: filtersArray, - 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: videoTypes, - isHd: filters.isSD ? true : filters.isHD || undefined, - is4K: filters.is4K || undefined, - is3D: filters.is3D || undefined, - enableUserData: true, - genreIds: genreFilter, - }); - } - return result.data; - }, - enabled: !!user?.Id && Boolean(currentViewType) && currentLib.isSuccess, - networkMode: "always", - }); - - const disableSort = useMemo(() => { - if (items.data?.TotalRecordCount === 0) { - return true; // Hide sorting options if library is empty - } - switch (currentViewType) { - case BaseItemKind.MusicArtist: - case BaseItemKind.Person: - case BaseItemKind.MusicGenre: - case BaseItemKind.Genre: - case "Artist": - return true; - default: - return false; - } - }, [currentViewType, items.isSuccess]); - const disableFilters = useMemo(() => { - switch (currentViewType) { - case BaseItemKind.Movie: - case BaseItemKind.Audio: - case BaseItemKind.Series: - case BaseItemKind.BoxSet: - return false; - default: - return true; - } - }, [currentViewType]); - - const rowVirtualizer = useWindowVirtualizer({ - count: items.data?.TotalRecordCount ?? 1, - estimateSize: () => 80, - overscan: 4, - }); - // const onlyStatusFilterViews = ["books", "music"]; - - const [filterButtonAnchorEl, setFilterButtonAnchorEl] = - useState(null); - const filterMenuOpen = useMemo( - () => Boolean(filterButtonAnchorEl), - [filterButtonAnchorEl], - ); - const handleFilterDropdown = useCallback( - (e: MouseEvent) => { - setFilterButtonAnchorEl(e.currentTarget); - }, - [currentViewType, filterButtonAnchorEl], - ); - - const scrollTrigger = useScrollTrigger({ - disableHysteresis: true, - threshold: 20, - }); - - // const setBackdrop = useBackdropStore((s) => s.setBackdrop); - - const backdropItems = useMemo(() => { - if (items.isSuccess) { - const temp = items.data?.Items?.filter( - (item) => item.BackdropImageTags?.length, - ); - if (temp?.[0]?.Id && api) { - // setBackdrop( - // getImageUrlsApi(api).getItemImageUrlById(temp?.[0]?.Id, "Backdrop", { - // tag: temp?.[0]?.BackdropImageTags?.[0], - // }), - // temp?.[0]?.BackdropImageTags?.[0] ?? "none", - // ); - } - return temp; - } - }, [items]); - - useEffect(() => { - if (backdropItems?.length && api) { - const intervalId = setInterval(() => { - const itemIndex = Math.floor(Math.random() * backdropItems?.length); - const _backdropItem = backdropItems[itemIndex]; - // enqueueSnackbar("Running"); - // setBackdrop( - // getImageUrlsApi(api).getItemImageUrlById( - // backdropItem.Id ?? "", - // "Backdrop", - // { - // tag: backdropItem.BackdropImageTags?.[0], - // }, - // ), - // backdropItem.BackdropImageTags?.[0] ?? "none", - // ); - }, 14000); // Update backdrop every 14s - return () => clearInterval(intervalId); - } - }, [backdropItems]); - return (
- - ) : ( - items.data?.TotalRecordCount - ) - } - />
-
-
- } - direction="row" - alignItems="center" - justifyContent="center" - gap={1.2} - className="library-items-options" - > - {!disableSort && sortBy && ( -
- { - sessionStorage.setItem( - `library-${currentLib.data?.Id}-config_sort`, - JSON.stringify({ - sortAscending: !sortAscending, - sortBy, - }), - ); - setSortAscending((state: boolean) => !state); - }} - > - - sort - - - { - setSortBy(e.target.value); - console.info(e); - sessionStorage.setItem( - `library-${currentLib.data?.Id}-config_sort`, - JSON.stringify({ - sortAscending, - sortBy: e.target.value, - }), - ); - }} - > - {sortByOptions.map((item) => ( - - {item.title} - - ))} - -
- )} - {availableViewTypes.length > 0 && ( - { - setCurrentViewType(e.target.value as BaseItemKind); - sessionStorage.setItem( - `library-${currentLib.data?.Id}-config_currentViewType`, - e.target.value, - ); - }} - > - {availableViewTypes.map((item) => ( - - {item.title} - - ))} - - )} - {!disableFilters && ( -
- - setFilterButtonAnchorEl(null)} - slotProps={{ - paper: { - style: { - width: "28em", - maxHeight: "32em", - }, - }, - }} - > - {currentViewType !== "Audio" && ( -
- - Filters - - - ) => - setFilters((s) => ({ - ...s, - isPlayed: e.target.checked, - })) - } - /> - } - label="Played" - /> - ) => - setFilters((s) => ({ - ...s, - isUnPlayed: e.target.checked, - })) - } - /> - } - label="Unplayed" - /> - ) => - setFilters((s) => ({ - ...s, - isResumable: e.target.checked, - })) - } - /> - } - label="Resumable" - /> - ) => - setFilters((s) => ({ - ...s, - isFavorite: e.target.checked, - })) - } - /> - } - label="Favorites" - /> - -
- )} - {currentViewType !== "Audio" && ( -
- - Features - - - ) => - setFilters((s) => ({ - ...s, - hasSubtitles: e.target.checked, - })) - } - /> - } - label="Subtitles" - /> - ) => - setFilters((s) => ({ - ...s, - hasTrailer: e.target.checked, - })) - } - /> - } - label="Trailer" - /> - ) => - setFilters((s) => ({ - ...s, - hasSpecialFeature: e.target.checked, - })) - } - /> - } - label="Special Features" - /> - ) => - setFilters((s) => ({ - ...s, - hasThemeSong: e.target.checked, - })) - } - /> - } - label="Theme Song" - /> - ) => - setFilters((s) => ({ - ...s, - hasThemeVideo: e.target.checked, - })) - } - /> - } - label="Theme Video" - /> - -
- )} - {currentViewType !== "Audio" && ( -
- - Video Types - - - ) => - setVideoTypesState((s) => ({ - ...s, - BluRay: e.target.checked, - })) - } - /> - } - label="BluRay" - /> - ) => - setVideoTypesState((s) => ({ - ...s, - Dvd: e.target.checked, - })) - } - /> - } - label="DVD" - /> - ) => { - setFilters((s) => ({ - ...s, - isHD: e.target.checked, - })); - }} - /> - } - label="HD" - /> - ) => - setFilters((s) => ({ - ...s, - is4K: e.target.checked, - })) - } - /> - } - label="4k" - /> - } - label="SD" - disabled - /> - - setFilters((s) => ({ - ...s, - is3D: e.target.checked, - })) - } - /> - } - label="3D" - /> - -
- )} - {genres.isSuccess && - (genres.data?.TotalRecordCount ?? 0) > 0 && ( -
- - Genres - - - {genres.data?.Items?.map( - (genre) => - genre.Id && ( - - handleGenreFilter(e, genre) - } - /> - } - label={genre.Name} - /> - ), - )} - -
- )} -
-
- )} -
+ }> +
+ +
- - {items.isPending ? ( - - ) : items.data?.TotalRecordCount === 0 ? ( - - ) : currentViewType !== BaseItemKind.Audio ? ( -
- {items.data?.Items?.map((item) => ( - - ))} -
- ) : ( -
- {rowVirtualizer.getVirtualItems().map((virtualRow) => { - const item = items.data?.Items?.[virtualRow.index]; - if (!item) return null; - return ( -
- -
- ); - })} -
- )} -
- - {items.isError && } + ); } + +// const genres = useQuery({ + // queryKey: ["library", "genreItem", id], + // queryFn: async () => { + // if (!api) return null; + // if (currentLib.data?.CollectionType === "music") { + // const result = await getMusicGenresApi(api).getMusicGenres({ + // parentId: id, + // userId: user?.Id, + // }); + // return result.data; + // } + // const result = await getGenresApi(api).getGenres({ + // parentId: id, + // userId: user?.Id, + // }); + // return result.data; + // }, + // }); + + // const [genreFilter, setGenreFilter] = useState(() => { + // const cachedVal = sessionStorage.getItem( + // `library-${currentLib.data?.Id}-config_genreFilter`, + // ); + // if (cachedVal) { + // return JSON.parse(cachedVal); + // } + // return []; + // }); + // const _handleGenreFilter = useCallback( + // (e: ChangeEvent, genre: BaseItemDto) => { + // if (e.target.checked) { + // const genreFilterTemp = [...genreFilter, genre.Id]; + // setGenreFilter((s) => (genre.Id ? [...s, genre.Id] : s)); + // sessionStorage.setItem( + // `library-${currentLib.data?.Id}-config_genreFilter`, + // JSON.stringify(genreFilterTemp), + // ); + // } else { + // const genreFilterTemp = genreFilter.filter((id) => id !== genre.Id); + // setGenreFilter((s) => s.filter((id) => id !== genre.Id)); + // sessionStorage.setItem( + // `library-${currentLib.data?.Id}-config_genreFilter`, + // JSON.stringify(genreFilterTemp), + // ); + // } + // }, + // [genres.data?.Items?.length, id], + // ); + + // const [currentViewType, setCurrentViewType] = useState< + // BaseItemKind | "Artist" + // >(undefined!); + + // // DONE + // const _availableViewTypes: ViewObject[] = useMemo(() => { + // const result: ViewObject[] = undefined!; + + // if ( + // sessionStorage.getItem( + // `library-${currentLib.data?.Id}-config_currentViewType`, + // ) + // ) { + // setCurrentViewType( + // sessionStorage.getItem( + // `library-${currentLib.data?.Id}-config_currentViewType`, + // ) as BaseItemKind | "Artist", + // ); + // } else { + // setCurrentViewType(result?.[0]?.value); + // } + // return result; + // }, [currentLib.data?.CollectionType]); + + // const [_videoTypesState, _setVideoTypesState] = useState< + // Record + // >({ + // VideoFile: undefined, + // Iso: undefined, + // BluRay: undefined, + // Dvd: undefined, + // }); + + // const [_sortBy, setSortBy] = useState("Name"); + // const [_sortAscending, setSortAscending] = useState( + // () => + // JSON.parse( + // sessionStorage.getItem( + // `library-${currentLib.data?.Id}-config_sort`, + // ) as string, + // )?.sortAscending ?? true, + // ); + // useEffect(() => { + // // I don't know why this works, don't ask... + // const cachedVal = sessionStorage.getItem( + // `library-${currentLib.data?.Id}-config_sort`, + // ) as unknown as { + // sortAscending?: boolean; + // sortBy?: ItemSortBy; + // }; + // if (cachedVal?.sortAscending) { + // console.log(cachedVal.sortAscending); + // setSortAscending(cachedVal.sortAscending); + // } + // }, [currentLib.data?.Id]); + // const _sortByOptions: SortByObject[] = useMemo(() => { + // const result: SortByObject[] = undefined!; + + // const cachedVal = JSON.parse( + // sessionStorage.getItem( + // `library-${currentLib.data?.Id}-config_sort`, + // ) as string, + // ); + // if (cachedVal) { + // setSortBy(cachedVal.sortBy); + // } else { + // setSortBy(result?.[0]?.value); + // } + // return result; + // }, [currentLib.data?.CollectionType, currentViewType]); + + // const [filters, _setFilters] = useState>( + // () => { + // const cachedVal = sessionStorage.getItem( + // `library-${currentLib.data?.Id}-config_fliters`, + // ); + // if (cachedVal !== null) { + // return JSON.parse(cachedVal); + // } + // return { + // isPlayed: false, + // isUnPlayed: false, + // isResumable: false, + // isFavorite: false, + // isLiked: false, + // isUnliked: false, + // hasSubtitles: false, + // hasTrailer: false, + // hasSpecialFeature: false, + // hasThemeSong: false, + // hasThemeVideo: false, + // isBluRay: false, + // isDVD: false, + // isHD: undefined, + // isSD: false, + // is4K: false, + // is3D: false, + // }; + // }, + // ); + // useEffect(() => { + // sessionStorage.setItem( + // `library-${currentLib.data?.Id}-config_fliters`, + // JSON.stringify(filters), + // ); + // }, [filters, currentLib.data?.Id]); + + // // const _rowVirtualizer = useWindowVirtualizer({ + // // count: items.data?.TotalRecordCount ?? 1, + // // estimateSize: () => 80, + // // overscan: 4, + // // }); + // // const onlyStatusFilterViews = ["books", "music"]; + + // const [filterButtonAnchorEl, setFilterButtonAnchorEl] = + // useState(null); + // const _filterMenuOpen = useMemo( + // () => Boolean(filterButtonAnchorEl), + // [filterButtonAnchorEl], + // ); + // const _handleFilterDropdown = useCallback( + // (e: MouseEvent) => { + // setFilterButtonAnchorEl(e.currentTarget); + // }, + // [currentViewType, filterButtonAnchorEl], + // ); \ No newline at end of file diff --git a/src/utils/constants/library.ts b/src/utils/constants/library.ts new file mode 100644 index 0000000..2f900f9 --- /dev/null +++ b/src/utils/constants/library.ts @@ -0,0 +1,258 @@ +import { + BaseItemKind, + CollectionType, + ItemSortBy, +} from "@jellyfin/sdk/lib/generated-client"; + +export const AVAILABLE_VIEWS: Array<{ + title: string; + value: BaseItemKind | "Artist"; + compatibleCollectionTypes: CollectionType[]; +}> = [ + { + title: "Movies", + value: BaseItemKind.Movie, + compatibleCollectionTypes: [CollectionType.Movies], + }, + { + title: "Trailers", + value: BaseItemKind.Trailer, + compatibleCollectionTypes: [CollectionType.Movies], + }, + { + title: "Collections", + value: BaseItemKind.CollectionFolder, + compatibleCollectionTypes: [CollectionType.Movies, CollectionType.Boxsets], + }, + { + title: "Genres", + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Tvshows, + CollectionType.Music, + ], + value: BaseItemKind.Genre, + }, + { + title: "Shows", + value: BaseItemKind.Series, + compatibleCollectionTypes: [CollectionType.Tvshows], + }, + { + title: "TV Networks", + value: BaseItemKind.Studio, + compatibleCollectionTypes: [CollectionType.Tvshows], + }, + { + title: "Albums", + value: BaseItemKind.MusicAlbum, + compatibleCollectionTypes: [CollectionType.Music], + }, + { + title: "Album Artist", + value: BaseItemKind.MusicArtist, + compatibleCollectionTypes: [CollectionType.Music], + }, + { + title: "Artist", + value: "Artist", + compatibleCollectionTypes: [CollectionType.Music], + }, + { + title: "Playlist", + value: BaseItemKind.Playlist, + compatibleCollectionTypes: [CollectionType.Music, CollectionType.Playlists], + }, + { + title: "Songs", + value: BaseItemKind.Audio, + compatibleCollectionTypes: [CollectionType.Music], + }, + { + title: "Books", + value: BaseItemKind.Book, + compatibleCollectionTypes: [CollectionType.Books], + }, +]; + +export const SORT_BY_OPTIONS: Array<{ + title: string; + value: ItemSortBy | ItemSortBy[]; + compatibleCollectionTypes?: CollectionType[]; + compatibleViewTypes?: BaseItemKind[]; +}> = [ + { + title: "Name", + value: ItemSortBy.SortName, + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Tvshows, + CollectionType.Boxsets, + CollectionType.Books, + ], + }, + { + title: "Track Name", + value: ItemSortBy.Name, + compatibleViewTypes: [ + BaseItemKind.Audio, + BaseItemKind.MusicAlbum, + BaseItemKind.Playlist, + ], + }, + { + title: "Album", + value: ItemSortBy.Album, + compatibleViewTypes: [BaseItemKind.Audio], + }, + { + title: "Album Artist", + value: ItemSortBy.AlbumArtist, + compatibleViewTypes: [BaseItemKind.Audio, BaseItemKind.MusicAlbum], + }, + { + title: "Artist", + value: ItemSortBy.Artist, + compatibleViewTypes: [BaseItemKind.Audio], + }, + { + title: "Date Added", + value: ItemSortBy.DateCreated, + compatibleViewTypes: [ + BaseItemKind.MusicAlbum, + BaseItemKind.Audio, + BaseItemKind.Playlist, + ], + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Boxsets, + CollectionType.Tvshows, + ], + }, + { + title: "Date Played", + value: ItemSortBy.DatePlayed, + compatibleViewTypes: [BaseItemKind.Audio, BaseItemKind.Playlist], + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Tvshows, + CollectionType.Boxsets, + ], + }, + { + title: "Play Count", + value: ItemSortBy.PlayCount, + compatibleViewTypes: [BaseItemKind.Audio, BaseItemKind.Playlist], + compatibleCollectionTypes: [CollectionType.Movies, CollectionType.Boxsets], + }, + { + title: "Release Date", + value: ItemSortBy.PremiereDate, + compatibleViewTypes: [ + BaseItemKind.MusicAlbum, + BaseItemKind.Audio, + BaseItemKind.Playlist, + ], + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Tvshows, + CollectionType.Boxsets, + ], + }, + { + title: "Runtime", + value: ItemSortBy.Runtime, + compatibleViewTypes: [ + BaseItemKind.MusicAlbum, + BaseItemKind.Audio, + BaseItemKind.Playlist, + ], + compatibleCollectionTypes: [CollectionType.Movies, CollectionType.Boxsets], + }, + { + title: "Random", + value: ItemSortBy.Random, + compatibleViewTypes: [ + BaseItemKind.MusicAlbum, + BaseItemKind.Audio, + BaseItemKind.Playlist, + ], + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Tvshows, + CollectionType.Boxsets, + ], + }, + { + title: "Community Rating", + value: ItemSortBy.CommunityRating, + compatibleViewTypes: [ + BaseItemKind.MusicAlbum, + BaseItemKind.Audio, + BaseItemKind.Playlist, + ], + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Tvshows, + CollectionType.Boxsets, + ], + }, + { + title: "Critics Rating", + value: ItemSortBy.CriticRating, + compatibleViewTypes: [ + BaseItemKind.MusicAlbum, + BaseItemKind.Audio, + BaseItemKind.Playlist, + ], + compatibleCollectionTypes: [CollectionType.Movies, CollectionType.Boxsets], + }, + { + title: "Folder", + value: [ItemSortBy.IsFolder, ItemSortBy.SortName], + compatibleViewTypes: [BaseItemKind.Playlist], + }, + { + title: "Parental Rating", + value: ItemSortBy.OfficialRating, + compatibleViewTypes: [BaseItemKind.Playlist], + compatibleCollectionTypes: [ + CollectionType.Movies, + CollectionType.Tvshows, + CollectionType.Boxsets, + ], + }, + { + title: "Release Date", + value: [ + ItemSortBy.ProductionYear, + ItemSortBy.PremiereDate, + ItemSortBy.SortName, + ], + compatibleViewTypes: [ + BaseItemKind.MusicAlbum, + BaseItemKind.Audio, + BaseItemKind.Playlist, + ], + }, + { + title: "Date Episode Added", + value: ItemSortBy.DateLastContentAdded, + compatibleCollectionTypes: [CollectionType.Tvshows], + }, +]; + +export type FILTERS = + | "isPlayed" + | "isUnPlayed" + | "isResumable" + | "isFavorite" + | "hasSubtitles" + | "hasTrailer" + | "hasSpecialFeature" + | "hasThemeSong" + | "hasThemeVideo" + | "isSD" + | "isHD" + | "is4K" + | "is3D";