@@ -16,13 +16,16 @@ import { DEFAULT_VALUE_MAX_LEN, getMaxVarNameLength } from '@/config'
1616import { checkKeys , getNewVar } from '@/utils/var'
1717import Switch from '@/app/components/base/switch'
1818import Toast from '@/app/components/base/toast'
19+ import { Timeout } from 'ahooks/lib/useRequest/src/types'
1920
2021export type IConfigVarProps = {
2122 promptVariables : PromptVariable [ ]
2223 readonly ?: boolean
2324 onPromptVariablesChange ?: ( promptVariables : PromptVariable [ ] ) => void
2425}
2526
27+ let conflictTimer : Timeout
28+
2629const ConfigVar : FC < IConfigVarProps > = ( { promptVariables, readonly, onPromptVariablesChange } ) => {
2730 const { t } = useTranslation ( )
2831 const hasVar = promptVariables . length > 0
@@ -34,11 +37,9 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
3437 return obj
3538 } ) ( )
3639
37- const updatePromptVariable = ( key : string , updateKey : string , newValue : any ) => {
38- if ( ! ( key in promptVariableObj ) )
39- return
40- const newPromptVariables = promptVariables . map ( ( item ) => {
41- if ( item . key === key ) {
40+ const updatePromptVariable = ( index : number , updateKey : string , newValue : any ) => {
41+ const newPromptVariables = promptVariables . map ( ( item , i ) => {
42+ if ( i === index ) {
4243 return {
4344 ...item ,
4445 [ updateKey ] : newValue ,
@@ -51,11 +52,9 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
5152 onPromptVariablesChange ?.( newPromptVariables )
5253 }
5354
54- const batchUpdatePromptVariable = ( key : string , updateKeys : string [ ] , newValues : any [ ] ) => {
55- if ( ! ( key in promptVariableObj ) )
56- return
57- const newPromptVariables = promptVariables . map ( ( item ) => {
58- if ( item . key === key ) {
55+ const batchUpdatePromptVariable = ( index : number , updateKeys : string [ ] , newValues : any [ ] ) => {
56+ const newPromptVariables = promptVariables . map ( ( item , i ) => {
57+ if ( i === index ) {
5958 const newItem : any = { ...item }
6059 updateKeys . forEach ( ( updateKey , i ) => {
6160 newItem [ updateKey ] = newValues [ i ]
@@ -68,8 +67,8 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
6867
6968 onPromptVariablesChange ?.( newPromptVariables )
7069 }
71-
7270 const updatePromptKey = ( index : number , newKey : string ) => {
71+ clearTimeout ( conflictTimer )
7372 const { isValid, errorKey, errorMessageKey } = checkKeys ( [ newKey ] , true )
7473 if ( ! isValid ) {
7574 Toast . notify ( {
@@ -78,17 +77,28 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
7877 } )
7978 return
8079 }
80+
8181 const newPromptVariables = promptVariables . map ( ( item , i ) => {
8282 if ( i === index ) {
8383 return {
8484 ...item ,
8585 key : newKey ,
8686 }
8787 }
88-
8988 return item
9089 } )
9190
91+ conflictTimer = setTimeout ( ( ) => {
92+ const isKeyExists = promptVariables . some ( item => item . key . trim ( ) === newKey . trim ( ) )
93+ if ( isKeyExists ) {
94+ Toast . notify ( {
95+ type : 'error' ,
96+ message : t ( `appDebug.varKeyError.keyAlreadyExists` , { key : newKey } ) ,
97+ } )
98+ return
99+ }
100+ } , 1000 )
101+
92102 onPromptVariablesChange ?.( newPromptVariables )
93103 }
94104
@@ -196,7 +206,7 @@ const ConfigVar: FC<IConfigVarProps> = ({ promptVariables, readonly, onPromptVar
196206 type = "text"
197207 placeholder = { key }
198208 value = { name }
199- onChange = { e => updatePromptVariable ( key , 'name' , e . target . value ) }
209+ onChange = { e => updatePromptVariable ( index , 'name' , e . target . value ) }
200210 maxLength = { getMaxVarNameLength ( name ) }
201211 className = "h-6 leading-6 block w-full rounded-md border-0 py-1.5 text-gray-900 placeholder:text-gray-400 focus:outline-none focus:ring-1 focus:ring-inset focus:ring-gray-200"
202212 /> )
0 commit comments