feat(plugins): add source-control extension contracts (#2117)
This commit is contained in:
@@ -31,6 +31,42 @@ describe("PluginModalHost", () => {
|
||||
expect(screen.getByTestId("modal-content")).not.toBeNull();
|
||||
});
|
||||
|
||||
it("renders a modal description inside the host-owned header", () => {
|
||||
pluginModalManager.openModal("plugin-a", {
|
||||
title: "Link Bitbucket pull request",
|
||||
description: "Use a Bitbucket pull request URL for this task.",
|
||||
content: () => <div data-testid="modal-content">Hello</div>,
|
||||
} as never);
|
||||
|
||||
render(<PluginModalHost />);
|
||||
|
||||
expect(screen.getByText("Use a Bitbucket pull request URL for this task.")).not.toBeNull();
|
||||
});
|
||||
|
||||
it("renders a host-owned drawer when the plugin requests mobile presentation", () => {
|
||||
pluginModalManager.openModal("plugin-a", {
|
||||
title: "Link pull request",
|
||||
content: () => <div data-testid="drawer-content">Mobile action</div>,
|
||||
presentation: "drawer",
|
||||
});
|
||||
|
||||
render(<PluginModalHost />);
|
||||
|
||||
expect(document.querySelector('[data-slot="drawer-content"]')).not.toBeNull();
|
||||
expect(screen.getByTestId("drawer-content")).not.toBeNull();
|
||||
});
|
||||
|
||||
it("gives title-less plugin surfaces an accessible fallback name", () => {
|
||||
pluginModalManager.openModal("plugin-a", {
|
||||
content: () => <div>Untitled content</div>,
|
||||
presentation: "drawer",
|
||||
});
|
||||
|
||||
render(<PluginModalHost />);
|
||||
|
||||
expect(screen.getByRole("dialog", { name: "Plugin dialog" })).not.toBeNull();
|
||||
});
|
||||
|
||||
it("removes the modal from the DOM once its handle is closed", () => {
|
||||
const handle = pluginModalManager.openModal("plugin-a", {
|
||||
content: () => <div data-testid="modal-content">Hello</div>,
|
||||
|
||||
@@ -1,6 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@kandev/ui/dialog";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@kandev/ui/dialog";
|
||||
import {
|
||||
Drawer,
|
||||
DrawerContent,
|
||||
DrawerDescription,
|
||||
DrawerHeader,
|
||||
DrawerTitle,
|
||||
} from "@kandev/ui/drawer";
|
||||
import { TooltipProvider } from "@kandev/ui/tooltip";
|
||||
import {
|
||||
pluginModalManager,
|
||||
@@ -8,6 +21,7 @@ import {
|
||||
type OpenPluginModal,
|
||||
} from "@/lib/plugins/modal-manager";
|
||||
import type { PluginModalOptions } from "@/lib/plugins/types";
|
||||
import { t } from "@/lib/i18n";
|
||||
import { PluginErrorBoundary } from "./plugin-error-boundary";
|
||||
|
||||
/** Maps `PluginModalOptions.size` to the host's Dialog width classes. */
|
||||
@@ -24,32 +38,81 @@ function preventWhenNotDismissible(dismissible: boolean) {
|
||||
};
|
||||
}
|
||||
|
||||
function PluginModalInstance({ modal }: { modal: OpenPluginModal }) {
|
||||
function pluginDialogLabel(): string {
|
||||
return t("plugins:pluginDialog");
|
||||
}
|
||||
|
||||
type ModalSurfaceProps = {
|
||||
modal: OpenPluginModal;
|
||||
dismissible: boolean;
|
||||
onOpenChange(open: boolean): void;
|
||||
};
|
||||
|
||||
function PluginDrawer({ modal, dismissible, onOpenChange }: ModalSurfaceProps) {
|
||||
const { instanceId, pluginId, options } = modal;
|
||||
const Content = options.content;
|
||||
const noDescriptionProps = options.description ? {} : { "aria-describedby": undefined };
|
||||
return (
|
||||
<Drawer open dismissible={dismissible} onOpenChange={onOpenChange}>
|
||||
<DrawerContent
|
||||
{...noDescriptionProps}
|
||||
className="max-h-[90dvh] pb-[max(1rem,env(safe-area-inset-bottom))]"
|
||||
>
|
||||
{(options.title || options.description) && (
|
||||
<DrawerHeader>
|
||||
{options.title ? (
|
||||
<DrawerTitle>{options.title}</DrawerTitle>
|
||||
) : (
|
||||
<DrawerTitle className="sr-only">{pluginDialogLabel()}</DrawerTitle>
|
||||
)}
|
||||
{options.description && <DrawerDescription>{options.description}</DrawerDescription>}
|
||||
</DrawerHeader>
|
||||
)}
|
||||
{!options.title && !options.description && (
|
||||
<DrawerTitle className="sr-only">{pluginDialogLabel()}</DrawerTitle>
|
||||
)}
|
||||
<div className="min-h-0 overflow-y-auto overscroll-contain px-4 pb-4">
|
||||
<PluginErrorBoundary context={`drawer "${instanceId}" (plugin "${pluginId}")`}>
|
||||
<Content />
|
||||
</PluginErrorBoundary>
|
||||
</div>
|
||||
</DrawerContent>
|
||||
</Drawer>
|
||||
);
|
||||
}
|
||||
|
||||
function PluginDialog({ modal, dismissible, onOpenChange }: ModalSurfaceProps) {
|
||||
const { instanceId, pluginId, options } = modal;
|
||||
const dismissible = options.dismissible ?? true;
|
||||
const Content = options.content;
|
||||
const guardClose = preventWhenNotDismissible(dismissible);
|
||||
|
||||
const handleOpenChange = (open: boolean) => {
|
||||
if (open || !dismissible) return;
|
||||
pluginModalManager.close(instanceId);
|
||||
};
|
||||
|
||||
const noDescriptionProps = options.description ? {} : { "aria-describedby": undefined };
|
||||
return (
|
||||
<Dialog open onOpenChange={handleOpenChange}>
|
||||
<Dialog open onOpenChange={onOpenChange}>
|
||||
<DialogContent
|
||||
className={SIZE_CLASSES[options.size ?? "md"]}
|
||||
{...noDescriptionProps}
|
||||
className={
|
||||
modal.layout === "task-link"
|
||||
? "w-[calc(100vw-2rem)] sm:max-w-lg"
|
||||
: SIZE_CLASSES[options.size ?? "md"]
|
||||
}
|
||||
showCloseButton={dismissible}
|
||||
onEscapeKeyDown={guardClose}
|
||||
onInteractOutside={guardClose}
|
||||
>
|
||||
{/* `options.title` is supplied by the plugin author, so it is
|
||||
third-party data rather than host copy. */}
|
||||
{options.title && (
|
||||
{/* Plugin-owned title/description; render either when supplied. */}
|
||||
{(options.title || options.description) && (
|
||||
<DialogHeader>
|
||||
<DialogTitle>{options.title}</DialogTitle>
|
||||
{options.title ? (
|
||||
<DialogTitle>{options.title}</DialogTitle>
|
||||
) : (
|
||||
<DialogTitle className="sr-only">{pluginDialogLabel()}</DialogTitle>
|
||||
)}
|
||||
{options.description && <DialogDescription>{options.description}</DialogDescription>}
|
||||
</DialogHeader>
|
||||
)}
|
||||
{!options.title && !options.description && (
|
||||
<DialogTitle className="sr-only">{pluginDialogLabel()}</DialogTitle>
|
||||
)}
|
||||
<PluginErrorBoundary context={`modal "${instanceId}" (plugin "${pluginId}")`}>
|
||||
<Content />
|
||||
</PluginErrorBoundary>
|
||||
@@ -58,17 +121,27 @@ function PluginModalInstance({ modal }: { modal: OpenPluginModal }) {
|
||||
);
|
||||
}
|
||||
|
||||
function PluginModalInstance({ modal }: { modal: OpenPluginModal }) {
|
||||
const dismissible = modal.options.dismissible ?? true;
|
||||
const handleOpenChange = (open: boolean) => {
|
||||
if (open || !dismissible) return;
|
||||
pluginModalManager.close(modal.instanceId);
|
||||
};
|
||||
const props = { modal, dismissible, onOpenChange: handleOpenChange };
|
||||
return modal.options.presentation === "drawer" ? (
|
||||
<PluginDrawer {...props} />
|
||||
) : (
|
||||
<PluginDialog {...props} />
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders every open plugin modal (`host.openModal(...)`) in a `@kandev/ui`
|
||||
* `Dialog`, each isolated behind its own `PluginErrorBoundary`. Mounted once
|
||||
* at the app root, alongside `<PluginBootBridge/>`.
|
||||
* inside `<AppShell/>`, where plugin-owned forms inherit the app providers.
|
||||
*
|
||||
* The `TooltipProvider` is not redundant with the app-wide one in
|
||||
* `app/layout.tsx`: this host mounts as a *sibling* of `<AppShell/>` (see
|
||||
* `src/main.tsx`), so it is outside that provider's subtree and a `Tooltip`
|
||||
* inside plugin modal content would throw for want of a provider. Radix
|
||||
* allows nesting, so this stays correct if the host is ever moved inside the
|
||||
* shell.
|
||||
* Keep the local `TooltipProvider` so this host is also safe in isolated
|
||||
* mounts and tests. Radix supports nesting it under AppShell's provider.
|
||||
*/
|
||||
export function PluginModalHost() {
|
||||
const modals = usePluginModals();
|
||||
|
||||
@@ -5,12 +5,12 @@ import { PageShell } from "@/components/page-shell";
|
||||
import { lookupPluginIcon } from "@/lib/plugins/icons";
|
||||
import { pluginRegistry } from "@/lib/plugins/registry";
|
||||
import type { PluginRouteRegistration } from "@/lib/plugins/registry";
|
||||
import type { NavItem, PluginPageChrome } from "@/lib/plugins/types";
|
||||
import type { NavItem, PluginIcon, PluginPageChrome } from "@/lib/plugins/types";
|
||||
|
||||
export interface ResolvedPageChrome {
|
||||
title: string;
|
||||
subtitle?: string;
|
||||
icon?: string;
|
||||
icon?: PluginIcon;
|
||||
backHref?: string;
|
||||
backLabel?: string;
|
||||
Actions?: ComponentType;
|
||||
|
||||
Reference in New Issue
Block a user