import React, { useMemo, useState } from 'react'; import { Form } from '@patternfly/react-core'; import { useField, useFormikContext } from 'formik'; import { FIELD_SECRET_FOR_COMPONENT_OPTION, SecretLinkOptionLabels, DEFAULT_OPAQUE_KEY_VALUES, DEFAULT_OPAQUE_LABELS, } from '~/consts/secrets'; import { InputField } from '~/shared/components/formik-base'; import KeyValueInputField from '~/shared/components/formik-fields/key-value-input-field/KeyValueInputField'; import { supportedPartnerTasksSecrets, isPartnerTask, isUsingExistingClusterSecret, SecretForComponentOption, } from '~/utils/secrets/secret-utils'; import { DropdownItemObject } from '../../shared/components/dropdown'; import KeyValueFileInputField from '../../shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField'; import SelectInputField from '../../shared/components/formik-fields/SelectInputField'; import { SecretFormValues, SecretTypeDropdownLabel, K8sSecretType, BuildTimeSecret, SourceSecretType, CurrentComponentRef, } from '../../types'; import { RawComponentProps } from '../modal/createModalLauncher'; import { useOpaqueSecretSync } from './hooks/useOpaqueSecretSync'; import { ImagePullSecretForm } from './SecretsForm/ImagePullSecretForm'; import { SecretLinkOptions } from './SecretsForm/SecretLinkOption'; import { SourceSecretForm } from './SecretsForm/SourceSecretForm'; import SecretTypeSelector from './SecretTypeSelector'; type SecretFormProps = RawComponentProps & { existingSecrets: BuildTimeSecret[]; currentComponent?: null | CurrentComponentRef; isEdit?: boolean; }; const SecretForm: React.FC> = ({ existingSecrets, currentComponent, isEdit = false, }) => { const { values, setFieldValue } = useFormikContext(); const [currentType, setCurrentType] = useState(values.type); const [{ value: secretForComponentOption }, , { setValue }] = useField( FIELD_SECRET_FOR_COMPONENT_OPTION, ); const { clearReadOnlyKeys, resetOpaqueFields, resetKeyValues, populateFromExistingOpaqueSecret } = useOpaqueSecretSync({ currentType, existingSecrets, }); const options = useMemo(() => { return existingSecrets .filter((secret) => secret.type === K8sSecretType[currentType]) .concat( currentType === SecretTypeDropdownLabel.opaque && existingSecrets.find((s) => s.name === 'snyk-secret') === undefined ? [supportedPartnerTasksSecrets.snyk] : [], ) .filter((secret) => secret.type !== K8sSecretType[SecretTypeDropdownLabel.image]) .map((secret) => ({ value: secret.name })); }, [currentType, existingSecrets]); const optionsValues = useMemo((): Record => { return existingSecrets .filter((secret) => secret.type === K8sSecretType[currentType]) .filter((secret) => secret.type !== K8sSecretType[SecretTypeDropdownLabel.image]) .reduce>( (dictOfSecrets, secret) => { dictOfSecrets[secret.name] = secret; return dictOfSecrets; }, { 'snyk-secret': supportedPartnerTasksSecrets.snyk }, ); }, [currentType, existingSecrets]); const isUsingExisting = isUsingExistingClusterSecret( values.secretName, currentType, existingSecrets, ); const isOpaqueNameSelection = isPartnerTask(values.secretName, supportedPartnerTasksSecrets) || isUsingExisting; const dropdownItems: DropdownItemObject[] = Object.entries(SecretTypeDropdownLabel).reduce( (acc, [key, value]) => { acc.push({ key, value }); return acc; }, [], ); const shouldShowSecretLinkOptions = !isUsingExisting && ((values?.source?.authType === SourceSecretType.basic && currentType === SecretTypeDropdownLabel.source) || currentType === SecretTypeDropdownLabel.image); const isIdentityLocked = isEdit && isUsingExisting; const secretNameHelperText = isIdentityLocked ? 'You cannot edit the existing secret name' : isUsingExisting ? 'Reusing an existing cluster secret.' : 'Unique name of the new secret.'; return (
{ setCurrentType(type); void setValue(SecretForComponentOption.none); if (type === SecretTypeDropdownLabel.image) { resetKeyValues(); values.secretName && isPartnerTask(values.secretName, optionsValues) && void setFieldValue('secretName', ''); } else if (type === SecretTypeDropdownLabel.source) { resetKeyValues(); values.secretName && isPartnerTask(values.secretName) && void setFieldValue('secretName', ''); } else { clearReadOnlyKeys(); } }} /> {currentType === SecretTypeDropdownLabel.opaque ? ( { if (currentType !== values.type || isOpaqueNameSelection) { resetOpaqueFields(); } }} onSelect={(_, value: string) => { if ( isPartnerTask(value, supportedPartnerTasksSecrets) || isUsingExistingClusterSecret(value, currentType, existingSecrets) ) { populateFromExistingOpaqueSecret(value); } void setFieldValue('secretName', value); }} /> ) : ( )} {shouldShowSecretLinkOptions && ( setValue(option)} radioLabels={SecretLinkOptionLabels.default} /> )} {currentType === SecretTypeDropdownLabel.source && ( )} {currentType === SecretTypeDropdownLabel.image && } {currentType === SecretTypeDropdownLabel.opaque && ( )} ); }; export default SecretForm;