All files / modules/30-secrets/modals/CreateOrSelectSecretModal useCreateOrSelectSecretModal.tsx

75.68% Statements 28/37
39.29% Branches 11/28
55.56% Functions 5/9
75% Lines 27/36

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              283x 283x   283x 283x 283x 283x 283x 283x   283x   283x   283x   283x 283x                             283x       359x   359x                     418x   359x       359x   359x                             359x 7x                                                                   7x           7x                           359x   7x           283x  
/*
 * 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 { useModalHook } from '@harness/use-modal'
import type { SelectOption } from '@wings-software/uicore'
import { Dialog } from '@blueprintjs/core'
import { pick } from 'lodash-es'
import cx from 'classnames'
import { useStrings } from 'framework/strings'
import { getReference } from '@common/utils/utils'
import CreateOrSelectSecret from '@secrets/components/CreateOrSelectSecret/CreateOrSelectSecret'
import type { SecretReference } from '@secrets/components/CreateOrSelectSecret/CreateOrSelectSecret'
import { SecretTypeEnum } from '@secrets/components/SecretReference/SecretReference'
import type { SecretResponseWrapper, ResponsePageSecretResponseWrapper, ConnectorInfoDTO } from 'services/cd-ng'
import { ReferenceSelectDialogTitle } from '@common/components/ReferenceSelect/ReferenceSelect'
 
import { getScopeFromDTO } from '@common/components/EntityReference/EntityReference'
import type { SecretFormData } from '@secrets/components/CreateUpdateSecret/CreateUpdateSecret'
import useCreateUpdateSecretModal from '../CreateSecretModal/useCreateUpdateSecretModal'
import css from './useCreateOrSelectSecretModal.module.scss'
 
export interface UseCreateOrSelectSecretModalProps {
  type?: SecretResponseWrapper['secret']['type']
  onSuccess?: (secret: SecretReference) => void
  secretsListMockData?: ResponsePageSecretResponseWrapper
  connectorTypeContext?: ConnectorInfoDTO['type']
  handleInlineSSHSecretCreation?: () => void
}
 
export interface UseCreateOrSelectSecretModalReturn {
  openCreateOrSelectSecretModal: () => void
  closeCreateOrSelectSecretModal: () => void
}
 
const useCreateOrSelectSecretModal = (
  props: UseCreateOrSelectSecretModalProps,
  inputs?: any[]
): UseCreateOrSelectSecretModalReturn => {
  const { getString } = useStrings()
 
  const secretTypeOptions: SelectOption[] = [
    {
      label: getString('secrets.secret.labelText'),
      value: 'SecretText'
    },
    {
      label: getString('secrets.secret.labelFile'),
      value: 'SecretFile'
    }
  ]
 
  const defaultSecretType = secretTypeOptions.findIndex(val => val.value === props.type)
  // defaultSecretType is -1 i.e. the type in props does not match the secretTypeOptions
  const [secretType, setSecretType] = React.useState<SelectOption>(
    secretTypeOptions[defaultSecretType === -1 ? 0 : defaultSecretType]
  )
 
  const inputDependencies = inputs?.length ? inputs.concat([secretType]) : [secretType]
 
  const { openCreateSecretModal } = useCreateUpdateSecretModal({
    onSuccess: data => {
      const secret = {
        ...data,
        scope: getScopeFromDTO<SecretFormData>(data)
      }
      /* istanbul ignore next */
      props.onSuccess?.({
        ...pick(secret, ['name', 'identifier', 'orgIdentifier', 'projectIdentifier']),
        referenceString: getReference(secret.scope, secret.identifier) as string
      })
      hideModal()
    }
  })
 
  const [showModal, hideModal] = useModalHook(() => {
    return (
      <Dialog
        isOpen={true}
        enforceFocus={false}
        onClose={() => {
          hideModal()
        }}
        title={ReferenceSelectDialogTitle({
          componentName: getString('secretType'),
          createNewLabel:
            props.type === SecretTypeEnum.SSH_KEY
              ? getString('secrets.secret.newSSHCredential')
              : props.type === SecretTypeEnum.SECRET_TEXT || secretType.value === SecretTypeEnum.SECRET_TEXT
              ? getString('secrets.secret.newSecretText')
              : getString('secrets.secret.newSecretFile'),
          createNewHandler: () => {
            if (props.type === SecretTypeEnum.SSH_KEY) {
              props.handleInlineSSHSecretCreation?.()
              hideModal()
            } else {
              openCreateSecretModal(
                props.type === SecretTypeEnum.SECRET_TEXT || secretType.value === SecretTypeEnum.SECRET_TEXT
                  ? SecretTypeEnum.SECRET_TEXT
                  : SecretTypeEnum.SECRET_FILE
              )
            }
          }
        })}
        className={cx(css.createSelectSecret, css.dialog)}
      >
        <CreateOrSelectSecret
          {...props}
          onCancel={hideModal}
          onSuccess={data => {
            const secret = {
              ...pick(data, ['name', 'identifier', 'orgIdentifier', 'projectIdentifier']),
              referenceString: getReference(getScopeFromDTO(data), data.identifier) as string
            }
            /* istanbul ignore next */
            props.onSuccess?.(secret)
            hideModal()
          }}
          connectorTypeContext={props.connectorTypeContext}
          handleInlineSSHSecretCreation={() => {
            props.handleInlineSSHSecretCreation?.()
            hideModal()
          }}
          secretType={secretType}
          setSecretType={setSecretType}
        />
      </Dialog>
    )
  }, inputDependencies)
 
  return {
    openCreateOrSelectSecretModal: () => {
      showModal()
    },
    closeCreateOrSelectSecretModal: hideModal
  }
}
 
export default useCreateOrSelectSecretModal