-
Notifications
You must be signed in to change notification settings - Fork 18
feat: visualize relationships #568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
...app/[workspaceSlug]/(app)/resources/(raw)/[resourceId]/visualize/RelationshipsDiagram.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| "use client"; | ||
|
|
||
| import type * as schema from "@ctrlplane/db/schema"; | ||
| import type { EdgeTypes, NodeTypes } from "reactflow"; | ||
| import ReactFlow, { | ||
| MarkerType, | ||
| ReactFlowProvider, | ||
| useEdgesState, | ||
| useNodesState, | ||
| } from "reactflow"; | ||
| import colors from "tailwindcss/colors"; | ||
|
|
||
| import { useLayoutAndFitView } from "~/app/[workspaceSlug]/(app)/_components/reactflow/layout"; | ||
| import { DepEdge } from "./DepEdge"; | ||
| import { ResourceNode } from "./ResourceNode"; | ||
|
|
||
| type ParentRelationship = { | ||
| ruleId: string; | ||
| type: schema.ResourceDependencyType; | ||
| target: schema.Resource; | ||
| reference: string; | ||
| }; | ||
|
|
||
| type ChildRelationship = { | ||
| ruleId: string; | ||
| type: schema.ResourceDependencyType; | ||
| source: schema.Resource; | ||
| reference: string; | ||
| }; | ||
|
|
||
| type RelationshipsDiagramProps = { | ||
| resource: schema.Resource; | ||
| parents: ParentRelationship[]; | ||
| children: ChildRelationship[]; | ||
| }; | ||
|
|
||
| const getNodes = (resources: schema.Resource[]) => | ||
| resources.map((r) => ({ | ||
| id: r.id, | ||
| type: "resource", | ||
| data: { ...r, label: r.identifier }, | ||
| position: { x: 0, y: 0 }, | ||
| })); | ||
|
|
||
| const markerEnd = { | ||
| type: MarkerType.Arrow, | ||
| color: colors.neutral[800], | ||
| }; | ||
|
|
||
| const nodeTypes: NodeTypes = { resource: ResourceNode }; | ||
| const edgeTypes: EdgeTypes = { default: DepEdge }; | ||
|
|
||
| const getParentEdges = ( | ||
| parents: ParentRelationship[], | ||
| resource: schema.Resource, | ||
| ) => | ||
| parents.map((p) => ({ | ||
| id: `${p.ruleId}-${p.target.id}`, | ||
| source: p.target.id, | ||
| target: resource.id, | ||
| style: { stroke: colors.neutral[800] }, | ||
| markerEnd, | ||
| label: p.type, | ||
| })); | ||
|
|
||
| const getChildEdges = ( | ||
| children: ChildRelationship[], | ||
| resource: schema.Resource, | ||
| ) => | ||
| children.map((c) => ({ | ||
| id: `${c.ruleId}-${c.source.id}`, | ||
| source: resource.id, | ||
| target: c.source.id, | ||
| style: { stroke: colors.neutral[800] }, | ||
| markerEnd, | ||
| label: c.type, | ||
| })); | ||
|
|
||
| export const RelationshipsDiagram: React.FC<RelationshipsDiagramProps> = ({ | ||
| resource, | ||
| parents, | ||
| children, | ||
| }) => { | ||
| const [nodes, _, onNodesChange] = useNodesState<{ label: string }>( | ||
| getNodes([ | ||
| resource, | ||
| ...parents.map((p) => p.target), | ||
| ...children.map((c) => c.source), | ||
| ]), | ||
| ); | ||
|
|
||
| const [edges, __, onEdgesChange] = useEdgesState([ | ||
| ...getParentEdges(parents, resource), | ||
| ...getChildEdges(children, resource), | ||
| ]); | ||
|
|
||
| const { setReactFlowInstance } = useLayoutAndFitView(nodes, { | ||
| direction: "LR", | ||
| extraEdgeLength: 50, | ||
| focusedNodeId: resource.id, | ||
| }); | ||
|
|
||
| return ( | ||
| <ReactFlow | ||
| nodes={nodes} | ||
| edges={edges} | ||
| onNodesChange={onNodesChange} | ||
| onEdgesChange={onEdgesChange} | ||
| fitView | ||
| proOptions={{ hideAttribution: true }} | ||
| onInit={setReactFlowInstance} | ||
| nodesDraggable | ||
| nodeTypes={nodeTypes} | ||
| edgeTypes={edgeTypes} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export const RelationshipsDiagramProvider: React.FC< | ||
| RelationshipsDiagramProps | ||
| > = ({ resource, parents, children }) => { | ||
| return ( | ||
| <ReactFlowProvider> | ||
| <RelationshipsDiagram | ||
| resource={resource} | ||
| parents={parents} | ||
| children={children} | ||
| /> | ||
| </ReactFlowProvider> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 0 additions & 86 deletions
86
...kspaceSlug]/(app)/resources/(raw)/[resourceId]/visualize/ResourceVisualizationDiagram.tsx
This file was deleted.
Oops, something went wrong.
140 changes: 0 additions & 140 deletions
140
.../webservice/src/app/[workspaceSlug]/(app)/resources/(raw)/[resourceId]/visualize/edges.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Replace clickable
<div>with a semantic<button>for a11y & keyboard supportThe node is an interactive element but is rendered as a bare
div.Screen-reader users won’t recognise it as clickable and keyboard users cannot focus it.
Benefits: built-in focus handling, space/enter activation, and correct ARIA semantics without extra work.
📝 Committable suggestion
🤖 Prompt for AI Agents