All files / modules/70-pipeline/components/StatusSelect StatusSelect.tsx

100% Statements 17/17
83.33% Branches 5/6
100% Functions 6/6
100% Lines 15/15

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              7x 7x 7x 7x 7x                               7x                         7x                                   7x 36x 36x   36x   130x             36x       8x             5x                
/*
 * 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 { MultiSelectDropDown, MultiSelectOption } from '@wings-software/uicore'
import { defaultTo } from 'lodash-es'
import { ExecutionStatus, ExecutionStatusEnum } from '@pipeline/utils/statusHelpers'
import { StringKeys, useStrings } from 'framework/strings'
 
type AllowedStatus = Exclude<
  ExecutionStatus,
  | 'NotStarted'
  | 'Queued'
  | 'Skipped'
  | 'Pausing'
  | 'Suspended'
  | 'AsyncWaiting'
  | 'TaskWaiting'
  | 'TimedWaiting'
  | 'Errored'
  | 'IgnoreFailed'
  | 'Discontinuing'
>
const allowedOptions = [
  ExecutionStatusEnum.Aborted,
  ExecutionStatusEnum.Expired,
  ExecutionStatusEnum.Failed,
  ExecutionStatusEnum.Running,
  ExecutionStatusEnum.Success,
  ExecutionStatusEnum.ApprovalRejected,
  ExecutionStatusEnum.Paused,
  ExecutionStatusEnum.ApprovalWaiting,
  ExecutionStatusEnum.InterventionWaiting,
  ExecutionStatusEnum.ResourceWaiting
] as AllowedStatus[]
 
const labelMap: Record<AllowedStatus, StringKeys> = {
  Aborted: 'pipeline.executionFilters.labels.Aborted',
  Expired: 'pipeline.executionFilters.labels.Expired',
  Failed: 'pipeline.executionFilters.labels.Failed',
  Running: 'pipeline.executionFilters.labels.Running',
  Success: 'pipeline.executionFilters.labels.Success',
  ApprovalRejected: 'pipeline.executionFilters.labels.ApprovalRejected',
  Paused: 'pipeline.executionFilters.labels.Paused',
  ApprovalWaiting: 'pipeline.executionFilters.labels.ApprovalWaiting',
  InterventionWaiting: 'pipeline.executionFilters.labels.InterventionWaiting',
  ResourceWaiting: 'pipeline.executionFilters.labels.Waiting'
}
 
export interface StatusSelectProps {
  value?: ExecutionStatus[] | null
  onSelect(status: ExecutionStatus[] | null): void
}
 
export default function StatusSelect(props: StatusSelectProps): React.ReactElement {
  const { value, onSelect } = props
  const { getString } = useStrings()
 
  const getAllowedOptions = React.useMemo(
    () =>
      allowedOptions.map(item => ({
        label: getString(labelMap[item]),
        value: item
      })) as MultiSelectOption[],
    [allowedOptions, labelMap]
  )
 
  return (
    <MultiSelectDropDown
      buttonTestId="status-select"
      value={defaultTo(
        value?.map(item => ({
          label: getString(labelMap[item as AllowedStatus]),
          value: item
        })),
        []
      )}
      onChange={option => {
        onSelect((option.map(item => item.value) as ExecutionStatus[]) || null)
      }}
      items={getAllowedOptions}
      usePortal={true}
      placeholder={getString('status')}
    />
  )
}