refactor: move more files to typescript

This commit is contained in:
Prayag Prajapati committed 2024-12-03 22:21:48 +05:30
1 parent 97526e29b4
commit cf8c5d4490
20 files changed
+123 -177

No files matched your search

+14 -11
View File
@@ -4,11 +4,11 @@ import { getRuntimeMusic } from "../../utils/date/time";
import { useAudioPlayback } from "../../utils/store/audioPlayback"; import { useAudioPlayback } from "../../utils/store/audioPlayback";
import LikeButton from "../buttons/likeButton"; import LikeButton from "../buttons/likeButton";
import PlayButton from "../buttons/playButton"; import PlayButton from "../buttons/playButton";
import TextLink from "../textLink";
import "./musicTrack.scss"; import "./musicTrack.scss";
import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; import getImageUrlsApi from "@/utils/methods/getImageUrlsApi";
import { useApiInContext } from "@/utils/store/api"; import { useApiInContext } from "@/utils/store/api";
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import { Link } from "@tanstack/react-router";
import lyrics_icon from "../../assets/icons/lyrics.svg"; import lyrics_icon from "../../assets/icons/lyrics.svg";
/** /**
@@ -114,18 +114,21 @@ const MusicTrack = ({
style={{ display: "inline-flex", alignItems: "center" }} style={{ display: "inline-flex", alignItems: "center" }}
key={artist.Id} key={artist.Id}
> >
<TextLink <Typography
variant={"subtitle2"} variant="subtitle2"
location={`/artist/${artist.Id}`} fontWeight="400"
otherProps={{ style={{
fontWeight: 400, textDecoration: "none",
style: {
textDecoration: "none",
},
}} }}
> >
{artist.Name} <Link
</TextLink> style={{ textDecoration: "none", color: "white" }}
to="/artist/$id"
params={{ id: artist.Id ?? "" }}
>
{artist.Name}
</Link>
</Typography>
{index !== (item.ArtistItems?.length ?? 0) - 1 && ( {index !== (item.ArtistItems?.length ?? 0) - 1 && (
<span <span
style={{ style={{
+1 -1
View File
@@ -1,6 +1,6 @@
import React from "react"; import React from "react";
import { Divider, LinearProgress, Skeleton } from "@mui/material"; import { Skeleton } from "@mui/material";
import { motion } from "framer-motion"; import { motion } from "framer-motion";
const ItemSkeleton = () => { const ItemSkeleton = () => {
-21
View File
@@ -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 (
<Typography
variant={variant}
className="link flex"
onClick={() => navigate({ to: location })}
{...otherProps}
>
{children}
</Typography>
);
};
export default TextLink;
-11
View File
@@ -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);
}
}
+33 -30
View File
@@ -419,38 +419,41 @@ function EpisodeTitlePage() {
)} )}
</div> </div>
<div className="item-hero-detail"> <div className="item-hero-detail">
<TextLink <Typography
otherProps={{ variant="h2"
fontWeight: 200, fontWeight="200"
mb: 2, mb={2}
style: { style={{
textDecoration: "none", textDecoration: "none",
},
}} }}
variant={"h2"}
location={`/series/${item.data.SeriesId}`}
> >
{item.data.ParentLogoItemId?.length > 0 ? ( <Link
<img style={{ textDecoration: "none", color: "white" }}
alt={item.data.SeriesName} to="/series/$id"
src={getImageUrlsApi(api).getItemImageUrlById( params={{ id: item.data.SeriesId ?? "" }}
item.data.ParentLogoItemId, >
"Logo", {item.data.ParentLogoItemId?.length > 0 ? (
{ <img
quality: 90, alt={item.data.SeriesName}
fillWidth: 592, src={getImageUrlsApi(api).getItemImageUrlById(
fillHeight: 592, item.data.ParentLogoItemId,
}, "Logo",
)} {
onLoad={(e) => { quality: 90,
e.currentTarget.style.opacity = 1; fillWidth: 592,
}} fillHeight: 592,
className="item-hero-logo" },
/> )}
) : ( onLoad={(e) => {
item.data.SeriesName e.currentTarget.style.opacity = 1;
)} }}
</TextLink> className="item-hero-logo"
/>
) : (
item.data.SeriesName
)}
</Link>
</Typography>
<Typography variant="h4" fontWeight={300} mb={2}> <Typography variant="h4" fontWeight={300} mb={2}>
S{item.data.ParentIndexNumber ?? 0}:E S{item.data.ParentIndexNumber ?? 0}:E
{item.data.IndexNumberEnd {item.data.IndexNumberEnd
-11
View File
@@ -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;
};
+11
View File
@@ -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;
};
@@ -3,7 +3,7 @@
* @param {number} ticks - C# ticks of a particular item * @param {number} ticks - C# ticks of a particular item
* @return {number} Converted ticks to Milliseconds * @return {number} Converted ticks to Milliseconds
*/ */
export const ticksToMs = (ticks) => { export const ticksToMs = (ticks: number) => {
return Math.round(ticks / 10000); return Math.round(ticks / 10000);
}; };
@@ -12,7 +12,7 @@ export const ticksToMs = (ticks) => {
* @param {number} ticks - C# ticks of a particular item * @param {number} ticks - C# ticks of a particular item
* @return {number} Converted ticks to Seconds * @return {number} Converted ticks to Seconds
*/ */
export const ticksToSec = (ticks) => { export const ticksToSec = (ticks: number) => {
return Math.round(ticksToMs(ticks) / 1000); return Math.round(ticksToMs(ticks) / 1000);
}; };
@@ -21,7 +21,7 @@ export const ticksToSec = (ticks) => {
* @param {number} sec - Sec of a particular item * @param {number} sec - Sec of a particular item
* @return {number} Converted Seconds to C# ticks * @return {number} Converted Seconds to C# ticks
*/ */
export const secToTicks = (ticks) => { export const secToTicks = (ticks: number) => {
return Math.round(ticks * 10000000); return Math.round(ticks * 10000000);
}; };
@@ -30,7 +30,7 @@ export const secToTicks = (ticks) => {
* @param {number} ticks - C# ticks of a particular item * @param {number} ticks - C# ticks of a particular item
* @return {number} Converted ticks to Hhr Mmin formate * @return {number} Converted ticks to Hhr Mmin formate
*/ */
export const getRuntime = (ticks) => { export const getRuntime = (ticks: number) => {
const time = ticksToMs(ticks); const time = ticksToMs(ticks);
let formatedTime = ""; let formatedTime = "";
let timeSec = Math.floor(time / 1000); let timeSec = Math.floor(time / 1000);
@@ -51,7 +51,7 @@ export const getRuntime = (ticks) => {
* @param {number} ticks - C# ticks of a particular item * @param {number} ticks - C# ticks of a particular item
* @return {number} Converted ticks to hh:mm:ss formate * @return {number} Converted ticks to hh:mm:ss formate
*/ */
export const getRuntimeMusic = (ticks) => { export const getRuntimeMusic = (ticks: number) => {
const time = ticksToMs(ticks); const time = ticksToMs(ticks);
let formatedTime = ""; let formatedTime = "";
let timeSec = Math.floor(time / 1000); let timeSec = Math.floor(time / 1000);
@@ -82,7 +82,7 @@ export const getRuntimeMusic = (ticks) => {
* @param {number} ticks - C# ticks of a particular item * @param {number} ticks - C# ticks of a particular item
* @return {number} Converted ticks to H hour M minutes formate * @return {number} Converted ticks to H hour M minutes formate
*/ */
export const getRuntimeFull = (ticks) => { export const getRuntimeFull = (ticks: number) => {
const time = ticksToMs(ticks); const time = ticksToMs(ticks);
let formatedTime = ""; let formatedTime = "";
let timeSec = Math.floor(time / 1000); let timeSec = Math.floor(time / 1000);
@@ -103,7 +103,7 @@ export const getRuntimeFull = (ticks) => {
* @param {number} ticks - C# ticks of a particular item * @param {number} ticks - C# ticks of a particular item
* @return {number} Converted ticks to Hh Mm formate * @return {number} Converted ticks to Hh Mm formate
*/ */
export const getRuntimeCompact = (ticks) => { export const getRuntimeCompact = (ticks: number) => {
const time = ticksToMs(ticks); const time = ticksToMs(ticks);
let formatedTime = ""; let formatedTime = "";
let timeSec = Math.floor(time / 1000); let timeSec = Math.floor(time / 1000);
@@ -124,7 +124,7 @@ export const getRuntimeCompact = (ticks) => {
* @param {number} ticks - C# ticks of a particular item * @param {number} ticks - C# ticks of a particular item
* @return {number} End time of an item * @return {number} End time of an item
*/ */
export const endsAt = (ticks) => { export const endsAt = (ticks: number) => {
const current = new Date(); const current = new Date();
const currentTime = current.getTime(); const currentTime = current.getTime();
const time = ticksToMs(ticks); const time = ticksToMs(ticks);
@@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
// see https://github.com/tannerlinsley/react-query/issues/293 // see https://github.com/tannerlinsley/react-query/issues/293
// see https://usehooks.com/useDebounce/ // 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 // State and setters for debounced value
const [debouncedValue, setDebouncedValue] = useState(value); const [debouncedValue, setDebouncedValue] = useState(value);
-21
View File
@@ -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;
+8 -4
View File
@@ -1,17 +1,21 @@
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
function useInterval(callback: () => void, delay) { function useInterval(callback: () => void, delay: number) {
const savedCallback = useRef(null); const savedCallback = useRef<null | (() => void)>(null);
// Remember the latest callback. // Remember the latest callback.
useEffect(() => { useEffect(() => {
savedCallback.current = callback; if (savedCallback.current) {
savedCallback.current = callback;
}
}, [callback]); }, [callback]);
// Set up the interval. // Set up the interval.
useEffect(() => { useEffect(() => {
function tick() { function tick() {
savedCallback.current(); if (savedCallback.current) {
savedCallback.current();
}
} }
if (delay !== null) { if (delay !== null) {
const id = setInterval(tick, delay); const id = setInterval(tick, delay);
@@ -1,16 +1,16 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
const useKeyPress = (targetKey) => { const useKeyPress = (targetKey: string) => {
// State for keeping track of whether key is pressed // State for keeping track of whether key is pressed
const [keyPressed, setKeyPressed] = useState(false); const [keyPressed, setKeyPressed] = useState(false);
// If pressed key is our target key then set to true // If pressed key is our target key then set to true
function downHandler({ key }) { function downHandler({ key }: KeyboardEvent) {
if (key === targetKey) { if (key === targetKey) {
setKeyPressed(true); setKeyPressed(true);
} }
} }
// If released key is our target key then set to false // If released key is our target key then set to false
const upHandler = ({ key }) => { const upHandler = ({ key }: KeyboardEvent) => {
if (key === targetKey) { if (key === targetKey) {
setKeyPressed(false); setKeyPressed(false);
} }
+1 -1
View File
@@ -1,4 +1,4 @@
import { MotionValue, useTransform } from "framer-motion"; import { type MotionValue, useTransform } from "framer-motion";
export default function useParallax( export default function useParallax(
value: MotionValue<number>, value: MotionValue<number>,
-1
View File
@@ -8,7 +8,6 @@ import { version as appVer } from "../../../package.json";
// Initial custom axios client to use tauri's http module // Initial custom axios client to use tauri's http module
import axios from "axios"; import axios from "axios";
import axiosTauriApiAdapter from "axios-tauri-api-adapter";
type ApiStore = { type ApiStore = {
api: Api | undefined; api: Api | undefined;
+2 -7
View File
@@ -1,13 +1,8 @@
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; 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 type React from "react";
import { shallow } from "zustand/shallow"; import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional"; import { createWithEqualityFn } from "zustand/traditional";
import { secToTicks } from "../date/time";
import playbackProfile from "../playback-profiles"; import playbackProfile from "../playback-profiles";
type AudioPlaybackStore = { type AudioPlaybackStore = {
@@ -90,11 +85,11 @@ export const generateAudioStreamUrl = (
enableRemoteMedia: false, enableRemoteMedia: false,
enableAudioVbrEncoding: true, enableAudioVbrEncoding: true,
}; };
const urlParams = new URLSearchParams(urlOptions).toString(); const urlParams = new URLSearchParams(urlOptions as any).toString();
return `${basePath}/Audio/${itemId}/universal?${urlParams}`; return `${basePath}/Audio/${itemId}/universal?${urlParams}`;
}; };
export const setAudioRef = (ref) => { export const setAudioRef = (ref: React.RefObject<HTMLAudioElement>) => {
const state = useAudioPlayback.getState(); const state = useAudioPlayback.getState();
state.player.ref = ref; state.player.ref = ref;
useAudioPlayback.setState(state); useAudioPlayback.setState(state);
+10 -17
View File
@@ -1,28 +1,21 @@
import { createStore } from "zustand";
import { shallow } from "zustand/shallow"; import { shallow } from "zustand/shallow";
import { useStoreWithEqualityFn } from "zustand/traditional"; import { createWithEqualityFn } from "zustand/traditional";
type BackdropStore = { type BackdropStore = {
backdropUrl: string; backdropUrl: string;
backdropId: string; backdropId: string;
setBackdrop: ( setBackdrop: (url: string | undefined, id: string | undefined) => void;
url: string | undefined | null,
id: string | undefined | null,
) => void;
}; };
export const backdropStore = createStore((set) => ({ export const useBackdropStore = createWithEqualityFn<BackdropStore>(
backdropUrl: "", (set) => ({
backdropId: "", backdropUrl: "",
setBackdrop: ( backdropId: "",
url: string | undefined | null, setBackdrop: (url, id) => set(() => ({ backdropUrl: url, backdropId: id })),
id: string | undefined | null, }),
) => set(() => ({ backdropUrl: url, backdropId: id })), shallow,
})); );
export function useBackdropStore<T>(selector?: (state: BackdropStore) => T) {
return useStoreWithEqualityFn(backdropStore, selector!, shallow);
}
/** /**
* @deprecated Please use setBackdrop method from the store * @deprecated Please use setBackdrop method from the store
*/ */
@@ -1,8 +1,11 @@
import { create } from "zustand";
import { shallow } from "zustand/shallow"; import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional"; import { createWithEqualityFn } from "zustand/traditional";
export const useCarouselStore = createWithEqualityFn( type CarouselStore = {
direction: "left" | "right";
setDirection: (dir: "left" | "right") => void;
};
export const useCarouselStore = createWithEqualityFn<CarouselStore>(
(set) => ({ (set) => ({
direction: "right", direction: "right",
setDirection: (dir) => set(() => ({ direction: dir })), setDirection: (dir) => set(() => ({ direction: dir })),
-10
View File
@@ -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,
);
+17
View File
@@ -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<DrawerStore>(
(set) => ({
open: false,
setOpen: (sopen) => {
set(() => ({ open: sopen }));
},
}),
shallow,
);
+9 -17
View File
@@ -1,10 +1,7 @@
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client"; 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 { immer } from "zustand/middleware/immer";
import { shallow } from "zustand/shallow"; import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional"; import { createWithEqualityFn } from "zustand/traditional";
import { usePlaybackStore } from "./playback";
type PhotosPlaybackStore = { type PhotosPlaybackStore = {
photos: BaseItemDto[] | null; photos: BaseItemDto[] | null;
@@ -12,21 +9,16 @@ type PhotosPlaybackStore = {
playPhotos: (photos: BaseItemDto[], index: number) => void; playPhotos: (photos: BaseItemDto[], index: number) => void;
}; };
type PhotosPlaybackStoreActions = {
playPhotos: (photos: BaseItemDto[]) => void;
};
export const usePhotosPlayback = createWithEqualityFn<PhotosPlaybackStore>()( export const usePhotosPlayback = createWithEqualityFn<PhotosPlaybackStore>()(
devtools( immer((set) => ({
immer((set) => ({ photos: null,
photos: null, playPhotos: (photos, index) =>
playPhotos: (photos, index) => set((state) => {
set((state) => { state.photos = photos;
state.photos = photos; state.index = index;
state.index = index; }),
}), index: 0,
index: 0, })),
})),
),
shallow, shallow,
); );