1
- import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
1
+ import React , { useCallback , useMemo , useState } from 'react' ;
2
2
import type { RenderInputProps } from '../type' ;
3
3
import { Box , BoxProps , Button , Flex , ModalFooter , useDisclosure } from '@chakra-ui/react' ;
4
4
import MyModal from '@fastgpt/web/components/common/MyModal' ;
@@ -43,30 +43,22 @@ const selectTemplateBtn: BoxProps = {
43
43
cursor : 'pointer'
44
44
} ;
45
45
46
- const SettingQuotePrompt = ( props : RenderInputProps ) => {
46
+ const EditModal = ( { onClose , ... props } : RenderInputProps & { onClose : ( ) => void } ) => {
47
47
const { inputs = [ ] , nodeId } = props ;
48
48
const { t } = useTranslation ( ) ;
49
- const { isOpen, onOpen, onClose } = useDisclosure ( ) ;
50
49
const onChangeNode = useContextSelector ( WorkflowContext , ( v ) => v . onChangeNode ) ;
51
50
const nodeList = useContextSelector ( WorkflowContext , ( v ) => v . nodeList ) ;
52
51
53
- const defaultValues = useMemo ( ( ) => {
54
- return {
52
+ const { watch , setValue , handleSubmit } = useForm ( {
53
+ defaultValues : {
55
54
quoteTemplate :
56
55
inputs . find ( ( input ) => input . key === NodeInputKeyEnum . aiChatQuoteTemplate ) ?. value || '' ,
57
56
quotePrompt :
58
57
inputs . find ( ( input ) => input . key === NodeInputKeyEnum . aiChatQuotePrompt ) ?. value || '' ,
59
58
quoteRole : ( inputs . find ( ( input ) => input . key === NodeInputKeyEnum . aiChatQuoteRole ) ?. value ||
60
59
'system' ) as AiChatQuoteRoleType
61
- } ;
62
- } , [ inputs ] ) ;
63
-
64
- const { watch, setValue, handleSubmit, reset } = useForm ( { defaultValues } ) ;
65
-
66
- useEffect ( ( ) => {
67
- reset ( defaultValues ) ;
68
- } , [ defaultValues , reset ] ) ;
69
-
60
+ }
61
+ } ) ;
70
62
const aiChatQuoteTemplate = watch ( 'quoteTemplate' ) ;
71
63
const aiChatQuotePrompt = watch ( 'quotePrompt' ) ;
72
64
const aiChatQuoteRole = watch ( 'quoteRole' ) ;
@@ -175,6 +167,137 @@ const SettingQuotePrompt = (props: RenderInputProps) => {
175
167
const quotePromptTemplates =
176
168
aiChatQuoteRole === 'user' ? Prompt_userQuotePromptList : Prompt_systemQuotePromptList ;
177
169
170
+ return (
171
+ < >
172
+ < MyModal
173
+ isOpen
174
+ iconSrc = { 'modal/edit' }
175
+ title = { t ( 'workflow:Quote_prompt_setting' ) }
176
+ w = { '100%' }
177
+ h = { [ '90vh' , '85vh' ] }
178
+ maxW = { [ '90vw' , '700px' ] }
179
+ isCentered
180
+ >
181
+ < ModalBody flex = { '1 0 0' } overflow = { 'auto' } >
182
+ < Flex { ...LabelStyles } alignItems = { 'center' } >
183
+ < FormLabel > { t ( 'workflow:dataset_quote_role' ) } </ FormLabel >
184
+ < QuestionTip label = { t ( 'workflow:dataset_quote_role_tip' ) } ml = { 1 } mr = { 5 } />
185
+ < MySelect < AiChatQuoteRoleType >
186
+ value = { aiChatQuoteRole }
187
+ list = { [
188
+ {
189
+ label : 'System' ,
190
+ value : 'system' ,
191
+ description : t ( 'workflow:dataset_quote_role_system_option_desc' )
192
+ } ,
193
+ {
194
+ label : 'User' ,
195
+ value : 'user' ,
196
+ description : t ( 'workflow:dataset_quote_role_user_option_desc' )
197
+ }
198
+ ] }
199
+ onchange = { ( e ) => {
200
+ setValue ( 'quoteRole' , e ) ;
201
+ } }
202
+ />
203
+ < Box ml = { 5 } >
204
+ { aiChatQuoteRole === 'user' ? (
205
+ < LightTip text = { t ( 'workflow:quote_role_user_tip' ) } />
206
+ ) : (
207
+ < LightTip text = { t ( 'workflow:quote_role_system_tip' ) } />
208
+ ) }
209
+ </ Box >
210
+ </ Flex >
211
+ < Box mt = { 4 } >
212
+ < Flex { ...LabelStyles } mb = { 1 } >
213
+ < FormLabel > { t ( 'common:core.app.Quote templates' ) } </ FormLabel >
214
+ < QuestionTip
215
+ ml = { 1 }
216
+ label = { t ( 'workflow:quote_content_tip' , {
217
+ default : Prompt_QuoteTemplateList [ 0 ] . value
218
+ } ) }
219
+ > </ QuestionTip >
220
+ < Box flex = { 1 } />
221
+ < Box
222
+ { ...selectTemplateBtn }
223
+ fontSize = { 'sm' }
224
+ onClick = { ( ) =>
225
+ setSelectTemplateData ( {
226
+ title : t ( 'common:core.app.Select quote template' ) ,
227
+ templates : Prompt_QuoteTemplateList
228
+ } )
229
+ }
230
+ >
231
+ { t ( 'common:common.Select template' ) }
232
+ </ Box >
233
+ </ Flex >
234
+
235
+ < PromptEditor
236
+ variables = { quoteTemplateVariables }
237
+ minH = { 160 }
238
+ title = { t ( 'common:core.app.Quote templates' ) }
239
+ placeholder = { t ( 'workflow:quote_content_placeholder' ) }
240
+ value = { aiChatQuoteTemplate }
241
+ onChange = { ( e ) => {
242
+ setValue ( 'quoteTemplate' , e ) ;
243
+ } }
244
+ />
245
+ </ Box >
246
+ < Box mt = { 4 } >
247
+ < Flex { ...LabelStyles } mb = { 1 } >
248
+ < FormLabel > { t ( 'common:core.app.Quote prompt' ) } </ FormLabel >
249
+ < QuestionTip
250
+ ml = { 1 }
251
+ label = { t ( 'workflow:quote_prompt_tip' , {
252
+ default : quotePromptTemplates [ 0 ] . value
253
+ } ) }
254
+ > </ QuestionTip >
255
+ </ Flex >
256
+ < PromptEditor
257
+ variables = { quotePromptVariables }
258
+ title = { t ( 'common:core.app.Quote prompt' ) }
259
+ minH = { 300 }
260
+ placeholder = { t ( 'workflow:quote_prompt_tip' , {
261
+ default : quotePromptTemplates [ 0 ] . value
262
+ } ) }
263
+ value = { aiChatQuotePrompt }
264
+ onChange = { ( e ) => {
265
+ setValue ( 'quotePrompt' , e ) ;
266
+ } }
267
+ />
268
+ </ Box >
269
+ </ ModalBody >
270
+ < ModalFooter >
271
+ < Button variant = { 'whiteBase' } mr = { 2 } onClick = { onClose } >
272
+ { t ( 'common:common.Close' ) }
273
+ </ Button >
274
+ < Button onClick = { handleSubmit ( onSubmit ) } > { t ( 'common:common.Confirm' ) } </ Button >
275
+ </ ModalFooter >
276
+ </ MyModal >
277
+ { /* Prompt template */ }
278
+ { ! ! selectTemplateData && (
279
+ < PromptTemplate
280
+ title = { selectTemplateData . title }
281
+ templates = { selectTemplateData . templates }
282
+ onClose = { ( ) => setSelectTemplateData ( undefined ) }
283
+ onSuccess = { ( e ) => {
284
+ const quoteVal = e . value ;
285
+
286
+ const promptVal = quotePromptTemplates . find ( ( item ) => item . title === e . title ) ?. value ;
287
+
288
+ setValue ( 'quoteTemplate' , quoteVal ) ;
289
+ setValue ( 'quotePrompt' , promptVal ) ;
290
+ } }
291
+ />
292
+ ) }
293
+ </ >
294
+ ) ;
295
+ } ;
296
+
297
+ const SettingQuotePrompt = ( props : RenderInputProps ) => {
298
+ const { t } = useTranslation ( ) ;
299
+ const { isOpen, onOpen, onClose } = useDisclosure ( ) ;
300
+
178
301
const Render = useMemo ( ( ) => {
179
302
return (
180
303
< >
@@ -201,146 +324,10 @@ const SettingQuotePrompt = (props: RenderInputProps) => {
201
324
< Reference { ...props } />
202
325
</ Box >
203
326
204
- < MyModal
205
- isOpen = { isOpen }
206
- iconSrc = { 'modal/edit' }
207
- title = { t ( 'workflow:Quote_prompt_setting' ) }
208
- w = { '100%' }
209
- h = { [ '90vh' , '85vh' ] }
210
- maxW = { [ '90vw' , '700px' ] }
211
- isCentered
212
- >
213
- < ModalBody flex = { '1 0 0' } overflow = { 'auto' } >
214
- < Flex { ...LabelStyles } alignItems = { 'center' } >
215
- < FormLabel > { t ( 'workflow:dataset_quote_role' ) } </ FormLabel >
216
- < QuestionTip label = { t ( 'workflow:dataset_quote_role_tip' ) } ml = { 1 } mr = { 5 } />
217
- < MySelect < AiChatQuoteRoleType >
218
- value = { aiChatQuoteRole }
219
- list = { [
220
- {
221
- label : 'System' ,
222
- value : 'system' ,
223
- description : t ( 'workflow:dataset_quote_role_system_option_desc' )
224
- } ,
225
- {
226
- label : 'User' ,
227
- value : 'user' ,
228
- description : t ( 'workflow:dataset_quote_role_user_option_desc' )
229
- }
230
- ] }
231
- onchange = { ( e ) => {
232
- setValue ( 'quoteRole' , e ) ;
233
- } }
234
- />
235
- < Box ml = { 5 } >
236
- { aiChatQuoteRole === 'user' ? (
237
- < LightTip text = { t ( 'workflow:quote_role_user_tip' ) } />
238
- ) : (
239
- < LightTip text = { t ( 'workflow:quote_role_system_tip' ) } />
240
- ) }
241
- </ Box >
242
- </ Flex >
243
- < Box mt = { 4 } >
244
- < Flex { ...LabelStyles } mb = { 1 } >
245
- < FormLabel > { t ( 'common:core.app.Quote templates' ) } </ FormLabel >
246
- < QuestionTip
247
- ml = { 1 }
248
- label = { t ( 'workflow:quote_content_tip' , {
249
- default : Prompt_QuoteTemplateList [ 0 ] . value
250
- } ) }
251
- > </ QuestionTip >
252
- < Box flex = { 1 } />
253
- < Box
254
- { ...selectTemplateBtn }
255
- fontSize = { 'sm' }
256
- onClick = { ( ) =>
257
- setSelectTemplateData ( {
258
- title : t ( 'common:core.app.Select quote template' ) ,
259
- templates : Prompt_QuoteTemplateList
260
- } )
261
- }
262
- >
263
- { t ( 'common:common.Select template' ) }
264
- </ Box >
265
- </ Flex >
266
-
267
- < PromptEditor
268
- variables = { quoteTemplateVariables }
269
- minH = { 160 }
270
- title = { t ( 'common:core.app.Quote templates' ) }
271
- placeholder = { t ( 'workflow:quote_content_placeholder' ) }
272
- value = { aiChatQuoteTemplate }
273
- onChange = { ( e ) => {
274
- setValue ( 'quoteTemplate' , e ) ;
275
- } }
276
- />
277
- </ Box >
278
- < Box mt = { 4 } >
279
- < Flex { ...LabelStyles } mb = { 1 } >
280
- < FormLabel > { t ( 'common:core.app.Quote prompt' ) } </ FormLabel >
281
- < QuestionTip
282
- ml = { 1 }
283
- label = { t ( 'workflow:quote_prompt_tip' , {
284
- default : quotePromptTemplates [ 0 ] . value
285
- } ) }
286
- > </ QuestionTip >
287
- </ Flex >
288
- < PromptEditor
289
- variables = { quotePromptVariables }
290
- title = { t ( 'common:core.app.Quote prompt' ) }
291
- minH = { 300 }
292
- placeholder = { t ( 'workflow:quote_prompt_tip' , {
293
- default : quotePromptTemplates [ 0 ] . value
294
- } ) }
295
- value = { aiChatQuotePrompt }
296
- onChange = { ( e ) => {
297
- setValue ( 'quotePrompt' , e ) ;
298
- } }
299
- />
300
- </ Box >
301
- </ ModalBody >
302
- < ModalFooter >
303
- < Button variant = { 'whiteBase' } mr = { 2 } onClick = { onClose } >
304
- { t ( 'common:common.Close' ) }
305
- </ Button >
306
- < Button onClick = { handleSubmit ( onSubmit ) } > { t ( 'common:common.Confirm' ) } </ Button >
307
- </ ModalFooter >
308
- </ MyModal >
309
- { /* Prompt template */ }
310
- { ! ! selectTemplateData && (
311
- < PromptTemplate
312
- title = { selectTemplateData . title }
313
- templates = { selectTemplateData . templates }
314
- onClose = { ( ) => setSelectTemplateData ( undefined ) }
315
- onSuccess = { ( e ) => {
316
- const quoteVal = e . value ;
317
-
318
- const promptVal = quotePromptTemplates . find ( ( item ) => item . title === e . title ) ?. value ;
319
-
320
- setValue ( 'quoteTemplate' , quoteVal ) ;
321
- setValue ( 'quotePrompt' , promptVal ) ;
322
- } }
323
- />
324
- ) }
327
+ { isOpen && < EditModal { ...props } onClose = { onClose } /> }
325
328
</ >
326
329
) ;
327
- } , [
328
- aiChatQuotePrompt ,
329
- aiChatQuoteRole ,
330
- aiChatQuoteTemplate ,
331
- handleSubmit ,
332
- isOpen ,
333
- onClose ,
334
- onOpen ,
335
- onSubmit ,
336
- props ,
337
- quotePromptTemplates ,
338
- quotePromptVariables ,
339
- quoteTemplateVariables ,
340
- selectTemplateData ,
341
- setValue ,
342
- t
343
- ] ) ;
330
+ } , [ isOpen , onClose , onOpen , t ] ) ;
344
331
345
332
return Render ;
346
333
} ;
0 commit comments