From cf8c5d4490171cea66fdf4a17a4b5362f2ddf89d Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Tue, 3 Dec 2024 22:21:48 +0530 Subject: [PATCH] refactor: move more files to typescript --- src/components/musicTrack/index.tsx | 25 ++++---- src/components/skeleton/item.tsx | 2 +- src/components/textLink/index.jsx | 21 ------- src/components/textLink/textLink.scss | 11 ---- src/routes/_api/episode/$id.tsx | 63 ++++++++++--------- src/utils/date/formateDate.js | 11 ---- src/utils/date/formateDate.ts | 11 ++++ src/utils/date/{time.js => time.ts} | 16 ++--- .../hooks/{useDebounce.js => useDebounce.ts} | 2 +- src/utils/hooks/useIntersecting.jsx | 21 ------- src/utils/hooks/useInterval.tsx | 12 ++-- .../{useKeyPress.jsx => useKeyPress.tsx} | 6 +- src/utils/hooks/useParallax.tsx | 2 +- src/utils/store/api.tsx | 1 - src/utils/store/audioPlayback.ts | 9 +-- src/utils/store/backdrop.tsx | 27 +++----- src/utils/store/{carousel.js => carousel.ts} | 7 ++- src/utils/store/drawer.js | 10 --- src/utils/store/drawer.ts | 17 +++++ src/utils/store/photosPlayback.ts | 26 +++----- 20 files changed, 123 insertions(+), 177 deletions(-) delete mode 100644 src/components/textLink/index.jsx delete mode 100644 src/components/textLink/textLink.scss delete mode 100644 src/utils/date/formateDate.js create mode 100644 src/utils/date/formateDate.ts rename src/utils/date/{time.js => time.ts} (89%) rename src/utils/hooks/{useDebounce.js => useDebounce.ts} (92%) delete mode 100644 src/utils/hooks/useIntersecting.jsx rename src/utils/hooks/{useKeyPress.jsx => useKeyPress.tsx} (85%) rename src/utils/store/{carousel.js => carousel.ts} (55%) delete mode 100644 src/utils/store/drawer.js create mode 100644 src/utils/store/drawer.ts diff --git a/src/components/musicTrack/index.tsx b/src/components/musicTrack/index.tsx index 0e704d3..deb889b 100644 --- a/src/components/musicTrack/index.tsx +++ b/src/components/musicTrack/index.tsx @@ -4,11 +4,11 @@ import { getRuntimeMusic } from "../../utils/date/time"; import { useAudioPlayback } from "../../utils/store/audioPlayback"; import LikeButton from "../buttons/likeButton"; import PlayButton from "../buttons/playButton"; -import TextLink from "../textLink"; import "./musicTrack.scss"; import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; import { useApiInContext } from "@/utils/store/api"; import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; +import { Link } from "@tanstack/react-router"; import lyrics_icon from "../../assets/icons/lyrics.svg"; /** @@ -114,18 +114,21 @@ const MusicTrack = ({ style={{ display: "inline-flex", alignItems: "center" }} key={artist.Id} > - - {artist.Name} - + + {artist.Name} + + {index !== (item.ArtistItems?.length ?? 0) - 1 && ( { diff --git a/src/components/textLink/index.jsx b/src/components/textLink/index.jsx deleted file mode 100644 index 0ede8ef..0000000 --- a/src/components/textLink/index.jsx +++ /dev/null @@ -1,21 +0,0 @@ -import Typography from "@mui/material/Typography"; - -import "./textLink.scss"; -import { useNavigate } from "@tanstack/react-router"; - -const TextLink = ({ children, location, variant, otherProps }) => { - const navigate = useNavigate(); - - return ( - navigate({ to: location })} - {...otherProps} - > - {children} - - ); -}; - -export default TextLink; diff --git a/src/components/textLink/textLink.scss b/src/components/textLink/textLink.scss deleted file mode 100644 index 088c077..0000000 --- a/src/components/textLink/textLink.scss +++ /dev/null @@ -1,11 +0,0 @@ - -.link { - text-decoration: underline; - text-decoration-color: rgb(255 255 255 / 0.5); - white-space: pre-line; - width: fit-content; - &:hover { - cursor: pointer; - text-decoration-color: rgb(255 255 255 / 1); - } -} diff --git a/src/routes/_api/episode/$id.tsx b/src/routes/_api/episode/$id.tsx index 5a55a46..679f8e2 100644 --- a/src/routes/_api/episode/$id.tsx +++ b/src/routes/_api/episode/$id.tsx @@ -419,38 +419,41 @@ function EpisodeTitlePage() { )}
- - {item.data.ParentLogoItemId?.length > 0 ? ( - {item.data.SeriesName} { - e.currentTarget.style.opacity = 1; - }} - className="item-hero-logo" - /> - ) : ( - item.data.SeriesName - )} - + + {item.data.ParentLogoItemId?.length > 0 ? ( + {item.data.SeriesName} { + e.currentTarget.style.opacity = 1; + }} + className="item-hero-logo" + /> + ) : ( + item.data.SeriesName + )} + + S{item.data.ParentIndexNumber ?? 0}:E {item.data.IndexNumberEnd diff --git a/src/utils/date/formateDate.js b/src/utils/date/formateDate.js deleted file mode 100644 index 76a8d04..0000000 --- a/src/utils/date/formateDate.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * @format - * @param {string} date - Date from server - * @returns {string} Formated Date - */ - -export const formateDate = (date) => { - let date = new Date(date); - date = new Intl.DateTimeFormat().format(date); - return date; -}; diff --git a/src/utils/date/formateDate.ts b/src/utils/date/formateDate.ts new file mode 100644 index 0000000..168b389 --- /dev/null +++ b/src/utils/date/formateDate.ts @@ -0,0 +1,11 @@ +/** + * @format + * @param {string} date - Date from server + * @returns {string} Formated Date + */ + +export const formateDate = (date: string | number | Date) => { + const dateObj = new Date(date); + const formatedDate = new Intl.DateTimeFormat().format(dateObj); + return formatedDate; +}; diff --git a/src/utils/date/time.js b/src/utils/date/time.ts similarity index 89% rename from src/utils/date/time.js rename to src/utils/date/time.ts index 8c055f6..c3a8433 100644 --- a/src/utils/date/time.js +++ b/src/utils/date/time.ts @@ -3,7 +3,7 @@ * @param {number} ticks - C# ticks of a particular item * @return {number} Converted ticks to Milliseconds */ -export const ticksToMs = (ticks) => { +export const ticksToMs = (ticks: number) => { return Math.round(ticks / 10000); }; @@ -12,7 +12,7 @@ export const ticksToMs = (ticks) => { * @param {number} ticks - C# ticks of a particular item * @return {number} Converted ticks to Seconds */ -export const ticksToSec = (ticks) => { +export const ticksToSec = (ticks: number) => { return Math.round(ticksToMs(ticks) / 1000); }; @@ -21,7 +21,7 @@ export const ticksToSec = (ticks) => { * @param {number} sec - Sec of a particular item * @return {number} Converted Seconds to C# ticks */ -export const secToTicks = (ticks) => { +export const secToTicks = (ticks: number) => { return Math.round(ticks * 10000000); }; @@ -30,7 +30,7 @@ export const secToTicks = (ticks) => { * @param {number} ticks - C# ticks of a particular item * @return {number} Converted ticks to Hhr Mmin formate */ -export const getRuntime = (ticks) => { +export const getRuntime = (ticks: number) => { const time = ticksToMs(ticks); let formatedTime = ""; let timeSec = Math.floor(time / 1000); @@ -51,7 +51,7 @@ export const getRuntime = (ticks) => { * @param {number} ticks - C# ticks of a particular item * @return {number} Converted ticks to hh:mm:ss formate */ -export const getRuntimeMusic = (ticks) => { +export const getRuntimeMusic = (ticks: number) => { const time = ticksToMs(ticks); let formatedTime = ""; let timeSec = Math.floor(time / 1000); @@ -82,7 +82,7 @@ export const getRuntimeMusic = (ticks) => { * @param {number} ticks - C# ticks of a particular item * @return {number} Converted ticks to H hour M minutes formate */ -export const getRuntimeFull = (ticks) => { +export const getRuntimeFull = (ticks: number) => { const time = ticksToMs(ticks); let formatedTime = ""; let timeSec = Math.floor(time / 1000); @@ -103,7 +103,7 @@ export const getRuntimeFull = (ticks) => { * @param {number} ticks - C# ticks of a particular item * @return {number} Converted ticks to Hh Mm formate */ -export const getRuntimeCompact = (ticks) => { +export const getRuntimeCompact = (ticks: number) => { const time = ticksToMs(ticks); let formatedTime = ""; let timeSec = Math.floor(time / 1000); @@ -124,7 +124,7 @@ export const getRuntimeCompact = (ticks) => { * @param {number} ticks - C# ticks of a particular item * @return {number} End time of an item */ -export const endsAt = (ticks) => { +export const endsAt = (ticks: number) => { const current = new Date(); const currentTime = current.getTime(); const time = ticksToMs(ticks); diff --git a/src/utils/hooks/useDebounce.js b/src/utils/hooks/useDebounce.ts similarity index 92% rename from src/utils/hooks/useDebounce.js rename to src/utils/hooks/useDebounce.ts index e49a343..2a418a7 100644 --- a/src/utils/hooks/useDebounce.js +++ b/src/utils/hooks/useDebounce.ts @@ -2,7 +2,7 @@ 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) { +export default function useDebounce(value: string, delay: number) { // State and setters for debounced value const [debouncedValue, setDebouncedValue] = useState(value); diff --git a/src/utils/hooks/useIntersecting.jsx b/src/utils/hooks/useIntersecting.jsx deleted file mode 100644 index 6d2056d..0000000 --- a/src/utils/hooks/useIntersecting.jsx +++ /dev/null @@ -1,21 +0,0 @@ -import { useEffect, useMemo, useState } from "react"; - -const useIntersecting = (ref) => { - const [isIntersecting, setIntersecting] = useState(false); - - const observer = useMemo( - () => - new IntersectionObserver(([entry]) => - setIntersecting(entry.isIntersecting), - ), - ); - - useEffect(() => { - observer.observe(ref.current); - return () => observer.disconnect(); - }); - - return isIntersecting; -}; - -export default useIntersecting; diff --git a/src/utils/hooks/useInterval.tsx b/src/utils/hooks/useInterval.tsx index 0bf5156..c06d607 100644 --- a/src/utils/hooks/useInterval.tsx +++ b/src/utils/hooks/useInterval.tsx @@ -1,17 +1,21 @@ import { useEffect, useRef } from "react"; -function useInterval(callback: () => void, delay) { - const savedCallback = useRef(null); +function useInterval(callback: () => void, delay: number) { + const savedCallback = useRef void)>(null); // Remember the latest callback. useEffect(() => { - savedCallback.current = callback; + if (savedCallback.current) { + savedCallback.current = callback; + } }, [callback]); // Set up the interval. useEffect(() => { function tick() { - savedCallback.current(); + if (savedCallback.current) { + savedCallback.current(); + } } if (delay !== null) { const id = setInterval(tick, delay); diff --git a/src/utils/hooks/useKeyPress.jsx b/src/utils/hooks/useKeyPress.tsx similarity index 85% rename from src/utils/hooks/useKeyPress.jsx rename to src/utils/hooks/useKeyPress.tsx index 961bba7..8e9e140 100644 --- a/src/utils/hooks/useKeyPress.jsx +++ b/src/utils/hooks/useKeyPress.tsx @@ -1,16 +1,16 @@ import { useEffect, useState } from "react"; -const useKeyPress = (targetKey) => { +const useKeyPress = (targetKey: string) => { // State for keeping track of whether key is pressed const [keyPressed, setKeyPressed] = useState(false); // If pressed key is our target key then set to true - function downHandler({ key }) { + function downHandler({ key }: KeyboardEvent) { if (key === targetKey) { setKeyPressed(true); } } // If released key is our target key then set to false - const upHandler = ({ key }) => { + const upHandler = ({ key }: KeyboardEvent) => { if (key === targetKey) { setKeyPressed(false); } diff --git a/src/utils/hooks/useParallax.tsx b/src/utils/hooks/useParallax.tsx index 9c281c6..6b69d66 100644 --- a/src/utils/hooks/useParallax.tsx +++ b/src/utils/hooks/useParallax.tsx @@ -1,4 +1,4 @@ -import { MotionValue, useTransform } from "framer-motion"; +import { type MotionValue, useTransform } from "framer-motion"; export default function useParallax( value: MotionValue, diff --git a/src/utils/store/api.tsx b/src/utils/store/api.tsx index 5e176ca..a29ae1c 100644 --- a/src/utils/store/api.tsx +++ b/src/utils/store/api.tsx @@ -8,7 +8,6 @@ import { version as appVer } from "../../../package.json"; // Initial custom axios client to use tauri's http module import axios from "axios"; -import axiosTauriApiAdapter from "axios-tauri-api-adapter"; type ApiStore = { api: Api | undefined; diff --git a/src/utils/store/audioPlayback.ts b/src/utils/store/audioPlayback.ts index 5fd04c1..f0d3746 100644 --- a/src/utils/store/audioPlayback.ts +++ b/src/utils/store/audioPlayback.ts @@ -1,13 +1,8 @@ import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; -import { create } from "zustand"; -import { setQueue } from "./queue"; -import { start } from "repl"; -import { max } from "lodash"; import type React from "react"; import { shallow } from "zustand/shallow"; import { createWithEqualityFn } from "zustand/traditional"; -import { secToTicks } from "../date/time"; import playbackProfile from "../playback-profiles"; type AudioPlaybackStore = { @@ -90,11 +85,11 @@ export const generateAudioStreamUrl = ( enableRemoteMedia: false, enableAudioVbrEncoding: true, }; - const urlParams = new URLSearchParams(urlOptions).toString(); + const urlParams = new URLSearchParams(urlOptions as any).toString(); return `${basePath}/Audio/${itemId}/universal?${urlParams}`; }; -export const setAudioRef = (ref) => { +export const setAudioRef = (ref: React.RefObject) => { const state = useAudioPlayback.getState(); state.player.ref = ref; useAudioPlayback.setState(state); diff --git a/src/utils/store/backdrop.tsx b/src/utils/store/backdrop.tsx index 52bcd44..2a54cee 100644 --- a/src/utils/store/backdrop.tsx +++ b/src/utils/store/backdrop.tsx @@ -1,28 +1,21 @@ -import { createStore } from "zustand"; import { shallow } from "zustand/shallow"; -import { useStoreWithEqualityFn } from "zustand/traditional"; +import { createWithEqualityFn } from "zustand/traditional"; type BackdropStore = { backdropUrl: string; backdropId: string; - setBackdrop: ( - url: string | undefined | null, - id: string | undefined | null, - ) => void; + setBackdrop: (url: string | undefined, id: string | undefined) => void; }; -export const backdropStore = createStore((set) => ({ - backdropUrl: "", - backdropId: "", - setBackdrop: ( - url: string | undefined | null, - id: string | undefined | null, - ) => set(() => ({ backdropUrl: url, backdropId: id })), -})); +export const useBackdropStore = createWithEqualityFn( + (set) => ({ + backdropUrl: "", + backdropId: "", + setBackdrop: (url, id) => set(() => ({ backdropUrl: url, backdropId: id })), + }), + shallow, +); -export function useBackdropStore(selector?: (state: BackdropStore) => T) { - return useStoreWithEqualityFn(backdropStore, selector!, shallow); -} /** * @deprecated Please use setBackdrop method from the store */ diff --git a/src/utils/store/carousel.js b/src/utils/store/carousel.ts similarity index 55% rename from src/utils/store/carousel.js rename to src/utils/store/carousel.ts index 72a9637..944c5b5 100644 --- a/src/utils/store/carousel.js +++ b/src/utils/store/carousel.ts @@ -1,8 +1,11 @@ -import { create } from "zustand"; import { shallow } from "zustand/shallow"; import { createWithEqualityFn } from "zustand/traditional"; -export const useCarouselStore = createWithEqualityFn( +type CarouselStore = { + direction: "left" | "right"; + setDirection: (dir: "left" | "right") => void; +}; +export const useCarouselStore = createWithEqualityFn( (set) => ({ direction: "right", setDirection: (dir) => set(() => ({ direction: dir })), diff --git a/src/utils/store/drawer.js b/src/utils/store/drawer.js deleted file mode 100644 index 1f77a3e..0000000 --- a/src/utils/store/drawer.js +++ /dev/null @@ -1,10 +0,0 @@ -import { shallow } from "zustand/shallow"; -import { createWithEqualityFn } from "zustand/traditional"; - -export const useDrawerStore = createWithEqualityFn( - (set) => ({ - open: false, - setOpen: (sopen) => set((state) => ({ ...state, open: sopen })), - }), - shallow, -); diff --git a/src/utils/store/drawer.ts b/src/utils/store/drawer.ts new file mode 100644 index 0000000..011a81a --- /dev/null +++ b/src/utils/store/drawer.ts @@ -0,0 +1,17 @@ +import { shallow } from "zustand/shallow"; +import { createWithEqualityFn } from "zustand/traditional"; + +type DrawerStore = { + open: boolean; + setOpen: (sopen: boolean) => void; +}; + +export const useDrawerStore = createWithEqualityFn( + (set) => ({ + open: false, + setOpen: (sopen) => { + set(() => ({ open: sopen })); + }, + }), + shallow, +); diff --git a/src/utils/store/photosPlayback.ts b/src/utils/store/photosPlayback.ts index 675e321..a49c429 100644 --- a/src/utils/store/photosPlayback.ts +++ b/src/utils/store/photosPlayback.ts @@ -1,10 +1,7 @@ import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; -import { create } from "zustand"; -import { devtools } from "zustand/middleware"; import { immer } from "zustand/middleware/immer"; import { shallow } from "zustand/shallow"; import { createWithEqualityFn } from "zustand/traditional"; -import { usePlaybackStore } from "./playback"; type PhotosPlaybackStore = { photos: BaseItemDto[] | null; @@ -12,21 +9,16 @@ type PhotosPlaybackStore = { playPhotos: (photos: BaseItemDto[], index: number) => void; }; -type PhotosPlaybackStoreActions = { - playPhotos: (photos: BaseItemDto[]) => void; -}; export const usePhotosPlayback = createWithEqualityFn()( - devtools( - immer((set) => ({ - photos: null, - playPhotos: (photos, index) => - set((state) => { - state.photos = photos; - state.index = index; - }), - index: 0, - })), - ), + immer((set) => ({ + photos: null, + playPhotos: (photos, index) => + set((state) => { + state.photos = photos; + state.index = index; + }), + index: 0, + })), shallow, ); \ No newline at end of file