import { Hook, Manifest } from "../../plugos/types.ts"; import { System } from "../../plugos/system.ts"; import { CodeWidgetCallback } from "$sb/types.ts"; export type PanelWidgetT = { panelWidget?: "top" | "bottom"; }; export class PanelWidgetHook implements Hook { callbacks = new Map(); constructor() { } collectAllPanelWidgets(system: System) { this.callbacks.clear(); for (const plug of system.loadedPlugs.values()) { for ( const [name, functionDef] of Object.entries( plug.manifest!.functions, ) ) { if (!functionDef.panelWidget) { continue; } this.callbacks.set( functionDef.panelWidget, (bodyText, pageName) => { return plug.invoke(name, [bodyText, pageName]); }, ); } } } apply(system: System): void { this.collectAllPanelWidgets(system); system.on({ plugLoaded: () => { this.collectAllPanelWidgets(system); }, }); } validateManifest(manifest: Manifest): string[] { const errors = []; for (const functionDef of Object.values(manifest.functions)) { if (!functionDef.panelWidget) { continue; } if (!["top", "bottom"].includes(functionDef.panelWidget)) { errors.push( `Panel widgets must be attached to either 'top' or 'bottom'.`, ); } } return errors; } }