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