The daemon hands the browser `http://127.0.0.1:<port>/#t=<token>`. The token is read once, stored through the connection store (so the `pi2-connection` marker is set too and a fresh browser does not bounce to /connect), and stripped from the address bar and history while the query string survives.
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
import { RouterProvider } from "@tanstack/react-router";
|
|
import { ThemeProvider } from "next-themes";
|
|
import { StrictMode } from "react";
|
|
import { createRoot } from "react-dom/client";
|
|
|
|
import "@/styles/app.css";
|
|
import "@/lib/i18n";
|
|
|
|
import { Toaster } from "@/components/ui/toast";
|
|
import { consumeHandoff } from "@/lib/handoff";
|
|
import { router } from "@/router";
|
|
import { useConnectionStore } from "@/stores/connection";
|
|
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: {
|
|
staleTime: 10_000,
|
|
retry: 1,
|
|
refetchOnWindowFocus: false,
|
|
},
|
|
},
|
|
});
|
|
|
|
function App() {
|
|
return (
|
|
<StrictMode>
|
|
<ThemeProvider
|
|
attribute="data-theme"
|
|
storageKey="pi2-theme"
|
|
defaultTheme="system"
|
|
enableSystem
|
|
disableTransitionOnChange
|
|
>
|
|
<QueryClientProvider client={queryClient}>
|
|
<RouterProvider router={router} />
|
|
<Toaster />
|
|
</QueryClientProvider>
|
|
</ThemeProvider>
|
|
</StrictMode>
|
|
);
|
|
}
|
|
|
|
// `:PiWeb` hands the device token over in the fragment. Consume it before the
|
|
// router resolves its first route, and go through the connection store (not
|
|
// `setDeviceToken`) so the `pi2-connection` marker the guards read is set too:
|
|
// otherwise a fresh browser would still bounce to `/connect` on its first load.
|
|
consumeHandoff(window.location, window.history, (token) => {
|
|
useConnectionStore.getState().connect({ token, endpoint: window.location.origin });
|
|
});
|
|
|
|
const container = document.getElementById("root");
|
|
if (!container) throw new Error("Missing #root element");
|
|
createRoot(container).render(<App />);
|