Add dialog for controlling chat feedback settings

This commit is contained in:
Ilango
2024-02-20 15:23:15 +05:30
parent 3bb2b39896
commit 78677d9ee5
4 changed files with 183 additions and 6 deletions
+18 -2
View File
@@ -1,8 +1,17 @@
// assets
import { IconTrash, IconFileUpload, IconFileExport, IconCopy, IconSearch, IconMessage, IconPictureInPictureOff } from '@tabler/icons'
import {
IconTrash,
IconFileUpload,
IconFileExport,
IconCopy,
IconSearch,
IconMessage,
IconPictureInPictureOff,
IconThumbUp
} from '@tabler/icons'
// constant
const icons = { IconTrash, IconFileUpload, IconFileExport, IconCopy, IconSearch, IconMessage, IconPictureInPictureOff }
const icons = { IconTrash, IconFileUpload, IconFileExport, IconCopy, IconSearch, IconMessage, IconPictureInPictureOff, IconThumbUp }
// ==============================|| SETTINGS MENU ITEMS ||============================== //
@@ -25,6 +34,13 @@ const settings = {
url: '',
icon: icons.IconMessage
},
{
id: 'chatFeedback',
title: 'Chat Feedback',
type: 'item',
url: '',
icon: icons.IconThumbUp
},
{
id: 'duplicateChatflow',
title: 'Duplicate Chatflow',
@@ -0,0 +1,137 @@
import { createPortal } from 'react-dom'
import { useDispatch } from 'react-redux'
import { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction, SET_CHATFLOW } from 'store/actions'
// material-ui
import { Button, Dialog, DialogContent, DialogTitle, DialogActions, Box } from '@mui/material'
import { IconX } from '@tabler/icons'
// Project import
import { StyledButton } from 'ui-component/button/StyledButton'
import { SwitchInput } from 'ui-component/switch/Switch'
// store
import { HIDE_CANVAS_DIALOG, SHOW_CANVAS_DIALOG } from 'store/actions'
import useNotifier from 'utils/useNotifier'
// API
import chatflowsApi from 'api/chatflows'
const ChatFeedbackDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
const portalElement = document.getElementById('portal')
const dispatch = useDispatch()
useNotifier()
const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args))
const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args))
const [chatFeedbackStatus, setChatFeedbackStatus] = useState(false)
const [chatbotConfig, setChatbotConfig] = useState({})
const handleChange = (value) => {
setChatFeedbackStatus(value)
}
const onSave = async () => {
try {
let value = {
chatFeedback: {
status: chatFeedbackStatus
}
}
chatbotConfig.chatFeedback = value.chatFeedback
const saveResp = await chatflowsApi.updateChatflow(dialogProps.chatflow.id, {
chatbotConfig: JSON.stringify(chatbotConfig)
})
if (saveResp.data) {
enqueueSnackbar({
message: 'Chat Feedback Settings Saved',
options: {
key: new Date().getTime() + Math.random(),
variant: 'success',
action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX />
</Button>
)
}
})
dispatch({ type: SET_CHATFLOW, chatflow: saveResp.data })
}
onConfirm()
} catch (error) {
const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}`
enqueueSnackbar({
message: `Failed to save Chat Feedback Settings: ${errorData}`,
options: {
key: new Date().getTime() + Math.random(),
variant: 'error',
persist: true,
action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX />
</Button>
)
}
})
}
}
useEffect(() => {
if (dialogProps.chatflow && dialogProps.chatflow.chatbotConfig) {
let chatbotConfig = JSON.parse(dialogProps.chatflow.chatbotConfig)
setChatbotConfig(chatbotConfig || {})
if (chatbotConfig.chatFeedback) {
setChatFeedbackStatus(chatbotConfig.chatFeedback.status)
}
}
return () => {}
}, [dialogProps])
useEffect(() => {
if (show) dispatch({ type: SHOW_CANVAS_DIALOG })
else dispatch({ type: HIDE_CANVAS_DIALOG })
return () => dispatch({ type: HIDE_CANVAS_DIALOG })
}, [show, dispatch])
const component = show ? (
<Dialog
onClose={onCancel}
open={show}
fullWidth
maxWidth='sm'
aria-labelledby='alert-dialog-title'
aria-describedby='alert-dialog-description'
>
<DialogTitle sx={{ fontSize: '1rem' }} id='alert-dialog-title'>
{dialogProps.title || 'Chat Feedback'}
</DialogTitle>
<DialogContent>
<Box sx={{ width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
<SwitchInput label='Enable chat feedback' onChange={handleChange} value={chatFeedbackStatus} />
</Box>
</DialogContent>
<DialogActions>
<Button onClick={onCancel}>Cancel</Button>
<StyledButton variant='contained' onClick={onSave}>
Save
</StyledButton>
</DialogActions>
</Dialog>
) : null
return createPortal(component, portalElement)
}
ChatFeedbackDialog.propTypes = {
show: PropTypes.bool,
dialogProps: PropTypes.object,
onCancel: PropTypes.func,
onConfirm: PropTypes.func
}
export default ChatFeedbackDialog
+13 -4
View File
@@ -1,13 +1,21 @@
import { useState } from 'react'
import { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { FormControl, Switch } from '@mui/material'
import { FormControl, Switch, Typography } from '@mui/material'
export const SwitchInput = ({ value, onChange, disabled = false }) => {
export const SwitchInput = ({ label, value, onChange, disabled = false }) => {
const [myValue, setMyValue] = useState(!!value ?? false)
useEffect(() => {
setMyValue(value)
}, [value])
return (
<>
<FormControl sx={{ mt: 1, width: '100%' }} size='small'>
<FormControl
sx={{ mt: 1, width: '100%', display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}
size='small'
>
{label && <Typography>{label}</Typography>}
<Switch
disabled={disabled}
checked={myValue}
@@ -22,6 +30,7 @@ export const SwitchInput = ({ value, onChange, disabled = false }) => {
}
SwitchInput.propTypes = {
label: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
onChange: PropTypes.func,
disabled: PropTypes.bool
@@ -17,6 +17,7 @@ import APICodeDialog from 'views/chatflows/APICodeDialog'
import AnalyseFlowDialog from 'ui-component/dialog/AnalyseFlowDialog'
import ViewMessagesDialog from 'ui-component/dialog/ViewMessagesDialog'
import StarterPromptsDialog from 'ui-component/dialog/StarterPromptsDialog'
import ChatFeedbackDialog from 'ui-component/dialog/ChatFeedbackDialog'
// API
import chatflowsApi from 'api/chatflows'
@@ -50,6 +51,8 @@ const CanvasHeader = ({ chatflow, handleSaveFlow, handleDeleteFlow, handleLoadFl
const [conversationStartersDialogProps, setConversationStartersDialogProps] = useState({})
const [viewMessagesDialogOpen, setViewMessagesDialogOpen] = useState(false)
const [viewMessagesDialogProps, setViewMessagesDialogProps] = useState({})
const [chatFeedbackDialogOpen, setChatFeedbackDialogOpen] = useState(false)
const [chatFeedbackDialogProps, setChatFeedbackDialogProps] = useState({})
const updateChatflowApi = useApi(chatflowsApi.updateChatflow)
const canvas = useSelector((state) => state.canvas)
@@ -65,6 +68,12 @@ const CanvasHeader = ({ chatflow, handleSaveFlow, handleDeleteFlow, handleLoadFl
chatflow: chatflow
})
setConversationStartersDialogOpen(true)
} else if (setting === 'chatFeedback') {
setChatFeedbackDialogProps({
title: `Chat Feedback - ${chatflow.name}`,
chatflow: chatflow
})
setChatFeedbackDialogOpen(true)
} else if (setting === 'analyseChatflow') {
setAnalyseDialogProps({
title: 'Analyse Chatflow',
@@ -391,6 +400,12 @@ const CanvasHeader = ({ chatflow, handleSaveFlow, handleDeleteFlow, handleLoadFl
onConfirm={() => setConversationStartersDialogOpen(false)}
onCancel={() => setConversationStartersDialogOpen(false)}
/>
<ChatFeedbackDialog
show={chatFeedbackDialogOpen}
dialogProps={chatFeedbackDialogProps}
onConfirm={() => setChatFeedbackDialogOpen(false)}
onCancel={() => setChatFeedbackDialogOpen(false)}
/>
<ViewMessagesDialog
show={viewMessagesDialogOpen}
dialogProps={viewMessagesDialogProps}