React 管理頁面和小工具

本頁內容

原生插件可以使用自訂 React 頁面和儀表板小工具擴充管理面板——沙盒插件將其 UI 描述為 Block Kit,因為將插件 JavaScript 傳送到管理面板會破壞沙盒隔離。

如果您的插件只需要一個設定表單,自動產生的 admin.settingsSchema 表單(參見您的第一個原生插件)無需編寫任何 React 即可涵蓋大多數情況。當您需要比 settingsSchema 提供的更豐富的 UI 時,請使用自訂元件。

管理進入點

具有管理 UI 的插件從 admin 進入點匯出 pageswidgets 物件:

import { SEOSettingsPage } from "./components/SEOSettingsPage";
import { SEODashboardWidget } from "./components/SEODashboardWidget";

export const widgets = {
	"seo-overview": SEODashboardWidget,
};

export const pages = {
	"/settings": SEOSettingsPage,
};

package.json 中設定進入點:

{
	"exports": {
		".": "./dist/index.js",
		"./admin": "./dist/admin.js"
	}
}

definePlugin() 參照它:

definePlugin({
	id: "seo",
	version: "1.0.0",

	admin: {
		entry: "@my-org/plugin-seo/admin",
		pages: [{ path: "/settings", label: "SEO Settings", icon: "settings" }],
		widgets: [{ id: "seo-overview", title: "SEO Overview", size: "half" }],
	},
});

描述符需要匹配的 adminEntry,以便 EmDash 知道在建置時在哪裡找到元件:

adminEntry: "@my-org/plugin-seo/admin",

管理頁面

管理頁面是掛載在 /_emdash/admin/plugins/<plugin-id>/<path> 下的 React 元件。

頁面定義

使用路徑、標籤和圖示在 admin.pages 下宣告每個頁面:

admin: {
	pages: [
		{
			path: "/settings",
			label: "Settings",
			icon: "settings",
		},
		{
			path: "/reports",
			label: "Reports",
			icon: "chart",
		},
	],
}

頁面元件

以下元件透過插件 API hook 讀取和儲存設定:

import { useState, useEffect } from "react";
import { usePluginAPI } from "@emdash-cms/admin";

export function SettingsPage() {
	const api = usePluginAPI();
	const [settings, setSettings] = useState<Record<string, unknown>>({});
	const [saving, setSaving] = useState(false);

	useEffect(() => {
		api.get("settings").then(setSettings);
	}, []);

	const handleSave = async () => {
		setSaving(true);
		await api.post("settings/save", settings);
		setSaving(false);
	};

	return (
		<div>
			<h1>Plugin Settings</h1>

			<label>
				Site Title
				<input
					type="text"
					value={(settings.siteTitle as string) || ""}
					onChange={(e) => setSettings({ ...settings, siteTitle: e.target.value })}
				/>
			</label>

			<button onClick={handleSave} disabled={saving}>
				{saving ? "Saving..." : "Save Settings"}
			</button>
		</div>
	);
}

插件 API hook

usePluginAPI() 使用插件 ID 前綴和自動新增的 X-EmDash-Request: 1 CSRF 標頭呼叫插件的路由:

import { usePluginAPI } from "@emdash-cms/admin";

function MyComponent() {
	const api = usePluginAPI();

	const data = await api.get("status");                       // GET /_emdash/api/plugins/<id>/status
	await api.post("settings/save", { enabled: true });          // POST with JSON body
	const result = await api.get("history?limit=50");            // query params supported
}

儀表板小工具

小工具出現在管理儀表板上並提供一目了然的資訊。

小工具定義

使用 id、標題和大小在 admin.widgets 下宣告每個小工具:

admin: {
	widgets: [
		{
			id: "seo-overview",
			title: "SEO Overview",
			size: "half",   // "full" | "half" | "third"
		},
	],
}

小工具元件

以下元件在掛載時取得其資料並呈現精簡的摘要:

import { useState, useEffect } from "react";
import { usePluginAPI } from "@emdash-cms/admin";

export function SEOWidget() {
	const api = usePluginAPI();
	const [data, setData] = useState({ score: 0, issues: [] });

	useEffect(() => {
		api.get("analyze").then(setData);
	}, []);

	return (
		<div className="widget-content">
			<div className="score">{data.score}%</div>
			<ul>
				{data.issues.map((issue, i) => (
					<li key={i}>{(issue as { message: string }).message}</li>
				))}
			</ul>
		</div>
	);
}

小工具大小

SizeDescription
full儀表板全寬
half儀表板半寬
third儀表板三分之一寬

小工具根據螢幕寬度自動換行。

匯出結構

管理進入點匯出兩個物件:

import { SettingsPage } from "./components/SettingsPage";
import { ReportsPage } from "./components/ReportsPage";
import { StatusWidget } from "./components/StatusWidget";
import { OverviewWidget } from "./components/OverviewWidget";

export const pages = {
	"/settings": SettingsPage,
	"/reports": ReportsPage,
};

export const widgets = {
	status: StatusWidget,
	overview: OverviewWidget,
};

使用管理元件

EmDash 為常見模式提供預先建置元件:

import {
	Card,
	Button,
	Input,
	Select,
	Toggle,
	Table,
	Pagination,
	Alert,
	Loading,
} from "@emdash-cms/admin";

function SettingsPage() {
	return (
		<Card title="Settings">
			<Input label="API Key" type="password" />
			<Toggle label="Enabled" defaultChecked />
			<Button variant="primary">Save</Button>
		</Card>
	);
}

自動產生的設定 UI

如果您的插件只需要一個設定表單,請使用 admin.settingsSchema 而不使用自訂元件:

admin: {
	settingsSchema: {
		apiKey: { type: "secret", label: "API Key" },
		enabled: { type: "boolean", label: "Enabled", default: true },
	},
},

EmDash 自動產生設定頁面。僅當您需要超出基本表單的行為時,才使用自訂 React 頁面。

導覽

插件頁面出現在插件名稱下的管理側邊欄中。順序與 admin.pages 陣列匹配,如下所示:

admin: {
	pages: [
		{ path: "/settings", label: "Settings", icon: "settings" },  // first
		{ path: "/history", label: "History", icon: "history" },     // second
		{ path: "/reports", label: "Reports", icon: "chart" },        // third
	],
}

建置設定

管理元件需要單獨的建置進入點。以下打包器設定同時建置伺服器和管理進入點:

tsdown

export default {
	entry: {
		index: "src/index.ts",
		admin: "src/admin.tsx",
	},
	format: "esm",
	dts: true,
	external: ["react", "react-dom", "emdash", "@emdash-cms/admin"],
};

tsup

export default {
	entry: ["src/index.ts", "src/admin.tsx"],
	format: "esm",
	dts: true,
	external: ["react", "react-dom", "emdash", "@emdash-cms/admin"],
};

將 React 和 EmDash admin 保留為外部相依性以避免打包重複項目。

啟用/停用插件

當在管理面板中停用插件時:

  • 側邊欄連結被隱藏。
  • 儀表板小工具不會呈現。
  • 管理頁面傳回 404。
  • 後端掛鉤仍然執行(為了資料安全)。

插件可以檢查其啟用狀態:

const enabled = await ctx.kv.get<boolean>("_emdash:enabled");

完整範例

以下插件定義了一個儀表板頁面、一個設定頁面和一個小工具,執行時和管理進入點位於單獨的檔案中。src/index.ts 檔案包含描述符和執行時:

import { definePlugin } from "emdash";
import type { PluginDescriptor } from "emdash";

export function analyticsPlugin(): PluginDescriptor {
	return {
		id: "analytics",
		version: "1.0.0",
		format: "native",
		entrypoint: "@my-org/plugin-analytics",
		adminEntry: "@my-org/plugin-analytics/admin",
		adminPages: [
			{ path: "/dashboard", label: "Dashboard", icon: "chart" },
			{ path: "/settings", label: "Settings", icon: "settings" },
		],
		adminWidgets: [{ id: "events-today", title: "Events Today", size: "third" }],
	};
}

export function createPlugin() {
	return definePlugin({
		id: "analytics",
		version: "1.0.0",

		capabilities: ["network:request"],
		allowedHosts: ["api.analytics.example.com"],

		storage: {
			events: { indexes: ["type", "createdAt"] },
		},

		admin: {
			entry: "@my-org/plugin-analytics/admin",
			settingsSchema: {
				trackingId: { type: "string", label: "Tracking ID" },
				enabled: { type: "boolean", label: "Enabled", default: true },
			},
			pages: [
				{ path: "/dashboard", label: "Dashboard", icon: "chart" },
				{ path: "/settings", label: "Settings", icon: "settings" },
			],
			widgets: [{ id: "events-today", title: "Events Today", size: "third" }],
		},

		routes: {
			stats: {
				handler: async (ctx) => {
					const today = new Date().toISOString().split("T")[0];
					const count = await ctx.storage.events.count({
						createdAt: { gte: today },
					});
					return { today: count };
				},
			},
		},
	});
}

export default createPlugin;

src/admin.tsx 檔案將頁面路徑和小工具 ID 對應到其 React 元件:

import { EventsWidget } from "./components/EventsWidget";
import { DashboardPage } from "./components/DashboardPage";
import { SettingsPage } from "./components/SettingsPage";

export const widgets = {
	"events-today": EventsWidget,
};

export const pages = {
	"/dashboard": DashboardPage,
	"/settings": SettingsPage,
};