confirmed SSR support with Remix

This commit is contained in:
dswbx
2024-11-25 19:59:46 +01:00
parent 1c94777317
commit eea76ebc28
15 changed files with 144 additions and 44 deletions

View File

@@ -1,4 +1,4 @@
import { notifications } from "@mantine/notifications";
//import { notifications } from "@mantine/notifications";
import { getDefaultConfig, getDefaultSchema } from "modules/ModuleManager";
import { createContext, startTransition, useContext, useEffect, useRef, useState } from "react";
import type { ModuleConfigs, ModuleSchemas } from "../../modules";
@@ -83,7 +83,6 @@ export function BkndProvider({
if (!fetched || !schema) return null;
const app = new AppReduced(schema?.config as any);
const actions = getSchemaActions({ client, setSchema, reloadSchema });
return (
@@ -93,6 +92,20 @@ export function BkndProvider({
);
}
type BkndWindowContext = {
user?: object;
logout_route: string;
};
export function useBkndWindowContext(): BkndWindowContext {
if (typeof window !== "undefined" && window.__BKND__) {
return window.__BKND__ as any;
} else {
return {
logout_route: "/api/auth/logout"
};
}
}
export function useBknd({ withSecrets }: { withSecrets?: boolean } = {}): BkndContext {
const ctx = useContext(BkndContext);
if (withSecrets) ctx.requireSecrets();

View File

@@ -1,5 +1,6 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { createContext, useContext, useEffect, useState } from "react";
import { useBkndWindowContext } from "ui/client/BkndProvider";
import { AppQueryClient } from "./utils/AppQueryClient";
const ClientContext = createContext<{ baseUrl: string; client: AppQueryClient }>({
@@ -15,8 +16,13 @@ export const queryClient = new QueryClient({
}
});
export const ClientProvider = ({ children, baseUrl }: { children?: any; baseUrl?: string }) => {
export const ClientProvider = ({
children,
baseUrl,
user
}: { children?: any; baseUrl?: string; user?: object }) => {
const [actualBaseUrl, setActualBaseUrl] = useState<string | null>(null);
const winCtx = useBkndWindowContext();
try {
const _ctx_baseUrl = useBaseUrl();
@@ -40,8 +46,8 @@ export const ClientProvider = ({ children, baseUrl }: { children?: any; baseUrl?
return null; // or a loader/spinner if desired
}
console.log("client provider11 with", { baseUrl, fallback: actualBaseUrl });
const client = createClient(actualBaseUrl);
//console.log("client provider11 with", { baseUrl, fallback: actualBaseUrl, user });
const client = createClient(actualBaseUrl, user ?? winCtx.user);
return (
<QueryClientProvider client={queryClient}>
@@ -52,11 +58,11 @@ export const ClientProvider = ({ children, baseUrl }: { children?: any; baseUrl?
);
};
export function createClient(baseUrl: string = window.location.origin) {
return new AppQueryClient(baseUrl);
export function createClient(baseUrl: string, user?: object) {
return new AppQueryClient(baseUrl, user);
}
export function createOrUseClient(baseUrl: string = window.location.origin) {
export function createOrUseClient(baseUrl: string) {
const context = useContext(ClientContext);
if (!context) {
console.warn("createOrUseClient returned a new client");

View File

@@ -13,9 +13,13 @@ import { queryClient } from "../ClientProvider";
export class AppQueryClient {
api: Api;
constructor(public baseUrl: string) {
constructor(
public baseUrl: string,
user?: object
) {
this.api = new Api({
host: baseUrl
host: baseUrl,
user
});
}