All files / modules/30-secrets/modals/CreateSecretModal useCreateUpdateSecretModal.tsx

95.65% Statements 22/23
62.5% Branches 10/16
80% Functions 4/5
95.65% Lines 22/23

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              285x 285x 285x 285x 285x       285x         285x 285x                             285x 784x 784x 784x 1x 1x   784x 784x   2x                                                           784x   2x 2x 2x           285x  
/*
 * 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, { useState } from 'react'
import { Button, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useModalHook } from '@harness/use-modal'
import { Dialog } from '@blueprintjs/core'
 
import type { SecretDTOV2, ConnectorInfoDTO } from 'services/cd-ng'
 
import CreateUpdateSecret, {
  SecretIdentifiers,
  SecretFormData
} from '@secrets/components/CreateUpdateSecret/CreateUpdateSecret'
 
import { useStrings } from 'framework/strings'
import css from './useCreateSecretModal.module.scss'
 
type SecretType = SecretDTOV2['type']
 
export interface UseCreateSecretModalProps {
  onSuccess?: ((data: SecretFormData) => void) | (() => void)
  connectorTypeContext?: ConnectorInfoDTO['type']
  privateSecret?: boolean
}
 
export interface UseCreateSecretModalReturn {
  openCreateSecretModal: (type: SecretType, secret?: SecretIdentifiers) => void
  closeCreateSecretModal: () => void
}
 
const useCreateUpdateSecretModal = (props: UseCreateSecretModalProps): UseCreateSecretModalReturn => {
  const [type, setType] = useState<SecretType>()
  const [secret, setSecret] = useState<SecretIdentifiers>()
  const handleSuccess = (data: SecretFormData) => {
    hideModal()
    props.onSuccess?.(data)
  }
  const { getString } = useStrings()
  const [showModal, hideModal] = useModalHook(
    () => (
      <Dialog
        isOpen={true}
        enforceFocus={false}
        onClose={() => {
          hideModal()
        }}
        className={css.dialog}
      >
        <Text font={{ size: 'medium' }} color={Color.BLACK} margin={{ bottom: 'large' }}>
          {secret?.identifier
            ? !type || type === 'SecretText'
              ? getString('secrets.secret.titleEditText')
              : getString('secrets.secret.titleEditFile')
            : type === 'SecretText'
            ? getString('secrets.secret.titleCreateText')
            : getString('secrets.secret.titleCreateFile')}
        </Text>
        <CreateUpdateSecret
          secret={secret}
          type={type}
          onSuccess={handleSuccess}
          connectorTypeContext={props.connectorTypeContext}
          privateSecret={props.privateSecret}
        />
        <Button minimal icon="cross" iconProps={{ size: 18 }} onClick={hideModal} className={css.crossIcon} />
      </Dialog>
    ),
    [type, secret]
  )
 
  return {
    openCreateSecretModal: (_type: SecretType | undefined, _secret: SecretIdentifiers | undefined) => {
      setType(_type)
      setSecret(_secret)
      showModal()
    },
    closeCreateSecretModal: hideModal
  }
}
 
export default useCreateUpdateSecretModal