refactor: cleanup api setting and optimize rerendering due to api setting

This commit is contained in:
--remove-section committed 2025-12-23 21:56:06 +05:30
1 parent 4834a31cb5
commit 6bf7f434b5
2 files changed
+53 -18

No files matched your search

+25 -2
View File
@@ -5,6 +5,8 @@ import { type StoreApi, createStore } from "zustand";
import { shallow } from "zustand/shallow";
import { useStoreWithEqualityFn } from "zustand/traditional";
import { version as appVer } from "../../../package.json";
import { getDefaultServer, getServer } from "../storage/servers";
import { getUser } from "../storage/user";
// Initial custom axios client to use tauri's http module
import axios from "axios";
@@ -44,12 +46,33 @@ export const jellyfin = new Jellyfin({
},
});
export const initializeApi = async () => {
const currentServerId = await getDefaultServer();
if (currentServerId) {
const currentServer = await getServer(currentServerId);
if (currentServer?.address) {
const userOnDisk = await getUser();
if (userOnDisk) {
return jellyfin.createApi(
currentServer.address,
userOnDisk.AccessToken,
);
}
return jellyfin.createApi(currentServer.address);
}
}
return undefined;
};
export const ApiContext = createContext<StoreApi<ApiStore>>(undefined!);
export const ApiProvider = ({ children }: { children: ReactNode }) => {
export const ApiProvider = ({
children,
initialApi,
}: { children: ReactNode; initialApi?: Api }) => {
const [store] = useState(() =>
createStore<ApiStore>()((set) => ({
api: undefined!,
api: initialApi,
deviceId: deviceId,
jellyfin: jellyfin,
createApi: (serverAddress, accessToken?) =>