All files / modules/85-cv/components/Timeline TimeLine.tsx

70.73% Statements 29/41
62.07% Branches 36/58
63.64% Functions 7/11
70% Lines 28/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159              9x 9x 9x             9x       9x 9x   9x   9x   9x                           30x 30x   30x           30x 18x         30x   10x             30x   19x       19x               30x 10x     30x   11x       30x         30x                         30x 30x                     30x                                                                              
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { Container } from '@wings-software/uicore'
import {
  calculateStartAndEndTimes,
  getDimensionsAsPerContainerWidth,
  getTimeFormat,
  getTimestampsForPeriodWithoutRiskData,
  limitMaxSliderWidth
} from '@cv/pages/monitored-service/components/ServiceHealth/ServiceHealth.utils'
import {
  DEFAULT_MAX_SLIDER_WIDTH,
  DEFAULT_MIN_SLIDER_WIDTH
} from '@cv/pages/monitored-service/components/ServiceHealth/ServiceHealth.constants'
import AnomaliesCard from '@cv/pages/monitored-service/components/ServiceHealth/components/AnomaliesCard/AnomaliesCard'
import ChangeTimeline from '../ChangeTimeline/ChangeTimeline'
import type { ChangesInfoCardData } from '../ChangeTimeline/ChangeTimeline.types'
import TimelineSlider from '../ChangeTimeline/components/TimelineSlider/TimelineSlider'
import type { TimelineProps } from './TimeLine.types'
import css from '../../pages/monitored-service/components/ServiceHealth/ServiceHealth.module.scss'
 
export const TimeLine = ({
  serviceIdentifier,
  environmentIdentifier,
  selectedTimePeriod,
  timestamps,
  setTimestamps,
  timeRange,
  setTimeRange,
  showTimelineSlider,
  setShowTimelineSlider,
  changeCategories,
  changeSourceTypes,
  resetFilter
}: TimelineProps): JSX.Element => {
  const [changeTimelineSummary, setChangeTimelineSummary] = useState<ChangesInfoCardData[] | null>(null)
  const containerRef = useRef<HTMLElement>(null)
 
  const resetSlider = useCallback(() => {
    setTimeRange({ startTime: 0, endTime: 0 })
    setShowTimelineSlider(false)
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
 
  useEffect(() => {
    Iif (showTimelineSlider) {
      setShowTimelineSlider(false)
    }
  }, [resetFilter])
 
  useEffect(() => {
    //changing timeperiod in dropdown should reset the timerange and remove the slider.
    Iif (showTimelineSlider) {
      resetSlider()
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [selectedTimePeriod?.value])
 
  // calculating the min and max width for the the timeline slider
  const sliderDimensions = useMemo(() => {
    // This is temporary change , will be removed once BE fix is done.
    const defaultMaxSliderWidth = limitMaxSliderWidth(selectedTimePeriod?.value as string)
      ? DEFAULT_MIN_SLIDER_WIDTH
      : DEFAULT_MAX_SLIDER_WIDTH
 
    return getDimensionsAsPerContainerWidth(
      defaultMaxSliderWidth,
      selectedTimePeriod,
      containerRef?.current?.offsetWidth
    )
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [containerRef?.current, selectedTimePeriod?.value])
 
  const timeFormat = useMemo(() => {
    return getTimeFormat(selectedTimePeriod?.value as string)
  }, [selectedTimePeriod?.value])
 
  useEffect(() => {
    // getTimestampsForPeriod(healthScoreData)
    setTimestamps(getTimestampsForPeriodWithoutRiskData(selectedTimePeriod.value as string))
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [selectedTimePeriod])
 
  const onFocusTimeRange = useCallback((startTime: number, endTime: number) => {
    setTimeRange({ startTime, endTime })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
 
  const onSliderDragEnd = useCallback(
    ({ startXPercentage, endXPercentage }) => {
      if (!showTimelineSlider) {
        setShowTimelineSlider(true)
      }
      const startAndEndtime = calculateStartAndEndTimes(startXPercentage, endXPercentage, timestamps)
      if (startAndEndtime) {
        onFocusTimeRange?.(startAndEndtime[0], startAndEndtime[1])
      }
    },
    [onFocusTimeRange, timestamps]
  )
 
  const renderInfoCard = useCallback(() => {
    return (
      <AnomaliesCard
        changeTimelineSummary={changeTimelineSummary || []}
        timeFormat={timeFormat}
        timeRange={timeRange}
        monitoredServiceIdentifier={''}
      />
    )
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [timeFormat, changeTimelineSummary, timeRange])
 
  return (
    <>
      <Container
        onClick={() => {
          if (!showTimelineSlider) {
            setShowTimelineSlider(true)
          }
        }}
        height={150}
        className={css.main}
        data-testid={'TimelineChartContainer'}
        ref={containerRef}
      >
        <TimelineSlider
          resetFocus={resetSlider}
          initialSliderWidth={sliderDimensions.minWidth}
          leftContainerOffset={90}
          hideSlider={!showTimelineSlider}
          className={css.slider}
          minSliderWidth={sliderDimensions.minWidth}
          maxSliderWidth={sliderDimensions.maxWidth}
          infoCard={renderInfoCard()}
          onSliderDragEnd={onSliderDragEnd}
        />
        <ChangeTimeline
          serviceIdentifier={serviceIdentifier}
          environmentIdentifier={environmentIdentifier}
          timeFormat={timeFormat}
          startTime={timeRange?.startTime as number}
          endTime={timeRange?.endTime as number}
          selectedTimePeriod={selectedTimePeriod}
          changeCategories={changeCategories}
          changeSourceTypes={changeSourceTypes}
          onSliderMoved={setChangeTimelineSummary}
        />
      </Container>
    </>
  )
}