import {useContext, useEffect, useRef, useState} from "react"; import {GlobalContext} from "../../context/GlobalContext.ts"; import {put, WebSocketBaseUrl} from "../../util/AjaxUtils.ts"; import {Button, Divider, Empty, Space, Spin, Table, TableProps} from "antd"; import {CheckOutlined, ReloadOutlined} from "@ant-design/icons"; import useMessage from "antd/es/message/useMessage"; type PropsType = { projId: string; projIntroduction?: string; projDesc?: string; } type ProjModType = { name: string, desc: string } export default function AiHelper(props: PropsType) { const globalContext = useContext(GlobalContext); const pingTimeout = useRef(-1); const ws = useRef(null); const [messageApi, messageApiHolder] = useMessage(); const [projIntroduction, setProjIntroduction] = useState(props.projIntroduction ? props.projIntroduction : ''); const [newProjIntroduction, setNewProjIntroduction] = useState(''); const [isProjIntroductionLoading, setIsProjIntroductionLoading] = useState(false); const [projDesc, setProjDesc] = useState(props.projDesc ? props.projDesc : ''); const [newProjDesc, setNewProjDesc] = useState(''); const [isProjDescLoading, setIsProjDescLoading] = useState(false); const [projModArray, setProjModArray] = useState([]); const [newProjModArray, setNewProjModArray] = useState([]); const [isProjModArrayLoading, setIsProjModArrayLoading] = useState(false); const ping = () => { clearTimeout(pingTimeout.current); pingTimeout.current = setTimeout(() => { if (!ws.current) { return; } ws.current.send("PING"); ping(); }, 3000); } const websocket = () => { ws.current = new WebSocket(`${WebSocketBaseUrl}/ws/ai/${globalContext.user.userId}`); ws.current.onopen = (event) => { console.log('打开', event); ping(); } ws.current.onmessage = (event) => { if (event.data == 'PONE') { return; } const data = JSON.parse(event.data); if (data.projId != props.projId) { return; } if (data.type == 'REFRESH_PROJ_INTRODUCTION') { setIsProjIntroductionLoading(false); setNewProjIntroduction(data.content); } else if (data.type == 'REFRESH_PROJ_DESC') { setIsProjDescLoading(false); setNewProjDesc(data.content); } else if (data.type == 'REFRESH_PROJ_MODS') { setIsProjModArrayLoading(false); const projMods = JSON.parse(data.content) as ProjModType[]; setNewProjModArray(projMods); } } ws.current.onerror = (event) => { console.log('error', event); } ws.current.onclose = (event) => { console.log('close', event); websocket() } } const projModColumnArray: TableProps['columns'] = [ {title: '模块名称', dataIndex: 'name', key: 'name', width: 150, align: 'center'}, {title: '模块描述', dataIndex: 'desc', key: 'desc', align: 'center'}, ]; const generateProjIntroduction = () => { ws.current?.send(JSON.stringify({ type: 'REFRESH_PROJ_INTRODUCTION', projId: props.projId })); ping(); setIsProjIntroductionLoading(true); } const generateProjDesc = () => { ws.current?.send(JSON.stringify({ type: 'REFRESH_PROJ_DESC', projId: props.projId })); ping(); setIsProjDescLoading(true); } const generateProjModArray = () => { ws.current?.send(JSON.stringify({ type: 'REFRESH_PROJ_MODS', projId: props.projId })); ping(); setIsProjModArrayLoading(true); } /** * 保存简介 */ const updateProjIntroduction = () => { put({ messageApi, url: `/api/proj/update-introduction/${props.projId}`, body: { content: newProjIntroduction }, onBefore() { setIsProjIntroductionLoading(true); }, onSuccess() { messageApi.success('保存成功'); setProjIntroduction(newProjIntroduction); setNewProjIntroduction(''); }, onFinally() { setIsProjIntroductionLoading(false) } }) } /** * 保存详情 */ const updateProjDesc = () => { put({ messageApi, url: `/api/proj/update-desc/${props.projId}`, body: { content: newProjDesc }, onBefore() { setIsProjDescLoading(true); }, onSuccess() { messageApi.success('保存成功').then(); setProjDesc(newProjDesc); setNewProjDesc(''); }, onFinally() { setIsProjDescLoading(false) } }) } useEffect(() => { if (!props.projId) { return; } websocket(); }, [globalContext.user.userId, props.projId]); return ( <> {messageApiHolder}
项目简介
{newProjIntroduction ? 原简介 : <>} {projIntroduction ?
{projIntroduction}
: } { newProjIntroduction ? ( <> 新简介
{newProjIntroduction}
) : <> }
{ newProjIntroduction ? ( ) : }
项目详情
{newProjDesc ? 原详情 : <>} {projDesc ?
{projDesc}
: } { newProjDesc ? ( <> 新详情
{newProjDesc}
) : <> }
{ newProjDesc ? ( ) : }
项目模块
{newProjModArray.length > 0 ? 原模块 : <>} { newProjModArray.length > 0 ? ( <> 新模块
) : <> }
{ newProjModArray.length > 0 ? ( ) : }
) }