ExplainVisualizer.NodeRow.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { ChevronDown, ChevronRight } from 'lucide-react'
  2. import { useState } from 'react'
  3. import { cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
  4. import { parseDetailLines } from './ExplainVisualizer.parser'
  5. import { RowCountIndicator } from './ExplainVisualizer.RowCountIndicator'
  6. import type { ExplainNode } from './ExplainVisualizer.types'
  7. import { formatNodeDuration, getScanBarColor, getScanBorderColor } from './ExplainVisualizer.utils'
  8. interface ExplainNodeRowProps {
  9. node: ExplainNode
  10. depth: number
  11. /** Maximum duration across all nodes, used to calculate bar width as % */
  12. maxDuration: number
  13. }
  14. export function ExplainNodeRow({ node, depth, maxDuration }: ExplainNodeRowProps) {
  15. const [isExpanded, setIsExpanded] = useState(false)
  16. const hasChildren = node.children.length > 0
  17. const hasDetails = Boolean(node.details?.trim())
  18. const canExpand = hasDetails
  19. const detailLines = parseDetailLines(node.details)
  20. const indentPx = depth * 24
  21. // Calculate duration and bar width as % of max duration
  22. const duration = node.actualTime ? node.actualTime.end - node.actualTime.start : 0
  23. const hasTimingData = node.actualTime && duration > 0
  24. const barWidthPercent = maxDuration > 0 ? (duration / maxDuration) * 100 : 0
  25. const barColorClass = getScanBarColor(node.operation)
  26. const borderColorClass = getScanBorderColor(node.operation)
  27. return (
  28. <>
  29. {/* Wrapper for group hover */}
  30. <div className="group">
  31. {/* Main row */}
  32. <div
  33. className={cn(
  34. 'flex items-stretch border-l-4 transition-colors bg-studio group-hover:bg-surface-100/50',
  35. borderColorClass
  36. )}
  37. >
  38. {/* Left section: expand button + operation info */}
  39. <div
  40. className="flex items-center gap-3 px-4 py-3 shrink-0 min-w-[400px]"
  41. style={{ paddingLeft: `${16 + indentPx}px` }}
  42. >
  43. {/* Expand/collapse button */}
  44. <button
  45. type="button"
  46. onClick={() => canExpand && setIsExpanded(!isExpanded)}
  47. disabled={!canExpand}
  48. className={cn(
  49. 'flex items-center justify-center w-5 h-5 rounded-sm border border-border-muted shrink-0',
  50. canExpand ? 'hover:bg-surface-200 cursor-pointer' : 'opacity-30 cursor-default'
  51. )}
  52. aria-label={isExpanded ? 'Collapse details' : 'Expand details'}
  53. >
  54. {isExpanded ? (
  55. <ChevronDown size={12} className="text-foreground-light" />
  56. ) : (
  57. <ChevronRight size={12} className="text-foreground-light" />
  58. )}
  59. </button>
  60. {/* Operation name and cost info */}
  61. <div className="flex items-center gap-2 font-mono text-xs min-w-0">
  62. <span className="text-foreground uppercase font-medium whitespace-nowrap">
  63. {node.operation}
  64. </span>
  65. <span className="text-foreground-muted whitespace-nowrap">
  66. (cost {node.cost?.end?.toFixed(1) ?? '-'}, estimated{' '}
  67. {node.rows?.toLocaleString() ?? '?'} {node.rows === 1 ? 'row' : 'rows'})
  68. </span>
  69. </div>
  70. </div>
  71. {/* Right section: duration bar visualization */}
  72. <div className="flex-1 relative min-h-[43px] flex items-center">
  73. {hasTimingData && (
  74. <>
  75. {/* Duration bar - width represents % of slowest operation */}
  76. <div
  77. className={cn('absolute left-0 top-0 h-full', barColorClass)}
  78. style={{ width: `${barWidthPercent}%` }}
  79. />
  80. {/* Duration and row count info */}
  81. <div className="relative flex items-center gap-2 font-mono text-xs whitespace-nowrap px-3">
  82. <Tooltip>
  83. <TooltipTrigger asChild>
  84. <span className="text-foreground-light cursor-help">
  85. {formatNodeDuration(duration)}
  86. </span>
  87. </TooltipTrigger>
  88. <TooltipContent side="top" className="max-w-xs font-sans">
  89. <p className="font-medium">Execution time: {formatNodeDuration(duration)}</p>
  90. <p className="text-foreground-lighter text-xs mt-1">
  91. This is how long this operation took to execute. The bar width shows this as
  92. a percentage of the slowest operation ({Math.round(barWidthPercent)}%) —
  93. wider bars indicate where more time is spent.
  94. </p>
  95. </TooltipContent>
  96. </Tooltip>
  97. <span className="text-foreground-muted">/</span>
  98. <RowCountIndicator
  99. actualRows={node.actualRows}
  100. estimatedRows={node.rows}
  101. rowsRemovedByFilter={node.rowsRemovedByFilter}
  102. />
  103. </div>
  104. </>
  105. )}
  106. </div>
  107. </div>
  108. {/* Expanded details section */}
  109. {isExpanded && detailLines.length > 0 && (
  110. <div
  111. className={cn(
  112. 'border-t-border-muted border-t border-l-4 bg-studio group-hover:bg-surface-100/50',
  113. borderColorClass
  114. )}
  115. style={{ paddingLeft: `${16 + indentPx + 32}px` }}
  116. >
  117. <div className="px-0 py-3 space-y-2 font-mono text-xs">
  118. {detailLines.map((line, idx) => (
  119. <div key={idx} className="flex items-start gap-1">
  120. {line.label && <span className="text-foreground-muted">{line.label}</span>}
  121. <span className="text-foreground-light break-all">{line.value}</span>
  122. </div>
  123. ))}
  124. </div>
  125. </div>
  126. )}
  127. </div>
  128. {/* Render children recursively */}
  129. {hasChildren &&
  130. node.children.map((child, idx) => (
  131. <ExplainNodeRow key={idx} node={child} depth={depth + 1} maxDuration={maxDuration} />
  132. ))}
  133. </>
  134. )
  135. }