Repository navigation
Joshen/fe 2287 summary nodegraph in replication page - #42042
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
6 Skipped Deployments
|
|
This pull request has been ignored for the connected project Preview Branches by Supabase. |
WalkthroughAdds a read-only replication topology diagram with new node components and Dagre-based layout; integrates the diagram into the replication page; changes Changes
Sequence Diagram(s)sequenceDiagram
participant User as User
participant Page as Replication Page
participant RDC as ReplicationDiagramContent
participant RRQ as ReadReplicasQuery
participant DQ as DestinationsQuery
participant PQ as PipelinesQuery
participant PSQ as PipelineStatusQuery
participant Dagre as Dagre(layout)
participant RF as ReactFlow
User->>Page: open replication page
Page->>RDC: mount ReplicationDiagramContent
RDC->>RRQ: fetch read replicas
RRQ-->>RDC: replicas data
RDC->>DQ: fetch destinations
DQ-->>RDC: destinations data
RDC->>PQ: fetch pipelines for destinations
PQ-->>RDC: pipelines data
RDC->>PSQ: poll pipeline statuses
PSQ-->>RDC: pipeline status data
RDC->>RDC: build nodes & edges (status-driven)
RDC->>Dagre: layout nodes & edges
Dagre-->>RDC: positioned nodes/edges
RDC->>RF: configure & render diagram
RF-->>User: display topology
Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| } = useReadReplicasQuery({ | ||
| projectRef, | ||
| }) | ||
| } = useReadReplicasQuery({ projectRef }, { refetchInterval: statusRefetchInterval }) |
There was a problem hiding this comment.
JFYI i think i can simplify the long polling logic here to not depend on the replica status query actually
will look into this separately
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In
`@apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/index.tsx`:
- Around line 75-81: The map callback in destinations.map uses a parameter named
x that shadows the outer destination variable, causing the find to compare
pipeline.destination_id === pipeline.id; fix by renaming the inner find
parameter (e.g., to p or pipelineCandidate) and compare p.destination_id ===
x.id (or rename the outer map param to destination and compare p.destination_id
=== destination.id) when searching pipelinesData?.pipelines, keeping the rest of
the logic (pipelineStatus via replicationKeys.pipelinesStatus, getStatusName,
isReplicating) unchanged.
🧹 Nitpick comments (6)
apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/ReplicationDiagram.utils.ts (1)
29-41: Minor: Unnecessary return insideforEachcallback.The
return nodestatement on line 40 has no effect sinceforEachignores return values. This appears to be a leftover from a potentialmapimplementation.♻️ Suggested fix
nodes.forEach((node) => { const nodeWithPosition = dagreGraph.node(node.id) node.sourcePosition = Position.Right node.targetPosition = Position.Left // We are shifting the dagre node position (anchor=center center) to the top left // so it matches the React Flow node anchor point (top left). node.position = { x: nodeWithPosition.x - nodeWithPosition.width / 2, y: nodeWithPosition.y - nodeWithPosition.height / 2, } - - return node })apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/index.tsx (3)
77-79: Avoid type casting; consider runtime validation or narrowing.The
as ReplicationPipelineStatusResponsecast bypasses type safety. Per coding guidelines, prefer runtime validation. SincegetQueryDatacan returnundefinedand the type may not match at runtime, consider handling this more defensively.♻️ Suggested approach
- const pipelineStatus = queryClient.getQueryData( - replicationKeys.pipelinesStatus(projectRef, pipeline?.id) - ) as ReplicationPipelineStatusResponse + const pipelineStatus = queryClient.getQueryData<ReplicationPipelineStatusResponse>( + replicationKeys.pipelinesStatus(projectRef, pipeline?.id) + )Using the generic parameter on
getQueryDatais the idiomatic approach and avoids explicit casting.
120-122: Potential excessive re-renders due to unstable dependency.The
nodesarray is recreated on every render, causing thisuseEffectto run repeatedly. Consider memoizing the nodes/edges arrays or using a stable dependency like the count or IDs.♻️ Suggested approach
+ const nodeCount = 1 + readReplicas.length + destinations.length + useEffect(() => { - if (nodes.length > 0 && isSuccessDestinations && isSuccessReplicas) setReactFlow() - }, [nodes, isSuccessDestinations, isSuccessReplicas]) + if (nodeCount > 0 && isSuccessDestinations && isSuccessReplicas) setReactFlow() + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [nodeCount, isSuccessDestinations, isSuccessReplicas])Alternatively, memoize the
nodesandedgesarrays withuseMemoso they have stable references when the underlying data hasn't changed.
110-118: Consider memoizingsetReactFlowwithuseCallback.The
setReactFlowfunction is recreated on every render. While not critical for correctness, memoizing it would provide a stable reference and is a best practice when the function is used in effects or passed to children.apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/Nodes.tsx (2)
70-70: Define explicit prop interfaces for components.Per coding guidelines, React components should have explicitly defined prop interfaces. Both
ReplicationNodeandReadReplicaNodeaccept anidprop that should be typed via an interface.♻️ Suggested approach
interface ReplicationNodeProps { id: string } export const ReplicationNode = ({ id }: ReplicationNodeProps) => { // ... } interface ReadReplicaNodeProps { id: string } export const ReadReplicaNode = ({ id }: ReadReplicaNodeProps) => { // ... }As per coding guidelines for
apps/studio/**/*.tsx.Also applies to: 129-129
21-33: Consider documenting or extracting the magic number.The width calculation
NODE_WIDTH / 2 + 55uses a magic number55. Consider extracting this as a named constant or adding a brief comment explaining its purpose (e.g., padding/margin compensation).
🎭 Playwright Test ResultsDetails
Flaky testsFeatures › table-editor.spec.ts › table editor › add enums and show enums on table Skipped testsFeatures › sql-editor.spec.ts › SQL Editor › snippet favourite works as expected |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In
`@apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/index.tsx`:
- Around line 43-96: The nodes and edges arrays are recreated each render
causing the useEffect that depends on nodes to retrigger; wrap the nodes and
edges construction in useMemo to memoize them and list correct dependencies
(e.g., projectRef, readReplicas, destinations, pipelinesData?.pipelines,
queryClient, and anything used to compute pipeline/status like replicationKeys
or getStatusName) so references are stable between renders; also simplify the
edge property animated: isReplicating ? true : false to animated: isReplicating.
🧹 Nitpick comments (3)
apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/index.tsx (3)
77-79: Consider using the generic type parameter instead of type assertion.Per coding guidelines, avoid type casting with
as. ThegetQueryDatamethod accepts a generic type parameter which provides better type safety.♻️ Proposed fix
- const pipelineStatus = queryClient.getQueryData( + const pipelineStatus = queryClient.getQueryData<ReplicationPipelineStatusResponse>( replicationKeys.pipelinesStatus(projectRef, pipeline?.id) - ) as ReplicationPipelineStatusResponse + )
110-118: WrapsetReactFlowinuseCallbackfor referential stability.If you memoize
nodesandedgesas suggested, this function should also be wrapped inuseCallbackto prevent it from being recreated on every render. The timeout workaround is acknowledged in the comment—this is a known ReactFlow quirk.♻️ Proposed fix
- const setReactFlow = async () => { + const setReactFlow = useCallback(async () => { const graph = getDagreGraphLayout(nodes, edges) reactFlow.setNodes(graph.nodes) reactFlow.setEdges(graph.edges) // [Joshen] Odd fix to ensure that react flow snaps back to center when adding nodes await timeout(1) reactFlow.fitView({ maxZoom: 0.9, minZoom: 0.9 }) - } + }, [nodes, edges, reactFlow])
120-122: IncludesetReactFlowin the dependency array.If
setReactFlowis wrapped inuseCallback, it should be included in the effect's dependency array to satisfy React's exhaustive-deps rule and ensure the effect uses the latest function reference.♻️ Proposed fix
useEffect(() => { if (nodes.length > 0 && isSuccessDestinations && isSuccessReplicas) setReactFlow() - }, [nodes, isSuccessDestinations, isSuccessReplicas]) + }, [nodes, isSuccessDestinations, isSuccessReplicas, setReactFlow])
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In
`@apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/index.tsx`:
- Around line 60-126: The edges useMemo reads pipeline status via
queryClient.getQueryData (replicationKeys.pipelinesStatus) which does not
subscribe to cache updates, so edge animated state can become stale; replace
that non-reactive read by subscribing to pipeline statuses (e.g., useQueries or
the existing useReplicationPipelineStatusQuery per pipeline id) and use those
reactive values when computing edges in the edges useMemo, and also add edges to
the dependency array of the useEffect that calls setReactFlow (currently [nodes,
isSuccessDestinations, isSuccessReplicas]) so the layout is recalculated when
edges change.
| const edges = useMemo(() => { | ||
| return [ | ||
| ...readReplicas.map((x) => { | ||
| const isReplicating = x.status === 'ACTIVE_HEALTHY' | ||
|
|
||
| return { | ||
| id: `${projectRef}-${x.identifier}`, | ||
| source: projectRef, | ||
| target: x.identifier, | ||
| type: 'smoothstep', | ||
| className: '!cursor-default', | ||
| animated: isReplicating, | ||
| style: { | ||
| opacity: isReplicating ? 1 : 0.4, | ||
| strokeDasharray: isReplicating ? undefined : '5 5', | ||
| }, | ||
| } | ||
| }), | ||
| ...destinations.map((x) => { | ||
| const pipeline = (pipelinesData?.pipelines ?? []).find((p) => p.destination_id === x.id) | ||
| const pipelineStatus = queryClient.getQueryData( | ||
| replicationKeys.pipelinesStatus(projectRef, pipeline?.id) | ||
| ) as ReplicationPipelineStatusResponse | ||
| const statusName = getStatusName(pipelineStatus?.status) | ||
| const isReplicating = statusName === 'started' | ||
|
|
||
| return { | ||
| id: `${projectRef}-${x.id}`, | ||
| source: projectRef, | ||
| target: x.id.toString(), | ||
| type: 'smoothstep', | ||
| className: '!cursor-default', | ||
| animated: isReplicating, | ||
| style: { | ||
| opacity: isReplicating ? 1 : 0.4, | ||
| strokeDasharray: isReplicating ? undefined : '5 5', | ||
| }, | ||
| } | ||
| }), | ||
| ] | ||
| }, [destinations, pipelinesData?.pipelines, projectRef, queryClient, readReplicas]) | ||
|
|
||
| const nodeTypes = useMemo( | ||
| () => ({ | ||
| primary: PrimaryDatabaseNode, | ||
| replication: ReplicationNode, | ||
| readReplica: ReadReplicaNode, | ||
| }), | ||
| [] | ||
| ) | ||
|
|
||
| const backgroundPatternColor = | ||
| resolvedTheme === 'dark' ? 'rgba(255, 255, 255, 0.3)' : 'rgba(0, 0, 0, 0.4)' | ||
|
|
||
| const setReactFlow = async () => { | ||
| const graph = getDagreGraphLayout(nodes, edges) | ||
| reactFlow.setNodes(graph.nodes) | ||
| reactFlow.setEdges(graph.edges) | ||
|
|
||
| // [Joshen] Odd fix to ensure that react flow snaps back to center when adding nodes | ||
| await timeout(1) | ||
| reactFlow.fitView({ maxZoom: 0.9, minZoom: 0.9 }) | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| if (nodes.length > 0 && isSuccessDestinations && isSuccessReplicas) setReactFlow() | ||
| }, [nodes, isSuccessDestinations, isSuccessReplicas]) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
find apps/studio -name "ReplicationDiagram" -o -path "*Replication*" -type f | head -20Repository: supabase/supabase
Length of output: 2171
🏁 Script executed:
fd -t f "index.tsx" | grep -i replication | grep -i diagramRepository: supabase/supabase
Length of output: 144
🏁 Script executed:
git ls-files | grep -i "ReplicationDiagram"Repository: supabase/supabase
Length of output: 330
🏁 Script executed:
cat -n apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/index.tsxRepository: supabase/supabase
Length of output: 6104
🏁 Script executed:
grep -r "pipelinesStatus" apps/studio/components/interfaces/Database/Replication/ --include="*.ts" --include="*.tsx" -nRepository: supabase/supabase
Length of output: 215
🏁 Script executed:
cat -n apps/studio/data/replication/keys.ts | head -50Repository: supabase/supabase
Length of output: 1656
🏁 Script executed:
grep -r "useQueries\|pipeline.*status" apps/studio/components/interfaces/Database/Replication/ --include="*.ts" --include="*.tsx" -B 2 -A 2 | head -80Repository: supabase/supabase
Length of output: 8604
🏁 Script executed:
grep -r "useReplicationPipelineStatusQuery" apps/studio --include="*.ts" --include="*.tsx" -lRepository: supabase/supabase
Length of output: 677
🏁 Script executed:
cat -n apps/studio/data/replication/pipelines-query.tsRepository: supabase/supabase
Length of output: 1839
🏁 Script executed:
cat -n apps/studio/data/replication/pipeline-status-query.tsRepository: supabase/supabase
Length of output: 2170
Edge animations can become stale due to non-reactive pipeline status reads.
Using queryClient.getQueryData(...) reads a cache snapshot without subscribing to updates. When pipeline statuses change in the cache elsewhere (e.g., via polling in other components), this component won't react. Additionally, the useEffect doesn't depend on edges, so even if they recalculate, the layout won't update. The immediate fix is to add edges to the dependency array so layout updates whenever edges change. However, for full reactivity, subscribe to individual pipeline statuses via useQueries (similar to how DestinationRow.tsx uses useReplicationPipelineStatusQuery).
🔧 Minimum fix
useEffect(() => {
if (nodes.length > 0 && isSuccessDestinations && isSuccessReplicas) setReactFlow()
- }, [nodes, isSuccessDestinations, isSuccessReplicas])
+ }, [edges, nodes, isSuccessDestinations, isSuccessReplicas])📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const edges = useMemo(() => { | |
| return [ | |
| ...readReplicas.map((x) => { | |
| const isReplicating = x.status === 'ACTIVE_HEALTHY' | |
| return { | |
| id: `${projectRef}-${x.identifier}`, | |
| source: projectRef, | |
| target: x.identifier, | |
| type: 'smoothstep', | |
| className: '!cursor-default', | |
| animated: isReplicating, | |
| style: { | |
| opacity: isReplicating ? 1 : 0.4, | |
| strokeDasharray: isReplicating ? undefined : '5 5', | |
| }, | |
| } | |
| }), | |
| ...destinations.map((x) => { | |
| const pipeline = (pipelinesData?.pipelines ?? []).find((p) => p.destination_id === x.id) | |
| const pipelineStatus = queryClient.getQueryData( | |
| replicationKeys.pipelinesStatus(projectRef, pipeline?.id) | |
| ) as ReplicationPipelineStatusResponse | |
| const statusName = getStatusName(pipelineStatus?.status) | |
| const isReplicating = statusName === 'started' | |
| return { | |
| id: `${projectRef}-${x.id}`, | |
| source: projectRef, | |
| target: x.id.toString(), | |
| type: 'smoothstep', | |
| className: '!cursor-default', | |
| animated: isReplicating, | |
| style: { | |
| opacity: isReplicating ? 1 : 0.4, | |
| strokeDasharray: isReplicating ? undefined : '5 5', | |
| }, | |
| } | |
| }), | |
| ] | |
| }, [destinations, pipelinesData?.pipelines, projectRef, queryClient, readReplicas]) | |
| const nodeTypes = useMemo( | |
| () => ({ | |
| primary: PrimaryDatabaseNode, | |
| replication: ReplicationNode, | |
| readReplica: ReadReplicaNode, | |
| }), | |
| [] | |
| ) | |
| const backgroundPatternColor = | |
| resolvedTheme === 'dark' ? 'rgba(255, 255, 255, 0.3)' : 'rgba(0, 0, 0, 0.4)' | |
| const setReactFlow = async () => { | |
| const graph = getDagreGraphLayout(nodes, edges) | |
| reactFlow.setNodes(graph.nodes) | |
| reactFlow.setEdges(graph.edges) | |
| // [Joshen] Odd fix to ensure that react flow snaps back to center when adding nodes | |
| await timeout(1) | |
| reactFlow.fitView({ maxZoom: 0.9, minZoom: 0.9 }) | |
| } | |
| useEffect(() => { | |
| if (nodes.length > 0 && isSuccessDestinations && isSuccessReplicas) setReactFlow() | |
| }, [nodes, isSuccessDestinations, isSuccessReplicas]) | |
| const edges = useMemo(() => { | |
| return [ | |
| ...readReplicas.map((x) => { | |
| const isReplicating = x.status === 'ACTIVE_HEALTHY' | |
| return { | |
| id: `${projectRef}-${x.identifier}`, | |
| source: projectRef, | |
| target: x.identifier, | |
| type: 'smoothstep', | |
| className: '!cursor-default', | |
| animated: isReplicating, | |
| style: { | |
| opacity: isReplicating ? 1 : 0.4, | |
| strokeDasharray: isReplicating ? undefined : '5 5', | |
| }, | |
| } | |
| }), | |
| ...destinations.map((x) => { | |
| const pipeline = (pipelinesData?.pipelines ?? []).find((p) => p.destination_id === x.id) | |
| const pipelineStatus = queryClient.getQueryData( | |
| replicationKeys.pipelinesStatus(projectRef, pipeline?.id) | |
| ) as ReplicationPipelineStatusResponse | |
| const statusName = getStatusName(pipelineStatus?.status) | |
| const isReplicating = statusName === 'started' | |
| return { | |
| id: `${projectRef}-${x.id}`, | |
| source: projectRef, | |
| target: x.id.toString(), | |
| type: 'smoothstep', | |
| className: '!cursor-default', | |
| animated: isReplicating, | |
| style: { | |
| opacity: isReplicating ? 1 : 0.4, | |
| strokeDasharray: isReplicating ? undefined : '5 5', | |
| }, | |
| } | |
| }), | |
| ] | |
| }, [destinations, pipelinesData?.pipelines, projectRef, queryClient, readReplicas]) | |
| const nodeTypes = useMemo( | |
| () => ({ | |
| primary: PrimaryDatabaseNode, | |
| replication: ReplicationNode, | |
| readReplica: ReadReplicaNode, | |
| }), | |
| [] | |
| ) | |
| const backgroundPatternColor = | |
| resolvedTheme === 'dark' ? 'rgba(255, 255, 255, 0.3)' : 'rgba(0, 0, 0, 0.4)' | |
| const setReactFlow = async () => { | |
| const graph = getDagreGraphLayout(nodes, edges) | |
| reactFlow.setNodes(graph.nodes) | |
| reactFlow.setEdges(graph.edges) | |
| // [Joshen] Odd fix to ensure that react flow snaps back to center when adding nodes | |
| await timeout(1) | |
| reactFlow.fitView({ maxZoom: 0.9, minZoom: 0.9 }) | |
| } | |
| useEffect(() => { | |
| if (nodes.length > 0 && isSuccessDestinations && isSuccessReplicas) setReactFlow() | |
| }, [edges, nodes, isSuccessDestinations, isSuccessReplicas]) |
🤖 Prompt for AI Agents
In
`@apps/studio/components/interfaces/Database/Replication/ReplicationDiagram/index.tsx`
around lines 60 - 126, The edges useMemo reads pipeline status via
queryClient.getQueryData (replicationKeys.pipelinesStatus) which does not
subscribe to cache updates, so edge animated state can become stale; replace
that non-reactive read by subscribing to pipeline statuses (e.g., useQueries or
the existing useReplicationPipelineStatusQuery per pipeline id) and use those
reactive values when computing edges in the edges useMemo, and also add edges to
the dependency array of the useEffect that calls setReactFlow (currently [nodes,
isSuccessDestinations, isSuccessReplicas]) so the layout is recalculated when
edges change.
| ] | ||
| }, [destinations, pipelinesData?.pipelines, projectRef, queryClient, readReplicas]) | ||
|
|
||
| const nodeTypes = useMemo( |
There was a problem hiding this comment.
Very much a nit, but this is static right? So you could just move it outside of the component
This comment has been minimized.
This comment has been minimized.
Braintrust eval report
|
Context
Related to unifying read replicas into database/replication - adds a read-only summary node graph to the top of the page
demo.mp4
Summary by CodeRabbit
New Features
Improvements
✏️ Tip: You can customize this high-level summary in your review settings.