refactor: move to tanstack router
This commit is contained in:
1 parent
a4ea932153
commit
522621ed69
136 files changed
+7063
-6127
No files matched your search
@@ -1,51 +0,0 @@
|
||||
import { Api, Jellyfin } from "@jellyfin/sdk";
|
||||
import { create } from "zustand";
|
||||
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";
|
||||
|
||||
export const axiosClient = axios.create({
|
||||
adapter: axiosTauriApiAdapter,
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
const deviceId = localStorage.getItem("deviceId") || crypto.randomUUID();
|
||||
|
||||
if (!localStorage.getItem("deviceId")) {
|
||||
localStorage.setItem("deviceId", deviceId);
|
||||
}
|
||||
|
||||
const jellyfin = new Jellyfin({
|
||||
clientInfo: {
|
||||
name: "JellyPlayer",
|
||||
version: appVer,
|
||||
},
|
||||
deviceInfo: {
|
||||
name: "JellyPlayer",
|
||||
id: deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
export const useApi = create<{
|
||||
api: Api | null;
|
||||
deviceId: string | null;
|
||||
jellyfin: Jellyfin;
|
||||
}>(() => ({
|
||||
api: null,
|
||||
deviceId: deviceId,
|
||||
jellyfin: jellyfin,
|
||||
}));
|
||||
|
||||
export const createApi = (
|
||||
serverAddress: string,
|
||||
accessToken: string | undefined,
|
||||
) =>
|
||||
useApi.setState((state) => ({
|
||||
...state,
|
||||
api: jellyfin.createApi(serverAddress, accessToken, axiosClient),
|
||||
}));
|
||||
@@ -0,0 +1,95 @@
|
||||
import { type Api, Jellyfin } from "@jellyfin/sdk";
|
||||
import React, { createContext, useContext, useRef } from "react";
|
||||
import { type StoreApi, createStore, useStore } from "zustand";
|
||||
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;
|
||||
deviceId: string | null;
|
||||
jellyfin: Jellyfin;
|
||||
createApi: (
|
||||
serverAddress: string | undefined,
|
||||
accessToken?: string | undefined,
|
||||
) => void;
|
||||
};
|
||||
|
||||
export const axiosClient = axios.create({
|
||||
adapter: axiosTauriApiAdapter,
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
const deviceId = localStorage.getItem("deviceId") || crypto.randomUUID();
|
||||
|
||||
if (!localStorage.getItem("deviceId")) {
|
||||
localStorage.setItem("deviceId", deviceId);
|
||||
}
|
||||
|
||||
export const jellyfin = new Jellyfin({
|
||||
clientInfo: {
|
||||
name: "JellyPlayer",
|
||||
version: appVer,
|
||||
},
|
||||
deviceInfo: {
|
||||
name: "JellyPlayer",
|
||||
id: deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
// export const useApi = create<{
|
||||
// api: Api;
|
||||
// deviceId: string | null;
|
||||
// jellyfin: Jellyfin;
|
||||
// }>(() => ({
|
||||
// api: undefined,
|
||||
// deviceId: deviceId,
|
||||
// jellyfin: jellyfin,
|
||||
// }));
|
||||
|
||||
const ApiContext = createContext(null);
|
||||
|
||||
export const ApiProvider = ({ children }) => {
|
||||
const storeRef = useRef<StoreApi<ApiStore>>();
|
||||
|
||||
if (!storeRef.current) {
|
||||
storeRef.current = createStore<ApiStore>()((set) => ({
|
||||
api: undefined,
|
||||
deviceId: deviceId,
|
||||
jellyfin: jellyfin,
|
||||
createApi: async (serverAddress, accessToken?) => {
|
||||
set((state) => ({
|
||||
...state,
|
||||
api: jellyfin.createApi(serverAddress, accessToken, axiosClient),
|
||||
}));
|
||||
await Promise.resolve();
|
||||
},
|
||||
}));
|
||||
}
|
||||
return (
|
||||
<ApiContext.Provider value={storeRef.current}>
|
||||
{children}
|
||||
</ApiContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export function useApiInContext<T>(selector?: (state: ApiStore) => T) {
|
||||
const store = useContext(ApiContext);
|
||||
if (!store) {
|
||||
throw new Error("Missing StoreProvider");
|
||||
}
|
||||
return useStore(store, selector!);
|
||||
}
|
||||
|
||||
export const createApi = (
|
||||
serverAddress: string | undefined,
|
||||
accessToken: string | undefined,
|
||||
) => {
|
||||
const createApiFn = useApiInContext((s) => s.createApi);
|
||||
createApiFn(serverAddress, accessToken);
|
||||
};
|
||||
@@ -1,27 +0,0 @@
|
||||
import { create } from "zustand";
|
||||
|
||||
const initialState = {
|
||||
display: false,
|
||||
url: "",
|
||||
item: {},
|
||||
tracks: [],
|
||||
currentTrack: 0,
|
||||
playlistItemId: "",
|
||||
};
|
||||
|
||||
export const useAudioPlayback = create((set) => ({
|
||||
...initialState,
|
||||
setUrl: (aurl) => set((state) => ({ ...state, url: aurl })),
|
||||
setDisplay: (adisplay) =>
|
||||
set((state) => ({
|
||||
...state,
|
||||
display: adisplay,
|
||||
})),
|
||||
setItem: (aitem) => set((state) => ({ ...state, item: aitem })),
|
||||
setPlaylistItemId: (aitem) =>
|
||||
set((state) => ({ ...state, playlistItemId: aitem })),
|
||||
setTracks: (aitem) => set((state) => ({ ...state, tracks: aitem })),
|
||||
setCurrentTrack: (aitem) =>
|
||||
set((state) => ({ ...state, currentTrack: aitem })),
|
||||
reset: () => set(initialState),
|
||||
}));
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
||||
import { create } from "zustand";
|
||||
import { setQueue } from "./queue";
|
||||
|
||||
type AudioPlaybackStore = {
|
||||
display: boolean;
|
||||
url: string;
|
||||
item: BaseItemDto | undefined | null;
|
||||
playlistItemId: string | undefined;
|
||||
};
|
||||
export const useAudioPlayback = create<AudioPlaybackStore>(() => ({
|
||||
display: false,
|
||||
url: "",
|
||||
item: undefined,
|
||||
playlistItemId: undefined,
|
||||
}));
|
||||
|
||||
export const playAudio = (
|
||||
url: string,
|
||||
item: BaseItemDto | undefined | null,
|
||||
playlistItemId: string | undefined,
|
||||
queue: BaseItemDto[] | undefined,
|
||||
queueItemIndex: number,
|
||||
): void => {
|
||||
useAudioPlayback.setState({
|
||||
display: true,
|
||||
url,
|
||||
item,
|
||||
playlistItemId,
|
||||
});
|
||||
setQueue(queue, queueItemIndex);
|
||||
};
|
||||
@@ -16,6 +16,7 @@ export const useCentralStore = create(() => ({
|
||||
/** This is the initial route that app goes to just after app startups */
|
||||
initialRoute: null,
|
||||
clientVersion: version,
|
||||
appReady: false,
|
||||
}));
|
||||
|
||||
/**
|
||||
@@ -25,3 +26,7 @@ export const useCentralStore = create(() => ({
|
||||
export const setInitialRoute = (route) => {
|
||||
useCentralStore.setState((state) => ({ ...state, initialRoute: route }));
|
||||
};
|
||||
|
||||
export const setAppReady = (appReady) => {
|
||||
useCentralStore.setState((state) => ({ ...state, appReady }));
|
||||
};
|
||||
+91
-178
@@ -6,110 +6,10 @@ import type {
|
||||
import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api/media-info-api";
|
||||
import { create } from "zustand";
|
||||
import playbackProfile from "../playback-profiles";
|
||||
import { useApi } from "./api";
|
||||
import { useApiInContext } from "./api";
|
||||
import { playAudio } from "./audioPlayback";
|
||||
import useQueue, { setQueue, setTrackIndex } from "./queue";
|
||||
|
||||
// export interface PlaybackState {
|
||||
// url: string;
|
||||
// startPosition: number;
|
||||
// duration: number;
|
||||
// itemId: string;
|
||||
// itemName: string | Element;
|
||||
// subtitleTracks: unknown[];
|
||||
// selectedSubtitleTrack: number;
|
||||
// mediaSourceId: string;
|
||||
// audioStreamIndex: number;
|
||||
// videoStreamIndex: number;
|
||||
// subtitleStreamIndex: number;
|
||||
// userId: string;
|
||||
// mediaContainer: string;
|
||||
// seriesId: string;
|
||||
// queue: unknown[];
|
||||
// episodeIndex: number;
|
||||
// item: BaseItemDto;
|
||||
// setUrl: (aurl: string) => void;
|
||||
// setPosition: (apos: number) => void;
|
||||
// setDuration: (adur: number) => void;
|
||||
// setItemId: (aid: string) => void;
|
||||
// setItemName: (anm: string | Element) => void;
|
||||
// setUserId: (anm: string) => void;
|
||||
// setSeriesId: (anm: string) => void;
|
||||
// setMediaContainer: (anm: string) => void;
|
||||
// setMediaSourceId: (anm: string) => void;
|
||||
// setAudioStreamIndex: (anm: number) => void;
|
||||
// setVideoStreamIndex: (anm: number) => void;
|
||||
// setSubtitleStreamIndex: (anm: number) => void;
|
||||
// setSubtitleTracks: (asub: unknown[]) => void;
|
||||
// setSelectedSubtitleTrack: (asub: number) => void;
|
||||
// setQueue: (qitem: unknown[]) => void;
|
||||
// setEpisodeIndex: (aitem: number) => void;
|
||||
// }
|
||||
|
||||
// export const usePlaybackStore = create<PlaybackState>((set) => ({
|
||||
// url: "",
|
||||
// startPosition: 0,
|
||||
// duration: 0,
|
||||
// itemId: "",
|
||||
// itemName: "",
|
||||
// subtitleTracks: [],
|
||||
// selectedSubtitleTrack: 0,
|
||||
// mediaSourceId: "",
|
||||
// audioStreamIndex: 0,
|
||||
// videoStreamIndex: 0,
|
||||
// subtitleStreamIndex: 0,
|
||||
// userId: "",
|
||||
// mediaContainer: "",
|
||||
// seriesId: "",
|
||||
// queue: [],
|
||||
// episodeIndex: 0,
|
||||
|
||||
// item: {} as BaseItemDto,
|
||||
// setUrl: (aurl: string) =>
|
||||
// set((state: PlaybackState) => ({ ...state, url: aurl })),
|
||||
// setPosition: (apos: number) =>
|
||||
// set((state: PlaybackState) => ({ ...state, startPosition: apos })),
|
||||
// setDuration: (adur: number) =>
|
||||
// set((state: PlaybackState) => ({ ...state, duration: adur })),
|
||||
// setItemId: (aid: string) =>
|
||||
// set((state: PlaybackState) => ({ ...state, itemId: aid })),
|
||||
// setItemName: (anm: string | Element) =>
|
||||
// set((state: PlaybackState) => ({ ...state, itemName: anm })),
|
||||
// setUserId: (anm: string) =>
|
||||
// set((state: PlaybackState) => ({ ...state, userId: anm })),
|
||||
// setSeriesId: (anm: string) =>
|
||||
// set((state: PlaybackState) => ({ ...state, seriesId: anm })),
|
||||
// setMediaContainer: (anm: string) =>
|
||||
// set((state: PlaybackState) => ({ ...state, mediaContainer: anm })),
|
||||
// setMediaSourceId: (anm: string) =>
|
||||
// set((state: PlaybackState) => ({ ...state, mediaSourceId: anm })),
|
||||
// setAudioStreamIndex: (anm: number) =>
|
||||
// set((state: PlaybackState) => ({ ...state, audioStreamIndex: anm })),
|
||||
// setVideoStreamIndex: (anm: number) =>
|
||||
// set((state: PlaybackState) => ({ ...state, videoStreamIndex: anm })),
|
||||
// setSubtitleStreamIndex: (anm: number) =>
|
||||
// set((state: PlaybackState) => ({ ...state, subtitleStreamIndex: anm })),
|
||||
// setSubtitleTracks: (asub: unknown[]) =>
|
||||
// set((state: PlaybackState) => ({
|
||||
// ...state,
|
||||
// subtitleTracks: asub,
|
||||
// })),
|
||||
// setSelectedSubtitleTrack: (asub: number) =>
|
||||
// set((state: PlaybackState) => ({
|
||||
// ...state,
|
||||
// selectedSubtitleTrack: asub,
|
||||
// })),
|
||||
// setQueue: (qitem: unknown[]) =>
|
||||
// set((state: PlaybackState) => ({ ...state, queue: qitem })),
|
||||
// setEpisodeIndex: (aitem: number) =>
|
||||
// set((state: PlaybackState) => ({ ...state, episodeIndex: aitem })),
|
||||
// }));
|
||||
|
||||
// export const setItem = (item: BaseItemDto) =>
|
||||
// usePlaybackStore.setState((state: PlaybackState) => ({
|
||||
// ...state,
|
||||
// item: item,
|
||||
// }));
|
||||
|
||||
type PlaybackStore = {
|
||||
itemName: string | React.Component;
|
||||
episodeTitle: string | React.Component;
|
||||
@@ -223,85 +123,98 @@ export const playItemFromQueue = async (
|
||||
? currentItemIndex - 1
|
||||
: index;
|
||||
const item = queueItems[requestedItemIndex];
|
||||
const api = useApi.getState().api;
|
||||
const mediaInfo = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
||||
audioStreamIndex: item.MediaSources[0].DefaultAudioStreamIndex ?? 0,
|
||||
subtitleStreamIndex: item.MediaSources[0].DefaultSubtitleStreamIndex ?? 0,
|
||||
itemId: item.Id,
|
||||
startTimeTicks: item.UserData?.PlaybackPositionTicks,
|
||||
userId: userId,
|
||||
mediaSourceId: item.MediaSources[0].Id,
|
||||
playbackInfoDto: {
|
||||
DeviceProfile: playbackProfile,
|
||||
},
|
||||
});
|
||||
console.log(mediaInfo);
|
||||
let itemName = item.Name;
|
||||
let episodeTitle = "";
|
||||
if (item.SeriesId) {
|
||||
itemName = item.SeriesName;
|
||||
episodeTitle = `S${item.ParentIndexNumber ?? 0}:E${item.IndexNumber ?? 0} ${
|
||||
item.Name
|
||||
}`;
|
||||
}
|
||||
const api = useApiInContext((s) => s.api);
|
||||
if (item.Type === "Audio") {
|
||||
const urlOptions = {
|
||||
deviceId: api?.deviceInfo.id,
|
||||
userId,
|
||||
api_key: api?.accessToken,
|
||||
};
|
||||
const urlParams = new URLSearchParams(urlOptions).toString();
|
||||
|
||||
// Select correct subtitle track, this is useful if item is played with playbutton from card since that does not provide coorect default subtitle track index.
|
||||
let selectedSubtitleTrack: number | "nosub" | undefined = -1;
|
||||
const subtitles = mediaInfo.data.MediaSources[0].MediaStreams?.filter(
|
||||
(value) => value.Type === "Subtitle",
|
||||
);
|
||||
let enableSubtitles = true;
|
||||
if (mediaInfo.data.MediaSources[0].DefaultSubtitleStreamIndex) {
|
||||
selectedSubtitleTrack =
|
||||
mediaInfo.data.MediaSources[0].DefaultSubtitleStreamIndex;
|
||||
} else if (subtitles?.length > 0) {
|
||||
selectedSubtitleTrack = subtitles[0].Index;
|
||||
const playbackUrl = `${api.basePath}/Audio/${item?.Id}/universal?${urlParams}`;
|
||||
console.log(item);
|
||||
playAudio(playbackUrl, item, undefined, queueItems, requestedItemIndex);
|
||||
} else {
|
||||
enableSubtitles = false;
|
||||
const mediaInfo = await getMediaInfoApi(api).getPostedPlaybackInfo({
|
||||
audioStreamIndex: item.MediaSources[0].DefaultAudioStreamIndex ?? 0,
|
||||
subtitleStreamIndex: item.MediaSources[0].DefaultSubtitleStreamIndex ?? 0,
|
||||
itemId: item.Id,
|
||||
startTimeTicks: item.UserData?.PlaybackPositionTicks,
|
||||
userId: userId,
|
||||
mediaSourceId: item.MediaSources[0].Id,
|
||||
playbackInfoDto: {
|
||||
DeviceProfile: playbackProfile,
|
||||
},
|
||||
});
|
||||
console.log(mediaInfo);
|
||||
let itemName = item.Name;
|
||||
let episodeTitle = "";
|
||||
if (item.SeriesId) {
|
||||
itemName = item.SeriesName;
|
||||
episodeTitle = `S${item.ParentIndexNumber ?? 0}:E${
|
||||
item.IndexNumber ?? 0
|
||||
} ${item.Name}`;
|
||||
}
|
||||
|
||||
// Select correct subtitle track, this is useful if item is played with playbutton from card since that does not provide coorect default subtitle track index.
|
||||
let selectedSubtitleTrack: number | "nosub" | undefined = -1;
|
||||
const subtitles = mediaInfo.data.MediaSources[0].MediaStreams?.filter(
|
||||
(value) => value.Type === "Subtitle",
|
||||
);
|
||||
let enableSubtitles = true;
|
||||
if (mediaInfo.data.MediaSources[0].DefaultSubtitleStreamIndex) {
|
||||
selectedSubtitleTrack =
|
||||
mediaInfo.data.MediaSources[0].DefaultSubtitleStreamIndex;
|
||||
} else if (subtitles?.length > 0) {
|
||||
selectedSubtitleTrack = subtitles[0].Index;
|
||||
} else {
|
||||
enableSubtitles = false;
|
||||
}
|
||||
const videoTrack = mediaInfo.data.MediaSources[0].MediaStreams?.filter(
|
||||
(value) => value.Type === "Subtitle",
|
||||
);
|
||||
|
||||
const urlOptions = {
|
||||
Static: true,
|
||||
tag: mediaInfo.data.MediaSources[0].ETag,
|
||||
mediaSourceId: mediaInfo.data.MediaSources[0].Id,
|
||||
deviceId: api?.deviceInfo.id,
|
||||
api_key: api?.accessToken,
|
||||
startTimeTicks: item.UserData?.PlaybackPositionTicks,
|
||||
};
|
||||
|
||||
const urlParams = new URLSearchParams(urlOptions).toString();
|
||||
let playbackUrl = `${api?.basePath}/Videos/${mediaInfo.data.MediaSources[0].Id}/stream.${mediaInfo.data.MediaSources[0].Container}?${urlParams}`;
|
||||
|
||||
if (
|
||||
mediaInfo.data.MediaSources[0].SupportsTranscoding &&
|
||||
mediaInfo.data.MediaSources[0].TranscodingUrl
|
||||
) {
|
||||
playbackUrl = `${api.basePath}${mediaInfo.data.MediaSources[0].TranscodingUrl}`;
|
||||
} else if (mediaInfo.data.MediaSources[0].hlsStream) {
|
||||
playbackUrl = mediaInfo.data.MediaSources[0].hlsStream;
|
||||
}
|
||||
playItem(
|
||||
itemName,
|
||||
episodeTitle,
|
||||
videoTrack[0].Index,
|
||||
mediaInfo.data.MediaSources[0].DefaultAudioStreamIndex ?? 0,
|
||||
selectedSubtitleTrack,
|
||||
mediaInfo.data?.MediaSources[0].Container ?? "mkv",
|
||||
enableSubtitles,
|
||||
playbackUrl,
|
||||
userId,
|
||||
item.UserData?.PlaybackPositionTicks,
|
||||
item.RunTimeTicks,
|
||||
item,
|
||||
queueItems,
|
||||
requestedItemIndex,
|
||||
subtitles,
|
||||
mediaInfo.data.MediaSources[0].Id,
|
||||
mediaInfo.data.PlaySessionId,
|
||||
);
|
||||
}
|
||||
const videoTrack = mediaInfo.data.MediaSources[0].MediaStreams?.filter(
|
||||
(value) => value.Type === "Subtitle",
|
||||
);
|
||||
|
||||
const urlOptions = {
|
||||
Static: true,
|
||||
tag: mediaInfo.data.MediaSources[0].ETag,
|
||||
mediaSourceId: mediaInfo.data.MediaSources[0].Id,
|
||||
deviceId: api?.deviceInfo.id,
|
||||
api_key: api?.accessToken,
|
||||
startTimeTicks: item.UserData?.PlaybackPositionTicks,
|
||||
};
|
||||
|
||||
const urlParams = new URLSearchParams(urlOptions).toString();
|
||||
let playbackUrl = `${api?.basePath}/Videos/${mediaInfo.data.MediaSources[0].Id}/stream.${mediaInfo.data.MediaSources[0].Container}?${urlParams}`;
|
||||
|
||||
if (
|
||||
mediaInfo.data.MediaSources[0].SupportsTranscoding &&
|
||||
mediaInfo.data.MediaSources[0].TranscodingUrl
|
||||
) {
|
||||
playbackUrl = `${api.basePath}${mediaInfo.data.MediaSources[0].TranscodingUrl}`;
|
||||
} else if (mediaInfo.data.MediaSources[0].hlsStream) {
|
||||
playbackUrl = mediaInfo.data.MediaSources[0].hlsStream;
|
||||
}
|
||||
playItem(
|
||||
itemName,
|
||||
episodeTitle,
|
||||
videoTrack[0].Index,
|
||||
mediaInfo.data.MediaSources[0].DefaultAudioStreamIndex ?? 0,
|
||||
selectedSubtitleTrack,
|
||||
mediaInfo.data?.MediaSources[0].Container ?? "mkv",
|
||||
enableSubtitles,
|
||||
playbackUrl,
|
||||
userId,
|
||||
item.UserData?.PlaybackPositionTicks,
|
||||
item.RunTimeTicks,
|
||||
item,
|
||||
queueItems,
|
||||
requestedItemIndex,
|
||||
subtitles,
|
||||
mediaInfo.data.MediaSources[0].Id,
|
||||
mediaInfo.data.PlaySessionId,
|
||||
);
|
||||
|
||||
return "playing"; // Return any value to end mutation pending status
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
||||
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
||||
import { create } from "zustand";
|
||||
|
||||
interface QueueStore {
|
||||
@@ -11,7 +11,10 @@ const useQueue = create<QueueStore>(() => ({
|
||||
currentItemIndex: 0,
|
||||
}));
|
||||
|
||||
export const setQueue = (tracks: BaseItemDto[], currentItemIndex: number) => {
|
||||
export const setQueue = (
|
||||
tracks: BaseItemDto[] | undefined,
|
||||
currentItemIndex: number,
|
||||
) => {
|
||||
useQueue.setState(() => ({ tracks, currentItemIndex }));
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user