portal-dashboard/app/root.tsx

117 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

import {Links, Meta, Outlet, Scripts, ScrollRestoration,} from "@remix-run/react";
2024-03-05 16:56:17 +00:00
import stylesheet from "./tailwind.css?url";
import type {LinksFunction} from "@remix-run/node";
2024-03-05 16:56:17 +00:00
// Supports weights 200-800
2024-03-22 17:53:39 +00:00
import "@fontsource-variable/manrope";
import {Refine} from "@refinedev/core";
2024-03-13 13:38:01 +00:00
import routerProvider from "@refinedev/remix-router";
import {notificationProvider} from "~/data/notification-provider";
import {SdkContextProvider, useSdk} from "~/components/lib/sdk-context";
import {Toaster} from "~/components/ui/toaster";
import {getProviders} from "~/data/providers.js";
import {Sdk} from "@lumeweb/portal-sdk";
import resources from "~/data/resources.js";
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
import {useEffect, useMemo, useState} from "react";
import {PinningProcess} from "~/data/pinning.js";
2024-03-05 16:56:17 +00:00
export const links: LinksFunction = () => [
2024-03-22 17:53:39 +00:00
{ rel: "stylesheet", href: stylesheet },
2024-03-05 16:56:17 +00:00
];
const queryClient = new QueryClient();
2024-03-22 17:53:39 +00:00
export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
2024-03-28 23:37:41 +00:00
<body className="max-h-screen">
2024-03-05 16:56:17 +00:00
{children}
2024-03-22 17:53:39 +00:00
<ScrollRestoration />
<Scripts />
</body>
</html>
);
2024-03-05 16:56:17 +00:00
}
function App() {
2024-03-22 17:53:39 +00:00
return (
2024-03-26 12:22:18 +00:00
<>
<Outlet />
<Toaster />
</>
2024-03-22 17:53:39 +00:00
);
2024-03-05 16:56:17 +00:00
}
export default function Root() {
2024-03-26 12:22:18 +00:00
const [portalUrl, setPortalUrl] = useState(import.meta.env.VITE_PORTAL_URL);
const [sdk, setSdk] = useState<Sdk| undefined>(portalUrl ? Sdk.create(portalUrl) : undefined);
2024-03-26 12:22:18 +00:00
useEffect(() => {
if (!portalUrl) {
2024-03-26 12:22:18 +00:00
fetch("/api/meta")
.then((response) => response.json())
.then((data) => {
setPortalUrl(`https://${data.domain}`);
})
.catch((error) => {
console.error("Failed to fetch portal url:", error);
});
}
2024-03-26 12:22:18 +00:00
}, [portalUrl]);
useEffect(() => {
if (portalUrl) {
setSdk(Sdk.create(portalUrl));
}
}, [portalUrl]);
2024-03-26 12:22:18 +00:00
if (!portalUrl) {
return <p>Loading...</p>;
}
return (
<SdkContextProvider sdk={sdk as Sdk}>
<SdkWrapper />
</SdkContextProvider>
);
}
function SdkWrapper() {
const sdk = useSdk();
PinningProcess.setupSdk(sdk as Sdk);
const providers = useMemo(() => getProviders(sdk as Sdk), [sdk]);
if (!sdk) {
return <p>Loading...</p>;
}
return (
<QueryClientProvider client={queryClient}>
<Refine
authProvider={providers.auth}
routerProvider={routerProvider}
notificationProvider={notificationProvider}
dataProvider={{
default: providers.default,
files: providers.files,
}}
resources={resources}
options={{disableTelemetry: true}}>
<App/>
</Refine>
</QueryClientProvider>
)
}
2024-03-05 16:56:17 +00:00
export function HydrateFallback() {
2024-03-22 17:53:39 +00:00
return <p>Loading...</p>;
2024-03-05 16:56:17 +00:00
}