mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
108 lines
3.3 KiB
TypeScript
108 lines
3.3 KiB
TypeScript
import { BaseEdge, EdgeLabelRenderer, getSmoothStepPath, type EdgeProps } from '@xyflow/react'
|
|
import { useParams, useReducedMotion } from 'common'
|
|
import { useMemo } from 'react'
|
|
|
|
import { getStatusName } from '../Pipeline.utils'
|
|
import {
|
|
EdgeVisualChip,
|
|
getEdgeVisual,
|
|
type ReplicationState,
|
|
} from '@/components/ui/ReactFlow/EdgeVisual'
|
|
import { useReplicationPipelineStatusQuery } from '@/data/replication/pipeline-status-query'
|
|
import { useReplicationPipelinesQuery } from '@/data/replication/pipelines-query'
|
|
import {
|
|
PipelineStatusRequestStatus,
|
|
usePipelineRequestStatus,
|
|
} from '@/state/replication-pipeline-request-status'
|
|
|
|
type EdgeData = {
|
|
identifier: string
|
|
shiftEdgeEnd: boolean
|
|
}
|
|
|
|
export const SmoothstepEdge = ({
|
|
sourceX,
|
|
sourceY,
|
|
targetX,
|
|
targetY,
|
|
sourcePosition,
|
|
targetPosition,
|
|
style = {},
|
|
markerEnd,
|
|
data,
|
|
}: EdgeProps) => {
|
|
const { ref: projectRef = 'default' } = useParams()
|
|
const prefersReducedMotion = useReducedMotion()
|
|
const { identifier, shiftEdgeEnd } = (data || {}) as EdgeData
|
|
|
|
const { data: pipelinesData } = useReplicationPipelinesQuery({ projectRef })
|
|
const pipeline = (pipelinesData?.pipelines ?? []).find(
|
|
(p) => p.destination_id.toString() === identifier
|
|
)
|
|
const { data: pipelineStatusData } = useReplicationPipelineStatusQuery(
|
|
{ projectRef, pipelineId: pipeline?.id },
|
|
{ enabled: !!pipeline?.id }
|
|
)
|
|
const { getRequestStatus } = usePipelineRequestStatus()
|
|
const requestStatus = pipeline?.id
|
|
? getRequestStatus(pipeline.id)
|
|
: PipelineStatusRequestStatus.None
|
|
|
|
const replicationState = useMemo<ReplicationState>(() => {
|
|
const isTransitioning = requestStatus !== PipelineStatusRequestStatus.None
|
|
const statusName = getStatusName(pipelineStatusData?.status)
|
|
return {
|
|
isReplicating: statusName === 'started' && !isTransitioning,
|
|
isComingUp: isTransitioning || statusName === 'starting' || statusName === 'stopping',
|
|
isFailed: statusName === 'failed',
|
|
}
|
|
}, [pipelineStatusData?.status, requestStatus])
|
|
|
|
const edgeTargetX = shiftEdgeEnd ? targetX - 8 : targetX
|
|
const [edgePath, labelX, labelY] = getSmoothStepPath({
|
|
sourceX,
|
|
sourceY,
|
|
sourcePosition,
|
|
targetX: edgeTargetX,
|
|
targetY,
|
|
targetPosition,
|
|
})
|
|
// Branched paths label their shared vertical bend, so move the chip onto each final segment.
|
|
const chipX = shiftEdgeEnd ? (labelX + edgeTargetX) / 2 : labelX
|
|
const chipY = shiftEdgeEnd ? targetY : labelY
|
|
|
|
const visual = getEdgeVisual(replicationState)
|
|
|
|
return (
|
|
<>
|
|
<BaseEdge
|
|
path={edgePath}
|
|
markerEnd={markerEnd}
|
|
style={{
|
|
...style,
|
|
stroke: visual.color,
|
|
strokeWidth: visual.strokeWidth,
|
|
opacity: visual.opacity,
|
|
strokeDasharray: visual.dashArray,
|
|
animation:
|
|
visual.shouldAnimate && !prefersReducedMotion
|
|
? 'dashdraw 0.5s linear infinite'
|
|
: undefined,
|
|
}}
|
|
/>
|
|
<EdgeLabelRenderer>
|
|
<div
|
|
style={{
|
|
position: 'absolute',
|
|
transform: `translate(-50%, -50%) translate(${chipX}px,${chipY}px)`,
|
|
pointerEvents: 'all',
|
|
}}
|
|
className="nodrag nopan"
|
|
>
|
|
<EdgeVisualChip visual={visual} />
|
|
</div>
|
|
</EdgeLabelRenderer>
|
|
</>
|
|
)
|
|
}
|