All files / modules/75-ce/components/PerspectiveBuilderFilters/views OperandSelector.tsx

91.67% Statements 33/36
58.57% Branches 41/70
72.73% Functions 8/11
91.18% Lines 31/34

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276              10x 10x 10x 10x 10x             10x 10x 10x   10x 10x   10x               10x         4x 4x   4x 16x     4x                                                                   16x                                                                   10x         1x     1x   1x                                 4x 1x               3x                                                                 1x                                                             10x             5x                                         5x   5x                                                             10x  
/*
 * 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 { Container, Layout, Icon, TextInput } from '@wings-software/uicore'
import { Popover, PopoverInteractionKind, Position } from '@blueprintjs/core'
import { Color } from '@harness/design-system'
import {
  QlceViewFieldIdentifierData,
  ViewFieldIdentifier,
  useFetchPerspectiveFiltersValueQuery,
  QlceViewFilterWrapperInput,
  QlceViewField
} from 'services/ce/services'
import CustomMenuItem from '@ce/components/CustomMenu/CustomMenuItem'
import { FIELD_TO_ICON_MAPPING } from '@ce/components/PerspectiveFilters/constants'
import { useStrings } from 'framework/strings'
import type { TimeRangeFilterType } from '@ce/types'
import { getTimeFilters } from '@ce/utils/perspectiveUtils'
import { getGMTEndDateTime, getGMTStartDateTime } from '@ce/utils/momentUtils'
import type { ProviderType } from '../PerspectiveBuilderFilter'
import css from '../PerspectiveBuilderFilter.module.scss'
 
interface LabelSelectorProps {
  service: QlceViewField
  labelData: any
  setProviderAndIdentifier: (providerData: ProviderType, serviceData: ProviderType) => void
}
 
const LabelSelector: (props: LabelSelectorProps) => JSX.Element = ({
  service,
  labelData,
  setProviderAndIdentifier
}) => {
  const { getString } = useStrings()
  const [searchText, setSearchText] = useState('')
 
  const filteredLabelData = (labelData || []).filter((label: string) =>
    label.toLocaleLowerCase().indexOf(searchText.toLocaleLowerCase()) < 0 ? false : true
  )
 
  return (
    <Popover
      key={service.fieldId}
      interactionKind={PopoverInteractionKind.HOVER}
      position={Position.RIGHT_TOP}
      modifiers={{
        flip: { boundariesElement: 'viewport', padding: 20 },
        offset: { offset: -5 },
        preventOverflow: { boundariesElement: 'viewport', padding: 20 }
      }}
      hoverCloseDelay={0}
      minimal={true}
      fill={true}
      usePortal={false}
      content={
        <Container color={Color.WHITE} className={css.groupByLabel}>
          <Container
            padding={{
              top: 'small',
              left: 'small',
              right: 'small'
            }}
          >
            <TextInput
              value={searchText}
              onChange={(e: any) => {
                setSearchText(e.target.value)
              }}
              placeholder={getString('ce.perspectives.createPerspective.filters.searchText')}
            />
          </Container>
          <Container className={css.labelValueContainer}>
            {filteredLabelData.length
              ? filteredLabelData.map((label: string) => (
                  <CustomMenuItem
                    hidePopoverOnClick={true}
                    key={label}
                    text={label}
                    onClick={() => {
                      setProviderAndIdentifier(
                        { id: 'LABEL', name: 'label' },
                        { id: 'labels.value', name: label || '' }
                      )
                    }}
                  />
                ))
              : null}
          </Container>
        </Container>
      }
    >
      <CustomMenuItem
        key={service?.fieldId}
        fontSize={'normal'}
        text={service?.fieldName || ''}
        rightIcon={'chevron-right'}
      />
    </Popover>
  )
}
 
interface PopoverContentProps {
  fieldValuesList: QlceViewFieldIdentifierData[]
  setProviderAndIdentifier: (providerData: ProviderType, serviceData: ProviderType) => void
  labelData: any
  labelFetching: boolean
}
 
export const OperandSelectorPopOverContent: React.FC<PopoverContentProps> = ({
  fieldValuesList,
  setProviderAndIdentifier,
  labelData
}) => {
  const nonCustomFields = fieldValuesList.filter(field => field.identifier !== ViewFieldIdentifier.Custom)
 
  const defaultPanelFields = (
    <Layout.Vertical>
      {nonCustomFields.map(field => {
        return (
          <Popover
            key={field.identifier}
            interactionKind={PopoverInteractionKind.HOVER}
            position={Position.RIGHT_TOP}
            hoverCloseDelay={0}
            modifiers={{
              flip: { boundariesElement: 'viewport', padding: 20 },
              offset: { offset: -5 },
              preventOverflow: { boundariesElement: 'viewport', padding: 20 }
            }}
            minimal={true}
            fill={true}
            usePortal={false}
            content={
              <Container>
                {field.values.map(service => {
                  if (service?.fieldId === 'label') {
                    return (
                      <LabelSelector
                        service={service}
                        labelData={labelData}
                        setProviderAndIdentifier={setProviderAndIdentifier}
                      />
                    )
                  }
                  return (
                    <CustomMenuItem
                      hidePopoverOnClick={true}
                      onClick={() => {
                        setProviderAndIdentifier(
                          {
                            id: field.identifier,
                            name: field.identifierName
                          },
                          { id: service?.fieldId || '', name: service?.fieldName || '' }
                        )
                      }}
                      key={service?.fieldId}
                      fontSize={'normal'}
                      text={service?.fieldName || ''}
                    />
                  )
                })}
              </Container>
            }
          >
            <CustomMenuItem
              text={field.identifierName}
              iconName={FIELD_TO_ICON_MAPPING[field.identifier]}
              fontSize={'normal'}
              rightIcon={'chevron-right'}
            ></CustomMenuItem>
          </Popover>
        )
      })}
    </Layout.Vertical>
  )
 
  return (
    <Container>
      {defaultPanelFields}
      {/* <Tabs id={'horizontalTabs'} defaultSelectedTabId={'defaultFields'} className={css.tabsContainer}>
        <Tab
          className={css.tabClass}
          panelClassName={css.panelClass}
          id={'defaultFields'}
          title={'Default fields'}
          panel={defaultPanelFields}
        />
        <Tab
          className={css.tabClass}
          panelClassName={css.panelClass}
          id={'customField'}
          title={'Custom Field'}
          panel={<Container>Blah</Container>}
        />
      </Tabs> */}
    </Container>
  )
}
 
interface OperandSelectorProps {
  fieldValuesList: QlceViewFieldIdentifierData[]
  provider: ProviderType | null | undefined
  service: ProviderType | null | undefined
  setProviderAndIdentifier: (providerData: ProviderType, serviceData: ProviderType) => void
  timeRange: TimeRangeFilterType
}
 
const OperandSelector: React.FC<OperandSelectorProps> = ({
  service,
  provider,
  fieldValuesList,
  setProviderAndIdentifier,
  timeRange
}) => {
  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: 1000
    }
  })
 
  const { data: labelResData, fetching: labelFetching } = labelResult
 
  return (
    <Popover
      className={css.operandContainer}
      interactionKind={PopoverInteractionKind.CLICK}
      position={Position.BOTTOM_LEFT}
      modifiers={{
        arrow: { enabled: false },
        flip: { enabled: true },
        keepTogether: { enabled: true },
        preventOverflow: { enabled: true }
      }}
      hoverCloseDelay={0}
      fill={true}
      usePortal={true}
      content={
        <OperandSelectorPopOverContent
          labelData={labelResData?.perspectiveFilters?.values || []}
          labelFetching={labelFetching}
          fieldValuesList={fieldValuesList}
          setProviderAndIdentifier={setProviderAndIdentifier}
        />
      }
    >
      <div className={css.operandSelectorContainer}>
        {provider?.id && service?.id ? `${provider.name || provider.id} > ${service.name}` : 'Choose Operand'}
        <Icon name="caret-down" />
      </div>
    </Popover>
  )
}
 
export default OperandSelector