All files / modules/72-triggers/pages/triggers/views/subviews ArtifactTableInfo.tsx

97.01% Statements 65/67
84.35% Branches 97/115
100% Functions 18/18
96.97% Lines 64/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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386              7x 7x 7x 7x   7x                                                                         7x 131x 131x                     5x                                                           7x             177x 177x 177x               7x 177x 177x                 7x 177x 177x                 7x 50x 50x                 7x 81x 81x                 7x 131x 131x             7x 46x                 2x             3x             7x                       61x   57x 57x       57x 114x     57x 57x 57x   27x 27x 57x   69x             57x       27x     57x     57x 57x                                                                                                         57x       57x 27x                   27x 11x                       16x                         27x                               30x                         57x             10x 7x 7x 7x                               7x  
/*
 * 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, { Dispatch, SetStateAction } from 'react'
import { Button, Layout, TableV2, Text } from '@wings-software/uicore'
import { RadioGroup, Radio } from '@blueprintjs/core'
import { useStrings } from 'framework/strings'
import type { artifactTableItem } from '../../utils/TriggersWizardPageUtils'
import css from './ArtifactTableInfo.module.scss'
 
interface ArtifactTableInfoInterface {
  artifactTableData?: any
  selectedArtifact?: any
  selectedArtifactLabel?: string
  artifactType?: string
  selectedStage?: string
  isManifest: boolean
  setSelectedStage?: Dispatch<SetStateAction<any>>
  setSelectedArtifact?: Dispatch<SetStateAction<any>>
  setSelectedArtifactLabel?: Dispatch<SetStateAction<any>>
  formikProps: any
  appliedArtifact?: any
  editArtifact?: () => void
  removeArtifact?: () => void
}
 
interface RenderColumnSelectColumn {
  selectedArtifactLabel?: string
}
 
interface RenderColumnArtifactLabelColumn {
  appliedArtifact?: any
}
export interface FormValues {
  artifact?: string
}
 
export interface RenderColumnRow {
  original: artifactTableItem
}
interface RenderColumnEditColumn {
  editArtifact: () => void
  removeArtifact: () => void
}
 
const RenderColumnSelect = ({ row, column }: { row: RenderColumnRow; column: RenderColumnSelectColumn }) => {
  const data = row.original
  return (
    <>
      <Layout.Horizontal
        className={data.disabled ? css.disabledRow : ''}
        spacing="small"
        style={{ justifyContent: 'center' }}
      >
        <RadioGroup
          selectedValue={column?.selectedArtifactLabel}
          label=""
          name="artifactLabel"
          onChange={e => e.preventDefault()}
          disabled={data.disabled}
        >
          <Radio value={data.artifactLabel} />
        </RadioGroup>
      </Layout.Horizontal>
    </>
  )
}
 
// const RenderColumnEdit = ({ row, column }) => {
//   const { values } = column.formikProps
//   const data = row.original
//   return (
//     <>
//       <Layout.Horizontal spacing="small" style={{ justifyContent: 'center' }} >
//         <Button
//           style={{ color: 'var(--primary-7)' }}
//           minimal
//           className={css.actionButton}
//           icon="edit"
//           onClick={e => {
 
//           }}
//         />
//       </Layout.Horizontal>
//     </>
//   )
// }
 
const RenderColumnArtifactLabel = ({
  row,
  column
}: {
  row: RenderColumnRow
  column: RenderColumnArtifactLabelColumn
}) => {
  const { appliedArtifact } = column
  const data = row.original
  return (
    <Layout.Horizontal className={data.disabled ? css.disabledRow : ''}>
      <Text width={appliedArtifact ? '296px' : '176px'} lineClamp={1}>
        {data.artifactLabel}
      </Text>
    </Layout.Horizontal>
  )
}
const RenderColumnArtifactRepository = ({ row }: { row: RenderColumnRow }) => {
  const data = row.original
  return (
    <Layout.Horizontal className={data.disabled ? css.disabledRow : ''}>
      <Text width="159px" lineClamp={1}>
        {data.artifactRepository}
      </Text>
    </Layout.Horizontal>
  )
}
 
const RenderColumnLocation = ({ row }: { row: RenderColumnRow }) => {
  const data = row.original
  return (
    <Layout.Horizontal className={data.disabled ? css.disabledRow : ''}>
      <Text style={{ minHeight: '18px' }} width="110px" lineClamp={1}>
        {data.location}
      </Text>
    </Layout.Horizontal>
  )
}
 
const RenderColumnBuildTag = ({ row }: { row: RenderColumnRow }) => {
  const data = row.original
  return (
    <Layout.Horizontal spacing="small" className={data.disabled ? css.disabledRow : ''}>
      <Text style={{ minHeight: '18px' }} width="100px" lineClamp={1}>
        {data.buildTag}
      </Text>
    </Layout.Horizontal>
  )
}
 
const RenderColumnVersion = ({ row }: { row: RenderColumnRow }) => {
  const data = row.original
  return (
    <Layout.Horizontal spacing="small" className={data.disabled ? css.disabledRow : ''}>
      <Text style={{ minHeight: '18px' }} lineClamp={1}>
        {data.version}
      </Text>
    </Layout.Horizontal>
  )
}
 
const RenderColumnHasRuntimeInputs = ({ row }: { row: RenderColumnRow }) => {
  const data = row.original
  return (
    <Layout.Horizontal spacing="small" className={data.disabled ? css.disabledRow : ''}>
      <Text>{data.hasRuntimeInputs ? 'Yes' : 'No'}</Text>
    </Layout.Horizontal>
  )
}
 
const RenderColumnEdit = ({ column }: { column: RenderColumnEditColumn }) => {
  return (
    <>
      <Layout.Horizontal spacing="small" style={{ justifyContent: 'center' }}>
        <Button
          // style={{ color: 'var(--primary-7)' }}
          minimal
          data-name="select-artifact-edit"
          // className={css.actionButton}
          icon="edit"
          onClick={() => column.editArtifact()}
        />
        <Button
          style={{ display: 'inline-block' }}
          minimal
          data-name="main-delete"
          icon="main-trash"
          onClick={() => column.removeArtifact()}
        />
      </Layout.Horizontal>
    </>
  )
}
 
const ArtifactTableInfo = (props: ArtifactTableInfoInterface): JSX.Element => {
  const {
    appliedArtifact,
    setSelectedArtifact,
    selectedArtifactLabel,
    setSelectedArtifactLabel,
    setSelectedStage,
    formikProps,
    isManifest,
    editArtifact,
    artifactTableData,
    removeArtifact
  } = props
 
  const { getString } = useStrings()
  const artifactOrManifestText = isManifest
    ? getString('manifestsText')
    : getString('pipeline.artifactTriggerConfigPanel.artifact')
 
  const getTooltipIds = (manifestTooltipIds: string, artifactTooltipIds: string): string => {
    return isManifest ? manifestTooltipIds : artifactTooltipIds
  }
 
  let newData = appliedArtifact || artifactTableData
  let showUniqueReferenceWarning = ''
  if (artifactTableData) {
    // hide all manifest overrides that are not unique
    const newFilteredData: artifactTableItem[] = []
    artifactTableData.forEach((data: artifactTableItem) => {
      Iif (
        newFilteredData.some(
          (nfd: artifactTableItem) => data.isStageOverrideManifest && nfd.artifactId === data.artifactId
        )
      ) {
        showUniqueReferenceWarning = getString('pipeline.artifactTriggerConfigPanel.artifactReferenceUnique', {
          artifact: artifactOrManifestText
        })
      } else {
        newFilteredData.push(data)
      }
    })
 
    newData = newFilteredData
  }
 
  const showWarning = artifactTableData?.some((data: any) => data.disabled)
  // should render differently for applied or data table
  // remove has runtime input and version when appliedTable
  const columns: any = React.useMemo(
    () => [
      {
        Header: (
          <Text
            inline={true}
            className={css.textHeading}
            tooltipProps={{ dataTooltipId: getTooltipIds('manifestName', 'artifactName') }}
          >
            {artifactOrManifestText?.toUpperCase()}
          </Text>
        ),
        accessor: 'artifactLabel',
        width: appliedArtifact ? '45%' : '25%',
        Cell: RenderColumnArtifactLabel,
        className: 'className',
        class: 'class',
        disableSortBy: !!appliedArtifact,
        appliedArtifact
      },
      {
        Header: (
          <Text
            inline={true}
            className={css.textHeading}
            tooltipProps={{ dataTooltipId: getTooltipIds('manifestRepository', 'artifactRepository') }}
          >
            {getString?.('pipeline.artifactTriggerConfigPanel.artifactRepository', {
              artifact: artifactOrManifestText
            }).toUpperCase()}
          </Text>
        ),
        accessor: 'activity',
        width: '25%',
        Cell: RenderColumnArtifactRepository,
        disableSortBy: true,
        getString
      },
      {
        Header: (
          <Text inline={true} className={css.textHeading} tooltipProps={{ dataTooltipId: 'artifactManifestLocation' }}>
            {getString?.('common.location').toUpperCase()}
          </Text>
        ),
        accessor: 'lastExecutionTime',
        width: '18%',
        Cell: RenderColumnLocation,
        disableSortBy: true,
        getString
      }
    ],
    [appliedArtifact, formikProps, getString]
  )
 
  Iif (!newData) {
    return <></>
  }
 
  if (!appliedArtifact) {
    columns.unshift({
      Header: '',
      accessor: 'select',
      width: '5%',
      disableSortBy: true,
      selectedArtifactLabel,
      formikProps,
      Cell: RenderColumnSelect
    })
    // Insert Location when available
    if (isManifest) {
      columns.push({
        Header: (
          <Text inline={true} className={css.textHeading} tooltipProps={{ dataTooltipId: 'manifestHasVersion' }}>
            {getString?.('version').toUpperCase()}
          </Text>
        ),
        accessor: 'version',
        width: '13%',
        Cell: RenderColumnVersion,
        disableSortBy: true
      })
    } else {
      columns.push({
        Header: (
          <Text inline={true} className={css.textHeading} tooltipProps={{ dataTooltipId: 'artifactBuildTag' }}>
            {getString?.('pipeline.artifactTriggerConfigPanel.buildTag').toUpperCase()}
          </Text>
        ),
        accessor: 'buildTag',
        width: '13%',
        Cell: RenderColumnBuildTag,
        disableSortBy: true
      })
    }
 
    columns.push({
      Header: (
        <Text
          inline={true}
          className={css.textHeading}
          tooltipProps={{ dataTooltipId: 'artifactManifestRuntimeInputs' }}
        >
          {getString?.('pipeline.artifactTriggerConfigPanel.hasRuntimeInputs').toUpperCase()}
        </Text>
      ),
      accessor: 'hasRuntimeInputs',
      width: '18%',
      Cell: RenderColumnHasRuntimeInputs,
      disableSortBy: true
    })
  } else {
    columns.push({
      Header: ' ',
      accessor: 'edit',
      width: '10%',
      disableSortBy: true,
      appliedArtifact,
      formikProps,
      editArtifact,
      removeArtifact,
      Cell: RenderColumnEdit
    })
  }
 
  return (
    <>
      <TableV2
        className={`${appliedArtifact ? css.appliedArtifact : css.selectArtifactTable}`}
        columns={columns}
        data={Array.isArray(newData) ? newData : [newData]}
        onRowClick={item => {
          if (!item?.disabled) {
            setSelectedArtifact?.(item?.artifactId)
            setSelectedStage?.(item?.stageId)
            setSelectedArtifactLabel?.(item?.artifactLabel)
          }
        }}
      />
      {!appliedArtifact && showWarning && (
        <Text style={{ color: '#FF7B26' }}>
          {getString?.('pipeline.artifactTriggerConfigPanel.chartVersionRuntimeInput', {
            artifact: artifactOrManifestText.toLowerCase(),
            runTimeStr: isManifest ? getString('pipeline.manifestType.http.chartVersion') : getString('tagLabel')
          })}
        </Text>
      )}
      {showUniqueReferenceWarning && <Text style={{ color: '#FF7B26' }}>{showUniqueReferenceWarning}</Text>}
    </>
  )
}
export default ArtifactTableInfo