All files / modules/70-pipeline/pages/pipeline-deployment-list/PipelineDeploymentListHeader PipelineDeploymentListHeader.tsx

84.62% Statements 33/39
68.75% Branches 11/16
71.43% Functions 5/7
84.62% Lines 33/39

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              7x 7x 7x 7x 7x 7x     7x 7x 7x 7x 7x     7x 7x 7x   7x 7x   7x                       7x   7x 39x 37x 36x 36x 36x                   1x 1x             5x 5x                 3x 2x     1x       36x                                                       1x                                                
/*
 * 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 from 'react'
import { useParams } from 'react-router-dom'
import { ButtonVariation, Checkbox, ExpandingSearchInput } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import cx from 'classnames'
import { String, useStrings } from 'framework/strings'
import type { PipelinePathProps, PipelineType } from '@common/interfaces/RouteInterfaces'
 
import StatusSelect from '@pipeline/components/StatusSelect/StatusSelect'
import NewPipelineSelect from '@pipeline/components/NewPipelineSelect/NewPipelineSelect'
import { getFeaturePropsForRunPipelineButton, getRbacButtonModules } from '@pipeline/utils/runPipelineUtils'
import { useUpdateQueryParams } from '@common/hooks'
import { Page } from '@common/exports'
import type { ExecutionStatus } from '@pipeline/utils/statusHelpers'
import type { GetListOfExecutionsQueryParams } from 'services/pipeline-ng'
import RbacButton from '@rbac/components/Button/Button'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
 
import { useFiltersContext } from '../FiltersContext/FiltersContext'
import { ExecutionFilters } from './ExecutionFilters/ExecutionFilters'
import type { QuickStatusParam } from '../types'
import css from './PipelineDeploymentListHeader.module.scss'
 
export interface FilterQueryParams {
  query?: string
  pipeline?: string
  status?: ExecutionStatus | null
}
export interface PipelineDeploymentListHeaderProps {
  onRunPipeline(): void
  isPipelineInvalid?: boolean
}
 
const defaultPageNumber = 1
 
export function PipelineDeploymentListHeader(props: PipelineDeploymentListHeaderProps): React.ReactElement {
  const { module, pipelineIdentifier } = useParams<Partial<PipelineType<PipelinePathProps>>>()
  const { queryParams } = useFiltersContext()
  const { updateQueryParams } = useUpdateQueryParams<Partial<GetListOfExecutionsQueryParams>>()
  const rbacButtonModules = getRbacButtonModules(module)
  const { getString } = useStrings()
  function handleQueryChange(query: string): void {
    if (query) {
      updateQueryParams({ searchTerm: query })
    } else {
      updateQueryParams({ searchTerm: [] as any }) // removes the param
    }
  }
 
  function handleMyDeployments(isChecked: boolean): void {
    Eif (isChecked) {
      updateQueryParams({ myDeployments: true })
    } else {
      updateQueryParams({ myDeployments: [] as any }) // removes the param
    }
  }
 
  function handleStatusChange(status?: QuickStatusParam | null): void {
    Eif (status) {
      updateQueryParams({ status, page: defaultPageNumber })
    } else {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      updateQueryParams({ status: [] as any }) // removes the param
    }
  }
 
  // eslint-disable-next-line @typescript-eslint/no-shadow
  function handlePipelineChange(pipelineIdentifier?: string): void {
    if (pipelineIdentifier) {
      updateQueryParams({ pipelineIdentifier, page: defaultPageNumber })
    } else {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      updateQueryParams({ pipelineIdentifier: [] as any }) // removes the param
    }
  }
 
  return (
    <Page.SubHeader className={css.main}>
      <div className={css.lhs}>
        <RbacButton
          variation={ButtonVariation.PRIMARY}
          className={css.runButton}
          onClick={props.onRunPipeline}
          disabled={props.isPipelineInvalid}
          tooltip={props.isPipelineInvalid ? getString('pipeline.cannotRunInvalidPipeline') : ''}
          permission={{
            resource: {
              resourceType: ResourceType.PIPELINE,
              resourceIdentifier: pipelineIdentifier || queryParams.pipelineIdentifier
            },
            permission: PermissionIdentifier.EXECUTE_PIPELINE,
            options: {
              skipCondition: ({ resourceIdentifier }) => !resourceIdentifier
            }
          }}
          featuresProps={getFeaturePropsForRunPipelineButton({ modules: rbacButtonModules, getString })}
        >
          <String stringID="runPipelineText" />
        </RbacButton>
        <Checkbox
          font={{ size: 'small', weight: 'semi-bold' }}
          color={Color.GREY_800}
          label={getString(module === 'ci' ? 'pipeline.myBuildsText' : 'pipeline.myDeploymentsText')}
          checked={queryParams.myDeployments}
          onChange={e => handleMyDeployments(e.currentTarget.checked)}
          className={cx(css.myDeploymentsCheckbox, { [css.selected]: queryParams.myDeployments })}
        />
        <StatusSelect value={queryParams.status as ExecutionStatus[]} onSelect={handleStatusChange} />
        {pipelineIdentifier ? null : (
          <NewPipelineSelect
            selectedPipeline={queryParams.pipelineIdentifier}
            onPipelineSelect={handlePipelineChange}
          />
        )}
      </div>
      <div className={css.rhs}>
        <ExpandingSearchInput
          defaultValue={queryParams.searchTerm}
          alwaysExpanded
          onChange={handleQueryChange}
          width={200}
          className={css.expandSearch}
        />
        <ExecutionFilters />
      </div>
    </Page.SubHeader>
  )
}