| 1 | import { toolsApi } from '@/api/tools'; |
| 2 | import { CommentData, icpReplyComment } from '@/icp/reply'; |
| 3 | import { SendOutlined } from '@ant-design/icons'; |
| 4 | import { Button, Form, Input, Modal, Tooltip, message } from 'antd'; |
| 5 | import { forwardRef, useImperativeHandle, useState } from 'react'; |
| 6 | import logoAi from '@/assets/logoAi.png'; |
| 7 | |
| 8 | export interface ReplyCommentRef { |
| 9 | init: (accountId: number, inInfo: CommentData) => Promise<void>; |
| 10 | } |
| 11 | |
| 12 | interface FormData { |
| 13 | content: string; |
| 14 | } |
| 15 | |
| 16 | const Com = forwardRef<ReplyCommentRef>((props: any, ref) => { |
| 17 | const [isModalOpen, setIsModalOpen] = useState(false); |
| 18 | const [accountId, setAccountId] = useState<number>(0); |
| 19 | const [commentData, setCommentData] = useState<CommentData | null>(null); |
| 20 | const [form] = Form.useForm<FormData>(); |
| 21 | async function init(accountId: number, inInfo: CommentData) { |
| 22 | setAccountId(accountId); |
| 23 | setCommentData(inInfo); |
| 24 | setIsModalOpen(true); |
| 25 | } |
| 26 | |
| 27 | useImperativeHandle(ref, () => ({ |
| 28 | init: init, |
| 29 | })); |
| 30 | |
| 31 | function handleCancel() { |
| 32 | setIsModalOpen(false); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * 回复评论 |
| 37 | */ |
| 38 | async function replyComment(content: string) { |
| 39 | const res = await icpReplyComment( |
| 40 | accountId, |
| 41 | commentData!.commentId, |
| 42 | content, |
| 43 | { |
| 44 | dataId: commentData!.dataId, |
| 45 | comment: commentData!.data, |
| 46 | }, |
| 47 | ); |
| 48 | // if (res) { |
| 49 | message.success('回复成功'); |
| 50 | setIsModalOpen(false); |
| 51 | form.resetFields(); |
| 52 | // } |
| 53 | } |
| 54 | |
| 55 | async function onFinish(values: FormData) { |
| 56 | await replyComment(values.content); |
| 57 | console.log('Success:', values); |
| 58 | } |
| 59 | |
| 60 | async function onFinishFailed(errorInfo: any) { |
| 61 | console.log('Failed:', errorInfo); |
| 62 | } |
| 63 | |
| 64 | async function getAiContent() { |
| 65 | if (!commentData?.content) return; |
| 66 | const res = await toolsApi.apiReviewAiRecover({ |
| 67 | content: commentData?.content, |
| 68 | }); |
| 69 | |
| 70 | form.setFieldsValue({ |
| 71 | content: res, |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | return ( |
| 76 | <> |
| 77 | <Modal |
| 78 | title="回复评论" |
| 79 | open={isModalOpen} |
| 80 | onCancel={handleCancel} |
| 81 | footer={null} |
| 82 | width={500} |
| 83 | className="reply-comment-modal" |
| 84 | > |
| 85 | <div className="p-6"> |
| 86 | <Form |
| 87 | form={form} |
| 88 | name="basic" |
| 89 | layout="vertical" |
| 90 | initialValues={{ remember: true }} |
| 91 | onFinish={onFinish} |
| 92 | onFinishFailed={onFinishFailed} |
| 93 | autoComplete="off" |
| 94 | > |
| 95 | <Form.Item |
| 96 | label="回复内容" |
| 97 | name="content" |
| 98 | rules={[{ required: true, message: '请输入回复内容!' }]} |
| 99 | > |
| 100 | <Input.TextArea rows={4} /> |
| 101 | </Form.Item> |
| 102 | |
| 103 | <div className="flex justify-end items-center gap-4"> |
| 104 | <Tooltip title="获取AI建议"> |
| 105 | <img |
| 106 | src={logoAi} |
| 107 | alt="logo" |
| 108 | width={32} |
| 109 | onClick={getAiContent} |
| 110 | className="cursor-pointer hover:opacity-80 transition-opacity" |
| 111 | /> |
| 112 | </Tooltip> |
| 113 | |
| 114 | <Tooltip title="发送"> |
| 115 | <Button |
| 116 | type="primary" |
| 117 | shape="circle" |
| 118 | htmlType="submit" |
| 119 | icon={<SendOutlined />} |
| 120 | className="flex items-center justify-center" |
| 121 | /> |
| 122 | </Tooltip> |
| 123 | </div> |
| 124 | </Form> |
| 125 | </div> |
| 126 | </Modal> |
| 127 | </> |
| 128 | ); |
| 129 | }); |
| 130 | export default Com; |
| 131 |