feat(plugins): foundation système Plugin Karbé

- Modèle Prisma Plugin (key, name, description, category, version, enabled,
  config JSONB, migrationsApplied, timestamps) + migration SQL
- PluginRegistry (src/lib/plugins/registry.ts) avec 12 plugins déclarés :
  visuels (theme-guyane, landing-hero, landing-sections, image-gallery-seed,
  demo-carbets-seed), métier (access-type, seasonality, pirogue-providers,
  min-stay), contenus (content-pages, legal-pages), i18n (i18n-fr-en)
- Server helpers (server.ts) : sync, isEnabled, getEnabledKeys, toggle avec
  hooks onEnable/onDisable, updateConfig, cache 5s
- Client bridge (client.tsx) : PluginProvider + useIsPluginEnabled
- Composant <IfPluginEnabled plugin=... fallback=...>
- Guard requirePluginOr404 pour pages et routes
- Page admin /admin/plugins avec table toggle par catégorie + édition config
- Route PATCH /api/admin/plugins/[key] + GET
- Layout async qui sync registry + passe enabledKeys au PluginProvider

Tous plugins en enabled=false par défaut, activation pilotée depuis l'admin.
This commit is contained in:
Claude Integration 2026-05-30 22:17:10 +00:00
parent d7de43a70e
commit 62cc464738
12 changed files with 577 additions and 2 deletions

129
src/lib/plugins/server.ts Normal file
View file

@ -0,0 +1,129 @@
/**
* Plugin Karbé accès serveur à l'état des plugins.
*
* - getPluginState(key): lit la DB (avec cache 5s pour éviter les hammer).
* - isPluginEnabled(key): helper booléen.
* - syncPluginsFromRegistry(): à appeler au démarrage pour insérer les plugins
* nouvellement ajoutés au code (sans toucher aux flags existants).
* - togglePlugin(key, enabled): admin action avec hook onEnable/onDisable.
*/
import "server-only";
import { prisma } from "@/lib/prisma";
import { PLUGINS, type PluginDescriptor } from "./registry";
import { pluginHooks } from "./hooks";
type PluginRow = {
key: string;
enabled: boolean;
config: Record<string, unknown>;
version: string;
category: string;
name: string;
description: string;
};
let cache: Map<string, PluginRow> | null = null;
let cacheStamp = 0;
const CACHE_TTL_MS = 5000;
async function loadAll(): Promise<Map<string, PluginRow>> {
const now = Date.now();
if (cache && now - cacheStamp < CACHE_TTL_MS) return cache;
const rows = await prisma.plugin.findMany();
const map = new Map<string, PluginRow>();
for (const r of rows) {
map.set(r.key, {
key: r.key,
enabled: r.enabled,
config: (r.config ?? {}) as Record<string, unknown>,
version: r.version,
category: r.category,
name: r.name,
description: r.description,
});
}
cache = map;
cacheStamp = now;
return map;
}
export function invalidatePluginCache(): void {
cache = null;
cacheStamp = 0;
}
export async function getPluginState(key: string): Promise<PluginRow | null> {
const m = await loadAll();
return m.get(key) ?? null;
}
export async function isPluginEnabled(key: string): Promise<boolean> {
const s = await getPluginState(key);
return !!s?.enabled;
}
export async function getEnabledPluginKeys(): Promise<string[]> {
const m = await loadAll();
return [...m.values()].filter((r) => r.enabled).map((r) => r.key);
}
export async function listAllPlugins(): Promise<PluginRow[]> {
const m = await loadAll();
return [...m.values()].sort((a, b) => a.category.localeCompare(b.category) || a.name.localeCompare(b.name));
}
export async function syncPluginsFromRegistry(): Promise<void> {
const existing = new Set((await prisma.plugin.findMany({ select: { key: true } })).map((p) => p.key));
const toInsert: PluginDescriptor[] = PLUGINS.filter((p) => !existing.has(p.key));
if (toInsert.length) {
await prisma.plugin.createMany({
data: toInsert.map((p) => ({
key: p.key,
name: p.name,
description: p.description,
category: p.category,
version: p.version,
enabled: false,
config: {},
})),
skipDuplicates: true,
});
invalidatePluginCache();
}
// Update name/description/version if the descriptor changed (idempotent).
for (const p of PLUGINS) {
if (existing.has(p.key)) {
await prisma.plugin.update({
where: { key: p.key },
data: { name: p.name, description: p.description, category: p.category, version: p.version },
});
}
}
invalidatePluginCache();
}
export async function togglePlugin(key: string, enabled: boolean): Promise<PluginRow | null> {
const before = await prisma.plugin.findUnique({ where: { key } });
if (!before) return null;
if (before.enabled === enabled) return await getPluginState(key);
const hook = pluginHooks[key];
if (enabled && hook?.onEnable) await hook.onEnable();
if (!enabled && hook?.onDisable) await hook.onDisable();
await prisma.plugin.update({
where: { key },
data: {
enabled,
lastEnabledAt: enabled ? new Date() : before.lastEnabledAt,
lastDisabledAt: !enabled ? new Date() : before.lastDisabledAt,
},
});
invalidatePluginCache();
return await getPluginState(key);
}
export async function updatePluginConfig(key: string, config: Record<string, unknown>): Promise<PluginRow | null> {
await prisma.plugin.update({ where: { key }, data: { config } });
invalidatePluginCache();
return await getPluginState(key);
}