2026-03-09 00:40:56 -05:00
|
|
|
import { useState, useCallback, useEffect } from 'react'
|
|
|
|
|
import Tree from 'react-d3-tree'
|
2026-03-09 22:44:46 -05:00
|
|
|
import { ZoomIn, ZoomOut, Maximize2 } from 'lucide-react'
|
2026-03-09 00:40:56 -05:00
|
|
|
import './PedigreeTree.css'
|
|
|
|
|
|
|
|
|
|
const PedigreeTree = ({ dogId, pedigreeData, coi }) => {
|
|
|
|
|
const [translate, setTranslate] = useState({ x: 0, y: 0 })
|
|
|
|
|
const [zoom, setZoom] = useState(0.8)
|
|
|
|
|
const [dimensions, setDimensions] = useState({ width: 0, height: 0 })
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const updateDimensions = () => {
|
|
|
|
|
const container = document.getElementById('tree-container')
|
|
|
|
|
if (container) {
|
2026-03-09 22:44:46 -05:00
|
|
|
setDimensions({ width: container.offsetWidth, height: container.offsetHeight })
|
|
|
|
|
setTranslate({ x: container.offsetWidth / 4, y: container.offsetHeight / 2 })
|
2026-03-09 00:40:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
updateDimensions()
|
|
|
|
|
window.addEventListener('resize', updateDimensions)
|
|
|
|
|
return () => window.removeEventListener('resize', updateDimensions)
|
|
|
|
|
}, [])
|
|
|
|
|
|
2026-03-09 22:44:46 -05:00
|
|
|
const handleZoomIn = () => setZoom(z => Math.min(z + 0.2, 2))
|
2026-03-09 00:40:56 -05:00
|
|
|
const handleZoomOut = () => setZoom(z => Math.max(z - 0.2, 0.2))
|
2026-03-09 22:44:46 -05:00
|
|
|
const handleReset = () => {
|
2026-03-09 00:40:56 -05:00
|
|
|
setZoom(0.8)
|
2026-03-09 22:44:46 -05:00
|
|
|
setTranslate({ x: dimensions.width / 4, y: dimensions.height / 2 })
|
2026-03-09 00:40:56 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 22:44:46 -05:00
|
|
|
const renderCustomNode = ({ nodeDatum }) => {
|
|
|
|
|
const isRoot = nodeDatum.attributes?.isRoot
|
|
|
|
|
const isMale = nodeDatum.attributes?.sex === 'male'
|
|
|
|
|
const hasId = !!nodeDatum.attributes?.id
|
|
|
|
|
const breed = nodeDatum.attributes?.breed
|
|
|
|
|
|
|
|
|
|
// Colour palette aligned to app theme
|
|
|
|
|
const maleColor = '#3b82f6'
|
|
|
|
|
const femaleColor = '#ec4899'
|
|
|
|
|
const rootGold = '#c2862a' // --primary
|
|
|
|
|
const rootAccent = '#9b3a10' // --accent
|
|
|
|
|
|
|
|
|
|
const nodeColor = isRoot ? rootGold : (isMale ? maleColor : femaleColor)
|
|
|
|
|
const glowColor = isRoot
|
|
|
|
|
? 'rgba(194,134,42,0.35)'
|
|
|
|
|
: (isMale ? 'rgba(59,130,246,0.3)' : 'rgba(236,72,153,0.3)')
|
|
|
|
|
const ringColor = isRoot ? rootAccent : nodeColor
|
|
|
|
|
|
2026-03-11 15:26:35 -05:00
|
|
|
const r = isRoot ? 46 : 38
|
2026-03-09 22:44:46 -05:00
|
|
|
|
2026-03-09 00:40:56 -05:00
|
|
|
return (
|
|
|
|
|
<g>
|
2026-03-09 22:44:46 -05:00
|
|
|
{/* Glow halo */}
|
|
|
|
|
<circle
|
2026-03-11 15:26:35 -05:00
|
|
|
r={r + 12}
|
2026-03-09 22:44:46 -05:00
|
|
|
fill={glowColor}
|
|
|
|
|
style={{ filter: 'blur(6px)' }}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{/* Outer ring */}
|
|
|
|
|
<circle
|
|
|
|
|
r={r + 4}
|
|
|
|
|
fill="none"
|
|
|
|
|
stroke={ringColor}
|
|
|
|
|
strokeWidth={isRoot ? 2 : 1.5}
|
|
|
|
|
strokeOpacity={0.5}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{/* Main node */}
|
2026-03-09 00:40:56 -05:00
|
|
|
<circle
|
2026-03-09 22:44:46 -05:00
|
|
|
r={r}
|
|
|
|
|
fill={isRoot
|
|
|
|
|
? `url(#rootGradient)`
|
|
|
|
|
: nodeColor}
|
|
|
|
|
stroke="rgba(255,255,255,0.15)"
|
|
|
|
|
strokeWidth={2}
|
|
|
|
|
style={{
|
|
|
|
|
cursor: hasId ? 'pointer' : 'default',
|
|
|
|
|
filter: isRoot ? 'drop-shadow(0 0 8px rgba(194,134,42,0.6))' : 'none'
|
|
|
|
|
}}
|
2026-03-09 00:40:56 -05:00
|
|
|
onClick={() => {
|
2026-03-09 22:44:46 -05:00
|
|
|
if (hasId) window.location.href = `/dogs/${nodeDatum.attributes.id}`
|
2026-03-09 00:40:56 -05:00
|
|
|
}}
|
|
|
|
|
/>
|
2026-03-09 22:44:46 -05:00
|
|
|
|
|
|
|
|
{/* SVG gradient definition for root node */}
|
|
|
|
|
{isRoot && (
|
|
|
|
|
<defs>
|
|
|
|
|
<radialGradient id="rootGradient" cx="35%" cy="35%">
|
|
|
|
|
<stop offset="0%" stopColor="#e0a84a" />
|
|
|
|
|
<stop offset="100%" stopColor="#9b3a10" />
|
|
|
|
|
</radialGradient>
|
|
|
|
|
</defs>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Gender / crown icon */}
|
2026-03-09 00:40:56 -05:00
|
|
|
<text
|
2026-03-11 15:33:23 -05:00
|
|
|
fontSize={isRoot ? 28 : 24}
|
2026-03-09 00:40:56 -05:00
|
|
|
textAnchor="middle"
|
2026-03-11 15:26:35 -05:00
|
|
|
dy="8"
|
2026-03-11 15:33:23 -05:00
|
|
|
style={{ fill: '#ffffff', pointerEvents: 'none', userSelect: 'none' }}
|
2026-03-09 00:40:56 -05:00
|
|
|
>
|
2026-03-09 22:44:46 -05:00
|
|
|
{isRoot ? '👑' : (isMale ? '♂' : '♀')}
|
2026-03-09 00:40:56 -05:00
|
|
|
</text>
|
2026-03-09 22:44:46 -05:00
|
|
|
|
|
|
|
|
{/* Name label */}
|
2026-03-09 00:40:56 -05:00
|
|
|
<text
|
2026-03-11 15:33:23 -05:00
|
|
|
fontSize={isRoot ? 22 : 18}
|
2026-03-09 22:44:46 -05:00
|
|
|
fontWeight={isRoot ? '700' : '600'}
|
|
|
|
|
fontFamily="Inter, sans-serif"
|
2026-03-09 00:40:56 -05:00
|
|
|
textAnchor="middle"
|
|
|
|
|
x="0"
|
2026-03-11 15:33:23 -05:00
|
|
|
y={r + 32}
|
|
|
|
|
style={{ fill: isRoot ? '#ffffff' : '#f8fafc', pointerEvents: 'none', textShadow: '0 2px 5px rgba(0,0,0,0.9)' }}
|
2026-03-09 00:40:56 -05:00
|
|
|
>
|
|
|
|
|
{nodeDatum.name}
|
|
|
|
|
</text>
|
2026-03-09 22:44:46 -05:00
|
|
|
|
|
|
|
|
{/* Breed label (subtle) */}
|
|
|
|
|
{breed && (
|
|
|
|
|
<text
|
2026-03-11 15:33:23 -05:00
|
|
|
fontSize="14"
|
2026-03-09 22:44:46 -05:00
|
|
|
fontFamily="Inter, sans-serif"
|
|
|
|
|
textAnchor="middle"
|
|
|
|
|
x="0"
|
2026-03-11 15:33:23 -05:00
|
|
|
y={r + 52}
|
|
|
|
|
style={{ fill: '#cbd5e1', pointerEvents: 'none', textShadow: '0 1px 4px rgba(0,0,0,0.9)' }}
|
2026-03-09 22:44:46 -05:00
|
|
|
>
|
|
|
|
|
{breed}
|
|
|
|
|
</text>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Registration number */}
|
2026-03-09 00:40:56 -05:00
|
|
|
{nodeDatum.attributes?.registration && (
|
|
|
|
|
<text
|
2026-03-11 15:33:23 -05:00
|
|
|
fontSize="14"
|
2026-03-09 22:44:46 -05:00
|
|
|
fontFamily="Inter, sans-serif"
|
2026-03-09 00:40:56 -05:00
|
|
|
textAnchor="middle"
|
|
|
|
|
x="0"
|
2026-03-11 15:33:23 -05:00
|
|
|
y={r + (breed ? 70 : 52)}
|
|
|
|
|
style={{ fill: '#94a3b8', pointerEvents: 'none', textShadow: '0 1px 4px rgba(0,0,0,0.9)' }}
|
2026-03-09 00:40:56 -05:00
|
|
|
>
|
|
|
|
|
{nodeDatum.attributes.registration}
|
|
|
|
|
</text>
|
|
|
|
|
)}
|
2026-03-09 22:44:46 -05:00
|
|
|
|
|
|
|
|
{/* Birth year */}
|
2026-03-09 00:40:56 -05:00
|
|
|
{nodeDatum.attributes?.birth_year && (
|
|
|
|
|
<text
|
2026-03-11 15:33:23 -05:00
|
|
|
fontSize="14"
|
2026-03-09 22:44:46 -05:00
|
|
|
fontFamily="Inter, sans-serif"
|
2026-03-09 00:40:56 -05:00
|
|
|
textAnchor="middle"
|
|
|
|
|
x="0"
|
2026-03-11 15:33:23 -05:00
|
|
|
y={r + (breed ? 88 : (nodeDatum.attributes?.registration ? 70 : 52))}
|
|
|
|
|
style={{ fill: '#94a3b8', pointerEvents: 'none', textShadow: '0 1px 4px rgba(0,0,0,0.9)' }}
|
2026-03-09 00:40:56 -05:00
|
|
|
>
|
|
|
|
|
({nodeDatum.attributes.birth_year})
|
|
|
|
|
</text>
|
|
|
|
|
)}
|
|
|
|
|
</g>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="pedigree-tree-wrapper">
|
2026-03-09 22:44:46 -05:00
|
|
|
|
|
|
|
|
{/* Controls */}
|
2026-03-09 00:40:56 -05:00
|
|
|
<div className="pedigree-controls">
|
|
|
|
|
<div className="control-group">
|
2026-03-09 22:44:46 -05:00
|
|
|
<button onClick={handleZoomIn} className="control-btn" title="Zoom In">
|
|
|
|
|
<ZoomIn size={18} />
|
2026-03-09 00:40:56 -05:00
|
|
|
</button>
|
|
|
|
|
<button onClick={handleZoomOut} className="control-btn" title="Zoom Out">
|
2026-03-09 22:44:46 -05:00
|
|
|
<ZoomOut size={18} />
|
2026-03-09 00:40:56 -05:00
|
|
|
</button>
|
2026-03-09 22:44:46 -05:00
|
|
|
<button onClick={handleReset} className="control-btn" title="Reset View">
|
|
|
|
|
<Maximize2 size={18} />
|
2026-03-09 00:40:56 -05:00
|
|
|
</button>
|
|
|
|
|
</div>
|
2026-03-09 22:44:46 -05:00
|
|
|
|
2026-03-09 00:40:56 -05:00
|
|
|
{coi !== null && coi !== undefined && (
|
|
|
|
|
<div className="coi-display">
|
2026-03-09 22:44:46 -05:00
|
|
|
<span className="coi-label">COI</span>
|
2026-03-11 13:09:08 -05:00
|
|
|
<span className={`coi-value ${coi > 0.10 ? 'high' : coi > 0.05 ? 'medium' : 'low'}`}>
|
|
|
|
|
{(coi * 100).toFixed(2)}%
|
2026-03-09 00:40:56 -05:00
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-09 22:44:46 -05:00
|
|
|
{/* Legend */}
|
2026-03-09 00:40:56 -05:00
|
|
|
<div className="pedigree-legend">
|
|
|
|
|
<div className="legend-item">
|
2026-03-09 22:44:46 -05:00
|
|
|
<div className="legend-color male" />
|
|
|
|
|
<span>Sire</span>
|
2026-03-09 00:40:56 -05:00
|
|
|
</div>
|
|
|
|
|
<div className="legend-item">
|
2026-03-09 22:44:46 -05:00
|
|
|
<div className="legend-color female" />
|
|
|
|
|
<span>Dam</span>
|
2026-03-09 00:40:56 -05:00
|
|
|
</div>
|
2026-03-09 22:44:46 -05:00
|
|
|
<div className="legend-item">
|
|
|
|
|
<div style={{
|
|
|
|
|
width: 14, height: 14, borderRadius: '50%',
|
|
|
|
|
background: 'linear-gradient(135deg, #e0a84a, #9b3a10)',
|
|
|
|
|
boxShadow: '0 0 8px rgba(194,134,42,0.5)',
|
|
|
|
|
border: '2px solid rgba(255,255,255,0.15)'
|
|
|
|
|
}} />
|
|
|
|
|
<span>Subject</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Zoom indicator */}
|
|
|
|
|
<div className="zoom-indicator">
|
|
|
|
|
{Math.round(zoom * 100)}%
|
2026-03-09 00:40:56 -05:00
|
|
|
</div>
|
|
|
|
|
|
2026-03-09 22:44:46 -05:00
|
|
|
{/* Tree canvas */}
|
2026-03-09 00:40:56 -05:00
|
|
|
<div id="tree-container" className="tree-container">
|
|
|
|
|
{pedigreeData && dimensions.width > 0 && (
|
|
|
|
|
<Tree
|
|
|
|
|
data={pedigreeData}
|
|
|
|
|
translate={translate}
|
|
|
|
|
zoom={zoom}
|
2026-03-09 22:44:46 -05:00
|
|
|
onUpdate={({ zoom: z, translate: t }) => {
|
|
|
|
|
setZoom(z)
|
|
|
|
|
setTranslate(t)
|
2026-03-09 00:40:56 -05:00
|
|
|
}}
|
|
|
|
|
orientation="horizontal"
|
|
|
|
|
pathFunc="step"
|
2026-03-11 15:26:35 -05:00
|
|
|
separation={{ siblings: 1.8, nonSiblings: 2.4 }}
|
|
|
|
|
nodeSize={{ x: 280, y: 200 }}
|
2026-03-09 00:40:56 -05:00
|
|
|
renderCustomNodeElement={renderCustomNode}
|
|
|
|
|
enableLegacyTransitions
|
|
|
|
|
transitionDuration={300}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 22:44:46 -05:00
|
|
|
export default PedigreeTree
|