diff --git a/packages/synapse-react-client/src/components/TimelinePlot/TimelinePlot.tsx b/packages/synapse-react-client/src/components/TimelinePlot/TimelinePlot.tsx index 3f87bd3e79..68c766383c 100644 --- a/packages/synapse-react-client/src/components/TimelinePlot/TimelinePlot.tsx +++ b/packages/synapse-react-client/src/components/TimelinePlot/TimelinePlot.tsx @@ -1,4 +1,4 @@ -import React, { createRef, useEffect, useState } from 'react' +import React, { createRef, useMemo, useState } from 'react' import { useGetFullTableQueryResults } from '../../synapse-queries' import { BUNDLE_MASK_QUERY_RESULTS } from '../../utils/SynapseConstants' import hardcodedPhasesQueryResponseData, { @@ -8,7 +8,6 @@ import hardcodedPhasesQueryResponseData, { import TimelinePhase from './TimelinePhase' import getColorPalette from '../ColorGradient/ColorGradient' import { Box } from '@mui/system' -import { Row } from '@sage-bionetworks/synapse-types' import { ObservationCardSchema } from '../row_renderers/ObservationCard' import { SQLOperator, @@ -53,7 +52,6 @@ export const TimelinePlot = ({ const [species, setSpecies] = useState( defaultSpecies, ) - const [phaseData, setPhaseData] = useState([]) const plotContainerRef = createRef() const dimensions = useRefDimensions(plotContainerRef) const queryFilters = @@ -96,7 +94,7 @@ export const TimelinePlot = ({ const { data: eventsData, isLoading } = eventTableQuery // filter the phases query response data to the specific species - useEffect(() => { + const phaseData = useMemo(() => { if (species) { const phasesForTargetSpecies = hardcodedPhasesQueryResponseData.queryResult?.queryResults.rows.filter( @@ -104,8 +102,8 @@ export const TimelinePlot = ({ return row.values[phaseSpeciesIndex] == species }, ) - setPhaseData(phasesForTargetSpecies) - } + return phasesForTargetSpecies + } else return undefined }, [species]) if (isLoading) { @@ -155,12 +153,9 @@ export const TimelinePlot = ({ doi: observationDoiIndex, } - if (!phaseData) { - return <> - } - - const widthPx = dimensions.width ? dimensions.width / phaseData.length : 0 - const gridTemplateColumns = phaseData.map(() => 'auto').join(' ') + const widthPx = + dimensions.width && phaseData ? dimensions.width / phaseData.length : 0 + const gridTemplateColumns = phaseData?.map(() => 'auto').join(' ') return ( <> @@ -188,23 +183,25 @@ export const TimelinePlot = ({ )} {/* Legend */} - - {phaseData.map((phaseRow, index) => { - const { colorPalette } = getColorPalette(index, 1) - return ( - - ) - })} - + {phaseData && ( + + {phaseData.map((phaseRow, index) => { + const { colorPalette } = getColorPalette(index, 1) + return ( + + ) + })} + + )} {/* Phase plots */} - {species && ( + {species && phaseData && (
@@ -50,7 +49,6 @@ export const TimelinePlotSpeciesSelector = ({