All files / modules/75-ce/components/PerspectiveFilters FilterPill.tsx

37.84% Statements 14/37
0% Branches 0/76
0% Functions 0/12
38.24% Lines 13/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              1x 1x 1x 1x             1x 1x 1x   1x   1x                   1x                                           1x                                               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 cx from 'classnames'
import { Icon, Layout, Text } from '@wings-software/uicore'
import { Popover, Position, PopoverInteractionKind } from '@blueprintjs/core'
import type {
  QlceViewFieldIdentifierData,
  QlceViewFilterOperator,
  QlceViewFilterInput,
  Maybe
} from 'services/ce/services'
import ProviderSelector from './views/ProviderSelector'
import ServiceSelector from './views/ServiceSelector'
import ValueSelector from './views/ValueSelector'
 
import css from './FilterPill.module.scss'
 
const ValueRendererPopUp: React.FC<{ valueList: string[] }> = ({ valueList }) => {
  return (
    <ul className="perspective-list-item">
      {valueList.map(value => (
        <li key={value}>{value}</li>
      ))}
    </ul>
  )
}
 
const ValueRenderer: React.FC<{ valueList: string[] }> = ({ valueList }) => {
  return (
    <Popover
      interactionKind={PopoverInteractionKind.HOVER}
      position={Position.RIGHT_TOP}
      modifiers={{
        arrow: { enabled: true },
        flip: { enabled: true },
        keepTogether: { enabled: true },
        preventOverflow: { enabled: true }
      }}
      usePortal={true}
      content={<ValueRendererPopUp valueList={valueList} />}
    >
      <div className={cx(css.valueListContainer, { [css.hideCount]: valueList.length <= 1 })}>
        <div className={css.firstValue}>{valueList[0]}</div>
        {valueList.length > 1 && <div className={css.count}>{`(+${valueList.length - 1})`}</div>}
      </div>
    </Popover>
  )
}
 
export const isFilterLabelOrTag: (filterType: string, fieldId: string) => boolean = (
  filterType: string,
  fieldId: string
) => {
  return filterType === 'LABEL' && fieldId === 'labels.key'
}
 
export type ProviderType = {
  id: string
  name: string
}
 
interface FilterPillProps {
  id: number
  removePill: () => void
  onChange: (id: number, data: QlceViewFilterInput) => void
  pillData: QlceViewFilterInput
  fieldValuesList: Maybe<QlceViewFieldIdentifierData>[]
  timeRange: {
    to: string
    from: string
  }
}
 
const FilterPill: React.FC<FilterPillProps> = ({ fieldValuesList, removePill, id, onChange, pillData, timeRange }) => {
  const { fieldName, identifierName, identifier, fieldId } = pillData.field
 
  const valuesObject: Record<string, boolean> = {}
  pillData?.values &&
    pillData?.values.forEach(val => {
      if (val) {
        valuesObject[val] = true
      }
    })
 
  const [provider, setProvider] = useState<ProviderType | null>({ id: identifier, name: identifierName || '' })
  const [service, setService] = useState<ProviderType | null>({ id: fieldId, name: fieldName })
  const [values, setValues] = useState<Record<string, boolean>>(valuesObject)
  const [operator, setOperator] = useState<QlceViewFilterOperator>(pillData.operator)
  const [showError, setShowError] = useState(false)
 
  const valueList = Object.keys(values).filter(val => values[val])
 
  const filteredFieldValuesList = fieldValuesList.filter(fieldValue => fieldValue) as QlceViewFieldIdentifierData[]
 
  return (
    <section className={css.filterPillContainer}>
      <Layout.Horizontal
        background="primary2"
        spacing="xsmall"
        padding="xsmall"
        className={cx(css.filterPillInnerContainer, { [css.errorPill]: showError })}
        style={{
          alignItems: 'center'
        }}
      >
        {provider?.id ? <Text font="small">{`${provider.name}:`}</Text> : null}
        {service?.id ? <Text font="small">{service.name}</Text> : null}
        {service?.id && operator ? (
          <Text color="primary7" font="small">
            {operator}
          </Text>
        ) : null}
        <Popover
          interactionKind={PopoverInteractionKind.CLICK}
          position={Position.BOTTOM_LEFT}
          defaultIsOpen={valueList.length === 0}
          modifiers={{
            arrow: { enabled: false },
            flip: { enabled: true },
            keepTogether: { enabled: true },
            preventOverflow: { enabled: true }
          }}
          usePortal={true}
          onClose={() =>
            onChange(id, {
              values: Object.keys(values).filter(val => values[val]),
              operator: operator,
              field: {
                fieldId: service?.id || '',
                fieldName: service?.name || '',
                identifier: provider?.id || ('' as any),
                identifierName: provider?.name || ''
              }
            })
          }
          onClosing={() => {
            setShowError(!valueList.length)
          }}
          onOpening={() => {
            setShowError(false)
          }}
          content={
            provider?.id ? (
              service?.id ? (
                <ValueSelector
                  timeRange={timeRange}
                  service={service}
                  provider={provider}
                  setService={setService}
                  values={values}
                  setOperator={setOperator}
                  setValues={setValues}
                  operator={operator}
                  isLabelOrTag={isFilterLabelOrTag(provider.id, service.id)}
                />
              ) : (
                <ServiceSelector setService={setService} provider={provider} fieldValueList={filteredFieldValuesList} />
              )
            ) : (
              <ProviderSelector
                setProvider={setProvider}
                setService={setService}
                fieldValueList={filteredFieldValuesList}
              />
            )
          }
        >
          {valueList.length ? (
            <ValueRenderer valueList={valueList} />
          ) : (
            <div className={css.placeholderContainer}></div>
          )}
        </Popover>
        <Icon name="cross" color={'blue500'} size={12} onClick={removePill} />
      </Layout.Horizontal>
    </section>
  )
}
 
export default FilterPill