Edge.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { BaseEdge, Edge, EdgeLabelRenderer, getSmoothStepPath, type EdgeProps } from '@xyflow/react'
  2. import { useParams } from 'common'
  3. import { Loader2 } from 'lucide-react'
  4. import { Tooltip, TooltipContent, TooltipTrigger } from 'ui'
  5. import { EdgeData, REPLICA_STATUS } from './InstanceConfiguration.constants'
  6. import { useReplicationLagQuery } from '@/data/read-replicas/replica-lag-query'
  7. import { formatDatabaseID } from '@/data/read-replicas/replicas.utils'
  8. export const SmoothstepEdge = ({
  9. sourceX,
  10. sourceY,
  11. targetX,
  12. targetY,
  13. sourcePosition,
  14. targetPosition,
  15. style = {},
  16. markerEnd,
  17. data,
  18. }: EdgeProps<Edge<EdgeData>>) => {
  19. const { ref } = useParams()
  20. // [Joshen] Only applicable for replicas
  21. const { status, identifier, connectionString } = data || {}
  22. const formattedId = formatDatabaseID(identifier ?? '')
  23. const [edgePath, labelX, labelY] = getSmoothStepPath({
  24. sourceX,
  25. sourceY,
  26. sourcePosition,
  27. targetX,
  28. targetY,
  29. targetPosition,
  30. })
  31. const {
  32. data: lagDuration,
  33. isPending: isLoading,
  34. isError,
  35. } = useReplicationLagQuery(
  36. {
  37. // Safe cast as the query isn't enable if identifier is null/undefined
  38. id: identifier as string,
  39. projectRef: ref,
  40. connectionString,
  41. },
  42. { enabled: identifier != null && status === REPLICA_STATUS.ACTIVE_HEALTHY }
  43. )
  44. const lagValue = Number(lagDuration?.toFixed(2) ?? 0).toLocaleString()
  45. return (
  46. <>
  47. <BaseEdge path={edgePath} markerEnd={markerEnd} style={style} />
  48. {data !== undefined && !isError && status === REPLICA_STATUS.ACTIVE_HEALTHY && (
  49. <EdgeLabelRenderer>
  50. <Tooltip>
  51. <TooltipTrigger asChild>
  52. <div
  53. className="bg-surface-100 px-1.5 py-0.5 rounded-sm absolute nodrag nopan"
  54. style={{
  55. transform: `translate(-50%, -50%) translate(${labelX}px,${labelY}px)`,
  56. pointerEvents: 'all',
  57. }}
  58. >
  59. {isLoading ? (
  60. <Loader2 size={12} className="animate-spin" />
  61. ) : (
  62. <p className="font-mono text-xs">{lagValue}s</p>
  63. )}
  64. </div>
  65. </TooltipTrigger>
  66. <TooltipContent side="bottom" align="center">
  67. {isLoading
  68. ? `Checking replication lag for replica ID: ${formattedId}`
  69. : `Replication lag (seconds) for replica ID: ${formattedId}`}
  70. </TooltipContent>
  71. </Tooltip>
  72. </EdgeLabelRenderer>
  73. )}
  74. </>
  75. )
  76. }