All files / modules/85-cv/components/MultiItemsSideNav MultiItemsSideNav.tsx

97.62% Statements 41/42
67.86% Branches 19/28
91.67% Functions 11/12
97.5% Lines 39/40

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              31x 31x 31x 31x 31x   31x                                         31x                         92x 91x 91x     91x   91x 21x 21x 2x 2x 2x   2x       91x 24x         91x 53x       91x   91x                   1x 1x 1x 1x 1x 1x                 1x 1x               2x 5x 2x 2x 2x 2x 2x                          
/*
 * 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, { useEffect, useMemo, useState } from 'react'
import { Button, Container, Utils } from '@wings-software/uicore'
import { PopoverInteractionKind } from '@blueprintjs/core'
import { SelectedAppsSideNav } from './components/SelectedAppsSideNav/SelectedAppsSideNav'
import { getCreatedMetricLength, getSelectedMetricIndex } from './MultiItemsSideNav.utils'
import type { GroupedCreatedMetrics } from './components/SelectedAppsSideNav/components/GroupedSideNav/GroupedSideNav.types'
import css from './MultiItemsSideNav.module.scss'
 
export interface MultiItemsSideNavProps {
  onSelectMetric: (selectedMetric: string, updatedList: string[], selectedMetricIndex: number) => void
  onRemoveMetric: (
    removedMetric: string,
    newSelectedMetric: string,
    updatedList: string[],
    selectedMetricIndex: number
  ) => void
  isValidInput: boolean
  renamedMetric?: string
  createdMetrics?: string[]
  defaultSelectedMetric?: string
  defaultMetricName: string
  tooptipMessage: string
  addFieldLabel: string
  groupedCreatedMetrics?: GroupedCreatedMetrics
  shouldBeAbleToDeleteLastMetric?: boolean
}
 
export function MultiItemsSideNav(props: MultiItemsSideNavProps): JSX.Element {
  const {
    onSelectMetric,
    createdMetrics: propsCreatedMetrics,
    renamedMetric,
    onRemoveMetric,
    isValidInput,
    defaultSelectedMetric,
    defaultMetricName,
    tooptipMessage,
    addFieldLabel,
    groupedCreatedMetrics,
    shouldBeAbleToDeleteLastMetric
  } = props
  const [filter, setFilter] = useState<string | undefined>()
  const [createdMetrics, setCreatedMetrics] = useState<string[]>(
    propsCreatedMetrics?.length ? propsCreatedMetrics : [defaultMetricName]
  )
  const [selectedMetric, setSelectedMetric] = useState<string | undefined>(defaultSelectedMetric || createdMetrics[0])
 
  useEffect(() => {
    const selectedMetricIndex = getSelectedMetricIndex(createdMetrics, selectedMetric, renamedMetric)
    if (selectedMetricIndex > -1) {
      setCreatedMetrics(oldMetrics => {
        Eif (selectedMetricIndex !== -1) oldMetrics[selectedMetricIndex] = renamedMetric as string
        return Array.from(oldMetrics)
      })
      setSelectedMetric(renamedMetric)
    }
  }, [renamedMetric])
 
  const metricsToRender = useMemo(() => {
    return filter
      ? createdMetrics.filter(metric => metric.toLocaleLowerCase().includes(filter?.toLocaleLowerCase()))
      : createdMetrics
  }, [filter, createdMetrics])
 
  const createdMetricsLength = useMemo(
    () => getCreatedMetricLength(createdMetrics, groupedCreatedMetrics),
    [groupedCreatedMetrics, createdMetrics]
  )
 
  const hasOnRemove = shouldBeAbleToDeleteLastMetric ? shouldBeAbleToDeleteLastMetric : createdMetricsLength > 1
 
  return (
    <Container className={css.main}>
      <Button
        icon="plus"
        minimal
        intent="primary"
        disabled={!isValidInput}
        tooltip={!isValidInput ? tooptipMessage : undefined}
        tooltipProps={{ interactionKind: PopoverInteractionKind.HOVER_TARGET_ONLY }}
        onClick={() => {
          Eif (isValidInput) {
            setCreatedMetrics(oldMetrics => {
              const newMetricName = `${defaultMetricName} ${Utils.randomId()}`
              onSelectMetric(newMetricName, [newMetricName, ...oldMetrics], 0)
              setSelectedMetric(newMetricName)
              return [newMetricName, ...oldMetrics]
            })
          }
        }}
      >
        {addFieldLabel}
      </Button>
      <SelectedAppsSideNav
        onSelect={(newlySelectedMetric, index) => {
          onSelectMetric(newlySelectedMetric, createdMetrics, index)
          setSelectedMetric(newlySelectedMetric)
        }}
        selectedItem={selectedMetric}
        selectedApps={metricsToRender}
        groupedSelectedApps={groupedCreatedMetrics}
        onRemoveItem={
          hasOnRemove
            ? (removedItem, index) => {
                setCreatedMetrics(oldMetrics => {
                  const filteredOldMetrics = oldMetrics.filter(item => item !== removedItem)
                  const updateIndex = index === 0 ? 0 : index - 1
                  const updatedMetric = filteredOldMetrics[updateIndex]
                  setSelectedMetric(updatedMetric)
                  onRemoveMetric(removedItem, updatedMetric, [...filteredOldMetrics], updateIndex)
                  return [...filteredOldMetrics]
                })
              }
            : undefined
        }
        filterProps={{
          onFilter: setFilter,
          className: css.metricsFilter
        }}
      />
    </Container>
  )
}