Graph: place capability labels beside the target node (ENB-22)
Edge labels sat at each curve's midpoint, where edges leaving one policy bunch up and overlap. Policy->path edges now use a custom edge that puts a clickable label just before the target, stacked apart when several edges share a target, and the path column is moved right to leave room for longer labels. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b55259c492
commit
0882d35123
4 files changed
+82
-14
No files matched your search
@@ -0,0 +1,32 @@
|
||||
import { BaseEdge, EdgeLabelRenderer, getBezierPath, type EdgeProps } from '@xyflow/react'
|
||||
|
||||
export interface GrantEdgeData {
|
||||
label: string
|
||||
/** Vertical nudge so labels on edges into the same node don't sit on top of each other. */
|
||||
offset: number
|
||||
onSelect: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* A policy -> path edge. The capability label sits just before the target node, where edges have
|
||||
* fanned out to different rows, instead of at the midpoint where edges from one policy bunch up.
|
||||
*/
|
||||
export function GrantEdge({ id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, markerEnd, selected, data }: EdgeProps) {
|
||||
const { label, offset, onSelect } = data as unknown as GrantEdgeData
|
||||
const [path] = getBezierPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition })
|
||||
return (
|
||||
<>
|
||||
<BaseEdge id={id} path={path} markerEnd={markerEnd} />
|
||||
<EdgeLabelRenderer>
|
||||
<button
|
||||
type="button"
|
||||
className={`nodrag nopan edge-label${selected ? ' edge-label-selected' : ''}`}
|
||||
style={{ transform: `translate(-100%, -50%) translate(${targetX - 18}px, ${targetY + offset}px)` }}
|
||||
onClick={onSelect}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
</EdgeLabelRenderer>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -17,10 +17,12 @@ import { layoutColumns } from '../../model/layout'
|
||||
import { RequireConnection } from '../shared/RequireConnection'
|
||||
import { useAccessInputs } from '../shared/useAccessInputs'
|
||||
import { Details, type Selection } from './Details'
|
||||
import { GrantEdge, type GrantEdgeData } from './GraphEdge'
|
||||
import { GraphNodeCard } from './GraphNode'
|
||||
|
||||
// Defined once, outside the component, so React Flow doesn't re-register node types on every render.
|
||||
const nodeTypes = { gnode: GraphNodeCard }
|
||||
const edgeTypes = { grant: GrantEdge }
|
||||
|
||||
// A grant with everything is easier to read as one word than as seven.
|
||||
const FULL = ['create', 'read', 'update', 'delete', 'list', 'sudo']
|
||||
@@ -29,7 +31,7 @@ function capabilityLabel(caps: string[] | undefined): string | undefined {
|
||||
return FULL.every((c) => caps.includes(c)) ? 'full access' : caps.join(', ')
|
||||
}
|
||||
|
||||
function toFlow(graph: AccessGraph, selected: Selection | null): { nodes: Node[]; edges: Edge[] } {
|
||||
function toFlow(graph: AccessGraph, selected: Selection | null, onEdgeSelect: (id: string) => void): { nodes: Node[]; edges: Edge[] } {
|
||||
const pos = layoutColumns(graph)
|
||||
const nodes: Node[] = graph.nodes.map((n) => ({
|
||||
id: n.id,
|
||||
@@ -46,17 +48,27 @@ function toFlow(graph: AccessGraph, selected: Selection | null): { nodes: Node[]
|
||||
sourcePosition: Position.Right,
|
||||
targetPosition: Position.Left,
|
||||
}))
|
||||
const edges: Edge[] = graph.edges.map((e) => ({
|
||||
id: e.id,
|
||||
source: e.source,
|
||||
target: e.target,
|
||||
label: capabilityLabel(e.capabilities),
|
||||
labelStyle: { fill: 'var(--ink-soft)', fontSize: 12 },
|
||||
labelBgStyle: { fill: 'var(--surface-sunken)' },
|
||||
labelBgPadding: [6, 3],
|
||||
markerEnd: { type: MarkerType.ArrowClosed },
|
||||
selected: selected?.kind === 'edge' && selected.id === e.id,
|
||||
}))
|
||||
// Edges into the same node get their labels stacked a little apart so they stay legible.
|
||||
const intoTarget = new Map<string, string[]>()
|
||||
for (const e of graph.edges) if (e.capabilities) intoTarget.set(e.target, [...(intoTarget.get(e.target) ?? []), e.id])
|
||||
const edges: Edge[] = graph.edges.map((e) => {
|
||||
const base = {
|
||||
id: e.id,
|
||||
source: e.source,
|
||||
target: e.target,
|
||||
markerEnd: { type: MarkerType.ArrowClosed },
|
||||
selected: selected?.kind === 'edge' && selected.id === e.id,
|
||||
}
|
||||
const label = capabilityLabel(e.capabilities)
|
||||
if (!label) return base
|
||||
const siblings = intoTarget.get(e.target) ?? []
|
||||
const data: GrantEdgeData = {
|
||||
label,
|
||||
offset: (siblings.indexOf(e.id) - (siblings.length - 1) / 2) * 24,
|
||||
onSelect: () => onEdgeSelect(e.id),
|
||||
}
|
||||
return { ...base, type: 'grant', data: data as unknown as Record<string, unknown> }
|
||||
})
|
||||
return { nodes, edges }
|
||||
}
|
||||
|
||||
@@ -76,7 +88,10 @@ function GraphView({ client }: { client: BaoClient }) {
|
||||
() => (full ? filterGraph(full, { identity, path, hidePolicies: hideDefault ? ['default'] : [] }) : null),
|
||||
[full, identity, path, hideDefault],
|
||||
)
|
||||
const flow = useMemo(() => (shown ? toFlow(shown, selected) : null), [shown, selected])
|
||||
const flow = useMemo(
|
||||
() => (shown ? toFlow(shown, selected, (id) => setSelected({ kind: 'edge', id })) : null),
|
||||
[shown, selected],
|
||||
)
|
||||
|
||||
if (inputs.isPending) return <p>Loading…</p>
|
||||
if (inputs.error) return <p role="alert">{inputs.error.message}</p>
|
||||
@@ -132,6 +147,7 @@ function GraphView({ client }: { client: BaoClient }) {
|
||||
nodes={flow.nodes}
|
||||
edges={flow.edges}
|
||||
nodeTypes={nodeTypes}
|
||||
edgeTypes={edgeTypes}
|
||||
fitView
|
||||
nodesDraggable={false}
|
||||
nodesConnectable={false}
|
||||
|
||||
@@ -1316,3 +1316,23 @@ button.danger-solid:hover:not(:disabled) {
|
||||
border-color: transparent;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
/* Capability label on a policy -> path edge. */
|
||||
.edge-label {
|
||||
position: absolute;
|
||||
pointer-events: all;
|
||||
padding: 1px 6px;
|
||||
background: var(--surface-sunken);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 4px;
|
||||
color: var(--ink-soft);
|
||||
font-size: 12px;
|
||||
line-height: 1.4;
|
||||
white-space: nowrap;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.edge-label-selected {
|
||||
border-color: var(--accent);
|
||||
color: var(--accent);
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
import type { AccessGraph, GraphNode } from './graph'
|
||||
|
||||
export const COLUMN_X = { identity: 0, policy: 380, path: 780 } as const
|
||||
export const COLUMN_X = { identity: 0, policy: 380, path: 860 } as const
|
||||
export const ROW_HEIGHT = 72
|
||||
|
||||
export interface Position {
|
||||
|
||||
Reference in new issue
Block a user