All files / modules/85-cv/hooks/useLogContentHook/views/ExternalAPICall ExternalAPICallContent.tsx

97.3% Statements 36/37
91.04% Branches 61/67
100% Functions 7/7
96.97% Lines 32/33

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              18x 18x 18x                       18x 18x   18x 18x 18x     18x   18x 4x   4x                                       18x                 19x   19x     19x   19x 2x     17x 2x     15x 2x     13x       13x 13x   13x 13x   13x   13x 13x             13x                                                                                                                                       18x  
/*
 * Copyright 2022 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 from 'react'
import cx from 'classnames'
import {
  Layout,
  Text,
  FontVariation,
  Collapse,
  Container,
  Heading,
  NoDataCard,
  Pagination,
  PageSpinner,
  PageError
} from '@harness/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { ApiCallLogDTO } from 'services/cv'
import { CopyText } from '@common/components/CopyText/CopyText'
import noLogsDataImage from '@cv/assets/genericEmptyState.svg'
import { formatDate, getStatusColor } from '../../useLogContentHook.utils'
import type { ExecutionAndAPICallLogProps } from '../../useLogContentHook.types'
import type { KeyValuePairProps } from './ExternalAPICall.types'
import css from './ExternalAPICall.module.scss'
 
const KeyValuePair: React.FC<KeyValuePairProps> = ({ keyText, value, isLink }) => {
  const { getString } = useStrings()
 
  return (
    <Layout.Horizontal spacing="small">
      <Text font={{ variation: FontVariation.BODY }} color={Color.GREY_400}>
        {keyText}:
      </Text>
      {isLink ? (
        <a href={value} target="_blank" rel="noreferrer">
          <Text lineClamp={1} font={{ variation: FontVariation.BODY }} color={Color.GREY_900}>
            {value ?? getString('na')}
          </Text>
        </a>
      ) : (
        <Text lineClamp={1} font={{ variation: FontVariation.BODY }} color={Color.GREY_900}>
          {value ?? getString('na')}
        </Text>
      )}
    </Layout.Horizontal>
  )
}
 
const ExternalAPICallContent: React.FC<ExecutionAndAPICallLogProps> = ({
  loading,
  errorMessage,
  refetchLogs,
  resource,
  setPageNumber,
  monitoredServiceIdentifier,
  showTimelineSlider
}) => {
  const { getString } = useStrings()
  const TEXT_NO_DATA =
    Boolean(monitoredServiceIdentifier) && !showTimelineSlider
      ? getString('cv.monitoredServices.noAvailableLogData')
      : getString('cv.changeSource.noDataAvaiableForCard')
  const { content = [], pageSize = 0, pageIndex = 0, totalPages = 0, totalItems = 0 } = resource ?? {}
 
  if (loading) {
    return <PageSpinner />
  }
 
  if (errorMessage) {
    return <PageError message={errorMessage} onClick={() => refetchLogs()} />
  }
 
  if (!content.length) {
    return <NoDataCard message={TEXT_NO_DATA} image={noLogsDataImage} containerClassName={css.noDataContainer} />
  }
 
  return (
    <>
      <Container className={css.container}>
        {content.map((log: ApiCallLogDTO, index) => {
          const { createdAt, requests, requestTime, responses, responseTime } = log
          const request = requests?.find(_request => _request.name === 'url')
 
          const status = responses?.find(_response => _response.name === 'Status Code') ?? {}
          const responseBody = responses?.find(_response => _response.name === 'Response Body') ?? {}
 
          let stringifyResponse = ''
 
          try {
            stringifyResponse = responseBody.value
              ? JSON.stringify(JSON.parse(responseBody.value), null, 4)
              : TEXT_NO_DATA
          } catch (e) {
            stringifyResponse = responseBody.value ?? TEXT_NO_DATA
          }
 
          return (
            <Collapse
              key={index}
              collapseClassName={cx(css.collapse, css[getStatusColor(status.value)])}
              collapseHeaderClassName={css.collapseHeader}
              collapsedIcon="main-chevron-down"
              expandedIcon="main-chevron-right"
              heading={
                <Layout.Horizontal padding={{ left: 'small' }} spacing="small">
                  <Text font={{ variation: FontVariation.BODY }} color={Color.GREY_400}>
                    {formatDate(createdAt)}
                  </Text>
                  <Text lineClamp={1} font={{ variation: FontVariation.BODY }} color={Color.GREY_900}>
                    {`${getString('cv.fetchingDataFrom')} ${request?.value?.split('?')[0]}`}
                  </Text>
                </Layout.Horizontal>
              }
            >
              <Container className={css.collapseContent}>
                <Container
                  border={{ bottom: true }}
                  padding={{ top: 'medium', right: 'large', bottom: 'medium', left: 'large' }}
                >
                  <Heading level={2} font={{ variation: FontVariation.BODY2 }} color={Color.GREY_900}>
                    {getString('common.request')}
                  </Heading>
                  <KeyValuePair keyText="URL" value={request?.value} isLink />
                  <KeyValuePair keyText="Request Timestamp" value={formatDate(requestTime)} />
                </Container>
                <Container padding={{ top: 'medium', right: 'large', bottom: 'medium', left: 'large' }}>
                  <Heading level={2} font={{ variation: FontVariation.BODY2 }} color={Color.GREY_900}>
                    {getString('cv.response')}
                  </Heading>
                  <KeyValuePair keyText="Status Code" value={status.value} />
                  <KeyValuePair keyText="Response Timestamp" value={formatDate(responseTime)} />
                  <Container flex={{ alignItems: 'flex-start' }}>
                    <Text font={{ variation: FontVariation.BODY }} color={Color.GREY_400}>
                      {getString('cv.responseBody')}:
                    </Text>
                    <CopyText
                      iconAlwaysVisible
                      iconName="duplicate"
                      className={css.copy}
                      textToCopy={stringifyResponse}
                    />
                  </Container>
                  <Container padding="small" background={Color.WHITE} className={css.responseBody}>
                    <pre>{stringifyResponse}</pre>
                  </Container>
                </Container>
              </Container>
            </Collapse>
          )
        })}
      </Container>
      <Container border={{ top: true }} padding={{ left: 'medium', right: 'medium' }}>
        <Pagination
          pageSize={pageSize}
          pageIndex={pageIndex}
          pageCount={totalPages}
          itemCount={totalItems}
          gotoPage={setPageNumber}
        />
      </Container>
    </>
  )
}
 
export default ExternalAPICallContent