| 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: AI评论截流 |
| 7 | */ |
| 8 | import { 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 | import { icpCreateInteractionOneKey } from '@/icp/replyother'; |
| 17 | |
| 18 | export interface InteractionOneKeyRef { |
| 19 | init: (accountId: number, data: WorkData[]) => Promise<void>; |
| 20 | } |
| 21 | |
| 22 | const Com = forwardRef<InteractionOneKeyRef>((props: any, ref) => { |
| 23 | const [isModalOpen, setIsModalOpen] = useState(false); |
| 24 | const [infoText, setInfoText] = useState(''); |
| 25 | const [errorText, setErrorText] = useState(''); |
| 26 | const [workDataList, setWorkDataList] = useState<WorkData[]>([]); |
| 27 | async function init(accountId: number, dataList: WorkData[]) { |
| 28 | window.ipcRenderer.on(SendChannelEnum.CommentRelyProgress, onGetNotice); |
| 29 | setWorkDataList(dataList); |
| 30 | setIsModalOpen(true); |
| 31 | |
| 32 | createInteractionOneKey(accountId, dataList); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * AI评论截流 |
| 37 | */ |
| 38 | async function createInteractionOneKey( |
| 39 | inAccountId: number, |
| 40 | inWorkDataList: WorkData[] = [], |
| 41 | ) { |
| 42 | if (!inWorkDataList.length) return; |
| 43 | const res = await icpCreateInteractionOneKey(inAccountId, inWorkDataList, { |
| 44 | commentContent: '不错啊!收藏了!', |
| 45 | }); |
| 46 | console.log('------ res', res); |
| 47 | } |
| 48 | |
| 49 | function closeShow() { |
| 50 | // 关闭界面 |
| 51 | setTimeout(() => { |
| 52 | handleCancel(); |
| 53 | }, 1000); |
| 54 | |
| 55 | setTimeout(() => { |
| 56 | setErrorText(''); |
| 57 | setInfoText(''); |
| 58 | }, 2000); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * 返回值处理 |
| 63 | * @param e |
| 64 | * @param args |
| 65 | */ |
| 66 | function onGetNotice( |
| 67 | e: any, |
| 68 | args: { |
| 69 | tag: AutorReplyCommentScheduleEvent; |
| 70 | status: -1 | 0 | 1; |
| 71 | data?: { |
| 72 | content: string; |
| 73 | aiContent: string; |
| 74 | }; |
| 75 | error?: any; |
| 76 | }, |
| 77 | ) { |
| 78 | switch (args.tag) { |
| 79 | case AutorReplyCommentScheduleEvent.Start: |
| 80 | setInfoText(`开始评论:${args.data?.content || ''}`); |
| 81 | break; |
| 82 | case AutorReplyCommentScheduleEvent.End: |
| 83 | setInfoText(`评论结束:${args.data?.aiContent || ''}`); |
| 84 | message.success(`部分平台评论需要审核,评论后的数据可能存在延时`); |
| 85 | closeShow(); |
| 86 | break; |
| 87 | case AutorReplyCommentScheduleEvent.Error: |
| 88 | setErrorText(args.error?.message || '未知错误'); |
| 89 | closeShow(); |
| 90 | break; |
| 91 | default: |
| 92 | const tagStr = |
| 93 | AutorReplyCommentScheduleEventTagStrMap.get(args.tag) || ''; |
| 94 | setInfoText(`${tagStr}`); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | useImperativeHandle(ref, () => ({ |
| 100 | init: init, |
| 101 | })); |
| 102 | |
| 103 | function handleCancel() { |
| 104 | window.ipcRenderer.off(SendChannelEnum.CommentRelyProgress, onGetNotice); |
| 105 | setIsModalOpen(false); |
| 106 | } |
| 107 | |
| 108 | return ( |
| 109 | <> |
| 110 | <Modal |
| 111 | title="一键评论" |
| 112 | open={isModalOpen} |
| 113 | onCancel={handleCancel} |
| 114 | footer={null} |
| 115 | width={400} |
| 116 | > |
| 117 | <div className="p-2 mb-4 border border-green-500 rounded-md shadow-md"> |
| 118 | <p className="text-green-500">{infoText}</p> |
| 119 | </div> |
| 120 | {errorText && ( |
| 121 | <div className="p-2 mb-4 border border-red-500 rounded-md shadow-md"> |
| 122 | <p className="text-red-500">{errorText}</p> |
| 123 | </div> |
| 124 | )} |
| 125 | </Modal> |
| 126 | </> |
| 127 | ); |
| 128 | }); |
| 129 | export default Com; |
| 130 |