| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | import React, { useEffect } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { v4 as uuid4 } from 'uuid' | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  | import { RiCloseLine } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | import { useContext } from 'use-context-selector' | 
					
						
							|  |  |  | import Button from '@/app/components/base/button' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import Input from '@/app/components/base/input' | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  | import Tooltip from '@/app/components/base/tooltip' | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | 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' | 
					
						
							| 
									
										
										
										
											2024-08-23 15:08:34 +08:00
										 |  |  | import { checkKeys } from '@/utils/var' | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  | export type ModalPropsType = { | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |   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<any>() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-23 15:08:34 +08:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleSave = () => { | 
					
						
							| 
									
										
										
										
											2024-08-23 15:08:34 +08:00
										 |  |  |     if (!checkVariableName(name)) | 
					
						
							|  |  |  |       return | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     if (!value) | 
					
						
							|  |  |  |       return notify({ type: 'error', message: 'value can not be empty' }) | 
					
						
							| 
									
										
										
										
											2025-04-23 15:55:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Add check for duplicate name when editing
 | 
					
						
							|  |  |  |     if (env && env.name !== name && envList.some(e => e.name === name)) | 
					
						
							|  |  |  |       return notify({ type: 'error', message: 'name is existed' }) | 
					
						
							|  |  |  |     // Original check for create new variable
 | 
					
						
							|  |  |  |     if (!env && envList.some(e => e.name === name)) | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |       return notify({ type: 'error', message: 'name is existed' }) | 
					
						
							| 
									
										
										
										
											2025-04-23 15:55:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     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 ( | 
					
						
							|  |  |  |     <div | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       className={cn('flex h-full w-[360px] flex-col rounded-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-2xl')} | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |     > | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className='system-xl-semibold mb-3 flex shrink-0 items-center justify-between p-4 pb-0 text-text-primary'> | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |         {!env ? t('workflow.env.modal.title') : t('workflow.env.modal.editTitle')} | 
					
						
							|  |  |  |         <div className='flex items-center'> | 
					
						
							|  |  |  |           <div | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             className='flex h-6 w-6 cursor-pointer items-center justify-center' | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |             onClick={onClose} | 
					
						
							|  |  |  |           > | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <RiCloseLine className='h-4 w-4 text-text-tertiary' /> | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |           </div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <div className='px-4 py-2'> | 
					
						
							|  |  |  |         {/* type */} | 
					
						
							|  |  |  |         <div className='mb-4'> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='system-sm-semibold mb-1 flex h-6 items-center text-text-secondary'>{t('workflow.env.modal.type')}</div> | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |           <div className='flex gap-2'> | 
					
						
							|  |  |  |             <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               'radius-md system-sm-regular flex w-[106px] cursor-pointer items-center justify-center border border-components-option-card-option-border bg-components-option-card-option-bg p-2 text-text-secondary hover:border-components-option-card-option-border-hover hover:bg-components-option-card-option-bg-hover hover:shadow-xs', | 
					
						
							|  |  |  |               type === 'string' && 'system-sm-medium border-[1.5px] border-components-option-card-option-selected-border bg-components-option-card-option-selected-bg text-text-primary shadow-xs hover:border-components-option-card-option-selected-border', | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |             )} onClick={() => setType('string')}>String</div> | 
					
						
							|  |  |  |             <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               'radius-md system-sm-regular flex w-[106px] cursor-pointer items-center justify-center border border-components-option-card-option-border bg-components-option-card-option-bg p-2 text-text-secondary hover:border-components-option-card-option-border-hover hover:bg-components-option-card-option-bg-hover hover:shadow-xs', | 
					
						
							|  |  |  |               type === 'number' && 'border-[1.5px] border-components-option-card-option-selected-border bg-components-option-card-option-selected-bg font-medium text-text-primary shadow-xs hover:border-components-option-card-option-selected-border', | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |             )} onClick={() => { | 
					
						
							|  |  |  |               setType('number') | 
					
						
							| 
									
										
										
										
											2025-04-14 15:28:20 +08:00
										 |  |  |               if (!(/^\d$/).test(value)) | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |                 setValue('') | 
					
						
							|  |  |  |             }}>Number</div> | 
					
						
							|  |  |  |             <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               'radius-md system-sm-regular flex w-[106px] cursor-pointer items-center justify-center border border-components-option-card-option-border bg-components-option-card-option-bg p-2 text-text-secondary hover:border-components-option-card-option-border-hover hover:bg-components-option-card-option-bg-hover hover:shadow-xs', | 
					
						
							|  |  |  |               type === 'secret' && 'border-[1.5px] border-components-option-card-option-selected-border bg-components-option-card-option-selected-bg font-medium text-text-primary shadow-xs hover:border-components-option-card-option-selected-border', | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |             )} onClick={() => setType('secret')}> | 
					
						
							|  |  |  |               <span>Secret</span> | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |               <Tooltip | 
					
						
							|  |  |  |                 popupContent={ | 
					
						
							|  |  |  |                   <div className='w-[240px]'> | 
					
						
							|  |  |  |                     {t('workflow.env.modal.secretTip')} | 
					
						
							|  |  |  |                   </div> | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 triggerClassName='ml-0.5 w-3.5 h-3.5' | 
					
						
							|  |  |  |               /> | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |         {/* name */} | 
					
						
							|  |  |  |         <div className='mb-4'> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='system-sm-semibold mb-1 flex h-6 items-center text-text-secondary'>{t('workflow.env.modal.name')}</div> | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |           <div className='flex'> | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |             <Input | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |               placeholder={t('workflow.env.modal.namePlaceholder') || ''} | 
					
						
							|  |  |  |               value={name} | 
					
						
							| 
									
										
										
										
											2024-08-23 15:08:34 +08:00
										 |  |  |               onChange={e => setName(e.target.value || '')} | 
					
						
							|  |  |  |               onBlur={e => checkVariableName(e.target.value)} | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |               type='text' | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |         {/* value */} | 
					
						
							|  |  |  |         <div className=''> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='system-sm-semibold mb-1 flex h-6 items-center text-text-secondary'>{t('workflow.env.modal.value')}</div> | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |           <div className='flex'> | 
					
						
							| 
									
										
										
										
											2025-04-11 15:24:16 +08:00
										 |  |  |             { | 
					
						
							|  |  |  |               type !== 'number' ? <textarea | 
					
						
							| 
									
										
										
										
											2025-06-17 16:11:57 +08:00
										 |  |  |                 className='system-sm-regular placeholder:system-sm-regular block h-20 w-full resize-none appearance-none rounded-lg border border-transparent bg-components-input-bg-normal p-2 text-components-input-text-filled caret-primary-600 outline-none placeholder:text-components-input-text-placeholder hover:border-components-input-border-hover hover:bg-components-input-bg-hover focus:border-components-input-border-active focus:bg-components-input-bg-active focus:shadow-xs' | 
					
						
							| 
									
										
										
										
											2025-04-11 15:24:16 +08:00
										 |  |  |                 value={value} | 
					
						
							|  |  |  |                 placeholder={t('workflow.env.modal.valuePlaceholder') || ''} | 
					
						
							|  |  |  |                 onChange={e => setValue(e.target.value)} | 
					
						
							|  |  |  |               /> | 
					
						
							|  |  |  |                 : <Input | 
					
						
							|  |  |  |                   placeholder={t('workflow.env.modal.valuePlaceholder') || ''} | 
					
						
							|  |  |  |                   value={value} | 
					
						
							|  |  |  |                   onChange={e => setValue(e.target.value)} | 
					
						
							|  |  |  |                   type="number" | 
					
						
							|  |  |  |                 /> | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |           </div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className='flex flex-row-reverse rounded-b-2xl p-4 pt-2'> | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |         <div className='flex gap-2'> | 
					
						
							|  |  |  |           <Button onClick={onClose}>{t('common.operation.cancel')}</Button> | 
					
						
							|  |  |  |           <Button variant='primary' onClick={handleSave}>{t('common.operation.save')}</Button> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default VariableModal |