From a7eab72639c1fde374ade97564274ba4722e57c1 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Mon, 25 Nov 2024 17:33:30 +0530 Subject: [PATCH] refactor: convert more files to typescript --- src/components/buttons/likeButton.tsx | 13 +- src/components/buttons/markPlayedButton.tsx | 15 +- src/components/buttons/playButton.tsx | 2 +- src/components/card/actorCards.jsx | 121 ---------- src/components/card/card.tsx | 14 +- src/components/card/episodeCard.jsx | 207 ------------------ .../{cardScroller.jsx => cardScroller.tsx} | 40 ++-- src/components/carousel/index.tsx | 58 ++--- .../homeSection/latestMediaSection.tsx | 2 +- .../layouts/library/genreView/index.jsx | 18 +- src/routes/_api/album/$id.tsx | 2 +- src/routes/_api/boxset/$id.tsx | 2 +- src/routes/_api/episode/$id.tsx | 2 +- src/routes/_api/favorite/index.tsx | 4 +- src/routes/_api/home/index.tsx | 2 +- src/routes/_api/item/$id.tsx | 2 +- src/routes/_api/playlist/$id.jsx | 4 +- src/routes/_api/search/index.tsx | 6 +- src/routes/_api/series/$id.tsx | 30 +-- 19 files changed, 93 insertions(+), 451 deletions(-) delete mode 100644 src/components/card/actorCards.jsx delete mode 100644 src/components/card/episodeCard.jsx rename src/components/cardScroller/{cardScroller.jsx => cardScroller.tsx} (85%) diff --git a/src/components/buttons/likeButton.tsx b/src/components/buttons/likeButton.tsx index d8fab5b..4a2e532 100644 --- a/src/components/buttons/likeButton.tsx +++ b/src/components/buttons/likeButton.tsx @@ -16,11 +16,11 @@ export default function LikeButton({ userId, itemName, }: { - itemId: string; - isFavorite: boolean; - queryKey: string[]; - userId: string; - itemName: string; + itemId: string | undefined; + isFavorite: boolean | undefined; + queryKey: string[] | undefined; + userId: string | undefined; + itemName: string | undefined; }) { const api = useApiInContext((s) => s.api); const queryClient = useQueryClient(); @@ -29,6 +29,9 @@ export default function LikeButton({ const handleLiking = async () => { let result: UserItemDataDto = null!; if (!api) return; + if (!userId) return; + if (!itemId) return; + if (!itemName) return; if (isFavorite) { result = ( await getUserLibraryApi(api).unmarkFavoriteItem({ diff --git a/src/components/buttons/markPlayedButton.tsx b/src/components/buttons/markPlayedButton.tsx index e98d5a9..908808b 100644 --- a/src/components/buttons/markPlayedButton.tsx +++ b/src/components/buttons/markPlayedButton.tsx @@ -16,11 +16,11 @@ export default function MarkPlayedButton({ userId, itemName, }: { - itemId: string; - isPlayed: boolean; - queryKey: string[]; - userId: string; - itemName: string; + itemId?: string; + isPlayed?: boolean; + queryKey?: string[]; + userId?: string; + itemName?: string; }) { const api = useApiInContext((s) => s.api); @@ -30,6 +30,11 @@ export default function MarkPlayedButton({ const handleMarking = async () => { let result = null; if (!api) return; + if (!api) return; + if (!userId) return; + if (!itemId) return; + if (!itemName) return; + if (!isPlayed) { result = await getPlaystateApi(api).markPlayedItem({ userId: userId, diff --git a/src/components/buttons/playButton.tsx b/src/components/buttons/playButton.tsx index a2bad4c..ba405bb 100644 --- a/src/components/buttons/playButton.tsx +++ b/src/components/buttons/playButton.tsx @@ -53,7 +53,7 @@ type PlayButtonProps = { iconOnly?: boolean; audio?: boolean; size?: "small" | "large" | "medium"; - playlistItem?: BaseItemDto; + playlistItem?: boolean; playlistItemId?: string; trackIndex?: number; }; diff --git a/src/components/card/actorCards.jsx b/src/components/card/actorCards.jsx deleted file mode 100644 index 2dc7dc4..0000000 --- a/src/components/card/actorCards.jsx +++ /dev/null @@ -1,121 +0,0 @@ -import React from "react"; -import { useNavigate } from "react-router-dom"; - -import MuiCard from "@mui/material/Card"; -import CardActionArea from "@mui/material/CardActionArea"; -import Typography from "@mui/material/Typography"; - -import { Blurhash } from "react-blurhash"; -import { useApi } from "../../utils/store/api"; -import "./card.scss"; -import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; - -/** - * @typedef {Object} Props - * @property {import("@jellyfin/sdk/lib/generated-client/models").BaseItemDto} item - * @property {string} cardTitle - * @property {string | number} cardCaption - * @property {string} imageBlurhash - * @property {string} seriesId - * @property {boolean} hideText - * @property {() => {}} onClick - * @property {boolean} disablePadding - */ - -/** - * @description Hero section for item pages - * @param {Props} - * @returns {React.Component} - */ - -export const ActorCard = ({ - item, - cardTitle, - cardCaption, - imageBlurhash, - seriesId, - hideText, - onClick, - disablePadding, -}) => { - const [api] = useApi((state) => [state.api]); - const navigate = useNavigate(); - const defaultOnClick = () => { - navigate(`/person/${item.Id}`); - }; - return ( -
-
-
- {!!imageBlurhash && ( - - )} -
-
person
-
- {item.Name} { - e.target.style.opacity = 1; - }} - className="card-image" - /> -
-
- - {cardTitle} - - - {cardCaption} - -
-
-
- ); -}; diff --git a/src/components/card/card.tsx b/src/components/card/card.tsx index 4cce857..c8fce83 100644 --- a/src/components/card/card.tsx +++ b/src/components/card/card.tsx @@ -37,8 +37,8 @@ const CardComponent = ({ item: BaseItemDto; cardTitle: string | undefined | null; cardCaption?: string | null | number; - imageType: ImageType; - cardType: string; + imageType?: ImageType; + cardType: "square" | "thumb" | "portrait"; queryKey?: string[]; userId?: string; seriesId?: string | null; @@ -145,17 +145,13 @@ const CardComponent = ({ {!disableOverlay && ( <> {} | undefined} onClick - */ - -/** - * @description Hero section for item pages - * @param {Props} - */ - -export const EpisodeCard = ({ - item, - cardTitle, - cardCaption, - imageBlurhash, - queryKey, - userId, - disableRunTime = false, - onClick, -}) => { - const api = useApiInContext((s) => s.api); - const navigate = useNavigate(); - const defaultOnClick = () => { - navigate({ to: `/episode/${item.Id}` }); - }; - return ( -
-
-
- {!!item.UserData && ( -
-
done
-
- )} - {!!imageBlurhash && ( - - )} -
-
tv_gen
-
- {item.Name} { - e.target.style.opacity = 1; - }} - className="card-image" - /> - -
- - - -
- {100 > item.UserData?.PlayedPercentage > 0 && ( - - )} -
-
-
- - {cardTitle} - - {!disableRunTime && ( - - {getRuntimeCompact(item.RunTimeTicks)} - - )} -
- - - {cardCaption} - -
-
-
- ); -}; diff --git a/src/components/cardScroller/cardScroller.jsx b/src/components/cardScroller/cardScroller.tsx similarity index 85% rename from src/components/cardScroller/cardScroller.jsx rename to src/components/cardScroller/cardScroller.tsx index fa284b1..3d2a551 100644 --- a/src/components/cardScroller/cardScroller.jsx +++ b/src/components/cardScroller/cardScroller.tsx @@ -1,11 +1,8 @@ -import PropTypes from "prop-types"; -import React, { useState } from "react"; +import React, { type ReactNode } from "react"; import Carousel from "react-multi-carousel"; import "react-multi-carousel/lib/styles.css"; -import Box from "@mui/material/Box"; -import ButtonGroup from "@mui/material/ButtonGroup"; import IconButton from "@mui/material/IconButton"; import Typography from "@mui/material/Typography"; @@ -30,14 +27,23 @@ function LeftButton(props) { ); } -export const CardScroller = ({ +type CardScrollerProps = { + children: ReactNode; + displayCards: number; + title: string; + headingProps?: object; + disableDecoration?: boolean; + boxProps?: object; +}; + +export default function CardScroller({ children, displayCards, title, headingProps, disableDecoration = false, boxProps, -}) => { +}: CardScrollerProps) { const responsive = { superLargeDesktop: { // the naming can be any, depends on you. @@ -73,9 +79,13 @@ export const CardScroller = ({ }; return ( - - +
{title} - +
{children} - +
); -}; - -CardScroller.propTypes = { - displayCards: PropTypes.number.isRequired, - title: PropTypes.string.isRequired, - headingProps: PropTypes.any, -}; +} diff --git a/src/components/carousel/index.tsx b/src/components/carousel/index.tsx index 8c738bc..3eb784a 100644 --- a/src/components/carousel/index.tsx +++ b/src/components/carousel/index.tsx @@ -1,7 +1,7 @@ import { AnimatePresence, motion } from "framer-motion"; +import type { PanInfo } from "framer-motion"; import React, { useEffect, useState, useMemo, useCallback } from "react"; - -import IconButton from "@mui/material/IconButton"; +import type { MouseEvent, PointerEvent } from "react"; import { useCarouselStore } from "../../utils/store/carousel"; import "./carousel.scss"; @@ -10,12 +10,11 @@ import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; import CarouselSlide from "../carouselSlide"; import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; -import ReactMultiCarousel from "react-multi-carousel"; import Slider from "../Slider"; import { getTypeIcon } from "../utils/iconsCollection"; const swipeConfidenceThreshold = 8000; -const swipePower = (offset, velocity) => { +const swipePower = (offset: number, velocity: number) => { return Math.abs(offset) * velocity; }; @@ -30,14 +29,11 @@ const Carousel = ({ onChange, }: { content: BaseItemDto[]; - onChange: () => void; + onChange: (currentSlide: number) => void; }) => { const [currentSlide, setCurrentSlide] = useState(0); - const [setDirection, dir] = useCarouselStore((state) => [ - state.setDirection, - state.direction, - ]); + const [setDirection] = useCarouselStore((state) => [state.setDirection]); useEffect(() => { onChange(currentSlide); @@ -45,27 +41,6 @@ const Carousel = ({ const api = useApiInContext((s) => s.api); - const responsive = { - superLargeDesktop: { - breakpoint: { max: 4000, min: 3000 }, - items: 6, - }, - desktop: { - breakpoint: { max: 3000, min: 1024 }, - items: 6, - }, - tablet: { - breakpoint: { max: 1024, min: 464 }, - items: 4, - }, - mobile: { - breakpoint: { max: 464, min: 0 }, - items: 1, - }, - }; - - - // Memoize the content mapping const sliderContent = useMemo(() => { return content.map((item, index) => { @@ -98,16 +73,23 @@ const Carousel = ({ > {item.ImageTags?.Thumb ? ( {item.Name ) : (
- {getTypeIcon(item.Type)} + {getTypeIcon(item.Type ?? "Movie")}
)} @@ -116,7 +98,10 @@ const Carousel = ({ }, [content, currentSlide, api, setDirection]); const handleDragEnd = useCallback( - (e, { offset, velocity }) => { + ( + _: MouseEvent | TouchEvent | PointerEvent, + { offset, velocity }: PanInfo, + ) => { const swipe = swipePower(offset.x, velocity.x); if ( currentSlide !== content.length - 1 && @@ -147,6 +132,7 @@ const Carousel = ({ drag={"x"} dragConstraints={{ left: 0, right: 0 }} dragElastic={1} + //@ts-ignore onDragEnd={handleDragEnd} style={{ height: "100%", diff --git a/src/components/layouts/homeSection/latestMediaSection.tsx b/src/components/layouts/homeSection/latestMediaSection.tsx index d3aa9c3..bce17c6 100644 --- a/src/components/layouts/homeSection/latestMediaSection.tsx +++ b/src/components/layouts/homeSection/latestMediaSection.tsx @@ -1,7 +1,7 @@ import { useQuery } from "@tanstack/react-query"; import React from "react"; import { Card } from "../../card/card"; -import { CardScroller } from "../../cardScroller/cardScroller"; +import CardScroller from "../../cardScroller/cardScroller"; import { CardsSkeleton } from "../../skeleton/cards"; import { useApiInContext } from "@/utils/store/api"; diff --git a/src/components/layouts/library/genreView/index.jsx b/src/components/layouts/library/genreView/index.jsx index 6346dd4..abc9815 100644 --- a/src/components/layouts/library/genreView/index.jsx +++ b/src/components/layouts/library/genreView/index.jsx @@ -6,7 +6,7 @@ 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"; +import cardScroller from "../../../cardScroller/cardScroller"; const GenreView = ({ libraryId, genreId, genreName, userId }) => { const api = useApiInContext((s) => s.api); @@ -25,7 +25,11 @@ const GenreView = ({ libraryId, genreId, genreName, userId }) => { if (items.isSuccess && items.data.Items.length > 0) { return (
- + {items.data.Items.map((item) => { return ( { item.Type === BaseItemKind.Episode ? `S${item.ParentIndexNumber}:E${item.IndexNumber} - ${item.Name}` : item.Type === BaseItemKind.Series - ? `${item.ProductionYear} - ${ + ? `${item.ProductionYear} - ${ item.EndDate ? new Date(item.EndDate).toLocaleString([], { year: "numeric", - }) + }) : "Present" - }` - : item.ProductionYear + }` + : item.ProductionYear } disableOverlay={ item.Type === BaseItemKind.Person || @@ -79,7 +83,7 @@ const GenreView = ({ libraryId, genreId, genreName, userId }) => { /> ); })} - +
); } diff --git a/src/routes/_api/album/$id.tsx b/src/routes/_api/album/$id.tsx index 47bfac9..fb21e75 100644 --- a/src/routes/_api/album/$id.tsx +++ b/src/routes/_api/album/$id.tsx @@ -19,7 +19,7 @@ import { useQuery } from "@tanstack/react-query"; import { getRuntimeCompact, getRuntimeMusic } from "@/utils/date/time"; import { Card } from "@/components/card/card"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import LikeButton from "@/components/buttons/likeButton"; import PlayButton from "@/components/buttons/playButton"; diff --git a/src/routes/_api/boxset/$id.tsx b/src/routes/_api/boxset/$id.tsx index bf66317..77a9ea6 100644 --- a/src/routes/_api/boxset/$id.tsx +++ b/src/routes/_api/boxset/$id.tsx @@ -22,7 +22,7 @@ import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api" import { useQuery } from "@tanstack/react-query"; import { Card } from "@/components/card/card"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice"; import ShowMoreText from "@/components/showMoreText"; diff --git a/src/routes/_api/episode/$id.tsx b/src/routes/_api/episode/$id.tsx index f8c78c5..fa4d892 100644 --- a/src/routes/_api/episode/$id.tsx +++ b/src/routes/_api/episode/$id.tsx @@ -30,7 +30,7 @@ import { useQuery } from "@tanstack/react-query"; import heroBg from "@/assets/herobg.png"; import { Card } from "@/components/card/card"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import LikeButton from "@/components/buttons/likeButton"; import MarkPlayedButton from "@/components/buttons/markPlayedButton"; diff --git a/src/routes/_api/favorite/index.tsx b/src/routes/_api/favorite/index.tsx index 7a79100..0c23b91 100644 --- a/src/routes/_api/favorite/index.tsx +++ b/src/routes/_api/favorite/index.tsx @@ -7,12 +7,12 @@ import { useQuery } from "@tanstack/react-query"; import "./favorite.scss"; import { Card } from "@/components/card/card"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; +import { useCentralStore } from "@/utils/store/central"; import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client"; import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; import { createFileRoute } from "@tanstack/react-router"; -import { useCentralStore } from "@/utils/store/central"; export const Route = createFileRoute("/_api/favorite/")({ component: FavoritePage, diff --git a/src/routes/_api/home/index.tsx b/src/routes/_api/home/index.tsx index 0496074..b946907 100644 --- a/src/routes/_api/home/index.tsx +++ b/src/routes/_api/home/index.tsx @@ -8,7 +8,7 @@ import Carousel from "@/components/carousel"; // Custom Components import { Card } from "@/components/card/card"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import { LatestMediaSection } from "@/components/layouts/homeSection/latestMediaSection"; import { CardsSkeleton } from "@/components/skeleton/cards"; import { CarouselSkeleton } from "@/components/skeleton/carousel"; diff --git a/src/routes/_api/item/$id.tsx b/src/routes/_api/item/$id.tsx index c41b27e..e52462c 100644 --- a/src/routes/_api/item/$id.tsx +++ b/src/routes/_api/item/$id.tsx @@ -30,7 +30,7 @@ import { useQuery, useSuspenseQuery } from "@tanstack/react-query"; import heroBg from "@/assets/herobg.png"; import { Card } from "@/components/card/card"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import "./item.scss"; diff --git a/src/routes/_api/playlist/$id.jsx b/src/routes/_api/playlist/$id.jsx index 2387d9c..749ba2e 100644 --- a/src/routes/_api/playlist/$id.jsx +++ b/src/routes/_api/playlist/$id.jsx @@ -13,14 +13,14 @@ import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api" import { useQuery } from "@tanstack/react-query"; import { Card } from "@/components/card/card"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import MusicTrack from "@/components/musicTrack"; import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice"; import { useBackdropStore } from "@/utils/store/backdrop"; import "./playlist.scss"; -import { createFileRoute } from "@tanstack/react-router"; import { useCentralStore } from "@/utils/store/central"; +import { createFileRoute } from "@tanstack/react-router"; function TabPanel(props) { const { children, value, index, ...other } = props; diff --git a/src/routes/_api/search/index.tsx b/src/routes/_api/search/index.tsx index 86b9324..147675c 100644 --- a/src/routes/_api/search/index.tsx +++ b/src/routes/_api/search/index.tsx @@ -15,7 +15,7 @@ import useDebounce from "@/utils/hooks/useDebounce"; import { Card } from "@/components/card/card"; import { EpisodeCard } from "@/components/card/episodeCard"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import { BaseItemKind } from "@jellyfin/sdk/lib/generated-client"; import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api"; import { getPersonsApi } from "@jellyfin/sdk/lib/utils/api/persons-api"; @@ -263,14 +263,14 @@ function SearchPage() { {episodes.data.SearchHints?.map((episode) => { return ( - ); })} diff --git a/src/routes/_api/series/$id.tsx b/src/routes/_api/series/$id.tsx index 2aabd08..d73f546 100644 --- a/src/routes/_api/series/$id.tsx +++ b/src/routes/_api/series/$id.tsx @@ -41,8 +41,7 @@ import { } from "@/utils/date/time"; import { Card } from "@/components/card/card"; -import { EpisodeCard } from "@/components/card/episodeCard"; -import { CardScroller } from "@/components/cardScroller/cardScroller"; +import CardScroller from "@/components/cardScroller/cardScroller"; import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice"; import "./series.scss"; @@ -885,33 +884,6 @@ function SeriesTitlePage() { - {nextUpEpisode.isSuccess && nextUpEpisode.data.TotalRecordCount > 0 && ( - - {nextUpEpisode.data.Items.map((episode) => { - return ( - - ); - })} - - )} - {seasons.isPending ? ( ) : (