refactor: cleanup api setting and optimize rerendering due to api setting
This commit is contained in:
2 files changed
+42
-7
No files matched your search
+17
-5
@@ -4,7 +4,11 @@ import React, { useEffect } from "react";
|
|||||||
import ReactDOM from "react-dom/client";
|
import ReactDOM from "react-dom/client";
|
||||||
// Import the generated route tree
|
// Import the generated route tree
|
||||||
import { routeTree } from "./routeTree.gen";
|
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";
|
import { CentralProvider, useCentralStore } from "./utils/store/central";
|
||||||
|
|
||||||
export const queryClient = new QueryClient({
|
export const queryClient = new QueryClient({
|
||||||
@@ -61,9 +65,11 @@ function ProviderWrapper() {
|
|||||||
s.currentUser,
|
s.currentUser,
|
||||||
s.fetchCurrentUser,
|
s.fetchCurrentUser,
|
||||||
]);
|
]);
|
||||||
|
useEffect(() => {
|
||||||
if (api?.accessToken && !user?.Id) {
|
if (api?.accessToken && !user?.Id) {
|
||||||
fetchCurrentUser(api);
|
fetchCurrentUser(api);
|
||||||
}
|
}
|
||||||
|
}, [api, user, fetchCurrentUser]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (api) {
|
if (api) {
|
||||||
console.log(`API is set - Route ${window.location.pathname}`);
|
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}`);
|
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
|
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 (
|
return (
|
||||||
<RouterProvider
|
<RouterProvider
|
||||||
router={router}
|
router={router}
|
||||||
@@ -88,16 +94,22 @@ function ProviderWrapper() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById("root")!).render(
|
const init = async () => {
|
||||||
|
const initialApi = await initializeApi();
|
||||||
|
|
||||||
|
ReactDOM.createRoot(document.getElementById("root")!).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<CentralProvider>
|
<CentralProvider>
|
||||||
<ApiProvider>
|
<ApiProvider initialApi={initialApi}>
|
||||||
<ProviderWrapper />
|
<ProviderWrapper />
|
||||||
</ApiProvider>
|
</ApiProvider>
|
||||||
</CentralProvider>
|
</CentralProvider>
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
</React.StrictMode>,
|
</React.StrictMode>,
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
init();
|
||||||
|
|
||||||
document.querySelector(".app-loading")!.classList.toggle("hidden");
|
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 { shallow } from "zustand/shallow";
|
||||||
import { useStoreWithEqualityFn } from "zustand/traditional";
|
import { useStoreWithEqualityFn } from "zustand/traditional";
|
||||||
import { version as appVer } from "../../../package.json";
|
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
|
// Initial custom axios client to use tauri's http module
|
||||||
import axios from "axios";
|
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 ApiContext = createContext<StoreApi<ApiStore>>(undefined!);
|
||||||
|
|
||||||
export const ApiProvider = ({ children }: { children: ReactNode }) => {
|
export const ApiProvider = ({
|
||||||
|
children,
|
||||||
|
initialApi,
|
||||||
|
}: { children: ReactNode; initialApi?: Api }) => {
|
||||||
const [store] = useState(() =>
|
const [store] = useState(() =>
|
||||||
createStore<ApiStore>()((set) => ({
|
createStore<ApiStore>()((set) => ({
|
||||||
api: undefined!,
|
api: initialApi,
|
||||||
deviceId: deviceId,
|
deviceId: deviceId,
|
||||||
jellyfin: jellyfin,
|
jellyfin: jellyfin,
|
||||||
createApi: (serverAddress, accessToken?) =>
|
createApi: (serverAddress, accessToken?) =>
|
||||||
|
|||||||
Reference in new issue
Block a user