import { memo } from 'react' import { BaseEdge, EdgeLabelRenderer, getSmoothStepPath, useReactFlow, type EdgeProps, type Edge, } from '@xyflow/react' import { formatNanoDelay } from '@/lib/format' export interface LinkEdgeData { delay: number cost: number [key: string]: unknown } export type LinkEdgeType = Edge function delayColor(delayNs: number): { stroke: string; badge: string } { const ms = delayNs / 1_000_000 if (delayNs <= 0 || !Number.isFinite(delayNs)) { return { stroke: 'hsl(var(--muted-foreground))', badge: 'bg-muted text-muted-foreground' } } if (ms < 1) { return { stroke: '#10b981', badge: 'bg-emerald-500/15 text-emerald-600 dark:text-emerald-400' } } if (ms < 20) { return { stroke: '#f59e0b', badge: 'bg-amber-500/15 text-amber-600 dark:text-amber-400' } } return { stroke: '#f43f5e', badge: 'bg-rose-500/15 text-rose-600 dark:text-rose-400' } } function LinkEdgeComponent({ id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, data, selected, }: EdgeProps) { const { setEdges } = useReactFlow() const [edgePath, labelX, labelY] = getSmoothStepPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, borderRadius: 16, }) const delay = data?.delay ?? 0 const { stroke, badge } = delayColor(delay) const showLabel = delay > 0 && Number.isFinite(delay) return ( <> {showLabel && (
)} ) } export const LinkEdge = memo(LinkEdgeComponent)