All files / modules/75-cd/components/DashboardList DashboardList.tsx

81.25% Statements 39/48
39.39% Branches 13/33
54.55% Functions 6/11
82.61% Lines 38/46

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              3x 3x   3x 3x 3x   3x 3x 3x 3x 3x   3x                         3x 6x 6x 6x                         3x 3x 3x                     3x 6x 6x                     3x                   6x   6x 6x 6x   6x   6x 3x 3x     6x       6x 6x             6x     6x 5x                 1x                                   6x                              
/*
 * 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, { useCallback, useContext, useEffect, useState } from 'react'
import { ExpandingSearchInput, Layout, Text, PageError, TableV2 } from '@wings-software/uicore'
import type { TableProps } from '@harness/uicore'
import { Color } from '@harness/design-system'
import { noop } from 'lodash-es'
import { useStrings } from 'framework/strings'
// import FilterSelector from '@common/components/Filter/FilterSelector/FilterSelector'
import { PageSpinner } from '@common/components'
import ServiceDetailsEmptyState from '@cd/icons/ServiceDetailsEmptyState.svg'
import { TimeRangeSelector } from '@common/components/TimeRangeSelector/TimeRangeSelector'
import { DeploymentsTimeRangeContext } from '../Services/common'
import css from '@cd/components/DashboardList/DashboardList.module.scss'
 
const PAGE_SIZE = 10
 
export interface DashboardListProps<T extends Record<string, any>> {
  HeaderCustomPrimary?: (props: { total: number }) => React.ReactElement
  HeaderCustomSecondary?: (props: { onChange: (val: string) => void }) => React.ReactElement
  columns: TableProps<T>['columns']
  loading: boolean
  error: boolean
  refetch: () => void
  data: T[]
  onRowClick: (data: T) => void
}
 
const HeaderFilterComponent: React.FC<{ onChange: (val: string) => void }> = props => {
  const { getString } = useStrings()
  const { onChange = noop } = props
  return (
    <Layout.Horizontal margin={{ left: 'small' }}>
      <ExpandingSearchInput flip width={232} placeholder={getString('search')} throttle={200} onChange={onChange} />
      {/* <FilterSelector<any>
        onFilterBtnClick={noop}
        onFilterSelect={noop}
        fieldToLabelMapping={new Map<string, string>()}
        filterWithValidFields={{}}
      /> */}
    </Layout.Horizontal>
  )
}
 
const applySearch = (items: any[], searchTerm: string): any[] => {
  Eif (!searchTerm) {
    return items
  }
  return items.filter(item => {
    const term = searchTerm.toLocaleLowerCase()
    return (
      (item?.id || '').toLocaleLowerCase().indexOf(term) !== -1 ||
      (item?.name || '').toLocaleLowerCase().indexOf(term) !== -1
    )
  })
}
 
const DeploymentTimeRangeSelector: React.FC = () => {
  const { timeRange, setTimeRange } = useContext(DeploymentsTimeRangeContext)
  return (
    <Layout.Horizontal className={css.timeRangeSelector} width="25%" flex={{ alignItems: 'center' }}>
      <div className={css.separator} />
      <Layout.Horizontal padding={{ left: 'small', right: 'small' }}>
        <TimeRangeSelector timeRange={timeRange?.range} setTimeRange={setTimeRange} minimal />
      </Layout.Horizontal>
      <div className={css.separator} />
    </Layout.Horizontal>
  )
}
 
export const DashboardList = <T extends Record<string, any>>(props: DashboardListProps<T>): React.ReactElement => {
  const {
    HeaderCustomPrimary = () => <></>,
    HeaderCustomSecondary = HeaderFilterComponent,
    columns,
    loading,
    error,
    refetch,
    data,
    onRowClick
  } = props
 
  const [pageIndex, setPageIndex] = useState(0)
  const [filteredData, setFilteredData] = useState<T[]>([])
  const [searchTerm, setSearchTerm] = useState('')
 
  const { getString } = useStrings()
 
  useEffect(() => {
    setPageIndex(0)
    setFilteredData(applySearch(data, searchTerm))
  }, [data, searchTerm])
 
  const onSearchChange = useCallback((val: string): void => {
    setSearchTerm(val)
  }, [])
 
  const getComponent = (): React.ReactElement => {
    Iif (loading) {
      return (
        <Layout.Horizontal className={css.loader}>
          <PageSpinner />
        </Layout.Horizontal>
      )
    }
    Iif (error) {
      return <PageError onClick={() => refetch()} />
    }
    if (!filteredData?.length) {
      return (
        <Layout.Vertical height="80%" flex={{ align: 'center-center' }} data-test="deploymentsWidgetEmpty">
          <img width="150" height="100" src={ServiceDetailsEmptyState} style={{ alignSelf: 'center' }} />
          <Text color={Color.GREY_400} margin={{ top: 'medium' }}>
            {getString('cd.serviceDashboard.noServiceDetails')}
          </Text>
        </Layout.Vertical>
      )
    }
    return (
      <TableV2<T>
        columns={columns}
        data={filteredData.slice(PAGE_SIZE * pageIndex, PAGE_SIZE * (pageIndex + 1))}
        pagination={{
          itemCount: filteredData.length,
          pageSize: PAGE_SIZE,
          pageCount: Math.ceil(filteredData.length / PAGE_SIZE),
          pageIndex: pageIndex,
          gotoPage: pageNum => {
            setPageIndex(pageNum)
          }
        }}
        onRowClick={onRowClick}
      />
    )
  }
 
  return (
    <Layout.Vertical className={css.container}>
      <Layout.Horizontal
        flex={{ distribution: 'space-between' }}
        padding={{ top: 'medium', bottom: 'medium' }}
        className={css.listHeader}
      >
        <HeaderCustomPrimary total={filteredData.length} />
        <HeaderCustomSecondary onChange={onSearchChange} />
        <DeploymentTimeRangeSelector />
      </Layout.Horizontal>
      {getComponent()}
    </Layout.Vertical>
  )
}