variable-modal.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React, { useEffect } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { v4 as uuid4 } from 'uuid'
  4. import { RiCloseLine } from '@remixicon/react'
  5. import { useContext } from 'use-context-selector'
  6. import Button from '@/app/components/base/button'
  7. import Input from '@/app/components/base/input'
  8. import Tooltip from '@/app/components/base/tooltip'
  9. import { ToastContext } from '@/app/components/base/toast'
  10. import { useStore } from '@/app/components/workflow/store'
  11. import type { EnvironmentVariable } from '@/app/components/workflow/types'
  12. import cn from '@/utils/classnames'
  13. import { checkKeys } from '@/utils/var'
  14. export type ModalPropsType = {
  15. env?: EnvironmentVariable
  16. onClose: () => void
  17. onSave: (env: EnvironmentVariable) => void
  18. }
  19. const VariableModal = ({
  20. env,
  21. onClose,
  22. onSave,
  23. }: ModalPropsType) => {
  24. const { t } = useTranslation()
  25. const { notify } = useContext(ToastContext)
  26. const envList = useStore(s => s.environmentVariables)
  27. const envSecrets = useStore(s => s.envSecrets)
  28. const [type, setType] = React.useState<'string' | 'number' | 'secret'>('string')
  29. const [name, setName] = React.useState('')
  30. const [value, setValue] = React.useState<any>()
  31. const checkVariableName = (value: string) => {
  32. const { isValid, errorMessageKey } = checkKeys([value], false)
  33. if (!isValid) {
  34. notify({
  35. type: 'error',
  36. message: t(`appDebug.varKeyError.${errorMessageKey}`, { key: t('workflow.env.modal.name') }),
  37. })
  38. return false
  39. }
  40. return true
  41. }
  42. const handleSave = () => {
  43. if (!checkVariableName(name))
  44. return
  45. if (!value)
  46. return notify({ type: 'error', message: 'value can not be empty' })
  47. if (!env && envList.some(env => env.name === name))
  48. return notify({ type: 'error', message: 'name is existed' })
  49. onSave({
  50. id: env ? env.id : uuid4(),
  51. value_type: type,
  52. name,
  53. value: type === 'number' ? Number(value) : value,
  54. })
  55. onClose()
  56. }
  57. useEffect(() => {
  58. if (env) {
  59. setType(env.value_type)
  60. setName(env.name)
  61. setValue(env.value_type === 'secret' ? envSecrets[env.id] : env.value)
  62. }
  63. }, [env, envSecrets])
  64. return (
  65. <div
  66. className={cn('flex flex-col w-[360px] bg-components-panel-bg rounded-2xl h-full border-[0.5px] border-components-panel-border shadow-2xl')}
  67. >
  68. <div className='shrink-0 flex items-center justify-between mb-3 p-4 pb-0 text-text-primary system-xl-semibold'>
  69. {!env ? t('workflow.env.modal.title') : t('workflow.env.modal.editTitle')}
  70. <div className='flex items-center'>
  71. <div
  72. className='flex items-center justify-center w-6 h-6 cursor-pointer'
  73. onClick={onClose}
  74. >
  75. <RiCloseLine className='w-4 h-4 text-text-tertiary' />
  76. </div>
  77. </div>
  78. </div>
  79. <div className='px-4 py-2'>
  80. {/* type */}
  81. <div className='mb-4'>
  82. <div className='mb-1 h-6 flex items-center text-text-secondary system-sm-semibold'>{t('workflow.env.modal.type')}</div>
  83. <div className='flex gap-2'>
  84. <div className={cn(
  85. 'w-[106px] flex items-center justify-center p-2 radius-md bg-components-option-card-option-bg border border-components-option-card-option-border text-text-secondary system-sm-regular cursor-pointer hover:shadow-xs hover:bg-components-option-card-option-bg-hover hover:border-components-option-card-option-border-hover',
  86. type === 'string' && 'text-text-primary system-sm-medium border-[1.5px] shadow-xs bg-components-option-card-option-selected-bg border-components-option-card-option-selected-border hover:border-components-option-card-option-selected-border',
  87. )} onClick={() => setType('string')}>String</div>
  88. <div className={cn(
  89. 'w-[106px] flex items-center justify-center p-2 radius-md bg-components-option-card-option-bg border border-components-option-card-option-border text-text-secondary system-sm-regular cursor-pointer hover:shadow-xs hover:bg-components-option-card-option-bg-hover hover:border-components-option-card-option-border-hover',
  90. type === 'number' && 'text-text-primary font-medium border-[1.5px] shadow-xs bg-components-option-card-option-selected-bg border-components-option-card-option-selected-border hover:border-components-option-card-option-selected-border',
  91. )} onClick={() => {
  92. setType('number')
  93. if (!(/^[0-9]$/).test(value))
  94. setValue('')
  95. }}>Number</div>
  96. <div className={cn(
  97. 'w-[106px] flex items-center justify-center p-2 radius-md bg-components-option-card-option-bg border border-components-option-card-option-border text-text-secondary system-sm-regular cursor-pointer hover:shadow-xs hover:bg-components-option-card-option-bg-hover hover:border-components-option-card-option-border-hover',
  98. type === 'secret' && 'text-text-primary font-medium border-[1.5px] shadow-xs bg-components-option-card-option-selected-bg border-components-option-card-option-selected-border hover:border-components-option-card-option-selected-border',
  99. )} onClick={() => setType('secret')}>
  100. <span>Secret</span>
  101. <Tooltip
  102. popupContent={
  103. <div className='w-[240px]'>
  104. {t('workflow.env.modal.secretTip')}
  105. </div>
  106. }
  107. triggerClassName='ml-0.5 w-3.5 h-3.5'
  108. />
  109. </div>
  110. </div>
  111. </div>
  112. {/* name */}
  113. <div className='mb-4'>
  114. <div className='mb-1 h-6 flex items-center text-text-secondary system-sm-semibold'>{t('workflow.env.modal.name')}</div>
  115. <div className='flex'>
  116. <Input
  117. placeholder={t('workflow.env.modal.namePlaceholder') || ''}
  118. value={name}
  119. onChange={e => setName(e.target.value || '')}
  120. onBlur={e => checkVariableName(e.target.value)}
  121. type='text'
  122. />
  123. </div>
  124. </div>
  125. {/* value */}
  126. <div className=''>
  127. <div className='mb-1 h-6 flex items-center text-text-secondary system-sm-semibold'>{t('workflow.env.modal.value')}</div>
  128. <div className='flex'>
  129. <Input
  130. placeholder={t('workflow.env.modal.valuePlaceholder') || ''}
  131. value={value}
  132. onChange={e => setValue(e.target.value)}
  133. type={type !== 'number' ? 'text' : 'number'}
  134. />
  135. </div>
  136. </div>
  137. </div>
  138. <div className='p-4 pt-2 flex flex-row-reverse rounded-b-2xl'>
  139. <div className='flex gap-2'>
  140. <Button onClick={onClose}>{t('common.operation.cancel')}</Button>
  141. <Button variant='primary' onClick={handleSave}>{t('common.operation.save')}</Button>
  142. </div>
  143. </div>
  144. </div>
  145. )
  146. }
  147. export default VariableModal