mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-04 00:56:01 +00:00
update admin basepath handling and window context integration (#155)
Refactored `useBkndWindowContext` to include `admin_basepath` and updated its usage in routing. Improved type consistency with `AdminBkndWindowContext` and ensured default values are applied for window context.
This commit is contained in:
@@ -8,9 +8,16 @@ import { Fragment } from "hono/jsx";
|
|||||||
import { css, Style } from "hono/css";
|
import { css, Style } from "hono/css";
|
||||||
import { Controller } from "modules/Controller";
|
import { Controller } from "modules/Controller";
|
||||||
import * as SystemPermissions from "modules/permissions";
|
import * as SystemPermissions from "modules/permissions";
|
||||||
|
import type { TApiUser } from "Api";
|
||||||
|
|
||||||
const htmlBkndContextReplace = "<!-- BKND_CONTEXT -->";
|
const htmlBkndContextReplace = "<!-- BKND_CONTEXT -->";
|
||||||
|
|
||||||
|
export type AdminBkndWindowContext = {
|
||||||
|
user?: TApiUser;
|
||||||
|
logout_route: string;
|
||||||
|
admin_basepath: string;
|
||||||
|
};
|
||||||
|
|
||||||
// @todo: add migration to remove admin path from config
|
// @todo: add migration to remove admin path from config
|
||||||
export type AdminControllerOptions = {
|
export type AdminControllerOptions = {
|
||||||
basepath?: string;
|
basepath?: string;
|
||||||
@@ -80,6 +87,7 @@ export class AdminController extends Controller {
|
|||||||
const obj = {
|
const obj = {
|
||||||
user: c.get("auth")?.user,
|
user: c.get("auth")?.user,
|
||||||
logout_route: this.withAdminBasePath(authRoutes.logout),
|
logout_route: this.withAdminBasePath(authRoutes.logout),
|
||||||
|
admin_basepath: this.options.adminBasepath,
|
||||||
};
|
};
|
||||||
const html = await this.getHtml(obj);
|
const html = await this.getHtml(obj);
|
||||||
if (!html) {
|
if (!html) {
|
||||||
@@ -142,7 +150,7 @@ export class AdminController extends Controller {
|
|||||||
return hono;
|
return hono;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getHtml(obj: any = {}) {
|
private async getHtml(obj: AdminBkndWindowContext) {
|
||||||
const bknd_context = `window.__BKND__ = JSON.parse('${JSON.stringify(obj)}');`;
|
const bknd_context = `window.__BKND__ = JSON.parse('${JSON.stringify(obj)}');`;
|
||||||
|
|
||||||
if (this.options.html) {
|
if (this.options.html) {
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import { Logo } from "ui/components/display/Logo";
|
|||||||
import * as AppShell from "ui/layouts/AppShell/AppShell";
|
import * as AppShell from "ui/layouts/AppShell/AppShell";
|
||||||
import { ClientProvider, type ClientProviderProps } from "./client";
|
import { ClientProvider, type ClientProviderProps } from "./client";
|
||||||
import { createMantineTheme } from "./lib/mantine/theme";
|
import { createMantineTheme } from "./lib/mantine/theme";
|
||||||
import { BkndModalsProvider } from "./modals";
|
|
||||||
import { Routes } from "./routes";
|
import { Routes } from "./routes";
|
||||||
|
|
||||||
export type BkndAdminProps = {
|
export type BkndAdminProps = {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Api, type ApiOptions, type TApiUser } from "Api";
|
import { Api, type ApiOptions, type TApiUser } from "Api";
|
||||||
import { isDebug } from "core";
|
import { isDebug } from "core";
|
||||||
import { createContext, type ReactNode, useContext } from "react";
|
import { createContext, type ReactNode, useContext } from "react";
|
||||||
|
import type { AdminBkndWindowContext } from "modules/server/AdminController";
|
||||||
|
|
||||||
const ClientContext = createContext<{ baseUrl: string; api: Api }>({
|
const ClientContext = createContext<{ baseUrl: string; api: Api }>({
|
||||||
baseUrl: undefined,
|
baseUrl: undefined,
|
||||||
@@ -68,16 +69,18 @@ export const useBaseUrl = () => {
|
|||||||
return context.baseUrl;
|
return context.baseUrl;
|
||||||
};
|
};
|
||||||
|
|
||||||
type BkndWindowContext = {
|
export function useBkndWindowContext(): AdminBkndWindowContext {
|
||||||
user?: TApiUser;
|
const defaults = {
|
||||||
logout_route: string;
|
logout_route: "/api/auth/logout",
|
||||||
};
|
admin_basepath: "",
|
||||||
export function useBkndWindowContext(): BkndWindowContext {
|
};
|
||||||
|
|
||||||
if (typeof window !== "undefined" && window.__BKND__) {
|
if (typeof window !== "undefined" && window.__BKND__) {
|
||||||
return window.__BKND__ as any;
|
|
||||||
} else {
|
|
||||||
return {
|
return {
|
||||||
logout_route: "/api/auth/logout",
|
...defaults,
|
||||||
|
...window.__BKND__,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return defaults;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import SettingsRoutes from "./settings";
|
|||||||
import { FlashMessage } from "ui/modules/server/FlashMessage";
|
import { FlashMessage } from "ui/modules/server/FlashMessage";
|
||||||
import { AuthRegister } from "ui/routes/auth/auth.register";
|
import { AuthRegister } from "ui/routes/auth/auth.register";
|
||||||
import { BkndModalsProvider } from "ui/modals";
|
import { BkndModalsProvider } from "ui/modals";
|
||||||
|
import { useBkndWindowContext } from "ui/client";
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const TestRoutes = lazy(() => import("./test"));
|
const TestRoutes = lazy(() => import("./test"));
|
||||||
@@ -20,11 +21,13 @@ export function Routes({
|
|||||||
basePath = "",
|
basePath = "",
|
||||||
}: { BkndWrapper: ComponentType<{ children: ReactNode }>; basePath?: string }) {
|
}: { BkndWrapper: ComponentType<{ children: ReactNode }>; basePath?: string }) {
|
||||||
const { theme } = useTheme();
|
const { theme } = useTheme();
|
||||||
|
const ctx = useBkndWindowContext();
|
||||||
|
const actualBasePath = basePath || ctx.admin_basepath;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id="bknd-admin" className={theme + " antialiased"}>
|
<div id="bknd-admin" className={theme + " antialiased"}>
|
||||||
<FlashMessage />
|
<FlashMessage />
|
||||||
<Router base={basePath}>
|
<Router base={actualBasePath}>
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route path="/auth/login" component={AuthLogin} />
|
<Route path="/auth/login" component={AuthLogin} />
|
||||||
<Route path="/auth/register" component={AuthRegister} />
|
<Route path="/auth/register" component={AuthRegister} />
|
||||||
|
|||||||
Reference in New Issue
Block a user