Files
Blink/src/main.tsx
T
1xDed 75b4ddd598
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
fix(auth): do not log out on transient network errors
Only 401/403 clears the saved session; other failures keep the token. Routes without a token now redirect to login instead of showing a permanent spinner.
2026-11-28 01:44:00 +03:00

151 lines
4.0 KiB
TypeScript

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 (
<RouterProvider
router={router}
context={{
api,
createApi,
user,
jellyfinSDK,
fetchCurrentUser,
// queryClient,
}}
/>
);
}
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<typeof setTimeout> | 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(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<CentralProvider>
<ApiProvider initialApi={initialApi}>
<ProviderWrapper />
</ApiProvider>
</CentralProvider>
</QueryClientProvider>
</React.StrictMode>,
);
};
init();
document.querySelector(".app-loading")!.classList.toggle("hidden");