All files / modules/75-ce/components/PersepectiveExplorerFilters PerspectiveExplorerFilters.tsx

87.5% Statements 21/24
75% Branches 3/4
50% Functions 3/6
86.96% Lines 20/23

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 160 161              1x 1x 1x 1x 1x 1x 1x           1x   1x 1x   1x 4x                           1x           4x 4x 4x                                                                                                                                               1x                   4x 4x                                                         1x  
/*
 * Copyright 2022 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 from 'react'
import { Container, Text, Button, Icon, FlexExpander } from '@wings-software/uicore'
import cx from 'classnames'
import { Menu, MenuItem, Popover, Position } from '@blueprintjs/core'
import { QlceViewTimeGroupType, QlceViewFilterInput } from 'services/ce/services'
import PerspectiveTimeRangePicker from '@ce/components/PerspectiveTimeRangePicker/PerspectiveTimeRangePicker'
import {
  CE_DATE_FORMAT_INTERNAL,
  DATE_RANGE_SHORTCUTS,
  getGMTEndDateTime,
  getGMTStartDateTime
} from '@ce/utils/momentUtils'
import { useStrings, UseStringsReturn } from 'framework/strings'
import type { setAggregationFn, setFiltersFn, setTimeRangeFn } from '@ce/types'
import ExplorerFilters from './ExplorerFilters'
import css from './PerspectiveExplorerFilters.module.scss'
 
const getAggregationText: (getString: UseStringsReturn['getString']) => Record<string, string> = getString => {
  return {
    [QlceViewTimeGroupType.Day]: getString('ce.perspectives.timeAggregation.daily'),
    [QlceViewTimeGroupType.Month]: getString('ce.perspectives.timeAggregation.monthly'),
    [QlceViewTimeGroupType.Hour]: getString('ce.perspectives.timeAggregation.hourly')
  }
}
 
interface TimeGranularityDropDownProps {
  aggregation: QlceViewTimeGroupType
  setAggregation: setAggregationFn
  showHourlyAggr?: boolean
  isHourlyEnabled?: boolean
}
 
export const TimeGranularityDropDown: React.FC<TimeGranularityDropDownProps> = ({
  aggregation,
  setAggregation,
  showHourlyAggr,
  isHourlyEnabled
}) => {
  const { getString } = useStrings()
  const aggregationTextMap = getAggregationText(getString)
  return (
    <Popover
      position={Position.BOTTOM_LEFT}
      modifiers={{
        arrow: { enabled: false },
        flip: { enabled: true },
        keepTogether: { enabled: true },
        preventOverflow: { enabled: true }
      }}
      hoverCloseDelay={0}
      transitionDuration={0}
      minimal={true}
      content={
        <Menu>
          <MenuItem
            active={aggregation === QlceViewTimeGroupType.Day}
            className={css.aggregationMenuItems}
            onClick={() => {
              setAggregation(QlceViewTimeGroupType.Day)
            }}
            text={aggregationTextMap[QlceViewTimeGroupType.Day]}
          />
          <MenuItem
            active={aggregation === QlceViewTimeGroupType.Month}
            className={css.aggregationMenuItems}
            onClick={() => {
              setAggregation(QlceViewTimeGroupType.Month)
            }}
            text={aggregationTextMap[QlceViewTimeGroupType.Month]}
          />
          {showHourlyAggr ? (
            <MenuItem
              disabled={!isHourlyEnabled}
              active={aggregation === QlceViewTimeGroupType.Hour}
              className={cx(css.aggregationMenuItems, { [css.disabled]: !isHourlyEnabled })}
              onClick={() => {
                setAggregation(QlceViewTimeGroupType.Hour)
              }}
              text={'Hourly'}
            />
          ) : null}
        </Menu>
      }
    >
      <Button
        intent="primary"
        minimal
        className={css.timeGranularityButton}
        text={aggregationTextMap[aggregation]}
        iconProps={{
          size: 16
        }}
        rightIcon="caret-down"
      />
    </Popover>
  )
}
 
interface PersepectiveExplorerFiltersProps {
  aggregation: QlceViewTimeGroupType
  setAggregation: setAggregationFn
  setTimeRange: setTimeRangeFn
  timeRange: {
    to: string
    from: string
  }
  setFilters: setFiltersFn
  filters: QlceViewFilterInput[]
  showHourlyAggr?: boolean
  featureEnabled?: boolean
}
 
const PersepectiveExplorerFilters: React.FC<PersepectiveExplorerFiltersProps> = ({
  aggregation,
  setAggregation,
  setTimeRange,
  setFilters,
  timeRange,
  filters,
  showHourlyAggr,
  featureEnabled
}) => {
  const last7DaysRange = DATE_RANGE_SHORTCUTS['LAST_7_DAYS']
  return (
    <Container
      background="white"
      padding="small"
      border={{
        bottom: true
      }}
    >
      <Container className={css.mainContainer}>
        <Icon name="ng-filter" size={20} />
        <ExplorerFilters timeRange={timeRange} filters={filters} setFilters={setFilters} />
        <FlexExpander />
        <PerspectiveTimeRangePicker timeRange={timeRange} setTimeRange={setTimeRange} featureEnabled={featureEnabled} />
        <Text color="primary7">|</Text>
        <TimeGranularityDropDown
          aggregation={aggregation}
          setAggregation={setAggregation}
          showHourlyAggr={showHourlyAggr}
          isHourlyEnabled={
            getGMTStartDateTime(timeRange.from) >=
              getGMTStartDateTime(last7DaysRange[0].format(CE_DATE_FORMAT_INTERNAL)) &&
            getGMTEndDateTime(timeRange.to) <= getGMTEndDateTime(last7DaysRange[1].format(CE_DATE_FORMAT_INTERNAL))
          }
        />
      </Container>
    </Container>
  )
}
 
export default PersepectiveExplorerFilters