From 7448e7a5065daf17f33fb2c21349b1cb2ed69d0d Mon Sep 17 00:00:00 2001 From: David Metcalfe <80915+DavidMetcalfe@users.noreply.github.com> Date: Sun, 21 Jun 2026 19:59:09 -0700 Subject: [PATCH] feat(plugins): expose Dialog/ConfirmDialog/Toast/useToast/useConfirmDelete on plugin SDK MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Additive expansion of window.__HERMES_PLUGIN_SDK__. Plugins can now render host-styled dialogs, confirmations, and toasts instead of falling back to window.alert/confirm/prompt. New components: Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, ConfirmDialog, Toast. New hooks: useToast (replaces showToast/toast pair), useConfirmDelete (single-id delete-confirm state machine). SDK_CONTRACT_VERSION unchanged at 1.1.0 — additive surface per sdk.d.ts:23-25 (no major bump required). Consumer: kanban plugin's 'replace native dialogs' work, see issue #50547. A reference prototype showing 4 design variants is committed at docs/design/kanban-dialogs/index.html. Adds web/src/plugins/registry.test.ts (3 vitest cases) that smoke-test the new keys are wired and that the version constant is unchanged. --- web/src/plugins/registry.test.ts | 54 ++++++++++++++++++++++++++++++++ web/src/plugins/registry.ts | 24 ++++++++++++++ web/src/plugins/sdk.d.ts | 24 ++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 web/src/plugins/registry.test.ts diff --git a/web/src/plugins/registry.test.ts b/web/src/plugins/registry.test.ts new file mode 100644 index 0000000000..8fe0647858 --- /dev/null +++ b/web/src/plugins/registry.test.ts @@ -0,0 +1,54 @@ +/** + * Smoke test for the plugin SDK surface additions. + * + * Verifies that `exposePluginSDK()` writes the new dialog/toast primitives to + * `window.__HERMES_PLUGIN_SDK__`. Each new key is checked individually so a + * regression in one helper doesn't mask the others. + * + * Companion to the additive PR "feat(plugins): expose Dialog/ConfirmDialog/ + * Toast/useToast/useConfirmDelete on the plugin SDK". See issue #50547. + */ +import { beforeEach, describe, expect, it } from "vitest"; +import { exposePluginSDK } from "./registry"; + +describe("plugin SDK dialog/toast surface", () => { + beforeEach(() => { + // Reset window between tests so exposePluginSDK() writes fresh. + (globalThis as any).window = { + __HERMES_PLUGINS__: undefined, + __HERMES_PLUGIN_SDK__: undefined, + }; + }); + + it("exposes Dialog + subcomponents on components", () => { + exposePluginSDK(); + const sdk = (globalThis as any).window.__HERMES_PLUGIN_SDK__; + expect(sdk.components.Dialog).toBeDefined(); + expect(sdk.components.DialogContent).toBeDefined(); + expect(sdk.components.DialogHeader).toBeDefined(); + expect(sdk.components.DialogTitle).toBeDefined(); + expect(sdk.components.DialogDescription).toBeDefined(); + expect(sdk.components.DialogFooter).toBeDefined(); + expect(sdk.components.DialogClose).toBeDefined(); + expect(sdk.components.ConfirmDialog).toBeDefined(); + expect(sdk.components.Toast).toBeDefined(); + }); + + it("exposes useToast and useConfirmDelete on hooks", () => { + exposePluginSDK(); + const sdk = (globalThis as any).window.__HERMES_PLUGIN_SDK__; + expect(typeof sdk.hooks.useToast).toBe("function"); + expect(typeof sdk.hooks.useConfirmDelete).toBe("function"); + // Original React hooks still present (no accidental removal). + expect(typeof sdk.hooks.useState).toBe("function"); + expect(typeof sdk.hooks.useCallback).toBe("function"); + }); + + it("does not bump SDK_CONTRACT_VERSION (additive change)", () => { + exposePluginSDK(); + const sdk = (globalThis as any).window.__HERMES_PLUGIN_SDK__; + // Pre-existing version per registry.ts:98. This test fails if a future + // PR accidentally bumps the major for an additive surface change. + expect(sdk.sdkVersion).toBe("1.1.0"); + }); +}); \ No newline at end of file diff --git a/web/src/plugins/registry.ts b/web/src/plugins/registry.ts index 392c536d0a..df7c77d8d0 100644 --- a/web/src/plugins/registry.ts +++ b/web/src/plugins/registry.ts @@ -22,6 +22,14 @@ import { cn, timeAgo, isoTimeAgo } from "@/lib/utils"; import { Badge } from "@nous-research/ui/ui/components/badge"; import { Button } from "@nous-research/ui/ui/components/button"; import { Checkbox } from "@nous-research/ui/ui/components/checkbox"; +import { ConfirmDialog } from "@nous-research/ui/ui/components/confirm-dialog"; +import { + Dialog, DialogClose, DialogContent, DialogDescription, + DialogFooter, DialogHeader, DialogTitle, +} from "@nous-research/ui/ui/components/dialog"; +import { Toast } from "@nous-research/ui/ui/components/toast"; +import { useConfirmDelete } from "@nous-research/ui/hooks/use-confirm-delete"; +import { useToast } from "@nous-research/ui/hooks/use-toast"; import { Select, SelectOption } from "@nous-research/ui/ui/components/select"; import { Card, CardHeader, CardTitle, CardContent } from "@nous-research/ui/ui/components/card"; import { Input } from "@nous-research/ui/ui/components/input"; @@ -121,6 +129,13 @@ export function exposePluginSDK() { useRef, useContext, createContext, + // useToast returns { showToast, toast } where toast is the current + // visible toast (or null) and showToast(message, 'success'|'error') + // replaces it. useConfirmDelete({ onDelete }) returns the state + // machine (requestDelete / confirm / cancel / isOpen / isDeleting / + // pendingId) for single-id delete confirmations. + useToast, + useConfirmDelete, }, // Hermes API client @@ -148,6 +163,14 @@ export function exposePluginSDK() { Badge, Button, Checkbox, + ConfirmDialog, + Dialog, + DialogClose, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, Input, Label, Select, @@ -156,6 +179,7 @@ export function exposePluginSDK() { Tabs, TabsList, TabsTrigger, + Toast, PluginSlot, }, diff --git a/web/src/plugins/sdk.d.ts b/web/src/plugins/sdk.d.ts index c55b855ab8..db31b73e51 100644 --- a/web/src/plugins/sdk.d.ts +++ b/web/src/plugins/sdk.d.ts @@ -105,6 +105,30 @@ export interface HermesPluginSDK { useRef: typeof import("react").useRef; useContext: typeof import("react").useContext; createContext: typeof import("react").createContext; + /** + * Toast feedback. Returns ``{ showToast, toast }`` where ``toast`` is the + * current visible toast (or null) and ``showToast(message, 'success' | 'error')`` + * replaces it. Mount the ``Toast`` component (also on ``components``) somewhere + * in your plugin's tree to render the visible toast. + */ + useToast: () => { + showToast: (message: string, type: "success" | "error") => void; + toast: { message: string; type: "success" | "error" } | null; + }; + /** + * Single-id delete-confirm state machine. Pass ``onDelete(id)`` (an async + * function). Returns ``{ requestDelete, confirm, cancel, isOpen, isDeleting, + * pendingId }``. Pair with the ``ConfirmDialog`` primitive (passed as + * ``open={isOpen}`` etc.) or any custom dialog. + */ + useConfirmDelete: (opts: { onDelete: (id: TId) => Promise }) => { + requestDelete: (id: TId) => void; + confirm: () => Promise; + cancel: () => void; + isOpen: boolean; + isDeleting: boolean; + pendingId: TId | null; + }; }; /**