refactor: we need to split root out for readability into a SdkWrapper component to reflect changes in init'ing the sdk down the component tree, and sdk should be a react var

This commit is contained in:
Derrick Hammer 2024-03-26 13:01:46 -04:00
parent 5bdd888f63
commit c5e22f52e1
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 52 additions and 46 deletions

View File

@ -1,27 +1,21 @@
import { import {Links, Meta, Outlet, Scripts, ScrollRestoration,} from "@remix-run/react";
Links,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";
import stylesheet from "./tailwind.css?url"; import stylesheet from "./tailwind.css?url";
import type { LinksFunction } from "@remix-run/node"; import type {LinksFunction} from "@remix-run/node";
// Supports weights 200-800 // Supports weights 200-800
import "@fontsource-variable/manrope"; import "@fontsource-variable/manrope";
import { Refine } from "@refinedev/core"; import {Refine} from "@refinedev/core";
import routerProvider from "@refinedev/remix-router"; import routerProvider from "@refinedev/remix-router";
import { notificationProvider } from "~/data/notification-provider"; import {notificationProvider} from "~/data/notification-provider";
import { SdkContextProvider, useSdk } from "~/components/lib/sdk-context"; import {SdkContextProvider, useSdk} from "~/components/lib/sdk-context";
import { Toaster } from "~/components/ui/toaster"; import {Toaster} from "~/components/ui/toaster";
import { getProviders } from "~/data/providers.js"; import {getProviders} from "~/data/providers.js";
import { Sdk } from "@lumeweb/portal-sdk"; import {Sdk} from "@lumeweb/portal-sdk";
import resources from "~/data/resources.js"; import resources from "~/data/resources.js";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
import { useEffect, useMemo, useState } from "react"; import {useEffect, useMemo, useState} from "react";
import { PinningProcess } from "~/data/pinning.js"; import {PinningProcess} from "~/data/pinning.js";
export const links: LinksFunction = () => [ export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet }, { rel: "stylesheet", href: stylesheet },
@ -58,16 +52,7 @@ function App() {
export default function Root() { export default function Root() {
const [portalUrl, setPortalUrl] = useState(import.meta.env.VITE_PORTAL_URL); const [portalUrl, setPortalUrl] = useState(import.meta.env.VITE_PORTAL_URL);
const sdk = Sdk.create(portalUrl); const [sdk, setSdk] = useState<Sdk| undefined>(portalUrl ? Sdk.create(portalUrl) : undefined);
const providers = useMemo(() => getProviders(sdk as Sdk), [sdk]);
useEffect(() => {
if (sdk) {
PinningProcess.setupSdk(sdk as Sdk);
}
}, [sdk]);
useEffect(() => { useEffect(() => {
if (!portalUrl) { if (!portalUrl) {
fetch("/api/meta") fetch("/api/meta")
@ -81,12 +66,34 @@ export default function Root() {
} }
}, [portalUrl]); }, [portalUrl]);
useEffect(() => {
if (portalUrl) {
setSdk(Sdk.create(portalUrl));
}
}, [portalUrl]);
if (!portalUrl) { if (!portalUrl) {
return <p>Loading...</p>; return <p>Loading...</p>;
} }
return ( return (
<SdkContextProvider sdk={sdk}> <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}> <QueryClientProvider client={queryClient}>
<Refine <Refine
authProvider={providers.auth} authProvider={providers.auth}
@ -97,12 +104,11 @@ export default function Root() {
files: providers.files, files: providers.files,
}} }}
resources={resources} resources={resources}
options={{ disableTelemetry: true }}> options={{disableTelemetry: true}}>
<App /> <App/>
</Refine> </Refine>
</QueryClientProvider> </QueryClientProvider>
</SdkContextProvider> )
);
} }
export function HydrateFallback() { export function HydrateFallback() {