All files / modules/75-ce/components/PerspectiveViews PerspectiveListView.tsx

84.85% Statements 56/66
66.67% Branches 40/60
76.47% Functions 13/17
84.85% Lines 56/66

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              2x 2x 2x   2x                     2x 2x 2x 2x 2x 2x 2x 2x                           2x           5x 5x     5x   5x   5x 20x         5x 20x 20x               20x           5x 20x             5x 20x     5x                 5x 20x 20x   20x     20x 12x   20x                   5x 24x   24x                                           24x 24x   24x           24x           24x 1x 1x 1x 1x       24x         2x                 1x 1x                                         5x 5x                                                                       5x     1x                     2x  
/*
 * 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, useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import moment from 'moment'
import type { CellProps, Renderer, Column } from 'react-table'
import {
  Icon,
  Text,
  Layout,
  Button,
  Popover,
  Container,
  IconName,
  TableV2,
  useConfirmationDialog
} from '@wings-software/uicore'
import { Classes, Menu, Position, Intent } from '@blueprintjs/core'
import { defaultTo } from 'lodash-es'
import routes from '@common/RouteDefinitions'
import { QlceView, ViewState, ViewType } from 'services/ce/services'
import { perspectiveDateLabelToDisplayText, SOURCE_ICON_MAPPING } from '@ce/utils/perspectiveUtils'
import formatCost from '@ce/utils/formatCost'
import { useStrings } from 'framework/strings'
import css from './PerspectiveListView.module.scss'
 
interface PerspectiveListViewProps {
  pespectiveData: QlceView[]
  navigateToPerspectiveDetailsPage: (
    perspectiveId: string,
    viewState: ViewState,
    name: string,
    viewType: ViewType
  ) => void
  deletePerpsective: (perspectiveId: string, perspectiveName: string) => void
  clonePerspective: (perspectiveId: string, perspectiveName: string) => void
}
 
const PerspectiveListView: React.FC<PerspectiveListViewProps> = ({
  pespectiveData,
  navigateToPerspectiveDetailsPage,
  deletePerpsective,
  clonePerspective
}) => {
  const history = useHistory()
  const { accountId } = useParams<{
    accountId: string
  }>()
  const { getString } = useStrings()
 
  const dateLabelToDisplayText = perspectiveDateLabelToDisplayText(getString)
 
  const TimePeriodCell: Renderer<CellProps<QlceView>> = cell => {
    return cell.value ? (
      <Text>{dateLabelToDisplayText[cell.value] || getString('common.repo_provider.customLabel')}</Text>
    ) : null
  }
 
  const DataSourcesCell: Renderer<CellProps<QlceView>> = cell => {
    const dataSources = (cell.value || []) as string[]
    return (
      <Layout.Horizontal
        spacing="small"
        style={{
          alignItems: 'center'
        }}
      >
        {dataSources.map(source => (
          <Icon key={source} size={22} name={SOURCE_ICON_MAPPING[source]} />
        ))}
      </Layout.Horizontal>
    )
  }
 
  const CostCell: Renderer<CellProps<QlceView>> = cell => {
    return !isNaN(cell.value) ? (
      <Text font={{ weight: 'semi-bold' }} color="grey800">
        {formatCost(cell.value)}
      </Text>
    ) : null
  }
 
  const LastEditedCell: Renderer<CellProps<QlceView>> = cell => {
    return cell.value ? moment(cell.value).format('LLL') : null
  }
 
  const onEditClick: (perspectiveId: string) => void = perspectiveId => {
    history.push(
      routes.toCECreatePerspective({
        accountId: accountId,
        perspectiveId: perspectiveId
      })
    )
  }
 
  const NameCell: Renderer<CellProps<QlceView>> = cell => {
    const viewState = (cell as any).row?.original?.viewState
    const viewType = (cell as any).row?.original?.viewType
    let iconName: IconName | undefined
    Iif (viewState === ViewState.Draft) {
      iconName = 'deployment-incomplete-new'
    }
    if (viewType === ViewType.Default) {
      iconName = 'harness'
    }
    return cell.value ? (
      <Container className={css.nameContainer}>
        <Text icon={iconName} color="grey800">
          {cell.value}
        </Text>
        {viewType === ViewType.Default && <Container className={css.sampleRibbon}></Container>}
      </Container>
    ) : null
  }
 
  const RenderColumnMenu: Renderer<CellProps<QlceView>> = ({ row }) => {
    const [menuOpen, setMenuOpen] = useState(false)
 
    const { openDialog } = useConfirmationDialog({
      contentText: (
        <div>
          <Text>
            {getString('ce.perspectives.confirmDeletePerspectiveMsg', {
              name: row.original.name
            })}
          </Text>
        </div>
      ),
      titleText: getString('ce.perspectives.confirmDeletePerspectiveTitle'),
      confirmButtonText: getString('delete'),
      cancelButtonText: getString('cancel'),
      intent: Intent.DANGER,
      buttonIntent: Intent.DANGER,
      onCloseDialog: async (isConfirmed: boolean) => {
        if (isConfirmed) {
          row.original.id && deletePerpsective(row.original.id, defaultTo(row.original.name, ''))
        }
      }
    })
 
    const viewType = row?.original?.viewType
    const disableActions = viewType === ViewType.Default ? true : false
 
    const editClick: (e: any) => void = e => {
      e.stopPropagation()
      setMenuOpen(false)
      row.original.id && onEditClick(row.original.id)
    }
 
    const onDeleteClick: (e: any) => void = e => {
      e.stopPropagation()
      setMenuOpen(false)
      openDialog()
    }
 
    const onCloneClick: (e: any) => void = e => {
      e.stopPropagation()
      setMenuOpen(false)
      Eif (row.original.id && row.original.name) {
        clonePerspective(row.original.id, row.original.name)
      }
    }
 
    return (
      <Layout.Horizontal>
        <Popover
          isOpen={menuOpen}
          onInteraction={nextOpenState => {
            setMenuOpen(nextOpenState)
          }}
          position={Position.BOTTOM_RIGHT}
          className={Classes.DARK}
        >
          <Button
            minimal
            icon="Options"
            onClick={e => {
              e.stopPropagation()
              setMenuOpen(true)
            }}
            data-testid={`menu-${row.original.id}`}
          />
          <Container>
            <Menu>
              <Menu.Item disabled={disableActions} onClick={editClick} icon="edit" text="Edit" />
              <Menu.Item
                onClick={onCloneClick}
                icon="duplicate"
                text="Clone"
                data-testid={`clone-perspective-${row.original.id}`}
              />
              <Menu.Item disabled={disableActions} onClick={onDeleteClick} icon="trash" text="Delete" />
            </Menu>
          </Container>
        </Popover>
      </Layout.Horizontal>
    )
  }
 
  const columns: Column<QlceView>[] = useMemo(
    () => [
      {
        Header: 'Name',
        accessor: 'name',
        width: '30%',
        Cell: NameCell
      },
      {
        Header: 'Data Sources',
        accessor: 'dataSources',
        width: '15%',
        Cell: DataSourcesCell
      },
      {
        Header: 'Total Cost',
        accessor: 'totalCost',
        width: '20%',
        Cell: CostCell
      },
      {
        Header: 'Time Period',
        accessor: 'timeRange',
        width: '15%',
        Cell: TimePeriodCell
      },
      { Header: 'Last Edit', accessor: 'lastUpdatedAt', width: '15%', Cell: LastEditedCell },
      {
        Header: '',
        id: 'menu',
        width: '5%',
        Cell: RenderColumnMenu,
        onEditClick: onEditClick
      }
    ],
    []
  )
  return (
    <TableV2<QlceView>
      onRowClick={row => {
        row.id &&
          row.viewState &&
          row.name &&
          navigateToPerspectiveDetailsPage(row.id, row.viewState, row.name, row.viewType || ViewType.Customer)
      }}
      columns={columns}
      data={pespectiveData}
    />
  )
}
 
export default PerspectiveListView