1
- import { useState } from 'react'
2
- import { Box , Paper , Typography , Button , Tooltip , TextField , Stack , FormControlLabel , Checkbox , IconButton , Link } from '@mui/material'
3
- import { ExpandLess , ExpandMore , Visibility , VisibilityOff , PriorityHigh , ContentCopyOutlined } from '@mui/icons-material'
1
+ import { Box , Paper , Typography , Button , Tooltip , IconButton , Link } from '@mui/material'
2
+ import { Visibility , VisibilityOff , PriorityHigh , ContentCopyOutlined , Delete , DeleteOutline , Book , BookOutlined , MenuBookOutlined } from '@mui/icons-material'
4
3
import { useTranslation } from 'react-i18next'
5
4
6
5
import { enqueueSnackbar } from 'notistack'
7
- import { Prompt as PromptType , SetState } from '../../../types'
8
- import { MessageItem } from '../../ChatV2/Conversation'
9
- import SystemMessage from '../../ChatV2/SystemMessage'
10
- import { useEditPromptMutation } from '../../../hooks/usePromptMutation'
6
+ import type { Prompt as PromptType } from '../../../types'
7
+ import { useDeletePromptMutation } from '../../../hooks/usePromptMutation'
11
8
import { useParams , Link as RouterLink } from 'react-router-dom'
12
9
import { IframeCopy } from '../../common/IframeCopy'
13
- import { PUBLIC_URL } from '../../../../config'
10
+ import { PUBLIC_URL } from '@config'
11
+ import { OutlineButtonBlack } from '../../ChatV2/general/Buttons'
14
12
15
- const ExpandButton = ( { expand, setExpand } : { expand : boolean ; setExpand : SetState < boolean > } ) => (
16
- < Button onClick = { ( ) => setExpand ( ! expand ) } > { expand ? < ExpandLess /> : < ExpandMore /> } </ Button >
17
- )
18
-
19
- const Prompt = ( { prompt, handleDelete, mandatoryPromptId } : { prompt : PromptType ; handleDelete : ( promptId : string ) => void ; mandatoryPromptId ?: string } ) => {
13
+ const Prompt = ( { prompt, handleEdit } : { prompt : PromptType ; handleEdit : ( ) => void } ) => {
20
14
const { t } = useTranslation ( )
21
15
const { id : courseId } = useParams ( )
22
- const mutation = useEditPromptMutation ( )
23
16
24
- const { id, name, systemMessage , messages , hidden, mandatory } = prompt
17
+ const { id, name, hidden, mandatory, ragIndexId } = prompt
25
18
26
- const [ expand , setExpand ] = useState ( false )
27
- const [ editPrompt , setEditPrompt ] = useState ( false )
28
- const [ message , setMessage ] = useState ( systemMessage )
29
- const [ updatedName , setUpdatedName ] = useState ( name )
30
- const [ updatedHidden , setUpdatedHidden ] = useState ( hidden )
31
- const [ updatedMandatory , setUpdatedMandatory ] = useState ( mandatory )
32
19
const chatPath = `/${ courseId } ?promptId=${ id } `
33
20
const directLink = `${ window . location . origin } ${ PUBLIC_URL } /${ chatPath } `
34
21
35
- const handleSave = async ( ) => {
36
- const updatedPrompt = {
37
- ...prompt ,
38
- systemMessage : message ,
39
- name : updatedName ,
40
- hidden : updatedHidden ,
41
- mandatory : updatedMandatory ,
42
- }
22
+ const deleteMutation = useDeletePromptMutation ( )
23
+
24
+ const handleDelete = ( promptId : string ) => {
25
+ if ( ! window . confirm ( t ( 'confirmDeletePrompt' ) as string ) ) return
43
26
44
27
try {
45
- await mutation . mutateAsync ( updatedPrompt )
46
- setEditPrompt ( false )
47
- enqueueSnackbar ( 'Prompt updated' , { variant : 'success' } )
28
+ deleteMutation . mutate ( promptId )
29
+ enqueueSnackbar ( 'Prompt deleted' , { variant : 'success' } )
48
30
} catch ( error : any ) {
49
31
enqueueSnackbar ( error . message , { variant : 'error' } )
50
32
}
51
33
}
52
34
53
35
return (
54
36
< Box key = { id } pt = { 2 } >
55
- < Paper variant = "outlined" sx = { { padding : '2% ' } } >
37
+ < Paper sx = { { py : '1rem' , px : '2rem ' } } >
56
38
< Box display = "flex" alignItems = "center" justifyContent = "space-between" >
57
39
< Box >
58
40
< Box display = "inline" mr = { 2 } >
@@ -73,82 +55,40 @@ const Prompt = ({ prompt, handleDelete, mandatoryPromptId }: { prompt: PromptTyp
73
55
</ Tooltip >
74
56
</ Box >
75
57
) }
76
- { ! editPrompt ? (
77
- < Box sx = { { display : 'flex' , flexDirection : 'column' , alignItems : 'flex-start' } } >
78
- < Typography variant = "h6" display = "inline" >
79
- { name }
80
- </ Typography >
81
- < Box sx = { { display : 'flex' , alignItems : 'center' } } >
82
- < Link component = { RouterLink } to = { chatPath } variant = "caption" >
83
- { t ( 'course:directPromptLink' , { name : prompt . name } ) }
84
- </ Link >
85
- < Tooltip title = { t ( 'course:copyDirectPromptLinkInfo' , { name : prompt . name } ) } >
86
- < IconButton size = "small" onClick = { ( ) => navigator . clipboard . writeText ( directLink ) } >
87
- < ContentCopyOutlined fontSize = "small" />
88
- </ IconButton >
89
- </ Tooltip >
90
- < IframeCopy courseId = { courseId ! } promptId = { prompt . id } />
91
- </ Box >
58
+ { ragIndexId && (
59
+ < Box display = "inline" mr = { 2 } >
60
+ < Tooltip title = { t ( 'rag:sourceMaterials' ) } >
61
+ < MenuBookOutlined />
62
+ </ Tooltip >
92
63
</ Box >
93
- ) : (
94
- < TextField defaultValue = { updatedName } sx = { { width : '650px' } } onChange = { ( e ) => setUpdatedName ( e . target . value ) } />
95
64
) }
65
+
66
+ < Box sx = { { display : 'flex' , flexDirection : 'column' , alignItems : 'flex-start' } } >
67
+ < Typography variant = "h6" display = "inline" >
68
+ { name }
69
+ </ Typography >
70
+ < Box sx = { { display : 'flex' , alignItems : 'center' } } >
71
+ < Link component = { RouterLink } to = { chatPath } variant = "caption" >
72
+ { t ( 'course:directPromptLink' , { name : prompt . name } ) }
73
+ </ Link >
74
+ < Tooltip title = { t ( 'course:copyDirectPromptLinkInfo' , { name : prompt . name } ) } >
75
+ < IconButton size = "small" onClick = { ( ) => navigator . clipboard . writeText ( directLink ) } >
76
+ < ContentCopyOutlined fontSize = "small" />
77
+ </ IconButton >
78
+ </ Tooltip >
79
+ < IframeCopy courseId = { courseId ! } promptId = { prompt . id } />
80
+ </ Box >
81
+ </ Box >
96
82
</ Box >
97
- < Box >
98
- < Button onClick = { ( ) => handleDelete ( prompt . id ) } color = "error" data-testid = { `delete-prompt-${ prompt . name } ` } >
99
- { t ( 'common:delete' ) }
100
- </ Button >
101
- < ExpandButton expand = { expand } setExpand = { setExpand } />
83
+ < Box sx = { { display : 'flex' , alignItems : 'center' } } >
84
+ < IconButton onClick = { ( ) => handleDelete ( prompt . id ) } color = "error" data-testid = { `delete-prompt-${ prompt . name } ` } aria-label = { t ( 'course:remove' ) } >
85
+ < DeleteOutline />
86
+ </ IconButton >
87
+ < OutlineButtonBlack onClick = { handleEdit } color = "primary" data-testid = { `edit-prompt-${ prompt . name } ` } aria-label = { t ( 'common:edit' ) } >
88
+ { t ( 'common:edit' ) }
89
+ </ OutlineButtonBlack >
102
90
</ Box >
103
91
</ Box >
104
-
105
- { expand && (
106
- < Box mt = { 2 } >
107
- { ! editPrompt ? (
108
- < >
109
- < Box width = "80%" >
110
- < SystemMessage system = { systemMessage } setSystem = { ( ) => { } } showInfo = { false } disabled />
111
- </ Box >
112
- < Box >
113
- { messages . map ( ( msg ) => (
114
- < MessageItem key = { msg . content } message = { msg } setActiveToolResult = { ( _d ) => { } } />
115
- ) ) }
116
- </ Box >
117
- </ >
118
- ) : (
119
- < >
120
- < TextField defaultValue = { systemMessage } sx = { { width : '80%' } } multiline onChange = { ( e ) => setMessage ( e . target . value ) } />
121
- { ! mandatoryPromptId || mandatoryPromptId === prompt . id ? (
122
- < FormControlLabel
123
- control = { < Checkbox checked = { updatedMandatory } onChange = { ( ) => setUpdatedMandatory ( ( prev ) => ! prev ) } /> }
124
- label = "Tee alustuksesta pakollinen opiskelijoille"
125
- sx = { { mr : 5 } }
126
- />
127
- ) : (
128
- < Tooltip title = "Kurssilla voi olla vain yksi pakollinen alustus" >
129
- < FormControlLabel
130
- control = { < Checkbox checked = { updatedMandatory } disabled /> }
131
- label = "Tee alustuksesta pakollinen opiskelijoille"
132
- sx = { { mr : 5 } }
133
- />
134
- </ Tooltip >
135
- ) }
136
-
137
- < FormControlLabel control = { < Checkbox checked = { updatedHidden } onChange = { ( ) => setUpdatedHidden ( ( prev ) => ! prev ) } /> } label = { t ( 'hidePrompt' ) } />
138
- </ >
139
- ) }
140
- < Stack direction = "row" spacing = { 2 } marginTop = { 2 } >
141
- { editPrompt && (
142
- < Button onClick = { handleSave } variant = "outlined" >
143
- { t ( 'common:save' ) }
144
- </ Button >
145
- ) }
146
- < Button variant = "outlined" onClick = { ( ) => setEditPrompt ( ! editPrompt ) } >
147
- { editPrompt ? t ( 'common:cancel' ) : t ( 'common:edit' ) }
148
- </ Button >
149
- </ Stack >
150
- </ Box >
151
- ) }
152
92
</ Paper >
153
93
</ Box >
154
94
)
0 commit comments