refactor: cleanup api setting and optimize rerendering due to api setting
This commit is contained in:
2 files changed
+53
-18
No files matched your search
+28
-16
@@ -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 (
|
||||
<RouterProvider
|
||||
router={router}
|
||||
@@ -88,16 +94,22 @@ function ProviderWrapper() {
|
||||
);
|
||||
}
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root")!).render(
|
||||
<React.StrictMode>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<CentralProvider>
|
||||
<ApiProvider>
|
||||
<ProviderWrapper />
|
||||
</ApiProvider>
|
||||
</CentralProvider>
|
||||
</QueryClientProvider>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
const init = async () => {
|
||||
const initialApi = await initializeApi();
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root")!).render(
|
||||
<React.StrictMode>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<CentralProvider>
|
||||
<ApiProvider initialApi={initialApi}>
|
||||
<ProviderWrapper />
|
||||
</ApiProvider>
|
||||
</CentralProvider>
|
||||
</QueryClientProvider>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
document.querySelector(".app-loading")!.classList.toggle("hidden");
|
||||
+25
-2
@@ -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?) =>
|
||||
|
||||
Reference in new issue
Block a user