All files / modules/75-ce/components/PerspectiveGrid PerspectiveGrid.tsx

75% Statements 30/40
44.19% Branches 19/43
55.56% Functions 5/9
74.36% Lines 29/39

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              7x 7x   7x   7x 7x 7x 7x 7x                                           7x                                 7x   7x 7x   7x 6x 3x   3x     7x 10x 6x 2x       7x 7x     7x               7x   7x                             7x   7x                                                           7x       7x  
/*
 * 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, { useMemo, useEffect, useState } from 'react'
import { Container, Icon } from '@wings-software/uicore'
import type { Column, Row } from 'react-table'
import { isEqual } from 'lodash-es'
import type { QlceViewFieldInputInput, QlceViewEntityStatsDataPoint, Maybe } from 'services/ce/services'
import ColumnSelector from './ColumnSelector'
import { addLegendColorToRow, GridData, getGridColumnsByGroupBy, DEFAULT_COLS } from './Columns'
import Grid from './Grid'
import './test.scss' // will find a alternative
import css from './PerspectiveGrid.module.scss'
 
export interface PerspectiveGridProps {
  columnSequence?: string[]
  setColumnSequence?: (cols: string[]) => void
  groupBy: QlceViewFieldInputInput
  showColumnSelector?: boolean
  tempGridColumns?: boolean
  gridData: Maybe<Maybe<QlceViewEntityStatsDataPoint>[]> | undefined
  gridFetching: boolean
  isClusterOnly?: boolean
  goToWorkloadDetails?: (clusterName: string, namespace: string, workloadName: string) => void
  highlightNode?: (id: string) => void
  resetNodeState?: () => void
  showPagination?: boolean
  totalItemCount?: number
  fetchData?: (pageIndex: number, pageSize: number) => void
  pageSize?: number
  gridPageIndex?: number
  goToNodeDetails?: (clusterName: string, nodeId: string) => void
}
 
const PerspectiveGrid: React.FC<PerspectiveGridProps> = props => {
  const {
    columnSequence,
    setColumnSequence,
    groupBy,
    showColumnSelector,
    gridData: response,
    gridFetching: fetching,
    isClusterOnly = false,
    goToWorkloadDetails,
    resetNodeState,
    highlightNode,
    totalItemCount,
    pageSize,
    gridPageIndex,
    fetchData,
    goToNodeDetails
  } = props
 
  const gridColumns = getGridColumnsByGroupBy(groupBy, isClusterOnly)
  const [selectedColumns, setSelectedColumns] = useState(gridColumns)
 
  const gridData = useMemo(() => {
    if (!fetching && response?.length) {
      return addLegendColorToRow(response as QlceViewEntityStatsDataPoint[])
    }
    return []
  }, [response, fetching])
 
  useEffect(() => {
    const newColumnSequence = gridData.slice(0, 12).map(row => row['id'])
    if (!isEqual(columnSequence, newColumnSequence) && setColumnSequence) {
      setColumnSequence(newColumnSequence as string[])
    }
  }, [gridData])
 
  useEffect(() => {
    setSelectedColumns(getGridColumnsByGroupBy(groupBy, isClusterOnly))
  }, [groupBy, isClusterOnly])
 
  Iif (fetching) {
    return (
      <Container className={css.gridLoadingContainer}>
        <Icon name="spinner" color="blue500" size={30} />
      </Container>
    )
  }
 
  const { fieldName } = groupBy
 
  const onRowClick = (row: Row<GridData>) => {
    if (fieldName === 'Workload Id' && isClusterOnly) {
      const { clusterName, namespace, workloadName } = row.original
      goToWorkloadDetails &&
        clusterName &&
        namespace &&
        workloadName &&
        goToWorkloadDetails(clusterName, namespace, workloadName)
    }
    if (fieldName === 'Node' && isClusterOnly) {
      const { clusterName, nodeId } = row.original as any
      goToNodeDetails && clusterName && nodeId && goToNodeDetails(clusterName, nodeId)
    }
  }
 
  const isRowClickable = fieldName === 'Workload Id' || fieldName === 'Node'
 
  return (
    <Container background="white">
      {showColumnSelector && (
        <ColumnSelector
          columns={gridColumns}
          selectedColumns={selectedColumns}
          onChange={columns => setSelectedColumns(columns)}
        />
      )}
      <Grid<GridData>
        data={gridData}
        onRowClick={onRowClick}
        onMouseEnter={row => {
          highlightNode && row.original.id && highlightNode(row.original.id)
        }}
        onMouseLeave={() => {
          resetNodeState && resetNodeState()
        }}
        columns={props.tempGridColumns ? (DEFAULT_COLS as Column<GridData>[]) : (selectedColumns as Column<GridData>[])}
        showPagination={props.showPagination}
        totalItemCount={totalItemCount}
        gridPageIndex={gridPageIndex}
        pageSize={pageSize}
        fetchData={fetchData}
        isRowClickable={isRowClickable}
      />
    </Container>
  )
}
 
PerspectiveGrid.defaultProps = {
  showColumnSelector: true
}
 
export default PerspectiveGrid