diff --git a/src/features/graph/GraphEdge.tsx b/src/features/graph/GraphEdge.tsx new file mode 100644 index 0000000..a54467d --- /dev/null +++ b/src/features/graph/GraphEdge.tsx @@ -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 ( + <> + + + + + + ) +} diff --git a/src/features/graph/GraphPage.tsx b/src/features/graph/GraphPage.tsx index 4756f80..3910ea3 100644 --- a/src/features/graph/GraphPage.tsx +++ b/src/features/graph/GraphPage.tsx @@ -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() + 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 } + }) 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

Loading…

if (inputs.error) return

{inputs.error.message}

@@ -132,6 +147,7 @@ function GraphView({ client }: { client: BaoClient }) { nodes={flow.nodes} edges={flow.edges} nodeTypes={nodeTypes} + edgeTypes={edgeTypes} fitView nodesDraggable={false} nodesConnectable={false} diff --git a/src/index.css b/src/index.css index 0a2327f..42cb2b1 100644 --- a/src/index.css +++ b/src/index.css @@ -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); +} diff --git a/src/model/layout.ts b/src/model/layout.ts index 2976db1..90e6568 100644 --- a/src/model/layout.ts +++ b/src/model/layout.ts @@ -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 {