import { apiInterceptors, cancelFeedback, feedbackAdd, getFeedbackReasons } from '@/client/api'; import { CopyOutlined, DislikeOutlined, LikeOutlined } from '@ant-design/icons'; import { useRequest } from 'ahooks'; import { Button, Divider, Input, Popover, Tag, message } from 'antd'; import classNames from 'classnames'; import copy from 'copy-to-clipboard'; import { useSearchParams } from 'next/navigation'; import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; interface Tags { reason: string; reason_type: string; } const DislikeContent: React.FC<{ list: Tags[]; loading: boolean; feedback: (params: { feedback_type: string; reason_types?: string[] | undefined; remark?: string | undefined; }) => void; setFeedbackOpen: React.Dispatch>; }> = ({ list, loading, feedback, setFeedbackOpen }) => { const { t } = useTranslation(); const [selectedTags, setSelectedTags] = useState([]); const [remark, setRemark] = useState(''); return (
{list?.map(item => { const isSelect = selectedTags.findIndex(tag => tag.reason_type === item.reason_type) > -1; return ( { setSelectedTags((preArr: Tags[]) => { const index = preArr.findIndex(tag => tag.reason_type === item.reason_type); if (index > -1) { return [...preArr.slice(0, index), ...preArr.slice(index + 1)]; } return [...preArr, item]; }); }} > {item.reason} ); })}
setRemark(e.target.value.trim())} />
); }; const Feedback: React.FC<{ content: Record }> = ({ content }) => { const { t } = useTranslation(); const searchParams = useSearchParams(); const chatId = searchParams?.get('id') ?? ''; const [messageApi, contextHolder] = message.useMessage(); const [feedbackOpen, setFeedbackOpen] = useState(false); const [status, setStatus] = useState<'like' | 'unlike' | 'none'>(content?.feedback?.feedback_type); const [list, setList] = useState(); // 复制回答 const onCopyContext = async (context: any) => { const pureStr = context?.replace(/\trelations:.*/g, ''); const result = copy(pureStr); if (result) { if (pureStr) { messageApi.open({ type: 'success', content: t('copy_success') }); } else { messageApi.open({ type: 'warning', content: t('copy_nothing') }); } } else { messageApi.open({ type: 'error', content: t('copy_failed') }); } }; // 点赞/踩 const { run: feedback, loading } = useRequest( async (params: { feedback_type: string; reason_types?: string[]; remark?: string }) => await apiInterceptors( feedbackAdd({ conv_uid: chatId, message_id: content.order + '', feedback_type: params.feedback_type, reason_types: params.reason_types, remark: params.remark, }), ), { manual: true, onSuccess: data => { const [, res] = data; setStatus(res?.feedback_type); message.success('反馈成功'); setFeedbackOpen(false); }, }, ); // 反馈原因类型 const { run: getReasonList } = useRequest(async () => await apiInterceptors(getFeedbackReasons()), { manual: true, onSuccess: data => { const [, res] = data; setList(res || []); if (res) { setFeedbackOpen(true); } }, }); // 取消反馈 const { run: cancel } = useRequest( async () => await apiInterceptors(cancelFeedback({ conv_uid: chatId, message_id: content?.order + '' })), { manual: true, onSuccess: data => { const [, res] = data; if (res) { setStatus('none'); message.success('操作成功'); } }, }, ); return ( <> {contextHolder}
{ if (status === 'like') { await cancel(); return; } await feedback({ feedback_type: 'like' }); }} /> } trigger='click' open={feedbackOpen} > { if (status === 'unlike') { await cancel(); return; } await getReasonList(); }} />
onCopyContext(content.context)} />
); }; export default Feedback;