import React, { useEffect } from 'react' import { useTranslation } from 'react-i18next' import { v4 as uuid4 } from 'uuid' import { RiCloseLine } from '@remixicon/react' import { useContext } from 'use-context-selector' import Button from '@/app/components/base/button' import Input from '@/app/components/base/input' import Tooltip from '@/app/components/base/tooltip' import { ToastContext } from '@/app/components/base/toast' import { useStore } from '@/app/components/workflow/store' import type { EnvironmentVariable } from '@/app/components/workflow/types' import cn from '@/utils/classnames' import { checkKeys } from '@/utils/var' export type ModalPropsType = { env?: EnvironmentVariable onClose: () => void onSave: (env: EnvironmentVariable) => void } const VariableModal = ({ env, onClose, onSave, }: ModalPropsType) => { const { t } = useTranslation() const { notify } = useContext(ToastContext) const envList = useStore(s => s.environmentVariables) const envSecrets = useStore(s => s.envSecrets) const [type, setType] = React.useState<'string' | 'number' | 'secret'>('string') const [name, setName] = React.useState('') const [value, setValue] = React.useState() const checkVariableName = (value: string) => { const { isValid, errorMessageKey } = checkKeys([value], false) if (!isValid) { notify({ type: 'error', message: t(`appDebug.varKeyError.${errorMessageKey}`, { key: t('workflow.env.modal.name') }), }) return false } return true } const handleSave = () => { if (!checkVariableName(name)) return if (!value) return notify({ type: 'error', message: 'value can not be empty' }) if (!env && envList.some(env => env.name === name)) return notify({ type: 'error', message: 'name is existed' }) onSave({ id: env ? env.id : uuid4(), value_type: type, name, value: type === 'number' ? Number(value) : value, }) onClose() } useEffect(() => { if (env) { setType(env.value_type) setName(env.name) setValue(env.value_type === 'secret' ? envSecrets[env.id] : env.value) } }, [env, envSecrets]) return (
{!env ? t('workflow.env.modal.title') : t('workflow.env.modal.editTitle')}
{/* type */}
{t('workflow.env.modal.type')}
setType('string')}>String
{ setType('number') if (!(/^[0-9]$/).test(value)) setValue('') }}>Number
setType('secret')}> Secret {t('workflow.env.modal.secretTip')}
} triggerClassName='ml-0.5 w-3.5 h-3.5' />
{/* name */}
{t('workflow.env.modal.name')}
setName(e.target.value || '')} onBlur={e => checkVariableName(e.target.value)} type='text' />
{/* value */}
{t('workflow.env.modal.value')}
setValue(e.target.value)} type={type !== 'number' ? 'text' : 'number'} />
) } export default VariableModal