All files / modules/75-ce/components/PerspectiveBuilderPreview/GroupByView GroupByView.tsx

96.88% Statements 31/32
80.65% Branches 25/31
100% Functions 5/5
96.88% Lines 31/32

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196              4x 4x 4x 4x 4x 4x 4x 4x             4x   4x     4x 4x 4x                     4x               8x   8x   8x   8x         8x   8x                                         8x   8x   8x     8x     24x 24x                     8x                                                                               1x               1x               1x                                                                     4x  
/*
 * 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, { useState } from 'react'
import { useParams } from 'react-router-dom'
import { Container, Text, Layout, FlexExpander, Icon, Button } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import cx from 'classnames'
import { Menu, MenuItem, Popover, Position, Drawer } from '@blueprintjs/core'
import { useStrings } from 'framework/strings'
import {
  useFetchViewFieldsQuery,
  QlceViewFilterWrapperInput,
  useFetchPerspectiveFiltersValueQuery,
  QlceViewFieldInputInput,
  ViewChartType
} from 'services/ce/services'
import { getTimeFilters } from '@ce/utils/perspectiveUtils'
 
import { getGMTEndDateTime, getGMTStartDateTime } from '@ce/utils/momentUtils'
import type { TimeRangeFilterType } from '@ce/types'
 
import BusinessMappingBuilder from '@ce/components/BusinessMappingBuilder/BusinessMappingBuilder'
import GroupByViewSubMenu from './GroupByViewSubMenu'
import css from '../PerspectiveBuilderPreview.module.scss'
 
interface GroupByViewProps {
  groupBy: QlceViewFieldInputInput
  setGroupBy: (groupBy: QlceViewFieldInputInput) => void
  chartType: ViewChartType
  setChartType: (type: ViewChartType) => void
  timeRange: TimeRangeFilterType
  showBusinessMappingButton?: boolean
}
 
const GroupByView: React.FC<GroupByViewProps> = ({
  groupBy,
  setGroupBy,
  chartType,
  setChartType,
  timeRange,
  showBusinessMappingButton
}) => {
  const [drawerOpen, setDrawerOpen] = useState<boolean>(false)
 
  const { perspectiveId } = useParams<{ perspectiveId: string }>()
 
  const { getString } = useStrings()
 
  const [result, refetch] = useFetchViewFieldsQuery({
    variables: {
      filters: [{ viewMetadataFilter: { viewId: perspectiveId, isPreview: true } } as QlceViewFilterWrapperInput]
    }
  })
  const { data, fetching } = result
 
  const [labelResult] = useFetchPerspectiveFiltersValueQuery({
    variables: {
      filters: [
        ...getTimeFilters(getGMTStartDateTime(timeRange.from), getGMTEndDateTime(timeRange.to)),
        {
          idFilter: {
            field: {
              fieldId: 'labels.key',
              fieldName: '',
              identifier: 'LABEL'
            },
            operator: 'IN',
            values: []
          }
        } as unknown as QlceViewFilterWrapperInput
      ],
      offset: 0,
      limit: 100
    }
  })
 
  const { data: labelResData } = labelResult
 
  const labelData = labelResData?.perspectiveFilters?.values
 
  const fieldIdentifierData = data?.perspectiveFields?.fieldIdentifierData
 
  const PopoverContent =
    fieldIdentifierData && fieldIdentifierData.length ? (
      <Menu>
        {fieldIdentifierData.map(field => {
          Eif (field) {
            return (
              <MenuItem className={css.menuItem} key={field.identifier} text={field.identifierName}>
                <GroupByViewSubMenu setGroupBy={setGroupBy} labelData={labelData || []} field={field} />
              </MenuItem>
            )
          }
          return null
        })}
      </Menu>
    ) : /* istanbul ignore next */ undefined
 
  return (
    <Layout.Horizontal
      spacing="small"
      style={{
        alignItems: 'center'
      }}
    >
      <Text font={{ variation: FontVariation.SMALL }} color={Color.GREY_400}>
        {getString('ce.perspectives.createPerspective.preview.groupBy')}
      </Text>
      <Popover
        disabled={fetching}
        position={Position.BOTTOM_LEFT}
        modifiers={{
          arrow: { enabled: false },
          flip: { enabled: true },
          keepTogether: { enabled: true },
          preventOverflow: { enabled: true }
        }}
        content={PopoverContent}
      >
        <Container
          background={Color.GREY_100}
          className={cx(css.groupBySelect, { [css.groupBySelectLoading]: fetching })}
        >
          {fetching ? (
            <Icon name="spinner" />
          ) : (
            <Text font={{ variation: FontVariation.SMALL }} background={Color.GREY_100} rightIcon="chevron-down">
              {groupBy.fieldName}
            </Text>
          )}
        </Container>
      </Popover>
      {showBusinessMappingButton ? (
        <Button
          icon="plus"
          intent="primary"
          minimal
          text={getString('ce.businessMapping.newButton')}
          onClick={() => setDrawerOpen(true)}
        />
      ) : null}
      <FlexExpander />
      <Icon
        name="timeline-bar-chart"
        size={18}
        onClick={() => {
          setChartType(ViewChartType.StackedTimeSeries)
        }}
        color={chartType === ViewChartType.StackedTimeSeries ? Color.PRIMARY_6 : Color.GREY_500}
      />
      <Icon
        name="timeline-area-chart"
        size={20}
        onClick={() => {
          setChartType(ViewChartType.StackedLineChart)
        }}
        color={chartType === ViewChartType.StackedLineChart ? Color.PRIMARY_6 : Color.GREY_500}
      />
      <Drawer
        autoFocus
        enforceFocus
        hasBackdrop
        usePortal
        canOutsideClickClose
        canEscapeKeyClose
        position={Position.RIGHT}
        isOpen={drawerOpen}
        onClose={
          /* istanbul ignore next */ () => {
            setDrawerOpen(false)
          }
        }
      >
        <BusinessMappingBuilder
          selectedBM={{}}
          onSave={
            /* istanbul ignore next */ () => {
              refetch({
                requestPolicy: 'cache-and-network'
              })
              setDrawerOpen(false)
            }
          }
        />
      </Drawer>
    </Layout.Horizontal>
  )
}
 
export default GroupByView