From 18aef998df03d181e5e221c91c161fc810a984b5 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Wed, 27 Sep 2023 16:16:50 +0530 Subject: [PATCH] Start adding search page #34 --- pnpm-lock.yaml | 12 +- src/App.jsx | 11 +- src/components/appBar/appBar.jsx | 2 +- src/routes/favorite/favorite.module.scss | 1 + src/routes/favorite/index.jsx | 7 + src/routes/favourite/index.jsx | 7 - src/routes/search/index.jsx | 739 +++++++++++++++++++++++ src/routes/search/search.module.scss | 24 + src/utils/hooks/useDebounce.js | 29 + 9 files changed, 815 insertions(+), 17 deletions(-) create mode 100644 src/routes/favorite/favorite.module.scss create mode 100644 src/routes/favorite/index.jsx delete mode 100644 src/routes/favourite/index.jsx create mode 100644 src/routes/search/index.jsx create mode 100644 src/routes/search/search.module.scss create mode 100644 src/utils/hooks/useDebounce.js diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6677235..5776702 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -79,10 +79,10 @@ dependencies: version: 1.1.0(react-dom@18.2.0)(react@18.2.0) tauri-plugin-log-api: specifier: github:tauri-apps/tauri-plugin-log#v1 - version: github.com/tauri-apps/tauri-plugin-log/9075595196d5a66f8f3265d2aaf7e2ffeb4cb2ab + version: github.com/tauri-apps/tauri-plugin-log/6f56109458b09942bfc6442d32ad8637c4e6a314 tauri-plugin-store-api: specifier: github:tauri-apps/tauri-plugin-store - version: github.com/tauri-apps/tauri-plugin-store/a65ce9bfb168a9a3cd7ed4102b9f22770cc3abfa + version: github.com/tauri-apps/tauri-plugin-store/66e06b7830037fdae0b42b5499e23334eaf4e017 uuid: specifier: ^9.0.1 version: 9.0.1 @@ -2315,16 +2315,16 @@ packages: use-sync-external-store: 1.2.0(react@18.2.0) dev: false - github.com/tauri-apps/tauri-plugin-log/9075595196d5a66f8f3265d2aaf7e2ffeb4cb2ab: - resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-log/tar.gz/9075595196d5a66f8f3265d2aaf7e2ffeb4cb2ab} + github.com/tauri-apps/tauri-plugin-log/6f56109458b09942bfc6442d32ad8637c4e6a314: + resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-log/tar.gz/6f56109458b09942bfc6442d32ad8637c4e6a314} name: tauri-plugin-log-api version: 0.0.0 dependencies: '@tauri-apps/api': 1.4.0 dev: false - github.com/tauri-apps/tauri-plugin-store/a65ce9bfb168a9a3cd7ed4102b9f22770cc3abfa: - resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-store/tar.gz/a65ce9bfb168a9a3cd7ed4102b9f22770cc3abfa} + github.com/tauri-apps/tauri-plugin-store/66e06b7830037fdae0b42b5499e23334eaf4e017: + resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-store/tar.gz/66e06b7830037fdae0b42b5499e23334eaf4e017} name: tauri-plugin-store-api version: 0.0.0 dependencies: diff --git a/src/App.jsx b/src/App.jsx index 08bc16d..cbb52fd 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -45,9 +45,10 @@ import { UserLogin, LoginWithImage, UserLoginManual } from "./routes/login"; import LibraryView from "./routes/library"; import ItemDetail from "./routes/item"; import SeriesTitlePage from "./routes/series/index.jsx"; -import FavouritePage from "./routes/favourite/index.jsx"; +import FavoritePage from "./routes/favorite/index.jsx"; import Settings from "./routes/settings"; import About from "./routes/about"; +import SearchPage from "./routes/search"; import { VideoPlayer } from "./routes/player/videoPlayer.jsx"; @@ -546,8 +547,12 @@ function App() { element={} /> } + path="/search" + element={} + /> + } /> {
- + navigate("/search")}> diff --git a/src/routes/favorite/favorite.module.scss b/src/routes/favorite/favorite.module.scss new file mode 100644 index 0000000..1a1f0eb --- /dev/null +++ b/src/routes/favorite/favorite.module.scss @@ -0,0 +1 @@ +/** @format */ diff --git a/src/routes/favorite/index.jsx b/src/routes/favorite/index.jsx new file mode 100644 index 0000000..c378d99 --- /dev/null +++ b/src/routes/favorite/index.jsx @@ -0,0 +1,7 @@ +/** @format */ + +const FavoritePage = () => { + <>; +}; + +export default FavoritePage; diff --git a/src/routes/favourite/index.jsx b/src/routes/favourite/index.jsx deleted file mode 100644 index 481c936..0000000 --- a/src/routes/favourite/index.jsx +++ /dev/null @@ -1,7 +0,0 @@ -/** @format */ - -const FavouritePage = () => { - return <>; -}; - -export default FavouritePage; diff --git a/src/routes/search/index.jsx b/src/routes/search/index.jsx new file mode 100644 index 0000000..98264d1 --- /dev/null +++ b/src/routes/search/index.jsx @@ -0,0 +1,739 @@ +/** @format */ +import { useEffect, useState } from "react"; + +import TextField from "@mui/material/TextField"; +import LinearProgress from "@mui/material/LinearProgress"; + +import { useBackdropStore } from "../../utils/store/backdrop"; + +import { MdiMagnify } from "../../components/icons/mdiMagnify"; + +import { useQuery } from "@tanstack/react-query"; + +import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; +import { getSearchApi } from "@jellyfin/sdk/lib/utils/api/search-api"; + +import useDebounce from "../../utils/hooks/useDebounce"; + +import "./search.module.scss"; +import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api"; +import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client"; +import { CardScroller } from "../../components/cardScroller/cardScroller"; +import { Card } from "../../components/card/card"; + +const SearchPage = () => { + const [searchTerm, setSearchTerm] = useState(""); + const searchParam = useDebounce(searchTerm, 500); + + const user = useQuery({ + queryKey: ["user"], + queryFn: async () => { + let usr = await getUserApi(window.api).getCurrentUser(); + return usr.data; + }, + networkMode: "always", + }); + + const items = useQuery({ + queryKey: ["search", "items", searchParam], + queryFn: async () => { + const result = await getItemsApi(window.api).getItemsByUserId({ + userId: user.data.Id, + searchTerm: searchTerm, + includeItemTypes: [ + BaseItemKind.Movie, + BaseItemKind.Series, + BaseItemKind.Audio, + BaseItemKind.MusicAlbum, + BaseItemKind.Book, + BaseItemKind.MusicArtist, + BaseItemKind.Person, + ], + recursive: true, + }); + return result.data; + }, + enabled: user.isSuccess, + }); + + /** + * @typedef {import("@jellyfin/sdk/lib/generated-client/models").BaseItemDto} BaseItemDto + * @type {[BaseItemDto[], Function]} + */ + const [movies, setMovies] = useState([]); + const [series, setSeries] = useState([]); + const [audios, setAudios] = useState([]); + const [musicAlbum, setMusicAlbum] = useState([]); + const [books, setBooks] = useState([]); + const [musicArtists, setMusicArtists] = useState([]); + const [persons, setPersons] = useState([]); + + const itemLimit = 16; + + useEffect(() => { + if (items.isSuccess) { + setMovies( + items.data.Items.filter( + (item) => item.Type == BaseItemKind.Movie, + ).slice(0, itemLimit), + ); + setSeries( + items.data.Items.filter( + (item) => item.Type == BaseItemKind.Series, + ).slice(0, itemLimit), + ); + setAudios( + items.data.Items.filter( + (item) => item.Type == BaseItemKind.Audio, + ).slice(0, itemLimit), + ); + setMusicAlbum( + items.data.Items.filter( + (item) => item.Type == BaseItemKind.MusicAlbum, + ).slice(0, itemLimit), + ); + setBooks( + items.data.Items.filter( + (item) => item.Type == BaseItemKind.Book, + ).slice(0, itemLimit), + ); + setMusicArtists( + items.data.Items.filter( + (item) => item.Type == BaseItemKind.MusicArtist, + ).slice(0, itemLimit), + ); + setPersons( + items.data.Items.filter( + (item) => item.Type == BaseItemKind.Person, + ).slice(0, itemLimit), + ); + } + }, [items.isSuccess]); + + const [setBackdrop] = useBackdropStore((state) => [state.setBackdrop]); + + useEffect(() => { + // Removing app's backdrop + setBackdrop("", ""); + }, []); + return ( +
+
+ setSearchTerm(e.target.value)} + /> +
+ {items.isLoading ? ( +
+ +
+ ) : ( +
+ {movies.length > 0 && ( + + {movies.map((item, index) => ( + + ))} + + )} + {series.length > 0 && ( + + {series.map((item, index) => ( + + ))} + + )} + {audios.length > 0 && ( + + {audios.map((item, index) => ( + + ))} + + )} + {musicAlbum.length > 0 && ( + + {musicAlbum.map((item, index) => ( + + ))} + + )} + {books.length > 0 && ( + + {books.map((item, index) => ( + + ))} + + )} + {musicArtists.length > 0 && ( + + {musicArtists.map((item, index) => ( + + ))} + + )} + {persons.length > 0 && ( + + {persons.map((item, index) => ( + + ))} + + )} +
+ )} +
+ ); +}; + +export default SearchPage; diff --git a/src/routes/search/search.module.scss b/src/routes/search/search.module.scss new file mode 100644 index 0000000..17b056d --- /dev/null +++ b/src/routes/search/search.module.scss @@ -0,0 +1,24 @@ +/** @format */ + +.search { + &-header { + width: 100%; + display: flex; + align-items: center; + justify-content: center; + gap: 1em; + height: 4em; + } + &-searchbar { + border-radius: 100px !important; + background: rgb(255 255 255 / 0.09); + border: none !important; + .MuiInputBase-root.MuiOutlinedInput-root.MuiInputBase-colorPrimary.MuiInputBase-formControl.MuiInputBase-hiddenLabel.css-187yyed-MuiInputBase-root-MuiOutlinedInput-root { + border-radius: 100px !important; + } + &-input { + text-align: center; + padding: 0.75em 1.5em !important; + } + } +} diff --git a/src/utils/hooks/useDebounce.js b/src/utils/hooks/useDebounce.js new file mode 100644 index 0000000..069c810 --- /dev/null +++ b/src/utils/hooks/useDebounce.js @@ -0,0 +1,29 @@ +/** @format */ + +import { useEffect, useState } from "react"; + +// see https://github.com/tannerlinsley/react-query/issues/293 +// see https://usehooks.com/useDebounce/ +export default function useDebounce(value, delay) { + // State and setters for debounced value + const [debouncedValue, setDebouncedValue] = useState(value); + + useEffect( + () => { + // Update debounced value after delay + const handler = setTimeout(() => { + setDebouncedValue(value); + }, delay); + + // Cancel the timeout if value changes (also on delay change or unmount) + // This is how we prevent debounced value from updating if value is changed ... + // .. within the delay period. Timeout gets cleared and restarted. + return () => { + clearTimeout(handler); + }; + }, + [value, delay], // Only re-call effect if value or delay changes + ); + + return debouncedValue; +}