import React, { useRef, useMemo, useEffect, useContext } from 'react'; import { v1 as uuidv1 } from 'uuid'; import { ClipPath } from '../../clippath'; import { xtransform, groupFeatures } from '../../../utils/coordinates'; import { renderTranscript, sortedTranscripts } from './utils'; import { bestFontSize } from '../wrapped/margin'; import { PackTranscriptTrackProps, TranscriptRow, Transcript } from './types'; import { TooltipContext } from "../../tooltip/tooltipcontext"; import { svgPoint } from '../../../utils/svg'; /** * Renders a transcript track in pack mode. Transcripts are packed to occupy the minimum * number of rows possible without overlap. */ export const PackTranscriptTrack: React.FC = props => { const tooltipContext = useContext(TooltipContext); const mouseOver = (event: React.MouseEvent, transcript: Transcript) => { if(props.svgRef) { let mousePosition = svgPoint(props.svgRef.current!,event) tooltipContext.setTooltip({ show:true, x: mousePosition[0] > props.width - 150 ? (mousePosition[0] - 150) +5 : mousePosition[0]+5, y: mousePosition[1], width: 150, height: 50, content: {transcript.id} }); } }; const mouseOut = () => { tooltipContext.setTooltip({ show: false, x: 0, y: 0, content: undefined, width:0, height: 0 }); }; const uuid = useRef(uuidv1()); const x = xtransform(props.domain, props.width); const color = props.color || "#000000"; const fontSize = bestFontSize(props.rowHeight) * 1.25; const rendered: TranscriptRow[] = useMemo( () => groupFeatures(sortedTranscripts(props.data || [], props.domain), x, fontSize).map( (group, i) => ({ y: i * props.rowHeight, transcripts: group.map( transcript => renderTranscript(transcript, x, props.domain, props.rowHeight, props.width) ) }) ), [ props.data, props.rowHeight, props.width, props.domain ] ); const height = useMemo( () => props.rowHeight * rendered.length, [ props.rowHeight, rendered.length ] ); useEffect( () => { props.onHeightChanged && props.onHeightChanged(height); }, [ height ]); return ( { rendered.map( (group, k) => ( { group.transcripts.map( (transcript, j) => ( )=> mouseOver(e, transcript.transcript)} onMouseOut={mouseOut} onClick={ () => props.onTranscriptClick && props.onTranscriptClick(transcript.transcript) } /> {transcript.transcript.name} )) } )) } ); }; export default PackTranscriptTrack;