chat-feedback.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { ChatContext } from '@/app/chat-context';
  2. import { apiInterceptors, getChatFeedBackItme, postChatFeedBackForm } from '@/client/api';
  3. import { FeedBack } from '@/types/chat';
  4. import { ChatFeedBackSchema } from '@/types/db';
  5. import { CloseRounded, MoreHoriz } from '@mui/icons-material';
  6. import {
  7. Box,
  8. Button,
  9. Dropdown,
  10. Grid,
  11. IconButton,
  12. Menu,
  13. MenuButton,
  14. MenuItem,
  15. Option,
  16. Select,
  17. Sheet,
  18. Slider,
  19. Textarea,
  20. Typography,
  21. styled,
  22. } from '@mui/joy';
  23. import { Tooltip, message } from 'antd';
  24. import { useCallback, useContext, useRef, useState } from 'react';
  25. import { useTranslation } from 'react-i18next';
  26. type Props = {
  27. conv_index: number;
  28. question: any;
  29. knowledge_space: string;
  30. select_param?: FeedBack;
  31. };
  32. const ChatFeedback = ({ conv_index, question, knowledge_space, select_param }: Props) => {
  33. const { t } = useTranslation();
  34. const { chatId } = useContext(ChatContext);
  35. const [ques_type, setQuesType] = useState('');
  36. const [score, setScore] = useState(4);
  37. const [text, setText] = useState('');
  38. const action = useRef(null);
  39. const [messageApi, contextHolder] = message.useMessage();
  40. const handleOpenChange = useCallback(
  41. (_: any, isOpen: boolean) => {
  42. if (isOpen) {
  43. apiInterceptors(getChatFeedBackItme(chatId, conv_index))
  44. .then(res => {
  45. const finddata = res[1] ?? {};
  46. setQuesType(finddata.ques_type ?? '');
  47. setScore(parseInt(finddata.score ?? '4'));
  48. setText(finddata.messages ?? '');
  49. })
  50. .catch(err => {
  51. console.log(err);
  52. });
  53. } else {
  54. setQuesType('');
  55. setScore(4);
  56. setText('');
  57. }
  58. },
  59. [chatId, conv_index],
  60. );
  61. const marks = [
  62. { value: 0, label: '0' },
  63. { value: 1, label: '1' },
  64. { value: 2, label: '2' },
  65. { value: 3, label: '3' },
  66. { value: 4, label: '4' },
  67. { value: 5, label: '5' },
  68. ];
  69. function valueText(value: number) {
  70. return {
  71. 0: t('Lowest'),
  72. 1: t('Missed'),
  73. 2: t('Lost'),
  74. 3: t('Incorrect'),
  75. 4: t('Verbose'),
  76. 5: t('Best'),
  77. }[value];
  78. }
  79. const Item = styled(Sheet)(({ theme }) => ({
  80. backgroundColor: theme.palette.mode === 'dark' ? '#FBFCFD' : '#0E0E10',
  81. ...theme.typography['body-sm'],
  82. padding: theme.spacing(1),
  83. display: 'flex',
  84. alignItems: 'center',
  85. justifyContent: 'center',
  86. borderRadius: 4,
  87. width: '100%',
  88. height: '100%',
  89. }));
  90. const handleSubmit = (event: any) => {
  91. event.preventDefault();
  92. const formData: ChatFeedBackSchema = {
  93. conv_uid: chatId,
  94. conv_index: conv_index,
  95. question: question,
  96. knowledge_space: knowledge_space,
  97. score: score,
  98. ques_type: ques_type,
  99. messages: text,
  100. };
  101. apiInterceptors(
  102. postChatFeedBackForm({
  103. data: formData,
  104. }),
  105. )
  106. .then(_ => {
  107. messageApi.open({ type: 'success', content: 'save success' });
  108. })
  109. .catch(_ => {
  110. messageApi.open({ type: 'error', content: 'save error' });
  111. });
  112. };
  113. return (
  114. <Dropdown onOpenChange={handleOpenChange}>
  115. {contextHolder}
  116. <Tooltip title={t('Rating')}>
  117. <MenuButton
  118. slots={{ root: IconButton }}
  119. slotProps={{ root: { variant: 'plain', color: 'primary' } }}
  120. sx={{ borderRadius: 40 }}
  121. >
  122. <MoreHoriz />
  123. </MenuButton>
  124. </Tooltip>
  125. <Menu>
  126. <MenuItem disabled sx={{ minHeight: 0 }} />
  127. <Box
  128. sx={{
  129. width: '100%',
  130. maxWidth: 350,
  131. display: 'grid',
  132. gap: 3,
  133. padding: 1,
  134. }}
  135. >
  136. <form onSubmit={handleSubmit}>
  137. <Grid container spacing={0.5} columns={13} sx={{ flexGrow: 1 }}>
  138. <Grid xs={3}>
  139. <Item>{t('Q_A_Category')}</Item>
  140. </Grid>
  141. <Grid xs={10}>
  142. <Select
  143. action={action}
  144. value={ques_type}
  145. placeholder='Choose one…'
  146. onChange={(_, newValue) => setQuesType(newValue ?? '')}
  147. {...(ques_type && {
  148. // display the button and remove select indicator
  149. // when user has selected a value
  150. endDecorator: (
  151. <IconButton
  152. size='sm'
  153. variant='plain'
  154. color='neutral'
  155. onMouseDown={event => {
  156. // don't open the popup when clicking on this button
  157. event.stopPropagation();
  158. }}
  159. onClick={() => {
  160. setQuesType('');
  161. action.current?.focusVisible();
  162. }}
  163. >
  164. <CloseRounded />
  165. </IconButton>
  166. ),
  167. indicator: null,
  168. })}
  169. sx={{ width: '100%' }}
  170. >
  171. {select_param &&
  172. Object.keys(select_param)?.map(paramItem => (
  173. <Option key={paramItem} value={paramItem}>
  174. {select_param[paramItem]}
  175. </Option>
  176. ))}
  177. </Select>
  178. </Grid>
  179. <Grid xs={3}>
  180. <Item>
  181. <Tooltip
  182. title={
  183. <Box>
  184. <div>{t('feed_back_desc')}</div>
  185. </Box>
  186. }
  187. variant='solid'
  188. placement='left'
  189. >
  190. {t('Q_A_Rating')}
  191. </Tooltip>
  192. </Item>
  193. </Grid>
  194. <Grid xs={10} sx={{ pl: 0, ml: 0 }}>
  195. <Slider
  196. aria-label='Custom'
  197. step={1}
  198. min={0}
  199. max={5}
  200. valueLabelFormat={valueText}
  201. valueLabelDisplay='on'
  202. marks={marks}
  203. sx={{ width: '90%', pt: 3, m: 2, ml: 1 }}
  204. onChange={event => setScore(event.target?.value)}
  205. value={score}
  206. />
  207. </Grid>
  208. <Grid xs={13}>
  209. <Textarea
  210. placeholder={t('Please_input_the_text')}
  211. value={text}
  212. onChange={event => setText(event.target.value)}
  213. minRows={2}
  214. maxRows={4}
  215. endDecorator={
  216. <Typography level='body-xs' sx={{ ml: 'auto' }}>
  217. {t('input_count') + text.length + t('input_unit')}
  218. </Typography>
  219. }
  220. sx={{ width: '100%', fontSize: 14 }}
  221. />
  222. </Grid>
  223. <Grid xs={13}>
  224. <Button type='submit' variant='outlined' sx={{ width: '100%', height: '100%' }}>
  225. {t('submit')}
  226. </Button>
  227. </Grid>
  228. </Grid>
  229. </form>
  230. </Box>
  231. </Menu>
  232. </Dropdown>
  233. );
  234. };
  235. export default ChatFeedback;