All files / modules/18-audit-trail/pages/AuditTrails/views AuditTrailsListView.tsx

98.44% Statements 63/64
82.86% Branches 87/105
95% Functions 19/20
98.44% Lines 63/64

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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264              2x 2x 2x   2x 2x   2x 2x   2x 2x 2x   2x   2x   2x           2x 50x 50x 50x               2x 50x 50x               2x 40x             2x 50x             2x 50x 50x 50x 50x   50x     2x 4x 4x 4x 4x   4x 50x 50x   50x 50x                     50x                                   4x 45x 45x     4x 50x   50x                                               4x 50x             4x 50x             1x 1x             4x       40x             40x             40x             40x                   30x                 40x                     40x                       4x                                           2x  
/*
 * 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, { ReactElement, useState } from 'react'
import { TableV2, Text, Layout, Avatar, Icon, Container, Popover } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { Column, Renderer, CellProps } from 'react-table'
import { Link, useParams } from 'react-router-dom'
import { PopoverInteractionKind, Position, Classes } from '@blueprintjs/core'
import type { IconProps } from '@harness/uicore/dist/icons/Icon'
import defaultTo from 'lodash-es/defaultTo'
import { actionToLabelMap, getModuleNameFromAuditModule, moduleInfoMap } from '@audit-trail/utils/RequestUtil'
import type { AuditEventDTO, PageAuditEventDTO, ResourceDTO } from 'services/audit'
import { useStrings } from 'framework/strings'
import { getReadableDateTime } from '@common/utils/dateUtils'
import AuditTrailFactory from '@audit-trail/factories/AuditTrailFactory'
import type { OrgPathProps } from '@common/interfaces/RouteInterfaces'
import EventSummary from '@audit-trail/components/EventSummary/EventSummary'
 
import css from './AuditTrailsListView.module.scss'
 
const DEFAULT_CELL_PLACEHOLDER = 'N/A'
interface AuditTrailsListViewProps {
  data: PageAuditEventDTO
  setPage: (page: number) => void
}
 
const renderColumnTimeStamp: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
  const time = getReadableDateTime(row.original.timestamp, 'hh:mm a')
  const date = getReadableDateTime(row.original.timestamp, 'MMM DD, YYYY')
  return (
    <>
      <Text margin={{ bottom: 'small' }}>{time}</Text>
      <Text>{date}</Text>
    </>
  )
}
 
const renderColumnUser: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
  const { labels, principal } = row.original.authenticationInfo
  return (
    <Layout.Horizontal padding={{ right: 'xlarge' }} flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
      <Avatar className={css.avatar} name={row.original.authenticationInfo.principal.identifier} hoverCard={false} />
      <Text lineClamp={1}>{labels?.username || principal.identifier}</Text>
    </Layout.Horizontal>
  )
}
 
const renderColumnOrganization: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
  return (
    <Text padding={{ right: 'xlarge' }} lineClamp={1}>
      {row.original.resourceScope.orgIdentifier || DEFAULT_CELL_PLACEHOLDER}
    </Text>
  )
}
 
const renderColumnProject: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
  return (
    <Text padding={{ right: 'xlarge' }} lineClamp={1}>
      {row.original?.resourceScope?.projectIdentifier || DEFAULT_CELL_PLACEHOLDER}
    </Text>
  )
}
 
const getModuleIconAndLabel = (module: AuditEventDTO['module'], resourceType: ResourceDTO['type']) => {
  const moduleInfo = module !== 'CORE' ? moduleInfoMap[module] : undefined
  const { moduleIcon, moduleLabel } = AuditTrailFactory.getResourceHandler(resourceType) || {}
  const label = moduleInfo ? moduleInfo.moduleLabel : moduleLabel
  const icon = moduleInfo ? moduleInfo.icon : moduleIcon
 
  return { label, icon }
}
 
const AuditTrailsListView: React.FC<AuditTrailsListViewProps> = ({ data, setPage }) => {
  const { orgIdentifier } = useParams<OrgPathProps>()
  const [showEventSummary, setShowEventSummary] = useState<boolean>(true)
  const [selectedAuditEvent, setSelectedAuditEvent] = useState<AuditEventDTO | undefined>()
  const { getString } = useStrings()
 
  const renderColumnResource: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
    const { resourceScope, resource, module } = row.original
    const { accountIdentifier } = resourceScope
 
    const resourceHandler = AuditTrailFactory.getResourceHandler(resource.type)
    const url = accountIdentifier
      ? resourceHandler?.resourceUrl?.(
          row.original.resource,
          {
            ...resourceScope,
            accountIdentifier: accountIdentifier
          },
          getModuleNameFromAuditModule(module)
        )
      : undefined
 
    return (
      <Layout.Vertical padding={{ right: 'xlarge' }}>
        {url ? (
          <Link className={css.resourceLink} to={url}>
            <Text lineClamp={1}>{defaultTo(resource.labels?.resourceName, resource.identifier)}</Text>
          </Link>
        ) : (
          <Text lineClamp={1}>{defaultTo(resource.labels?.resourceName, resource.identifier)}</Text>
        )}
        {resourceHandler?.resourceLabel ? (
          <Text padding={{ top: 'xsmall' }} lineClamp={1}>{`${getString('typeLabel')}: ${getString(
            resourceHandler?.resourceLabel
          )}`}</Text>
        ) : undefined}
      </Layout.Vertical>
    )
  }
 
  const renderModuleIcon = (icon: IconProps): ReactElement => {
    const navSettingsIcon = icon.name === 'nav-settings'
    return <Icon className={navSettingsIcon ? css.navSettingIcon : undefined} name={icon.name} size={icon.size || 30} />
  }
 
  const renderColumnModule: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
    const { icon, label } = getModuleIconAndLabel(row.original.module, row.original.resource.type)
 
    return icon?.name ? (
      <Container flex={{ justifyContent: 'center' }}>
        {label ? (
          <Popover
            position={Position.TOP}
            interactionKind={PopoverInteractionKind.HOVER}
            className={Classes.DARK}
            content={
              <Text color={Color.WHITE} padding="small">
                {getString(label)}
              </Text>
            }
          >
            {renderModuleIcon(icon)}
          </Popover>
        ) : (
          renderModuleIcon(icon)
        )}
      </Container>
    ) : (
      ''
    )
  }
 
  const renderColumnAction: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
    return (
      <Layout.Horizontal padding={{ right: 'xlarge' }}>
        <Text lineClamp={1}>{getString(actionToLabelMap[row.original.action])}</Text>
      </Layout.Horizontal>
    )
  }
 
  const renderEventSummary: Renderer<CellProps<AuditEventDTO>> = ({ row }) => {
    return (
      <Layout.Horizontal flex={{ justifyContent: 'center' }}>
        <Icon
          name="main-notes"
          size={20}
          className={css.notesIcon}
          onClick={() => {
            setShowEventSummary(true)
            setSelectedAuditEvent(row.original)
          }}
        />
      </Layout.Horizontal>
    )
  }
 
  const columns: Column<AuditEventDTO>[] = [
    {
      Header: getString('common.timePstLabel'),
      id: 'time',
      accessor: row => row.timestamp,
      width: '8%',
      Cell: renderColumnTimeStamp
    },
    {
      Header: getString('common.userLabel'),
      id: 'user',
      accessor: row => row.authenticationInfo.principal.identifier,
      width: orgIdentifier ? '18%' : '16%',
      Cell: renderColumnUser
    },
    {
      Header: getString('action'),
      id: 'action',
      accessor: row => row.action,
      width: '10%',
      Cell: renderColumnAction
    },
    {
      Header: getString('common.resourceLabel'),
      id: 'resource',
      accessor: row => row.resource.identifier,
      width: orgIdentifier ? '23%' : '18%',
      Cell: renderColumnResource
    },
    // If orgIdentifier is not present, organisation column will be visible
    ...(!orgIdentifier
      ? [
          {
            Header: getString('orgLabel'),
            id: 'organization',
            accessor: row => row.resourceScope.orgIdentifier,
            width: '15%',
            Cell: renderColumnOrganization
          } as Column<AuditEventDTO>
        ]
      : []),
    {
      Header: getString('projectLabel'),
      id: 'project',
      accessor: row => row.resourceScope.projectIdentifier,
      width: orgIdentifier ? '18' : '13%',
      Cell: renderColumnProject
    },
    {
      Header: (
        <Text color={Color.GREY_900} flex={{ justifyContent: 'center' }}>
          {getString('common.moduleLabel')}
        </Text>
      ),
      id: 'module',
      accessor: row => row.module,
      width: '12%',
      Cell: renderColumnModule
    },
    {
      Header: '',
      id: 'eventSummary',
      width: '8%',
      Cell: renderEventSummary
    }
  ]
 
  return (
    <>
      <TableV2<AuditEventDTO>
        data={data.content || []}
        columns={columns}
        className={css.table}
        pagination={{
          itemCount: data?.totalItems || 0,
          pageSize: data?.pageSize || 10,
          pageCount: data?.totalPages || 0,
          pageIndex: data?.pageIndex || 0,
          gotoPage: setPage,
          className: css.pagination
        }}
      />
      {showEventSummary && selectedAuditEvent && (
        <EventSummary auditEvent={selectedAuditEvent} onClose={() => setShowEventSummary(false)} />
      )}
    </>
  )
}
 
export default AuditTrailsListView