All files / modules/35-connectors/components/activityHistory/ActivityList ActivityList.tsx

96.15% Statements 50/52
80.65% Branches 75/93
100% Functions 9/9
96.15% Lines 50/52

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 173 174 175 176 177 178 179 180 181 182 183 184              11x 11x   11x 11x 11x     11x       11x 11x   11x                                   18x 2x   16x 9x   7x       11x 18x 18x   18x 11x 7x 7x     18x 2x   18x     18x       18x     11x 18x 18x 18x                   11x 18x 18x     11x 18x   18x                                         11x 3x 3x 3x 3x                                           3x 3x       3x 3x         3x 2x                           1x     3x                       11x  
/*
 * 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 moment from 'moment'
 
import { Layout, Text, Icon, IconName, Container, TableV2 } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import type { CellProps, Renderer, Column } from 'react-table'
import type { DateRange } from '@blueprintjs/datetime'
import { useStrings } from 'framework/strings'
 
import type { ResponsePageActivity, Activity, ResponseConnectivityCheckSummary } from 'services/cd-ng'
 
import { Page } from '@common/exports'
import { ActivityStatus, ActivityType } from '@connectors/constants'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import css from '../ActivityHistory/ActivityHistory.module.scss'
 
interface ActivityListProps {
  activityList: ResponsePageActivity
  connectivitySummary: ResponseConnectivityCheckSummary
  entityIdentifier: string | undefined
  dateRange: DateRange
  refetchActivities: () => Promise<void>
  refetchConnectivitySummary: () => Promise<void>
  showOtherActivity: boolean
  showConnectivityChecks: boolean
}
 
interface AllActivity extends Activity {
  failureCount?: number
}
 
function getIconForActivity(data: Activity): IconName {
  if (data.type === 'CONNECTIVITY_CHECK') {
    return 'main-delete'
  }
  if (data.activityStatus === ActivityStatus.FAILED) {
    return 'warning-sign'
  } else {
    return 'tick'
  }
}
 
const GetStatusText: React.FC<AllActivity> = (data: AllActivity) => {
  const { getString } = useStrings()
  let status = ''
 
  if (data.activityStatus === ActivityStatus.FAILED) {
    status = getString('common.test.connectionFailed')
  } else Eif (data.activityStatus === ActivityStatus.SUCCESS) {
    status = getString('common.test.connectionSuccessful')
  }
 
  if (data.type === ActivityType.CONNECTIVITY_CHECK) {
    status = `${getString('activityHistory.heartbeatFailure')} (${data.failureCount})`
  }
  Iif (data.type === ActivityType.ENTITY_CREATION) {
    status = getString('activityHistory.createdSuccessfully')
  }
  Iif (data.type === ActivityType.ENTITY_UPDATE) {
    status = getString('activityHistory.updatedSuccessfully')
  }
 
  return <Text color={Color.GREY_900}>{status}</Text>
}
 
const RenderColumnTime: Renderer<CellProps<Activity>> = ({ row }) => {
  const { getString } = useStrings()
  const data = row.original
  return (
    <Layout.Vertical spacing="xsmall">
      <Text color={Color.GREY_900}>{getString('activityLog')}</Text>
      <Text font={{ size: 'small' }} color={Color.GREY_450}>
        {moment(data.activityTime).format('DD MMM YYYY')}
      </Text>
    </Layout.Vertical>
  )
}
 
const RenderColumnActivity: Renderer<CellProps<Activity>> = ({ row }) => {
  const data = row.original
  return <Text>{data.description}</Text>
}
 
const RenderColumnStatus: Renderer<CellProps<AllActivity>> = ({ row }) => {
  const data = row.original
 
  return (
    <Layout.Horizontal>
      <Icon
        name={getIconForActivity(data)}
        size={12}
        color={data.activityStatus === ActivityStatus.SUCCESS ? Color.GREEN_500 : Color.RED_500}
        style={{
          margin: '2px 5px'
        }}
      />
 
      <Layout.Vertical>
        {GetStatusText(data)}
        <Text font={{ size: 'small' }} color={Color.GREY_450}>
          {data.detail?.activityStatusMessage}
        </Text>
      </Layout.Vertical>
    </Layout.Horizontal>
  )
}
 
const ActivityList: React.FC<ActivityListProps> = props => {
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
  const columns: Column<Activity>[] = useMemo(
    () => [
      {
        Header: getString('timeLabel')?.toUpperCase(),
        accessor: 'activityTime',
        width: '25%',
        Cell: RenderColumnTime
      },
      {
        Header: getString('activity')?.toUpperCase(),
        accessor: 'description',
        width: '50%',
        Cell: RenderColumnActivity
      },
      {
        Header: getString('status')?.toUpperCase(),
        accessor: 'activityStatus',
        width: '25%',
        Cell: RenderColumnStatus
      }
    ],
    [props.refetchActivities, props.refetchConnectivitySummary]
  )
  const activity: AllActivity[] = useMemo(
    () => (props.showOtherActivity ? props.activityList?.data?.content || [] : []),
    [props.activityList?.data?.content, props.showOtherActivity]
  )
 
  const connectivity = useMemo(
    () => (props.showConnectivityChecks ? props.connectivitySummary?.data || null : null),
    [props.connectivitySummary?.data, props.showConnectivityChecks]
  )
 
  let dataArray
  if (props.showConnectivityChecks && props.connectivitySummary?.data && connectivity?.failureCount) {
    dataArray = activity.concat([
      {
        accountIdentifier: accountId,
        type: ActivityType.CONNECTIVITY_CHECK,
        activityTime: connectivity?.startTime as number,
        description: getString('activityHistory.connectivityCheck'),
        activityStatus: 'FAILED',
        detail: {
          activityStatusMessage: getString('activityHistory.connectivityCheckFailed')
        },
        failureCount: connectivity?.failureCount
      }
    ])
  } else {
    dataArray = activity
  }
 
  return (
    <Layout.Vertical className={css.activity}>
      {dataArray?.length ? (
        <TableV2<AllActivity> columns={columns} data={dataArray || []} />
      ) : (
        <Container height={'200px'}>
          <Page.NoDataCard icon="nav-dashboard" message={getString('activityHistory.noData')} />
        </Container>
      )}
    </Layout.Vertical>
  )
}
export default ActivityList