fix: react infinite depth error

This commit is contained in:
Prayag Prajapati committed 2024-09-29 00:02:47 +05:30
1 parent d78676560f
commit a1bb8f8e23
19 files changed
+377 -370

No files matched your search

+4 -2
View File
@@ -1,7 +1,9 @@
import { type Api, Jellyfin } from "@jellyfin/sdk";
import React, { type ReactNode } from "react";
import { createContext, useContext, useState } from "react";
import { type StoreApi, createStore, useStore } from "zustand";
import { type StoreApi, createStore } from "zustand";
import { shallow } from "zustand/shallow";
import { useStoreWithEqualityFn } from "zustand/traditional";
import { version as appVer } from "../../../package.json";
// Initial custom axios client to use tauri's http module
@@ -69,5 +71,5 @@ export function useApiInContext<T>(selector?: (state: ApiStore) => T) {
if (!store) {
throw new Error("Missing ApiProvider");
}
return useStore(store, selector!);
return useStoreWithEqualityFn(store, selector!, shallow);
}
+16 -11
View File
@@ -3,6 +3,8 @@ import { create } from "zustand";
import { setQueue } from "./queue";
import type React from "react";
import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional";
import { secToTicks } from "../date/time";
type AudioPlaybackStore = {
@@ -16,17 +18,20 @@ type AudioPlaybackStore = {
ref: React.Ref<HTMLAudioElement> | null;
};
};
export const useAudioPlayback = create<AudioPlaybackStore>(() => ({
display: false,
url: "",
item: undefined,
playlistItemId: undefined,
player: {
currentTick: 0,
playing: false,
ref: null,
},
}));
export const useAudioPlayback = createWithEqualityFn<AudioPlaybackStore>(
() => ({
display: false,
url: "",
item: undefined,
playlistItemId: undefined,
player: {
currentTick: 0,
playing: false,
ref: null,
},
}),
shallow,
);
export const playAudio = (
url: string,
-12
View File
@@ -1,12 +0,0 @@
import { create } from "zustand";
export const useBackdropStore = create((set) => ({
backdropUrl: "",
backdropId: "",
setBackdrop: (url, id) =>
set((state) => ({ backdropUrl: url, backdropId: id })),
}));
export const setBackdrop = (url, id) => {
useBackdropStore.setState(() => ({ backdropUrl: url, backdropId: id }));
};
+31
View File
@@ -0,0 +1,31 @@
import { createStore } from "zustand";
import { shallow } from "zustand/shallow";
import { useStoreWithEqualityFn } from "zustand/traditional";
type BackdropStore = {
backdropUrl: string;
backdropId: string;
setBackdrop: (
url: string | undefined | null,
id: string | undefined | null,
) => void;
};
export const backdropStore = createStore((set) => ({
backdropUrl: "",
backdropId: "",
setBackdrop: (
url: string | undefined | null,
id: string | undefined | null,
) => set(() => ({ backdropUrl: url, backdropId: id })),
}));
export function useBackdropStore<T>(selector?: (state: BackdropStore) => T) {
return useStoreWithEqualityFn(backdropStore, selector!, shallow);
}
/**
* @deprecated Please use setBackdrop method from the store
*/
export const setBackdrop = () => {
// useBackdropStore.setState(() => ({ backdropUrl: url, backdropId: id }));
};
+9 -4
View File
@@ -1,6 +1,11 @@
import { create } from "zustand";
import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional";
export const useCarouselStore = create((set) => ({
direction: "right",
setDirection: (dir) => set(() => ({ direction: dir })),
}));
export const useCarouselStore = createWithEqualityFn(
(set) => ({
direction: "right",
setDirection: (dir) => set(() => ({ direction: dir })),
}),
shallow,
);
+12 -12
View File
@@ -1,19 +1,19 @@
import React from "react";
import { createStore, StoreApi, useStore } from "zustand";
import type { Api } from "@jellyfin/sdk";
import type { UserDto } from "@jellyfin/sdk/lib/generated-client";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import React, { type ReactNode } from "react";
import { createContext, useContext, useState } from "react";
import { type StoreApi, createStore } from "zustand";
import { shallow } from "zustand/shallow";
import { useStoreWithEqualityFn } from "zustand/traditional";
import { version } from "../../../package.json";
import {
type ServerInfo,
getAllServers,
getDefaultServer,
getServer,
ServerInfo,
} from "../storage/servers";
import { getUser } from "../storage/user";
import { useApiInContext } from "./api";
import { Api } from "@jellyfin/sdk";
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import { createContext, useContext, useState } from "react";
import { type UserDto } from "@jellyfin/sdk/lib/generated-client";
import { type ReactNode } from "@tanstack/react-router";
type CentralStore = {
defaultServerOnDisk: () => Promise<string | null>;
@@ -32,14 +32,14 @@ type CentralStore = {
* Sets app inital route
* @deprecated
*/
export const setInitialRoute = (route) => {
export const setInitialRoute = () => {
// useCentralStore.setState((state) => ({ ...state, initialRoute: route }));
};
/**
* @deprecated
*/
export const setAppReady = (appReady) => {
export const setAppReady = () => {
// useCentralStore.setState((state) => ({ ...state, appReady }));
};
@@ -75,5 +75,5 @@ export function useCentralStore<T>(selector?: (state: CentralStore) => T) {
if (!store) {
throw new Error("Missing CentralProvider");
}
return useStore(store, selector!);
return useStoreWithEqualityFn(store, selector!, shallow);
}
+9 -5
View File
@@ -1,6 +1,10 @@
import { create } from "zustand";
import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional";
export const useDrawerStore = create((set) => ({
open: false,
setOpen: (sopen) => set((state) => ({ ...state, open: sopen })),
}));
export const useDrawerStore = createWithEqualityFn(
(set) => ({
open: false,
setOpen: (sopen) => set((state) => ({ ...state, open: sopen })),
}),
shallow,
);
+42 -36
View File
@@ -6,12 +6,13 @@ import type {
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api/playstate-api";
import type { AxiosResponse } from "axios";
import { create } from "zustand";
import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional";
import getSubtitle from "../methods/getSubtitles";
import playbackProfile from "../playback-profiles";
import type IntroMediaInfo from "../types/introMediaInfo";
import type subtitlePlaybackInfo from "../types/subtitlePlaybackInfo";
import { axiosClient, useApiInContext } from "./api";
import { axiosClient } from "./api";
import { playAudio } from "./audioPlayback";
import useQueue, { setQueue, setTrackIndex } from "./queue";
@@ -34,31 +35,34 @@ type PlaybackStore = {
intro: IntroMediaInfo | undefined;
};
export const usePlaybackStore = create<PlaybackStore>(() => ({
itemName: undefined!,
episodeTitle: "",
mediaSource: {
videoTrack: 0,
audioTrack: 0,
container: "",
id: undefined,
subtitle: {
enable: false,
track: undefined!,
format: "ass",
allTracks: undefined,
url: undefined,
export const usePlaybackStore = createWithEqualityFn<PlaybackStore>(
() => ({
itemName: undefined!,
episodeTitle: "",
mediaSource: {
videoTrack: 0,
audioTrack: 0,
container: "",
id: undefined,
subtitle: {
enable: false,
track: undefined!,
format: "ass",
allTracks: undefined,
url: undefined,
},
},
},
enableSubtitle: true,
playbackStream: "",
userId: "",
startPosition: 0,
itemDuration: 0,
item: null,
playsessionId: "",
intro: undefined,
}));
enableSubtitle: true,
playbackStream: "",
userId: "",
startPosition: 0,
itemDuration: 0,
item: null,
playsessionId: "",
intro: undefined,
}),
shallow,
);
export const playItem = (
itemName: string | React.Component | undefined | null,
@@ -249,16 +253,18 @@ interface PlaybackDataLoadState {
setisPending: (loading: boolean) => void;
}
export const usePlaybackDataLoadStore = create<PlaybackDataLoadState>(
(set) => ({
isPending: false,
setisPending: (loading: boolean) =>
set((state: PlaybackDataLoadState) => ({
...state,
isPending: loading,
})),
}),
);
export const usePlaybackDataLoadStore =
createWithEqualityFn<PlaybackDataLoadState>(
(set) => ({
isPending: false,
setisPending: (loading: boolean) =>
set((state: PlaybackDataLoadState) => ({
...state,
isPending: loading,
})),
}),
shallow,
);
export const changeSubtitleTrack = (
trackIndex: number,
-14
View File
@@ -1,14 +0,0 @@
import { create } from "zustand";
import { getRuntimeMusic, secToTicks } from "../date/time";
const usePlayer = create(() => ({
timeString: 0,
}));
export const setTime = (sec: number) => {
const a = getRuntimeMusic(secToTicks(sec));
console.log(a);
usePlayer.setState(() => ({ timeString: a }));
};
export default usePlayer;
+9 -5
View File
@@ -1,15 +1,19 @@
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import { create } from "zustand";
import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional";
interface QueueStore {
tracks: BaseItemDto[];
currentItemIndex: number;
}
const useQueue = create<QueueStore>(() => ({
tracks: [],
currentItemIndex: 0,
}));
const useQueue = createWithEqualityFn<QueueStore>(
() => ({
tracks: [],
currentItemIndex: 0,
}),
shallow,
);
export const setQueue = (
tracks: BaseItemDto[] | undefined,
-7
View File
@@ -1,7 +0,0 @@
import { create } from "zustand";
export const useServerStore = create(() => ({
reachable: true,
}));
export const setServerReachability = (reach) =>
useServerStore.setState(() => ({ reachable: reach }));
+9 -4
View File
@@ -1,9 +1,14 @@
import { create } from "zustand";
import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional";
const useSettingsStore = create(() => ({
dialogOpen: false,
tabValue: 0,
}));
const useSettingsStore = createWithEqualityFn(
() => ({
dialogOpen: false,
tabValue: 0,
}),
shallow,
);
const setDialogOpen = (dialog: boolean) =>
useSettingsStore.setState((state) => ({ ...state, dialogOpen: dialog }));