From 6bf7f434b5ed439d07f836d8fea1b40d6580d3a0 Mon Sep 17 00:00:00 2001 From: --remove-section Date: Tue, 23 Dec 2025 21:56:06 +0530 Subject: [PATCH] refactor: cleanup api setting and optimize rerendering due to api setting --- src/main.tsx | 44 ++++++++++++++++++++++++++--------------- src/utils/store/api.tsx | 27 +++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 9a2126f..a037456 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,7 +4,11 @@ import React, { useEffect } from "react"; import ReactDOM from "react-dom/client"; // Import the generated route tree import { routeTree } from "./routeTree.gen"; -import { ApiProvider, useApiInContext } from "./utils/store/api"; +import { + ApiProvider, + initializeApi, + useApiInContext, +} from "./utils/store/api"; import { CentralProvider, useCentralStore } from "./utils/store/central"; export const queryClient = new QueryClient({ @@ -61,9 +65,11 @@ function ProviderWrapper() { s.currentUser, s.fetchCurrentUser, ]); - if (api?.accessToken && !user?.Id) { - fetchCurrentUser(api); - } + useEffect(() => { + if (api?.accessToken && !user?.Id) { + fetchCurrentUser(api); + } + }, [api, user, fetchCurrentUser]); useEffect(() => { if (api) { console.log(`API is set - Route ${window.location.pathname}`); @@ -72,7 +78,7 @@ function ProviderWrapper() { console.log(`API is not set - Route ${window.location.pathname}`); } router.invalidate(); // This is a hack to force the router to re-evaluate the routes and re-run the beforeLoad functions - }, [api]); + }, [api?.accessToken]); return ( - - - - - - - - , -); +const init = async () => { + const initialApi = await initializeApi(); + + ReactDOM.createRoot(document.getElementById("root")!).render( + + + + + + + + + , + ); +}; + +init(); document.querySelector(".app-loading")!.classList.toggle("hidden"); diff --git a/src/utils/store/api.tsx b/src/utils/store/api.tsx index e6e7021..ce5307d 100644 --- a/src/utils/store/api.tsx +++ b/src/utils/store/api.tsx @@ -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>(undefined!); -export const ApiProvider = ({ children }: { children: ReactNode }) => { +export const ApiProvider = ({ + children, + initialApi, +}: { children: ReactNode; initialApi?: Api }) => { const [store] = useState(() => createStore()((set) => ({ - api: undefined!, + api: initialApi, deviceId: deviceId, jellyfin: jellyfin, createApi: (serverAddress, accessToken?) =>