From 98078585d681cffe86ef15b26d130e40e2c31ed1 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Thu, 28 Nov 2024 22:05:17 +0530 Subject: [PATCH] refactor: change GenreView to genreFilter --- src-tauri/tauri.conf.json | 1 - src/components/buttons/playButton.tsx | 2 +- .../homeSection/latestMediaSection.tsx | 16 +- .../layouts/library/genreView/index.jsx | 92 --- src/components/layouts/tracksList/index.tsx | 90 --- .../layouts/tracksList/tracksList.scss | 39 - src/components/musicTrack/index.tsx | 13 - src/routes/_api/library/$id.tsx | 688 ++++++++++-------- 8 files changed, 389 insertions(+), 552 deletions(-) delete mode 100644 src/components/layouts/library/genreView/index.jsx delete mode 100644 src/components/layouts/tracksList/index.tsx delete mode 100644 src/components/layouts/tracksList/tracksList.scss diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 78b70f4..93ac6fa 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,5 +1,4 @@ { - "$schema": "../node_modules/@tauri-apps/cli/schema.json", "build": { "beforeBuildCommand": "pnpm run build", "beforeDevCommand": "pnpm run dev", diff --git a/src/components/buttons/playButton.tsx b/src/components/buttons/playButton.tsx index bac38f7..1477b2a 100644 --- a/src/components/buttons/playButton.tsx +++ b/src/components/buttons/playButton.tsx @@ -379,7 +379,7 @@ const PlayButton = ({ // Subtitle const subtitle = getSubtitle( currentSubTrack, - result?.mediaSource?.MediaSources?.[0].MediaStreams, + result?.mediaSource?.MediaSources?.[0]?.MediaStreams, ); // URL generation const urlOptions: URLSearchParams = { diff --git a/src/components/layouts/homeSection/latestMediaSection.tsx b/src/components/layouts/homeSection/latestMediaSection.tsx index bce17c6..395d9f4 100644 --- a/src/components/layouts/homeSection/latestMediaSection.tsx +++ b/src/components/layouts/homeSection/latestMediaSection.tsx @@ -10,7 +10,6 @@ import { type BaseItemDto, BaseItemKind, } from "@jellyfin/sdk/lib/generated-client"; -import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api"; /** @@ -22,8 +21,9 @@ export const LatestMediaSection = ({ const api = useApiInContext((s) => s.api); const user = useCentralStore((s) => s.currentUser); const fetchLatestMedia = async (library: BaseItemDto) => { + if (!api || !user?.Id) return null; const media = await getUserLibraryApi(api).getLatestMedia({ - userId: user?.Id, + userId: user.Id, parentId: library.Id, limit: 16, fields: ["PrimaryImageAspectRatio", "ParentId"], @@ -39,10 +39,10 @@ export const LatestMediaSection = ({ if (data.isPending) { return ; } - if (data.isSuccess && data.data.length >= 1) { + if (data.isSuccess && (data.data?.length ?? 0) >= 1) { return ( - {data.data.map((item) => { + {data.data?.map((item) => { return ( ); })} diff --git a/src/components/layouts/library/genreView/index.jsx b/src/components/layouts/library/genreView/index.jsx deleted file mode 100644 index abc9815..0000000 --- a/src/components/layouts/library/genreView/index.jsx +++ /dev/null @@ -1,92 +0,0 @@ -import { useApiInContext } from "@/utils/store/api"; -import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client"; -import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api"; -import Typography from "@mui/material/Typography"; -import { useQuery } from "@tanstack/react-query"; -import { useRouteContext } from "@tanstack/react-router"; -import React from "react"; -import { Card } from "../../../card/card"; -import cardScroller from "../../../cardScroller/cardScroller"; - -const GenreView = ({ libraryId, genreId, genreName, userId }) => { - const api = useApiInContext((s) => s.api); - - const items = useQuery({ - queryKey: ["library", "genreItem", genreId], - queryFn: async () => { - const result = await getItemsApi(api).getItems({ - genreIds: [genreId], - parentId: libraryId, - userId: userId, - }); - return result.data; - }, - }); - if (items.isSuccess && items.data.Items.length > 0) { - return ( -
- - {items.data.Items.map((item) => { - return ( - - ); - })} - -
- ); - } -}; - -export default GenreView; diff --git a/src/components/layouts/tracksList/index.tsx b/src/components/layouts/tracksList/index.tsx deleted file mode 100644 index 5629b1b..0000000 --- a/src/components/layouts/tracksList/index.tsx +++ /dev/null @@ -1,90 +0,0 @@ -import Typography from "@mui/material/Typography"; -import React from "react"; - -// Types -import type { BaseItemDto, UserDto } from "@jellyfin/sdk/lib/generated-client"; - -import { getRuntimeMusic } from "../../../utils/date/time"; -import { useAudioPlayback } from "../../../utils/store/audioPlayback"; -import LikeButton from "../../buttons/likeButton"; -import "./tracksList.scss"; -import { useApiInContext } from "@/utils/store/api"; -import { useRouteContext } from "@tanstack/react-router"; - -export default function TracksList({ - tracks, - user, - queryKey, -}: { tracks: BaseItemDto[]; user: UserDto; queryKey: [] }) { - const api = useApiInContext((s) => s.api); - - const [ - currentItem, - setCurrentAudioTrack, - setAudioUrl, - setAudioDisplay, - setAudioItem, - setAudioTracks, - ] = useAudioPlayback((state) => [ - state.item, - state.setCurrentTrack, - state.setUrl, - state.setDisplay, - state.setItem, - state.setTracks, - ]); - const playTrack = (index: number) => { - setAudioTracks(tracks); - setCurrentAudioTrack(index); - setAudioUrl( - `${api.basePath}/Audio/${tracks[index].Id}/universal?deviceId=${api.deviceInfo.id}&userId=${user.Id}&api_key=${api.accessToken}`, - ); - setAudioItem(tracks[index]); - setAudioDisplay(true); - }; - return ( -
-
- # -
{""}
- - Name - - schedule -
- {tracks.map((track, index) => ( -
playTrack(index)} - > - {currentItem?.Id === track.Id ? ( - equalizer - ) : ( - {track.IndexNumber ?? "-"} - )} - - - {track.Name} - - {getRuntimeMusic(track?.RunTimeTicks)} -
- ))} -
- ); -} diff --git a/src/components/layouts/tracksList/tracksList.scss b/src/components/layouts/tracksList/tracksList.scss deleted file mode 100644 index 41f8cc5..0000000 --- a/src/components/layouts/tracksList/tracksList.scss +++ /dev/null @@ -1,39 +0,0 @@ -.track{ - display: grid; - grid-template-columns: 4% 1fr 80% 1fr; - align-items: center; - justify-items: center; - gap: 0.5em; - width: 100%; - transition: background $transition-time-default; - padding: 0.4em; - border-bottom: 1px solid rgb(255 255 255 / 0.1); - cursor: pointer; - box-sizing: border-box; - - &:hover{ - background: rgb(255 255 255 / 0.1); - } - &.playing { - background: rgb(255 255 255 / 0.2); - } - - &-list { - background: rgb(0 0 0 / 0.4); - backdrop-filter: blur(5px); - border-radius: 20px; - overflow: hidden; - border: 1px solid rgb(255 255 255 / 0.2); - margin-bottom: 2em; - } - &-header{ - background: rgb(0 0 0 / 0.4) !important; - padding: 0.8em 0.4em; - } - > *:first-child { - justify-self: end; - } - &:last-child { - border-bottom: 0px solid !important; - } -} \ No newline at end of file diff --git a/src/components/musicTrack/index.tsx b/src/components/musicTrack/index.tsx index b563e26..0e704d3 100644 --- a/src/components/musicTrack/index.tsx +++ b/src/components/musicTrack/index.tsx @@ -11,22 +11,9 @@ import { useApiInContext } from "@/utils/store/api"; import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; import lyrics_icon from "../../assets/icons/lyrics.svg"; -/** - * @typedef {Object} Props - * @property {import("@jellyfin/sdk/lib/generated-client/models").BaseItemDto} item - * @property {Array} queryKey - * @property {string} userId - * @property {bool} playlistItem - * @property {number} trackIndex - * @property {string | undefined} playlistItemId - */ - /** * @description Music Track element displayed in ArtistTitlePage and Songs library - * @param {Props} - * @returns {React.Component} */ - const MusicTrack = ({ item, queryKey, diff --git a/src/routes/_api/library/$id.tsx b/src/routes/_api/library/$id.tsx index c85ddfe..8094129 100644 --- a/src/routes/_api/library/$id.tsx +++ b/src/routes/_api/library/$id.tsx @@ -28,11 +28,11 @@ 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 GenreView from "@/components/layouts/library/genreView"; import MusicTrack from "@/components/musicTrack"; import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice"; import { useBackdropStore } from "@/utils/store/backdrop"; import { + type BaseItemDto, type BaseItemDtoQueryResult, BaseItemKind, CollectionType, @@ -43,13 +43,7 @@ import { } from "@jellyfin/sdk/lib/generated-client"; import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api"; -import { - FormControl, - Menu, - Select, - Stack, - useScrollTrigger, -} from "@mui/material"; +import { FormControl, Menu, Stack, useScrollTrigger } from "@mui/material"; import { useWindowVirtualizer } from "@tanstack/react-virtual"; import type { AxiosResponse } from "axios"; import "./library.scss"; @@ -83,10 +77,10 @@ const useWindowWidth = () => { }; export const Route = createFileRoute("/_api/library/$id")({ - component: LibraryView, + component: Library, }); -function LibraryView() { +function Library() { const api = useApiInContext((s) => s.api); const { id } = Route.useParams(); @@ -94,8 +88,9 @@ function LibraryView() { const user = useCentralStore((s) => s.currentUser); const currentLib = useQuery({ - queryKey: ["libraryView", "currentLib", id], + queryKey: ["library", "currentLib", id], queryFn: async () => { + if (!api) return null; const result = await getUserLibraryApi(api).getItem({ userId: user?.Id, itemId: id, @@ -107,9 +102,58 @@ function LibraryView() { staleTime: 0, }); - const [currentViewType, setCurrentViewType] = useState( - undefined!, + 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) { @@ -486,7 +530,7 @@ function LibraryView() { const items = useQuery({ queryKey: [ - "libraryView", + "library", "currentLibItems", id, { @@ -495,9 +539,11 @@ function LibraryView() { sortBy: sortBy, filters, videoTypesState, + genreFilter, }, ], queryFn: async () => { + if (!api) return null; let result: AxiosResponse; if (currentViewType === "MusicArtist") { result = await getArtistsApi(api).getAlbumArtists({ @@ -514,16 +560,6 @@ function LibraryView() { userId: user?.Id, personTypes: ["Actor"], }); - } else if (currentViewType === "Genre") { - result = await getGenresApi(api).getGenres({ - userId: user?.Id, - parentId: id, - }); - } else if (currentViewType === "MusicGenre") { - result = await getMusicGenresApi(api).getMusicGenres({ - userId: user?.Id, - parentId: id, - }); } else if (currentViewType === "Studio") { result = await getStudiosApi(api).getStudios({ userId: user?.Id, @@ -563,6 +599,7 @@ function LibraryView() { is4K: filters.is4K || undefined, is3D: filters.is3D || undefined, enableUserData: true, + genreIds: genreFilter, }); } else { const videoTypes: VideoType[] = []; @@ -598,14 +635,13 @@ function LibraryView() { is4K: filters.is4K || undefined, is3D: filters.is3D || undefined, enableUserData: true, + genreIds: genreFilter, }); } return result.data; }, - // enabled: false, enabled: !!user?.Id && Boolean(currentViewType) && currentLib.isSuccess, networkMode: "always", - // gcTime: 0, }); const disableSort = useMemo(() => { @@ -646,7 +682,7 @@ function LibraryView() { ? 260 : 356, [items.dataUpdatedAt], - ); // This sets the approx height of each card in libraryView to estimate no of virtual rows to render + ); // This sets the approx height of each card in library to estimate no of virtual rows to render const windowWidth = useWindowWidth(); const itemsPerRow = useMemo( () => Math.max(1, Math.floor(windowWidth / 200)), @@ -694,10 +730,10 @@ function LibraryView() { const backdropItems = useMemo(() => { if (items.isSuccess) { - const temp = items.data.Items?.filter( + const temp = items.data?.Items?.filter( (item) => item.BackdropImageTags?.length, ); - if (temp?.[0]?.Id) { + if (temp?.[0]?.Id && api) { setBackdrop( getImageUrlsApi(api).getItemImageUrlById(temp?.[0]?.Id, "Backdrop", { tag: temp?.[0]?.BackdropImageTags?.[0], @@ -710,7 +746,7 @@ function LibraryView() { }, [items]); useEffect(() => { - if (backdropItems?.length) { + if (backdropItems?.length && api) { const intervalId = setInterval(() => { const itemIndex = Math.floor(Math.random() * backdropItems?.length); const backdropItem = backdropItems[itemIndex]; @@ -730,8 +766,6 @@ function LibraryView() { } }, [backdropItems]); - const [test, setTest] = useState(1); - if (items.isError) { console.error(items.error); } @@ -778,7 +812,7 @@ function LibraryView() { {currentLib.isPending ? ( ) : ( - currentLib.data.Name + currentLib.data?.Name )} ) : ( - items.data.TotalRecordCount + items.data?.TotalRecordCount ) } /> @@ -880,7 +914,7 @@ function LibraryView() { )} {!disableFilters && ( - <> +
)} @@ -1174,18 +1287,6 @@ function LibraryView() { "Install the trailers channel to enhance your movie experience by adding a library of internet trailers." } /> - ) : currentViewType === BaseItemKind.Genre ? ( - items.data?.Items?.map((item) => { - return ( - - ); - }) ) : currentViewType !== BaseItemKind.Audio ? (
{rowVirtualizer.getVirtualItems().map((virtualRow) => { const item = items.data?.Items?.[virtualRow.index]; + if (!item) return null; return (
@@ -1341,5 +1421,3 @@ function LibraryView() { return ; } } - -export default LibraryView;