import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { createRouter, RouterProvider } from "@tanstack/react-router";
import React, { useEffect } from "react";
import ReactDOM from "react-dom/client";
// Import the generated route tree
import { routeTree } from "./routeTree.gen";
import {
ApiProvider,
initializeApi,
useApiInContext,
} from "./utils/store/api";
import { CentralProvider, useCentralStore } from "./utils/store/central";
import { delUser } from "./utils/storage/user";
const queryClient = new QueryClient({
defaultOptions: {
queries: {
networkMode: "always",
},
mutations: {
networkMode: "always",
},
},
});
//TODO: Need help implementing modal routes in TanStack Router
// const SearchMask = createRouteMask({
// routeTree,
// from: "/searchModal,
// to: "/search",
// params: true,
// });
// Create a new router instance
const router = createRouter({
routeTree,
context: {
api: undefined!,
createApi: undefined!,
user: undefined,
jellyfinSDK: undefined!,
fetchCurrentUser: undefined!,
queryClient,
},
defaultPreload: false,
defaultPreloadStaleTime: 0,
defaultViewTransition: false,
scrollRestoration: true,
scrollRestorationBehavior: "auto",
});
// Register the router instance for type safety
declare module "@tanstack/react-router" {
interface Register {
router: typeof router;
}
}
function ProviderWrapper() {
const [api, createApi, jellyfinSDK] = useApiInContext((s) => [
s.api,
s.createApi,
s.jellyfin,
]);
const [user, fetchCurrentUser] = useCentralStore((s) => [
s.currentUser,
s.fetchCurrentUser,
]);
useEffect(() => {
if (api?.accessToken && !user?.Id) {
fetchCurrentUser(api).catch(async (error) => {
console.error("Could not restore the saved session", error);
const status = (error as { response?: { status?: number } })?.response
?.status;
// Only an actual authorization failure should log the user out.
if (status !== 401 && status !== 403) return;
await delUser();
createApi(api.basePath, undefined);
router.navigate({
to: "/login/manual",
search: { redirect: window.location.pathname },
});
});
}
}, [api, user, fetchCurrentUser, createApi]);
useEffect(() => {
if (api) {
console.log(`API is set - Route ${window.location.pathname}`);
console.log(api);
} else {
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?.accessToken]);
return (
);
}
const init = async () => {
// Frameless titlebar: macOS keeps the native traffic lights; Windows and
// Linux use the custom window buttons in the header.
const userAgent = navigator.userAgent;
if (userAgent.includes("Macintosh")) {
document.documentElement.classList.add("is-macos");
} else if (userAgent.includes("Windows")) {
document.documentElement.classList.add("is-windows");
} else {
document.documentElement.classList.add("is-linux");
}
// Layout transitions during a live window resize cause visible wobble
// (headers, cards, MUI components). Disable them until resizing settles.
let resizeTimer: ReturnType | undefined;
window.addEventListener("resize", () => {
document.documentElement.classList.add("is-resizing");
if (resizeTimer) clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
document.documentElement.classList.remove("is-resizing");
}, 200);
});
const initialApi = await initializeApi();
ReactDOM.createRoot(document.getElementById("root")!).render(
,
);
};
init();
document.querySelector(".app-loading")!.classList.toggle("hidden");