feat: access api endpoint relatively to get the portal domain if VITE_PORTAL_URL is not set

This commit is contained in:
Derrick Hammer 2024-03-26 00:41:43 -04:00
parent ab425c6f2c
commit d5f63490a5
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 17 additions and 2 deletions

View File

@ -20,7 +20,7 @@ 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 { useMemo } 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 = () => [
@ -71,7 +71,22 @@ function App() {
} }
export default function Root() { export default function Root() {
const sdk = Sdk.create(import.meta.env.VITE_PORTAL_URL); const [portalUrl, setPortalUrl] = useState(import.meta.env.VITE_PORTAL_URL);
useEffect(() => {
if (!portalUrl) {
fetch('/api/meta')
.then(response => response.json())
.then(data => {
setPortalUrl(data.domain);
})
.catch((error: any) => {
console.error('Failed to fetch portal url:', error);
});
}
}, [portalUrl]);
const sdk = Sdk.create(portalUrl);
return ( return (
<SdkContextProvider sdk={sdk}> <SdkContextProvider sdk={sdk}>
<App /> <App />