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

92.59% Statements 25/27
94.44% Branches 17/18
50% Functions 2/4
92.59% Lines 25/27

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              36x 36x 36x 36x 36x 36x 36x 36x   36x   36x                     49x 49x 49x   49x 46x       49x 1x                 48x 2x 46x 24x                 22x 2x                           20x     49x              
/*
 * 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 } from 'react'
import cx from 'classnames'
import { Container, Icon, StackTraceList, Text, PageError, NoDataCard } from '@wings-software/uicore'
import { isEmpty } from 'lodash-es'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { transformSampleData } from './utils'
import type { RecordsProps } from './types'
import css from './Records.module.scss'
 
export function Records(props: RecordsProps): JSX.Element {
  const {
    data,
    loading,
    error,
    fetchRecords,
    isQueryExecuted,
    query,
    queryNotExecutedMessage,
    recordsClassName,
    fetchEntityName
  } = props
  const { getString } = useStrings()
  let content: JSX.Element = <></>
 
  const { records } = useMemo(() => {
    return transformSampleData(data)
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [data])
 
  if (error) {
    content = (
      <PageError
        message={getErrorMessage(error)}
        disabled={isEmpty(query)}
        onClick={() => {
          fetchRecords()
        }}
      />
    )
  } else if (loading) {
    content = <Icon name="steps-spinner" size={32} color={Color.GREY_600} className={css.centerElement} />
  } else if (!isQueryExecuted) {
    content = (
      <Text
        icon="timeline-line-chart"
        className={cx(css.noQueryChart, css.centerElement)}
        iconProps={{ size: 50, intent: 'success' }}
      >
        {queryNotExecutedMessage || getString('cv.monitoringSources.gcoLogs.submitQueryToSeeRecords')}
      </Text>
    )
  } else if (!records?.length) {
    content = (
      <Container className={css.noRecords}>
        <NoDataCard
          icon="warning-sign"
          message={getString('cv.monitoringSources.gcoLogs.noRecordsForQuery')}
          onClick={() => {
            fetchRecords()
          }}
          buttonText={getString('retry')}
          buttonDisabled={isEmpty(query)}
        />
      </Container>
    )
  } else {
    content = <StackTraceList stackTraceList={records} className={css.recordContainer} />
  }
 
  return (
    <Container className={css.queryAndRecords}>
      <Text className={css.labelText}>{fetchEntityName ?? getString('cv.monitoringSources.gcoLogs.records')}</Text>
      <Container className={cx(css.chartContainer, recordsClassName)}>{content}</Container>
    </Container>
  )
}