All files / modules/70-pipeline/components/Dashboards RangeSelector.tsx

80% Statements 12/15
20% Branches 1/5
75% Functions 3/4
80% Lines 12/15

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              3x 3x 3x 3x   3x                                 3x 1x 1x           3x                             3x             1x 1x                  
/*
 * 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, { useState } from 'react'
import { MenuItem } from '@blueprintjs/core'
import { SelectV2, SelectOption, Text, Container } from '@wings-software/uicore'
import styles from './RangeSelector.module.scss'
 
export const rangeOptions = [
  { label: 'Last 30 days', value: 30 },
  { label: 'Last 60 days', value: 60 },
  { label: 'Last 7 days', value: 7 }
]
 
export interface RangeSelectorProps {
  defaultOption?: SelectOption
  onRangeSelected?(range: number[]): void
  titleClsName?: string
}
 
export interface RangeSelectorWithTitleProps extends RangeSelectorProps {
  title: React.ReactNode
  tooltipId?: string
}
 
export default function RangeSelector({ defaultOption = rangeOptions[0], onRangeSelected }: RangeSelectorProps) {
  const [option, setOption] = useState(defaultOption)
  return (
    <SelectV2
      className={styles.rangeSelector}
      items={rangeOptions}
      filterable={false}
      itemRenderer={(item, { handleClick }) => {
        return <MenuItem text={item.label} onClick={handleClick} key={item.label} />
      }}
      onChange={opt => {
        setOption(opt)
        const now = Date.now()
        onRangeSelected?.([now - (opt.value as number) * 24 * 60 * 60 * 1000, now])
      }}
    >
      <Text font={{ size: 'xsmall' }} padding={{ top: 'xsmall', bottom: 'xsmall' }} rightIcon="chevron-down">
        {option.label}
      </Text>
    </SelectV2>
  )
}
 
export function RangeSelectorWithTitle({
  defaultOption,
  onRangeSelected,
  title,
  titleClsName,
  tooltipId
}: RangeSelectorWithTitleProps) {
  const containerCls = styles.titleAndFilter
  return (
    <Container className={containerCls}>
      <Text className={titleClsName} tooltipProps={{ dataTooltipId: tooltipId }}>
        {title}
      </Text>
      <RangeSelector defaultOption={defaultOption} onRangeSelected={onRangeSelected} />
    </Container>
  )
}