@blackbelt-technology/pi-dashboard-client-utils 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +103 -0
- package/src/ActionList.tsx +97 -0
- package/src/AgentCardShell.tsx +56 -0
- package/src/ConfirmDialog.tsx +37 -0
- package/src/DialogPortal.tsx +20 -0
- package/src/Popover.tsx +153 -0
- package/src/SearchableSelectDialog.tsx +133 -0
- package/src/StatusPill.tsx +83 -0
- package/src/ZoomControls.tsx +45 -0
- package/src/__tests__/ActionList.test.tsx +53 -0
- package/src/__tests__/DialogPortal.test.tsx +58 -0
- package/src/__tests__/StatusPill.test.tsx +44 -0
- package/src/__tests__/useMobile.test.tsx +92 -0
- package/src/__tests__/useZoomPan.test.ts +64 -0
- package/src/agent-card-utils.ts +42 -0
- package/src/extension-ui/AgentMetricSlot.tsx +45 -0
- package/src/extension-ui/BreadcrumbSlot.tsx +63 -0
- package/src/extension-ui/GateSlot.tsx +65 -0
- package/src/extension-ui/decorator-utils.ts +23 -0
- package/src/minimal-chat/MinimalChatView.tsx +381 -0
- package/src/minimal-chat/__tests__/MinimalChatView.test.tsx +232 -0
- package/src/minimal-chat/index.ts +16 -0
- package/src/minimal-chat/types.ts +90 -0
- package/src/useMediaQuery.ts +23 -0
- package/src/useMobile.tsx +30 -0
- package/src/useZoomPan.ts +157 -0
package/package.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blackbelt-technology/pi-dashboard-client-utils",
|
|
3
|
+
"version": "0.5.3",
|
|
4
|
+
"description": "Small reusable React components, hooks, and helpers shared between the pi-dashboard shell and dashboard plugins. Per-subpath exports; no markdown stack.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/BlackBeltTechnology/pi-agent-dashboard",
|
|
13
|
+
"directory": "packages/client-utils"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
"./AgentCardShell": {
|
|
17
|
+
"types": "./src/AgentCardShell.tsx",
|
|
18
|
+
"default": "./src/AgentCardShell.tsx"
|
|
19
|
+
},
|
|
20
|
+
"./agent-card-utils": {
|
|
21
|
+
"types": "./src/agent-card-utils.ts",
|
|
22
|
+
"default": "./src/agent-card-utils.ts"
|
|
23
|
+
},
|
|
24
|
+
"./DialogPortal": {
|
|
25
|
+
"types": "./src/DialogPortal.tsx",
|
|
26
|
+
"default": "./src/DialogPortal.tsx"
|
|
27
|
+
},
|
|
28
|
+
"./Popover": {
|
|
29
|
+
"types": "./src/Popover.tsx",
|
|
30
|
+
"default": "./src/Popover.tsx"
|
|
31
|
+
},
|
|
32
|
+
"./ConfirmDialog": {
|
|
33
|
+
"types": "./src/ConfirmDialog.tsx",
|
|
34
|
+
"default": "./src/ConfirmDialog.tsx"
|
|
35
|
+
},
|
|
36
|
+
"./SearchableSelectDialog": {
|
|
37
|
+
"types": "./src/SearchableSelectDialog.tsx",
|
|
38
|
+
"default": "./src/SearchableSelectDialog.tsx"
|
|
39
|
+
},
|
|
40
|
+
"./ZoomControls": {
|
|
41
|
+
"types": "./src/ZoomControls.tsx",
|
|
42
|
+
"default": "./src/ZoomControls.tsx"
|
|
43
|
+
},
|
|
44
|
+
"./useZoomPan": {
|
|
45
|
+
"types": "./src/useZoomPan.ts",
|
|
46
|
+
"default": "./src/useZoomPan.ts"
|
|
47
|
+
},
|
|
48
|
+
"./useMobile": {
|
|
49
|
+
"types": "./src/useMobile.tsx",
|
|
50
|
+
"default": "./src/useMobile.tsx"
|
|
51
|
+
},
|
|
52
|
+
"./useMediaQuery": {
|
|
53
|
+
"types": "./src/useMediaQuery.ts",
|
|
54
|
+
"default": "./src/useMediaQuery.ts"
|
|
55
|
+
},
|
|
56
|
+
"./extension-ui/AgentMetricSlot": {
|
|
57
|
+
"types": "./src/extension-ui/AgentMetricSlot.tsx",
|
|
58
|
+
"default": "./src/extension-ui/AgentMetricSlot.tsx"
|
|
59
|
+
},
|
|
60
|
+
"./extension-ui/BreadcrumbSlot": {
|
|
61
|
+
"types": "./src/extension-ui/BreadcrumbSlot.tsx",
|
|
62
|
+
"default": "./src/extension-ui/BreadcrumbSlot.tsx"
|
|
63
|
+
},
|
|
64
|
+
"./extension-ui/GateSlot": {
|
|
65
|
+
"types": "./src/extension-ui/GateSlot.tsx",
|
|
66
|
+
"default": "./src/extension-ui/GateSlot.tsx"
|
|
67
|
+
},
|
|
68
|
+
"./extension-ui/decorator-utils": {
|
|
69
|
+
"types": "./src/extension-ui/decorator-utils.ts",
|
|
70
|
+
"default": "./src/extension-ui/decorator-utils.ts"
|
|
71
|
+
},
|
|
72
|
+
"./ActionList": {
|
|
73
|
+
"types": "./src/ActionList.tsx",
|
|
74
|
+
"default": "./src/ActionList.tsx"
|
|
75
|
+
},
|
|
76
|
+
"./StatusPill": {
|
|
77
|
+
"types": "./src/StatusPill.tsx",
|
|
78
|
+
"default": "./src/StatusPill.tsx"
|
|
79
|
+
},
|
|
80
|
+
"./minimal-chat": {
|
|
81
|
+
"types": "./src/minimal-chat/index.ts",
|
|
82
|
+
"default": "./src/minimal-chat/index.ts"
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
"files": [
|
|
86
|
+
"src/"
|
|
87
|
+
],
|
|
88
|
+
"peerDependencies": {
|
|
89
|
+
"react": ">=18.0.0",
|
|
90
|
+
"react-dom": ">=18.0.0"
|
|
91
|
+
},
|
|
92
|
+
"dependencies": {
|
|
93
|
+
"@blackbelt-technology/pi-dashboard-shared": "^0.5.3",
|
|
94
|
+
"@mdi/js": "^7.4.47",
|
|
95
|
+
"@mdi/react": "^1.6.1"
|
|
96
|
+
},
|
|
97
|
+
"devDependencies": {
|
|
98
|
+
"@types/react": "^19.0.0",
|
|
99
|
+
"@types/react-dom": "^19.0.0",
|
|
100
|
+
"react": "^19.0.0",
|
|
101
|
+
"react-dom": "^19.0.0"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ActionList primitive — horizontal row of action buttons.
|
|
3
|
+
*
|
|
4
|
+
* Registered as `ui:action-list` in the primitive registry. Plugins emit
|
|
5
|
+
* `{primitive: "ui:action-list", props: {actions: [...]}}` and each
|
|
6
|
+
* connected client renders this component.
|
|
7
|
+
*
|
|
8
|
+
* See change: adopt-server-driven-intent-rendering.
|
|
9
|
+
*/
|
|
10
|
+
import React from "react";
|
|
11
|
+
import Icon from "@mdi/react";
|
|
12
|
+
// We accept icon as a string key (MDI), look up via resolveMdiIcon at render time.
|
|
13
|
+
import type { UiActionListProps } from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/ui-primitives.js";
|
|
14
|
+
import { sendPluginAction } from "@blackbelt-technology/dashboard-plugin-runtime";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Extended item shape: in addition to `onClick`, items may carry a
|
|
18
|
+
* server-side action descriptor in `dataAction`. When the user clicks,
|
|
19
|
+
* `dataAction` (if present) is dispatched via `sendPluginAction`.
|
|
20
|
+
* Plugins emitting intents use `dataAction`; direct React callers can
|
|
21
|
+
* still pass `onClick`.
|
|
22
|
+
*/
|
|
23
|
+
interface ExtendedActionItem {
|
|
24
|
+
label: string;
|
|
25
|
+
icon?: string;
|
|
26
|
+
tooltip?: string;
|
|
27
|
+
onClick?: () => void;
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
dataAction?: {
|
|
30
|
+
pluginId: string;
|
|
31
|
+
sessionId?: string | null;
|
|
32
|
+
action: string;
|
|
33
|
+
payload?: Record<string, unknown>;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function ActionList({ actions }: UiActionListProps) {
|
|
38
|
+
if (!actions || actions.length === 0) return null;
|
|
39
|
+
return (
|
|
40
|
+
<div className="flex flex-row flex-wrap gap-1 items-center">
|
|
41
|
+
{(actions as ExtendedActionItem[]).map((a, i) => {
|
|
42
|
+
const handleClick = () => {
|
|
43
|
+
if (a.disabled) return;
|
|
44
|
+
if (a.dataAction) {
|
|
45
|
+
sendPluginAction(
|
|
46
|
+
a.dataAction.pluginId,
|
|
47
|
+
a.dataAction.sessionId ?? null,
|
|
48
|
+
a.dataAction.action,
|
|
49
|
+
a.dataAction.payload,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (a.onClick) a.onClick();
|
|
53
|
+
};
|
|
54
|
+
return (
|
|
55
|
+
<button
|
|
56
|
+
key={i}
|
|
57
|
+
type="button"
|
|
58
|
+
onClick={handleClick}
|
|
59
|
+
disabled={a.disabled}
|
|
60
|
+
title={a.tooltip}
|
|
61
|
+
className={`inline-flex items-center gap-1 px-2 py-1 rounded text-xs border border-[var(--border-subtle)] bg-[var(--bg-secondary)] hover:bg-[var(--bg-tertiary)] text-[var(--text-primary)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors`}
|
|
62
|
+
>
|
|
63
|
+
{a.icon ? <IconByKey iconKey={a.icon} /> : null}
|
|
64
|
+
<span>{a.label}</span>
|
|
65
|
+
</button>
|
|
66
|
+
);
|
|
67
|
+
})}
|
|
68
|
+
</div>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Render an MDI icon by its export-name key. Best-effort — unknown keys
|
|
74
|
+
* render nothing. The shell ships @mdi/js so this is a flat property
|
|
75
|
+
* lookup.
|
|
76
|
+
*/
|
|
77
|
+
function IconByKey({ iconKey }: { iconKey: string }) {
|
|
78
|
+
// We don't want to pull in @mdi/js at module load time (size); use a
|
|
79
|
+
// dynamic property lookup via a registered importer pattern instead.
|
|
80
|
+
// For v1, lazy-load @mdi/js at the renderer level and cache.
|
|
81
|
+
const [path, setPath] = React.useState<string | null>(null);
|
|
82
|
+
React.useEffect(() => {
|
|
83
|
+
let cancelled = false;
|
|
84
|
+
import("@mdi/js")
|
|
85
|
+
.then((mdi) => {
|
|
86
|
+
if (cancelled) return;
|
|
87
|
+
const candidate = (mdi as Record<string, unknown>)[iconKey];
|
|
88
|
+
setPath(typeof candidate === "string" ? candidate : null);
|
|
89
|
+
})
|
|
90
|
+
.catch(() => setPath(null));
|
|
91
|
+
return () => {
|
|
92
|
+
cancelled = true;
|
|
93
|
+
};
|
|
94
|
+
}, [iconKey]);
|
|
95
|
+
if (!path) return null;
|
|
96
|
+
return <Icon path={path} size={0.6} />;
|
|
97
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentCardShell: reusable card container for agent-like UI.
|
|
3
|
+
* Provides: status-colored border, header row (icon + name), optional stats line.
|
|
4
|
+
* Consumers pass tool-specific content as children.
|
|
5
|
+
*/
|
|
6
|
+
import React, { type ReactNode } from "react";
|
|
7
|
+
import { getStatusIcon } from "./agent-card-utils.js";
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
/** Agent display name (e.g. "Explore", "general-purpose") */
|
|
11
|
+
name: string;
|
|
12
|
+
/** Status key for icon + border color */
|
|
13
|
+
status: string;
|
|
14
|
+
/** Optional right-aligned header content (e.g. duration badge) */
|
|
15
|
+
headerRight?: ReactNode;
|
|
16
|
+
/** Optional stats line below header */
|
|
17
|
+
stats?: ReactNode;
|
|
18
|
+
/** Whether the card is clickable/selected */
|
|
19
|
+
onClick?: () => void;
|
|
20
|
+
selected?: boolean;
|
|
21
|
+
/** Tool-specific content */
|
|
22
|
+
children?: ReactNode;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function AgentCardShell({ name, status, headerRight, stats, onClick, selected, children }: Props) {
|
|
26
|
+
const { icon, color } = getStatusIcon(status);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div
|
|
30
|
+
onClick={onClick}
|
|
31
|
+
className={`rounded-lg border p-2.5 transition-all duration-150 flex flex-col
|
|
32
|
+
${onClick ? "cursor-pointer hover:shadow-md" : ""}
|
|
33
|
+
${selected ? "border-blue-500/60 bg-[var(--bg-surface)]" : "border-[var(--border-subtle)] bg-[color-mix(in_srgb,var(--bg-secondary),var(--bg-tertiary))]"}
|
|
34
|
+
${status === "running" ? "border-yellow-500/30" : ""}
|
|
35
|
+
${status === "error" ? "border-red-500/30" : ""}
|
|
36
|
+
`}
|
|
37
|
+
>
|
|
38
|
+
{/* Header: icon + name + optional right content */}
|
|
39
|
+
<div className="flex items-center gap-1.5">
|
|
40
|
+
<span className={`${color} inline-flex`}>{icon}</span>
|
|
41
|
+
<span className="text-sm font-medium text-[var(--text-primary)] truncate flex-1">{name}</span>
|
|
42
|
+
{headerRight}
|
|
43
|
+
</div>
|
|
44
|
+
|
|
45
|
+
{/* Stats line */}
|
|
46
|
+
{stats && (
|
|
47
|
+
<div className="text-[11px] text-[var(--text-tertiary)] mt-0.5 truncate">
|
|
48
|
+
{stats}
|
|
49
|
+
</div>
|
|
50
|
+
)}
|
|
51
|
+
|
|
52
|
+
{/* Tool-specific content */}
|
|
53
|
+
<div className="flex-1 flex flex-col">{children}</div>
|
|
54
|
+
</div>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Icon } from "@mdi/react";
|
|
3
|
+
import { mdiCheck } from "@mdi/js";
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
message: string;
|
|
7
|
+
confirmLabel?: string;
|
|
8
|
+
onConfirm: () => void;
|
|
9
|
+
onCancel: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function ConfirmDialog({ message, confirmLabel = "Confirm", onConfirm, onCancel }: Props) {
|
|
13
|
+
return (
|
|
14
|
+
<div className="fixed inset-0 z-[60] flex items-center justify-center" data-testid="confirm-dialog">
|
|
15
|
+
<div className="absolute inset-0 bg-[var(--bg-overlay)]" onClick={onCancel} />
|
|
16
|
+
<div className="relative bg-[var(--bg-secondary)] border border-[var(--border-secondary)] rounded-lg p-4 max-w-sm mx-4 space-y-3">
|
|
17
|
+
<p className="text-sm text-[var(--text-secondary)]">{message}</p>
|
|
18
|
+
<div className="flex justify-end gap-2">
|
|
19
|
+
<button
|
|
20
|
+
onClick={onCancel}
|
|
21
|
+
className="text-xs px-3 py-1.5 rounded border border-[var(--border-secondary)] text-[var(--text-secondary)] hover:text-[var(--text-primary)]"
|
|
22
|
+
data-testid="confirm-cancel"
|
|
23
|
+
>
|
|
24
|
+
Cancel
|
|
25
|
+
</button>
|
|
26
|
+
<button
|
|
27
|
+
onClick={onConfirm}
|
|
28
|
+
className="text-xs px-3 py-1.5 rounded bg-red-600 text-[var(--text-primary)] hover:bg-red-500"
|
|
29
|
+
data-testid="confirm-ok"
|
|
30
|
+
>
|
|
31
|
+
<Icon path={mdiCheck} size={0.45} className="inline mr-0.5" />{confirmLabel}
|
|
32
|
+
</button>
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEffect, useRef, type ReactNode } from "react";
|
|
2
|
+
import { createPortal } from "react-dom";
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function DialogPortal({ children }: Props) {
|
|
9
|
+
const prevOverflow = useRef<string>("");
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
prevOverflow.current = document.body.style.overflow;
|
|
13
|
+
document.body.style.overflow = "hidden";
|
|
14
|
+
return () => {
|
|
15
|
+
document.body.style.overflow = prevOverflow.current;
|
|
16
|
+
};
|
|
17
|
+
}, []);
|
|
18
|
+
|
|
19
|
+
return createPortal(children, document.body);
|
|
20
|
+
}
|
package/src/Popover.tsx
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Popover — floating panel anchored to a DOM element.
|
|
3
|
+
*
|
|
4
|
+
* The companion of `DialogPortal` for non-modal floating UI. Plugins render
|
|
5
|
+
* `<Popover anchorEl={btnRef.current} onDismiss={...}>...</Popover>` inline
|
|
6
|
+
* from their own component; the popover positions itself relative to
|
|
7
|
+
* `anchorEl`'s viewport rect, lives in a body-mounted portal so it
|
|
8
|
+
* escapes parent overflow/transform contexts, and dismisses on outside
|
|
9
|
+
* click or Esc.
|
|
10
|
+
*
|
|
11
|
+
* NOT a modal. NOT a scroll-lock. NOT a tooltip. Strictly a click-anchored
|
|
12
|
+
* floating panel.
|
|
13
|
+
*
|
|
14
|
+
* Registered as the `"ui:popover"` UI primitive via
|
|
15
|
+
* `registerUiPrimitive(registry, UI_PRIMITIVE_KEYS.popover, Popover)` in
|
|
16
|
+
* `packages/client/src/main.tsx`. Plugins look it up with
|
|
17
|
+
* `useUiPrimitive(UI_PRIMITIVE_KEYS.popover)` and avoid importing this file
|
|
18
|
+
* directly.
|
|
19
|
+
*
|
|
20
|
+
* Positioning strategy:
|
|
21
|
+
* - Default placement is "below the anchor, left-aligned to its left edge".
|
|
22
|
+
* - If the popover would overflow the viewport bottom, it flips above.
|
|
23
|
+
* - If it would overflow the right, it shifts left to stay in-viewport.
|
|
24
|
+
* - Recomputed on window resize/scroll.
|
|
25
|
+
*
|
|
26
|
+
* See change: add-ui-popover-primitive.
|
|
27
|
+
*/
|
|
28
|
+
import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react";
|
|
29
|
+
import { createPortal } from "react-dom";
|
|
30
|
+
|
|
31
|
+
export interface PopoverProps {
|
|
32
|
+
/** DOM element the popover is anchored to. Required. */
|
|
33
|
+
anchorEl: HTMLElement;
|
|
34
|
+
/** Called when the user clicks outside or presses Esc. */
|
|
35
|
+
onDismiss: () => void;
|
|
36
|
+
/** Popover content. */
|
|
37
|
+
children: ReactNode;
|
|
38
|
+
/** Optional gap (px) between the anchor and the popover edge. Default 6. */
|
|
39
|
+
offset?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface Position {
|
|
43
|
+
top: number;
|
|
44
|
+
left: number;
|
|
45
|
+
placement: "below" | "above";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Compute the popover's viewport position given the anchor and the popover's
|
|
50
|
+
* own measured size. Pure helper to ease unit testing.
|
|
51
|
+
*/
|
|
52
|
+
export function computePopoverPosition(
|
|
53
|
+
anchorRect: DOMRect,
|
|
54
|
+
popoverSize: { width: number; height: number },
|
|
55
|
+
viewport: { width: number; height: number },
|
|
56
|
+
offset: number,
|
|
57
|
+
): Position {
|
|
58
|
+
// Default: below anchor, left-aligned to anchor's left edge.
|
|
59
|
+
let top = anchorRect.bottom + offset;
|
|
60
|
+
let left = anchorRect.left;
|
|
61
|
+
let placement: "below" | "above" = "below";
|
|
62
|
+
|
|
63
|
+
// Flip above if it would overflow the bottom.
|
|
64
|
+
if (top + popoverSize.height > viewport.height && anchorRect.top - offset - popoverSize.height >= 0) {
|
|
65
|
+
top = anchorRect.top - offset - popoverSize.height;
|
|
66
|
+
placement = "above";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Shift left if it would overflow the right edge.
|
|
70
|
+
if (left + popoverSize.width > viewport.width) {
|
|
71
|
+
left = Math.max(8, viewport.width - popoverSize.width - 8);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Don't go off the left edge.
|
|
75
|
+
if (left < 8) left = 8;
|
|
76
|
+
|
|
77
|
+
return { top, left, placement };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function Popover({ anchorEl, onDismiss, children, offset = 6 }: PopoverProps) {
|
|
81
|
+
const popoverRef = useRef<HTMLDivElement>(null);
|
|
82
|
+
const [position, setPosition] = useState<Position | null>(null);
|
|
83
|
+
|
|
84
|
+
// Recompute position on mount + on window resize/scroll. useLayoutEffect
|
|
85
|
+
// avoids a one-frame flash at the wrong coordinates.
|
|
86
|
+
useLayoutEffect(() => {
|
|
87
|
+
function update() {
|
|
88
|
+
if (!popoverRef.current) return;
|
|
89
|
+
const anchorRect = anchorEl.getBoundingClientRect();
|
|
90
|
+
const { offsetWidth, offsetHeight } = popoverRef.current;
|
|
91
|
+
setPosition(
|
|
92
|
+
computePopoverPosition(
|
|
93
|
+
anchorRect,
|
|
94
|
+
{ width: offsetWidth, height: offsetHeight },
|
|
95
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
96
|
+
offset,
|
|
97
|
+
),
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
update();
|
|
101
|
+
window.addEventListener("resize", update);
|
|
102
|
+
// Capture-phase scroll listener catches scrolls in any ancestor.
|
|
103
|
+
window.addEventListener("scroll", update, true);
|
|
104
|
+
return () => {
|
|
105
|
+
window.removeEventListener("resize", update);
|
|
106
|
+
window.removeEventListener("scroll", update, true);
|
|
107
|
+
};
|
|
108
|
+
}, [anchorEl, offset]);
|
|
109
|
+
|
|
110
|
+
// Outside-click + Esc dismissal. Use mousedown so the dismissal happens
|
|
111
|
+
// before any click handler the outside element might run (matches
|
|
112
|
+
// browser-native dropdown UX).
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
function onMouseDown(e: MouseEvent) {
|
|
115
|
+
if (!popoverRef.current) return;
|
|
116
|
+
const target = e.target as Node;
|
|
117
|
+
// Click inside the popover OR on the anchor itself: do nothing.
|
|
118
|
+
if (popoverRef.current.contains(target)) return;
|
|
119
|
+
if (anchorEl.contains(target)) return;
|
|
120
|
+
onDismiss();
|
|
121
|
+
}
|
|
122
|
+
function onKeyDown(e: KeyboardEvent) {
|
|
123
|
+
if (e.key === "Escape") onDismiss();
|
|
124
|
+
}
|
|
125
|
+
document.addEventListener("mousedown", onMouseDown);
|
|
126
|
+
document.addEventListener("keydown", onKeyDown);
|
|
127
|
+
return () => {
|
|
128
|
+
document.removeEventListener("mousedown", onMouseDown);
|
|
129
|
+
document.removeEventListener("keydown", onKeyDown);
|
|
130
|
+
};
|
|
131
|
+
}, [anchorEl, onDismiss]);
|
|
132
|
+
|
|
133
|
+
return createPortal(
|
|
134
|
+
<div
|
|
135
|
+
ref={popoverRef}
|
|
136
|
+
role="dialog"
|
|
137
|
+
data-testid="ui-popover"
|
|
138
|
+
data-placement={position?.placement ?? "below"}
|
|
139
|
+
style={{
|
|
140
|
+
position: "fixed",
|
|
141
|
+
top: position?.top ?? -9999,
|
|
142
|
+
left: position?.left ?? -9999,
|
|
143
|
+
zIndex: 1000,
|
|
144
|
+
// Until the first layout effect runs we render off-screen so the
|
|
145
|
+
// measurement happens without flicker.
|
|
146
|
+
visibility: position ? "visible" : "hidden",
|
|
147
|
+
}}
|
|
148
|
+
>
|
|
149
|
+
{children}
|
|
150
|
+
</div>,
|
|
151
|
+
document.body,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import React, { useState, useRef, useEffect, useMemo } from "react";
|
|
2
|
+
import { DialogPortal } from "./DialogPortal.js";
|
|
3
|
+
|
|
4
|
+
export interface SelectOption {
|
|
5
|
+
value: string;
|
|
6
|
+
label: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
badge?: string;
|
|
9
|
+
badgeColor?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface Props {
|
|
13
|
+
title: string;
|
|
14
|
+
options: SelectOption[];
|
|
15
|
+
onSelect: (value: string) => void;
|
|
16
|
+
onCancel: () => void;
|
|
17
|
+
placeholder?: string;
|
|
18
|
+
emptyMessage?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function SearchableSelectDialog({
|
|
22
|
+
title,
|
|
23
|
+
options,
|
|
24
|
+
onSelect,
|
|
25
|
+
onCancel,
|
|
26
|
+
placeholder = "Search...",
|
|
27
|
+
emptyMessage = "No items found",
|
|
28
|
+
}: Props) {
|
|
29
|
+
const [query, setQuery] = useState("");
|
|
30
|
+
const [selectedIndex, setSelectedIndex] = useState(0);
|
|
31
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
32
|
+
const listRef = useRef<HTMLDivElement>(null);
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
inputRef.current?.focus();
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
const filtered = useMemo(() => {
|
|
39
|
+
if (!query.trim()) return options;
|
|
40
|
+
const q = query.toLowerCase();
|
|
41
|
+
return options.filter(
|
|
42
|
+
(o) => o.label.toLowerCase().includes(q) || o.description?.toLowerCase().includes(q),
|
|
43
|
+
);
|
|
44
|
+
}, [options, query]);
|
|
45
|
+
|
|
46
|
+
// Reset selection when filter changes
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
setSelectedIndex(0);
|
|
49
|
+
}, [filtered.length]);
|
|
50
|
+
|
|
51
|
+
// Scroll selected item into view
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const el = listRef.current?.children[selectedIndex] as HTMLElement | undefined;
|
|
54
|
+
el?.scrollIntoView?.({ block: "nearest" });
|
|
55
|
+
}, [selectedIndex]);
|
|
56
|
+
|
|
57
|
+
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
58
|
+
if (e.key === "ArrowDown") {
|
|
59
|
+
e.preventDefault();
|
|
60
|
+
setSelectedIndex((i) => Math.min(i + 1, filtered.length - 1));
|
|
61
|
+
} else if (e.key === "ArrowUp") {
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
setSelectedIndex((i) => Math.max(i - 1, 0));
|
|
64
|
+
} else if (e.key === "Enter") {
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
if (filtered[selectedIndex]) {
|
|
67
|
+
onSelect(filtered[selectedIndex].value);
|
|
68
|
+
}
|
|
69
|
+
} else if (e.key === "Escape") {
|
|
70
|
+
onCancel();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<DialogPortal>
|
|
76
|
+
<div className="fixed inset-0 z-[60] flex items-center justify-center">
|
|
77
|
+
<div className="absolute inset-0 bg-[var(--bg-overlay)]" onClick={onCancel} />
|
|
78
|
+
<div className="relative bg-[var(--bg-secondary)] rounded-xl border border-[var(--border-subtle)] shadow-2xl w-[90vw] max-w-sm overflow-hidden">
|
|
79
|
+
{/* Header */}
|
|
80
|
+
<div className="px-3 pt-3 pb-2">
|
|
81
|
+
<div className="text-sm font-medium text-[var(--text-primary)] mb-2">{title}</div>
|
|
82
|
+
<input
|
|
83
|
+
ref={inputRef}
|
|
84
|
+
type="text"
|
|
85
|
+
value={query}
|
|
86
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
87
|
+
onKeyDown={handleKeyDown}
|
|
88
|
+
placeholder={placeholder}
|
|
89
|
+
className="w-full px-2.5 py-1.5 text-sm bg-[var(--bg-tertiary)] border border-[var(--border-subtle)] rounded-lg text-[var(--text-primary)] placeholder:text-[var(--text-muted)] focus:outline-none focus:border-blue-500/50"
|
|
90
|
+
/>
|
|
91
|
+
</div>
|
|
92
|
+
|
|
93
|
+
{/* Options list */}
|
|
94
|
+
<div ref={listRef} className="max-h-64 overflow-y-auto px-1 pb-2">
|
|
95
|
+
{filtered.length === 0 ? (
|
|
96
|
+
<div className="px-3 py-4 text-xs text-[var(--text-muted)] text-center">{emptyMessage}</div>
|
|
97
|
+
) : (
|
|
98
|
+
filtered.map((opt, i) => (
|
|
99
|
+
<div
|
|
100
|
+
key={opt.value}
|
|
101
|
+
onClick={() => onSelect(opt.value)}
|
|
102
|
+
onMouseEnter={() => setSelectedIndex(i)}
|
|
103
|
+
className={`px-3 py-1.5 rounded-lg cursor-pointer flex items-center gap-2 ${
|
|
104
|
+
i === selectedIndex
|
|
105
|
+
? "bg-blue-500/15 text-[var(--text-primary)]"
|
|
106
|
+
: "text-[var(--text-secondary)] hover:bg-[var(--bg-tertiary)]"
|
|
107
|
+
}`}
|
|
108
|
+
>
|
|
109
|
+
<div className="flex-1 min-w-0">
|
|
110
|
+
<div className="text-xs truncate">{opt.label}</div>
|
|
111
|
+
{opt.description && (
|
|
112
|
+
<div className="text-[10px] text-[var(--text-muted)] truncate">{opt.description}</div>
|
|
113
|
+
)}
|
|
114
|
+
</div>
|
|
115
|
+
{opt.badge && (
|
|
116
|
+
<span className={`text-[9px] px-1 py-0.5 rounded flex-shrink-0 ${opt.badgeColor || "text-[var(--text-tertiary)]"}`}>
|
|
117
|
+
{opt.badge}
|
|
118
|
+
</span>
|
|
119
|
+
)}
|
|
120
|
+
</div>
|
|
121
|
+
))
|
|
122
|
+
)}
|
|
123
|
+
</div>
|
|
124
|
+
|
|
125
|
+
{/* Footer hint */}
|
|
126
|
+
<div className="px-3 py-1.5 border-t border-[var(--border-subtle)] text-[10px] text-[var(--text-muted)]">
|
|
127
|
+
↑↓ navigate · Enter select · Esc cancel
|
|
128
|
+
</div>
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
131
|
+
</DialogPortal>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StatusPill primitive — state-tinted pill (badge) with optional MDI icon.
|
|
3
|
+
*
|
|
4
|
+
* Registered as `ui:status-pill` in the primitive registry. Plugins emit
|
|
5
|
+
* `{primitive: "ui:status-pill", props: {state, text, icon?}}`.
|
|
6
|
+
*
|
|
7
|
+
* Mirrors the badge style from honcho-plugin's HonchoBadge so plugins
|
|
8
|
+
* migrating to intent rendering get visual parity. See change:
|
|
9
|
+
* adopt-server-driven-intent-rendering.
|
|
10
|
+
*/
|
|
11
|
+
import React, { useEffect, useState } from "react";
|
|
12
|
+
import Icon from "@mdi/react";
|
|
13
|
+
import type {
|
|
14
|
+
UiStatusPillProps,
|
|
15
|
+
UiStatusPillState,
|
|
16
|
+
} from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/ui-primitives.js";
|
|
17
|
+
|
|
18
|
+
const STATE_STYLE_DARK: Record<UiStatusPillState, { bg: string; fg: string }> = {
|
|
19
|
+
running: { bg: "rgba(34, 197, 94, 0.15)", fg: "rgb(134, 239, 172)" },
|
|
20
|
+
success: { bg: "rgba(34, 197, 94, 0.15)", fg: "rgb(134, 239, 172)" },
|
|
21
|
+
info: { bg: "rgba(59, 130, 246, 0.15)", fg: "rgb(147, 197, 253)" },
|
|
22
|
+
warn: { bg: "rgba(234, 179, 8, 0.15)", fg: "rgb(253, 224, 71)" },
|
|
23
|
+
error: { bg: "rgba(239, 68, 68, 0.15)", fg: "rgb(252, 165, 165)" },
|
|
24
|
+
muted: { bg: "rgba(107, 114, 128, 0.2)", fg: "rgb(209, 213, 219)" },
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const STATE_STYLE_LIGHT: Record<UiStatusPillState, { bg: string; fg: string }> = {
|
|
28
|
+
running: { bg: "rgba(34, 197, 94, 0.18)", fg: "rgb(21, 128, 61)" },
|
|
29
|
+
success: { bg: "rgba(34, 197, 94, 0.18)", fg: "rgb(21, 128, 61)" },
|
|
30
|
+
info: { bg: "rgba(59, 130, 246, 0.15)", fg: "rgb(29, 78, 216)" },
|
|
31
|
+
warn: { bg: "rgba(234, 179, 8, 0.20)", fg: "rgb(161, 98, 7)" },
|
|
32
|
+
error: { bg: "rgba(239, 68, 68, 0.15)", fg: "rgb(185, 28, 28)" },
|
|
33
|
+
muted: { bg: "rgba(107, 114, 128, 0.20)", fg: "rgb(55, 65, 81)" },
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function useIsLightTheme(): boolean {
|
|
37
|
+
const read = () =>
|
|
38
|
+
typeof document !== "undefined" &&
|
|
39
|
+
document.documentElement.getAttribute("data-theme") === "light";
|
|
40
|
+
const [light, setLight] = useState(read);
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (typeof document === "undefined") return;
|
|
43
|
+
const obs = new MutationObserver(() => setLight(read()));
|
|
44
|
+
obs.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
|
|
45
|
+
return () => obs.disconnect();
|
|
46
|
+
}, []);
|
|
47
|
+
return light;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function StatusPill({ state, text, icon, tooltip }: UiStatusPillProps) {
|
|
51
|
+
const light = useIsLightTheme();
|
|
52
|
+
const palette = light ? STATE_STYLE_LIGHT[state] : STATE_STYLE_DARK[state];
|
|
53
|
+
return (
|
|
54
|
+
<span
|
|
55
|
+
title={tooltip}
|
|
56
|
+
className="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium"
|
|
57
|
+
style={{ backgroundColor: palette.bg, color: palette.fg }}
|
|
58
|
+
data-status-pill={state}
|
|
59
|
+
>
|
|
60
|
+
{icon ? <IconByKey iconKey={icon} /> : null}
|
|
61
|
+
<span>{text}</span>
|
|
62
|
+
</span>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function IconByKey({ iconKey }: { iconKey: string }) {
|
|
67
|
+
const [path, setPath] = useState<string | null>(null);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
let cancelled = false;
|
|
70
|
+
import("@mdi/js")
|
|
71
|
+
.then((mdi) => {
|
|
72
|
+
if (cancelled) return;
|
|
73
|
+
const candidate = (mdi as Record<string, unknown>)[iconKey];
|
|
74
|
+
setPath(typeof candidate === "string" ? candidate : null);
|
|
75
|
+
})
|
|
76
|
+
.catch(() => setPath(null));
|
|
77
|
+
return () => {
|
|
78
|
+
cancelled = true;
|
|
79
|
+
};
|
|
80
|
+
}, [iconKey]);
|
|
81
|
+
if (!path) return null;
|
|
82
|
+
return <Icon path={path} size={0.55} />;
|
|
83
|
+
}
|