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
});
}

View File

@@ -14,6 +14,7 @@ import {
} from "react-icons/tb";
import { Button } from "ui";
import { useAuth, useBknd } from "ui/client";
import { useBkndWindowContext } from "ui/client/BkndProvider";
import { useBkndSystemTheme } from "ui/client/schema/system/use-bknd-system";
import { IconButton } from "ui/components/buttons/IconButton";
import { Logo } from "ui/components/display/Logo";
@@ -147,11 +148,12 @@ export function Header({ hasSidebar = true }) {
function UserMenu() {
const auth = useAuth();
const [navigate] = useNavigate();
const { logout_route } = useBkndWindowContext();
async function handleLogout() {
await auth.logout();
// @todo: grab from somewhere constant
window.location.href = "/auth/logout";
navigate(logout_route, { reload: true });
}
async function handleLogin() {

View File

@@ -63,8 +63,15 @@ export function useNavigate() {
return [
(
url: string,
options?: { query?: object; absolute?: boolean; replace?: boolean; state?: any }
options?:
| { query?: object; absolute?: boolean; replace?: boolean; state?: any }
| { reload: true }
) => {
if (options && "reload" in options) {
window.location.href = url;
return;
}
const _url = options?.absolute ? `~/${basepath}${url}`.replace(/\/+/g, "/") : url;
navigate(options?.query ? withQuery(_url, options?.query) : _url, {
replace: options?.replace,