返回 AiToEarn
oneKeyReply.tsx
根目录 / project / aitoearn-electron / src / views / reply / components / oneKeyReply.tsx
1 /*
2 * @Author: nevin
3 * @Date: 2025-03-18 21:02:38
4 * @LastEditTime: 2025-03-31 11:02:12
5 * @LastEditors: nevin
6 * @Description: 一键评论
7 */
8 import { icpReplyCommentList, WorkData } from '@/icp/reply';
9 import { message, Modal } from 'antd';
10 import { forwardRef, useImperativeHandle, useState } from 'react';
11 import { SendChannelEnum } from '@@/UtilsEnum';
12 import {
13 AutorReplyCommentScheduleEvent,
14 AutorReplyCommentScheduleEventTagStrMap,
15 } from '@@/types/reply';
16
17 export interface OneKeyReplyRef {
18 init: (accountId: number, data: WorkData) => Promise<void>;
19 }
20
21 const Com = forwardRef<OneKeyReplyRef>((props: any, ref) => {
22 const [isModalOpen, setIsModalOpen] = useState(false);
23 const [infoText, setInfoText] = useState('');
24 const [errorText, setErrorText] = useState('');
25 const [workData, setWorkData] = useState<WorkData | null>(null);
26 async function init(accountId: number, data: WorkData) {
27 window.ipcRenderer.on(SendChannelEnum.CommentRelyProgress, onGetNotice);
28 setWorkData(data);
29 setIsModalOpen(true);
30
31 replyCommentList(accountId, data);
32 }
33
34 /**
35 * 一键AI评论
36 */
37 async function replyCommentList(inAccountId: number, inWorkData: WorkData) {
38 if (!inWorkData?.dataId) return;
39 const res = await icpReplyCommentList(inAccountId, inWorkData);
40 console.log('------ res', res);
41 }
42
43 function closeShow() {
44 // 关闭界面
45 setTimeout(() => {
46 handleCancel();
47 }, 1000);
48
49 setTimeout(() => {
50 setErrorText('');
51 setInfoText('');
52 }, 2000);
53 }
54
55 /**
56 * 返回值处理
57 * @param e
58 * @param args
59 */
60 function onGetNotice(
61 e: any,
62 args: {
63 tag: AutorReplyCommentScheduleEvent;
64 status: -1 | 0 | 1;
65 data?: {
66 content: string;
67 aiContent: string;
68 };
69 error?: any;
70 },
71 ) {
72 switch (args.tag) {
73 case AutorReplyCommentScheduleEvent.Start:
74 setInfoText(`开始评论:${args.data?.content || ''}`);
75 break;
76 case AutorReplyCommentScheduleEvent.End:
77 setInfoText(`评论结束:${args.data?.aiContent || ''}`);
78 message.success(`部分平台评论需要审核,评论后的数据可能存在延时`);
79 closeShow();
80 break;
81 case AutorReplyCommentScheduleEvent.Error:
82 setErrorText(args.error?.message || '未知错误');
83 closeShow();
84 break;
85 default:
86 const tagStr =
87 AutorReplyCommentScheduleEventTagStrMap.get(args.tag) || '';
88 setInfoText(`${tagStr}`);
89 break;
90 }
91 }
92
93 useImperativeHandle(ref, () => ({
94 init: init,
95 }));
96
97 function handleCancel() {
98 window.ipcRenderer.off(SendChannelEnum.CommentRelyProgress, onGetNotice);
99 setIsModalOpen(false);
100 }
101
102 return (
103 <>
104 <Modal
105 title="一键评论"
106 open={isModalOpen}
107 onCancel={handleCancel}
108 footer={null}
109 width={400}
110 >
111 <div className="p-2 mb-4 border border-green-500 rounded-md shadow-md">
112 <p className="text-green-500">{infoText}</p>
113 </div>
114 {errorText && (
115 <div className="p-2 mb-4 border border-red-500 rounded-md shadow-md">
116 <p className="text-red-500">{errorText}</p>
117 </div>
118 )}
119 </Modal>
120 </>
121 );
122 });
123 export default Com;
124
124 lines Plain Text