import { BaseEdge, EdgeLabelRenderer, getSmoothStepPath, type EdgeProps } from '@xyflow/react' import { useParams } from 'common' import { Loader2, X } from 'lucide-react' import { cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui' import { useReplicationLagQuery } from '@/data/read-replicas/replica-lag-query' import { formatDatabaseID } from '@/data/read-replicas/replicas.utils' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' type EdgeData = { type: string identifier: string isComingUp: boolean isReplicating: boolean isFailed: boolean shiftEdgeEnd: boolean } export const SmoothstepEdge = ({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, style = {}, markerEnd, data, }: EdgeProps) => { const { ref } = useParams() const { data: project } = useSelectedProjectQuery() const { type, identifier, isComingUp, isReplicating, isFailed, shiftEdgeEnd } = (data || {}) as EdgeData const formattedId = type === 'replica' ? formatDatabaseID(identifier ?? '') : identifier const [edgePath, labelX, labelY] = getSmoothStepPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, }) const { data: lagDuration, isPending: isLoading, isError, } = useReplicationLagQuery( { id: identifier, projectRef: ref, connectionString: project?.connectionString, }, { enabled: type === 'replica' && isReplicating, refetchInterval: 10000 } ) const lagValue = Number(lagDuration?.toFixed(2) ?? 0).toLocaleString() const hasReplicationLagData = data !== undefined && !isError && isReplicating return ( <> {isFailed && (
)} {(isComingUp || hasReplicationLagData) && (
{isLoading || isComingUp ? ( ) : (

{lagValue}s

)}
{!isComingUp && ( {isLoading ? `Checking replication lag for replica ID: ${formattedId}` : `Replication lag (seconds) for replica ID: ${formattedId}`} )}
)} ) }