211 lines
7.9 KiB
TypeScript
211 lines
7.9 KiB
TypeScript
|
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} from "antd";
|
||
|
import {CheckOutlined, ReloadOutlined} from "@ant-design/icons";
|
||
|
import useMessage from "antd/es/message/useMessage";
|
||
|
|
||
|
type PropsType = {
|
||
|
projId: string;
|
||
|
projIntroduction?: string;
|
||
|
projDesc?: string;
|
||
|
}
|
||
|
|
||
|
export default function AiHelper(props: PropsType) {
|
||
|
const globalContext = useContext(GlobalContext);
|
||
|
const pingTimeout = useRef(-1);
|
||
|
const ws = useRef<WebSocket | null>(null);
|
||
|
const [messageApi, messageApiHolder] = useMessage();
|
||
|
const [projIntroduction, setProjIntroduction] = useState<string>(props.projIntroduction ? props.projIntroduction : '');
|
||
|
const [newProjIntroduction, setNewProjIntroduction] = useState<string>('');
|
||
|
const [isProjIntroductionLoading, setIsProjIntroductionLoading] = useState(false);
|
||
|
const [projDesc, setProjDesc] = useState<string>(props.projDesc ? props.projDesc : '');
|
||
|
const [newProjDesc, setNewProjDesc] = useState<string>('');
|
||
|
const [isProjDescLoading, setIsProjDescLoading] = 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.type == 'REFRESH_PROJ_INTRODUCTION') {
|
||
|
if (data.projId != props.projId) {
|
||
|
return;
|
||
|
}
|
||
|
setIsProjIntroductionLoading(false);
|
||
|
setNewProjIntroduction(data.content);
|
||
|
return;
|
||
|
}
|
||
|
if (data.type == 'REFRESH_PROJ_DESC') {
|
||
|
if (data.projId != props.projId) {
|
||
|
return;
|
||
|
}
|
||
|
setIsProjDescLoading(false);
|
||
|
setNewProjDesc(data.content);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
ws.current.onerror = (event) => {
|
||
|
console.log('error', event);
|
||
|
}
|
||
|
ws.current.onclose = (event) => {
|
||
|
console.log('close', event);
|
||
|
websocket()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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 updateProjIntroduction = () => {
|
||
|
put<any>({
|
||
|
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<any>({
|
||
|
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}
|
||
|
<div style={{padding: '5px 0 0 0', fontWeight: 'bold'}}>项目简介</div>
|
||
|
<Spin tip="正在处理,请稍后..." size="small" spinning={isProjIntroductionLoading}>
|
||
|
<div style={{padding: '5px 0 0 0'}}>
|
||
|
{newProjIntroduction ? <Divider orientation="right" plain>原简介</Divider> : <></>}
|
||
|
{projIntroduction ? <div>{projIntroduction}</div> : <Empty description="暂无内容"/>}
|
||
|
{
|
||
|
newProjIntroduction ? (
|
||
|
<>
|
||
|
<Divider orientation="right" plain>新简介</Divider>
|
||
|
<div>{newProjIntroduction}</div>
|
||
|
</>
|
||
|
) : <></>
|
||
|
}
|
||
|
</div>
|
||
|
<div style={{padding: '5px 0 0 0', textAlign: 'center'}}>
|
||
|
{
|
||
|
newProjIntroduction ? (
|
||
|
<Space>
|
||
|
<Button type="link" style={{cursor: 'pointer'}}
|
||
|
onClick={updateProjIntroduction}><CheckOutlined/> 保存结果</Button>
|
||
|
<Button type="link" style={{cursor: 'pointer'}}
|
||
|
onClick={generateProjIntroduction}><ReloadOutlined/> 重新生成</Button>
|
||
|
</Space>
|
||
|
) : <Button type="link" style={{cursor: 'pointer'}}
|
||
|
onClick={generateProjIntroduction}>AI生成</Button>
|
||
|
}
|
||
|
</div>
|
||
|
</Spin>
|
||
|
<Divider dashed/>
|
||
|
<div style={{padding: '5px 0 0 0', fontWeight: 'bold'}}>项目详情</div>
|
||
|
<Spin tip="正在处理,请稍后..." size="small" spinning={isProjDescLoading}>
|
||
|
<div style={{padding: '5px 0 0 0'}}>
|
||
|
{newProjDesc ? <Divider orientation="right" plain>原详情</Divider> : <></>}
|
||
|
{projDesc ? <div>{projDesc}</div> : <Empty description="暂无内容"/>}
|
||
|
{
|
||
|
newProjDesc ? (
|
||
|
<>
|
||
|
<Divider orientation="right" plain>新详情</Divider>
|
||
|
<div>{newProjDesc}</div>
|
||
|
</>
|
||
|
) : <></>
|
||
|
}
|
||
|
</div>
|
||
|
<div style={{padding: '5px 0 0 0', textAlign: 'center'}}>
|
||
|
<div style={{padding: '5px 0 0 0', textAlign: 'center'}}>
|
||
|
{
|
||
|
newProjDesc ? (
|
||
|
<Space>
|
||
|
<Button type="link" style={{cursor: 'pointer'}}
|
||
|
onClick={updateProjDesc}><CheckOutlined/> 保存结果</Button>
|
||
|
<Button type="link" style={{cursor: 'pointer'}}
|
||
|
onClick={generateProjDesc}><ReloadOutlined/> 重新生成</Button>
|
||
|
</Space>
|
||
|
) : <Button type="link" style={{cursor: 'pointer'}}
|
||
|
onClick={generateProjDesc}>AI生成</Button>
|
||
|
}
|
||
|
</div>
|
||
|
</div>
|
||
|
</Spin>
|
||
|
</>
|
||
|
)
|
||
|
}
|