| 1 | import { |
| 2 | AutoRun, |
| 3 | AutoRunStatus, |
| 4 | AutoRunType, |
| 5 | ipcGetAutoRunList, |
| 6 | ipcRunNowAutoRun, |
| 7 | ipcUpdateAutoRunStatus, |
| 8 | } from '@/icp/autoRun'; |
| 9 | import { Button, Popconfirm, Space, Table } from 'antd'; |
| 10 | import React from 'react'; |
| 11 | import AutoRunRecord, { AutoRunRecordRef } from './components/autoRunRecord'; |
| 12 | import { ParseCronSchedule } from '@/components/CronSchedule'; |
| 13 | import { WorkData } from '@/icp/reply'; |
| 14 | |
| 15 | const { Column } = Table; |
| 16 | |
| 17 | const Page: React.FC = () => { |
| 18 | const Ref_AutoRunRecord = React.useRef<AutoRunRecordRef>(null); |
| 19 | |
| 20 | const [autoRunList, setAutoRunList] = React.useState<AutoRun[]>([]); |
| 21 | // 分页组件的数据 |
| 22 | const [pagination, setPagination] = React.useState({ |
| 23 | page: 1, |
| 24 | pageSize: 10, |
| 25 | total: 0, |
| 26 | }); |
| 27 | |
| 28 | /** |
| 29 | * 获取自动运行的列表 |
| 30 | */ |
| 31 | async function getAutoRunList() { |
| 32 | const res = await ipcGetAutoRunList( |
| 33 | { |
| 34 | page: pagination.page, |
| 35 | pageSize: pagination.pageSize, |
| 36 | }, |
| 37 | { |
| 38 | type: AutoRunType.ReplyComment, |
| 39 | }, |
| 40 | ); |
| 41 | |
| 42 | setAutoRunList(res.list); |
| 43 | setPagination({ |
| 44 | ...pagination, |
| 45 | total: res.count, |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | async function changeAutoRunStatus(id: number, status: AutoRunStatus) { |
| 50 | await ipcUpdateAutoRunStatus(id, status); |
| 51 | getAutoRunList(); |
| 52 | } |
| 53 | |
| 54 | // 打开Ref_AutoRunRecord |
| 55 | function openAutoRunRecord(record: AutoRun) { |
| 56 | Ref_AutoRunRecord.current?.init(record); |
| 57 | } |
| 58 | |
| 59 | React.useEffect(() => { |
| 60 | getAutoRunList(); |
| 61 | }, [pagination.page, pagination.pageSize]); |
| 62 | |
| 63 | // 添加刷新功能的函数 |
| 64 | function refreshAutoRunList() { |
| 65 | getAutoRunList(); |
| 66 | } |
| 67 | |
| 68 | function runNowAutoRun(data: AutoRun) { |
| 69 | ipcRunNowAutoRun(data.id); |
| 70 | } |
| 71 | |
| 72 | // 添加状态映射 |
| 73 | const statusMap: { [key in AutoRunStatus]: string } = { |
| 74 | [AutoRunStatus.DOING]: '活跃', |
| 75 | [AutoRunStatus.PAUSE]: '暂停', |
| 76 | [AutoRunStatus.DELETE]: '删除', |
| 77 | // 可以根据需要添加更多状态 |
| 78 | }; |
| 79 | |
| 80 | // 添加类型映射 |
| 81 | const typeMap: { [key in AutoRunType]: string } = { |
| 82 | [AutoRunType.ReplyComment]: '回复评论', |
| 83 | // 可以根据需要添加更多类型 |
| 84 | }; |
| 85 | |
| 86 | function domDataInfo(data: string) { |
| 87 | const info: WorkData = JSON.parse(data); |
| 88 | return ( |
| 89 | <div> |
| 90 | <img className="w-[50px] h-[100px]" src={info.coverUrl} alt="封面" /> |
| 91 | </div> |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | return ( |
| 96 | <div style={{ width: '100%' }}> |
| 97 | {/* 添加刷新按钮 */} |
| 98 | <Button onClick={refreshAutoRunList} style={{ marginBottom: 16 }}> |
| 99 | 刷新 |
| 100 | </Button> |
| 101 | <Table |
| 102 | rowKey="id" |
| 103 | dataSource={autoRunList} |
| 104 | style={{ width: '100%' }} |
| 105 | pagination={pagination} |
| 106 | onChange={(newPagination) => { |
| 107 | setPagination({ |
| 108 | ...pagination, |
| 109 | page: newPagination.current!, |
| 110 | pageSize: newPagination.pageSize!, |
| 111 | }); |
| 112 | }} |
| 113 | > |
| 114 | <Column title="ID" dataIndex="id" key="id" /> |
| 115 | <Column title="账户ID" dataIndex="accountId" key="accountId" /> |
| 116 | <Column title="运行次数" dataIndex="runCount" key="runCount" /> |
| 117 | <Column |
| 118 | title="状态" |
| 119 | dataIndex="status" |
| 120 | key="status" |
| 121 | render={(status: AutoRunStatus) => statusMap[status]} |
| 122 | /> |
| 123 | <Column |
| 124 | title="类型" |
| 125 | dataIndex="type" |
| 126 | key="type" |
| 127 | render={(type: AutoRunType) => typeMap[type]} |
| 128 | /> |
| 129 | <Column |
| 130 | title="触发周期" |
| 131 | dataIndex="cycleType" |
| 132 | key="cycleType" |
| 133 | render={(cycleType: string) => ( |
| 134 | <ParseCronSchedule cronExpression={cycleType} /> |
| 135 | )} |
| 136 | /> |
| 137 | |
| 138 | <Column |
| 139 | title="数据" |
| 140 | dataIndex="data" |
| 141 | key="data" |
| 142 | render={(data: any) => domDataInfo(data)} |
| 143 | /> |
| 144 | |
| 145 | <Column |
| 146 | title="创建时间" |
| 147 | key="createTime" |
| 148 | render={(_: any, record: AutoRun) => ( |
| 149 | <p>{new Date(record.createTime).toLocaleString()}</p> |
| 150 | )} |
| 151 | /> |
| 152 | |
| 153 | <Column |
| 154 | title="操作" |
| 155 | key="action" |
| 156 | render={(_: any, record: AutoRun) => ( |
| 157 | <Space size="middle"> |
| 158 | <Popconfirm |
| 159 | title="确认暂停该任务" |
| 160 | onConfirm={(e?: React.MouseEvent<HTMLElement>) => { |
| 161 | changeAutoRunStatus(record.id, AutoRunStatus.PAUSE); |
| 162 | }} |
| 163 | okText="是" |
| 164 | cancelText="否" |
| 165 | > |
| 166 | <a>暂停</a> |
| 167 | </Popconfirm> |
| 168 | |
| 169 | <Popconfirm |
| 170 | title="确认删除该任务" |
| 171 | onConfirm={(e?: React.MouseEvent<HTMLElement>) => { |
| 172 | changeAutoRunStatus(record.id, AutoRunStatus.DELETE); |
| 173 | }} |
| 174 | okText="是" |
| 175 | cancelText="否" |
| 176 | > |
| 177 | <a>删除</a> |
| 178 | </Popconfirm> |
| 179 | <a onClick={() => runNowAutoRun(record)}>立即执行</a> |
| 180 | <a onClick={() => openAutoRunRecord(record)}>打开记录</a> |
| 181 | </Space> |
| 182 | )} |
| 183 | /> |
| 184 | </Table> |
| 185 | <AutoRunRecord ref={Ref_AutoRunRecord} /> |
| 186 | </div> |
| 187 | ); |
| 188 | }; |
| 189 | |
| 190 | export default Page; |
| 191 |