forgeplus-react/src/forge/Main/Diff.jsx

187 lines
6.3 KiB
React
Raw Normal View History

import React, { useEffect, useState , useRef } from "react";
2020-06-03 09:56:07 +08:00
import styled from "styled-components";
import { Button ,Spin } from "antd";
2021-09-29 21:04:40 +08:00
import { timeFormat, truncateCommitId } from '../common/util';
2020-11-10 16:42:32 +08:00
import { getImageUrl } from 'educoder';
import Files from '../Merge/Files';
2021-09-24 11:27:32 +08:00
import Tree from "./img/tree.png";
2020-06-03 09:56:07 +08:00
import User from "../Component/User";
2021-10-11 17:41:58 +08:00
import RenderHtml from "../../components/render-html";
2020-06-03 09:56:07 +08:00
import axios from "axios";
2021-09-27 10:08:52 +08:00
import { Link } from "react-router-dom";
2020-05-29 17:00:42 +08:00
const Infos = styled.div`
2021-09-24 11:27:32 +08:00
border: 1px solid #FAFCFF;
2020-11-10 16:42:32 +08:00
margin-bottom:15px;
2020-06-03 09:56:07 +08:00
& .commitinfos {
background-color: #f1f8ff;
2021-09-24 11:27:32 +08:00
border: 1px solid rgba(42, 97, 255, 0.23);
border-radius: 3px 3px 0px 0px;
2021-09-27 10:08:52 +08:00
padding: 10px 20px 10px 16px;
& .markdown-body table{
background: #f1f8ff;
}
& .btnblue{
margin-top: 12px;
2021-09-24 11:27:32 +08:00
}
& .task-hide{
width: 65rem;
overflow:hidden;
white-space:normal;
word-break:break-all;
font-weight: bold;
color: #333333;
font-size: 16px;
}
2020-05-29 17:00:42 +08:00
}
2020-06-03 09:56:07 +08:00
& > .f-wrap-between {
2021-09-27 10:08:52 +08:00
padding: 14px 20px 14px 16px;
2022-01-07 16:32:01 +08:00
border-radius: 0px 0px 3px 3px;
2021-09-24 11:27:32 +08:00
border: 1px solid #D0D0D0;
2022-01-07 16:32:01 +08:00
border-top: none;
2021-09-24 11:27:32 +08:00
.df{
align-items: center;
2021-09-27 10:08:52 +08:00
& .underline:hover{
text-decoration: underline;
}
2021-09-24 11:27:32 +08:00
}
2020-05-29 17:00:42 +08:00
}
`;
2021-09-24 11:27:32 +08:00
//提交详情页
2021-09-27 13:43:47 +08:00
export default (props) => {
2022-12-08 11:01:52 +08:00
const {match , history, projectDetail } = props;
2020-07-07 09:25:56 +08:00
const [data, setData] = useState({undefined});
2020-06-03 09:56:07 +08:00
const [commit, setCommit] = useState(undefined);
const [parents, setParents] = useState(undefined);
const [committer, setCommitter] = useState(undefined);
const [isSpin, setIsSpin] = useState(true);
const { sha , projectsId, owner } = match.params;
2022-12-08 11:01:52 +08:00
const [ dropLoading, setDropLoading] = useState(false); //加载loading
const limit = 100;
const fRef = useRef();
useEffect(()=>{
window.addEventListener("scroll",scrollListener);
return ()=>{window.removeEventListener("scroll",scrollListener);}
},[])
const scrollListener=()=>{
let scrollHeight = document.documentElement.scrollHeight;
let clientHeight = document.documentElement.clientHeight;
let scrollTop = document.documentElement.scrollTop;
if(Math.ceil(scrollTop+clientHeight) >= scrollHeight-5){
InitDiffData();
}
}
2022-12-08 11:01:52 +08:00
useEffect(()=>{
if(projectDetail){
const { author, name} = projectDetail;
document.title = `${commit && commit.message ? commit.message+'-' : ''}${sha}-${author.name}/${name}`;
}
}, [projectDetail, sha, commit])
2020-06-03 09:56:07 +08:00
useEffect(() => {
2020-11-10 16:42:32 +08:00
if (projectsId && owner && sha) {
InitDiffData();
2020-11-10 16:42:32 +08:00
const url = `/${owner}/${projectsId}/commits/${sha}.json`;
2020-06-03 09:56:07 +08:00
axios
.get(url)
.then(result => {
if (result) {
// setData(result.data);
2020-06-03 09:56:07 +08:00
setCommit(result.data.commit);
setParents(result.data.parents);
setCommitter(result.data.committer || (result.data.commit && result.data.commit.committer));
setIsSpin(false);
}
})
.catch(error => {
console.log(error);
});
}
2020-11-10 16:42:32 +08:00
}, [projectsId , owner, sha]);
async function InitDiffData (){
let f = fRef.current ? fRef.current.files : [];
let p = fRef.current ? fRef.current.page : 1;
let hm = fRef.current ? fRef.current.hasMore : true;
if (!hm || dropLoading || !isSpin) {
return;
}
setDropLoading(true);
const url = `/v1/${owner}/${projectsId}/commits/${sha}/files.json`;
await axios.get(url,{
params:{page:p,limit}
}).then(res=>{
if(res?.status === 200){
let arr = p === 1 ? res.data.files : [...f,...res.data.files];
setData(res.data);
fRef.current = {files:arr,page:p+1,hasMore:arr.length === limit};
setDropLoading(false);
}
})
}
2020-06-03 09:56:07 +08:00
return (
<div className="main" style={{padding:"0px",border:"none"}}>
2020-06-03 09:56:07 +08:00
<Spin spinning={isSpin}>
<Infos>
<div className="commitinfos">
2020-11-10 16:42:32 +08:00
<div className="f-wrap-between">
2021-09-24 11:27:32 +08:00
<div>
2021-09-26 11:36:30 +08:00
{commit && commit.message &&
<RenderHtml className="task-hide" value={commit.message}/>
2021-09-26 11:36:30 +08:00
}
2021-10-12 15:14:49 +08:00
<Link to={`/${owner}/${projectsId}/tree/${data.branch}`}><i className="iconfont icon-fenzhi2 font-18"></i>{data.branch}</Link>
2021-09-24 11:27:32 +08:00
</div>
2021-09-24 14:29:57 +08:00
<Button type="primary" onClick={()=>{history.push(`/${owner}/${projectsId}/tree/${truncateCommitId(sha)}`)}} className="btnblue" style={{height:"36px"}}>浏览文件</Button>
2020-11-10 16:42:32 +08:00
</div>
2020-06-03 09:56:07 +08:00
</div>
<div className="f-wrap-between" style={{ alignItems: "center" }}>
<ul className="df">
<User
2021-09-26 11:36:30 +08:00
id = {committer && committer.id}
2021-10-08 14:41:02 +08:00
url={(committer && getImageUrl(`/${committer.image_url}`))}
2020-06-03 09:56:07 +08:00
name={committer && committer.name}
2021-09-27 10:08:52 +08:00
login={committer && committer.login}
2020-06-03 09:56:07 +08:00
/>
2021-09-27 10:08:52 +08:00
{commit && commit.timestamp && <li className="ml4">提交于{timeFormat(commit.timestamp)}</li>}
2020-06-03 09:56:07 +08:00
</ul>
<li className="df">
{
parents && parents.length > 0 && parents.map((item,key)=>{
return(
2021-09-24 11:27:32 +08:00
<div className="ml40 f-wrap-alignCenter">
<label className="mr8">父节点</label>
<img src={Tree} alt="sha" width={"16px"} className="mr4"/>
<Link to={`/${owner}/${projectsId}/commits/${truncateCommitId(`${item.sha}`)}/${data.branch}`} className="underline">{truncateCommitId(item.sha)}</Link>
2021-09-24 11:27:32 +08:00
</div>
2020-06-03 09:56:07 +08:00
)
})
}
2021-09-24 11:27:32 +08:00
<div className="ml40 f-wrap-alignCenter">
<label className="mr8">当前节点</label>
<img src={Tree} alt="sha" width={"16px"} className="mr4"/>
<span>{truncateCommitId(sha)}</span>
</div>
2020-06-03 09:56:07 +08:00
</li>
</div>
</Infos>
2020-11-10 16:42:32 +08:00
<Files
history={history}
data={data}
filesData={fRef.current && fRef.current.files}
2020-11-10 16:42:32 +08:00
owner={owner}
projectsId={projectsId}
2021-10-08 14:14:39 +08:00
parentsSha={parents && parents.length > 0 && parents[0].sha}
mergeId={`commits/${sha}`}
2020-11-10 16:42:32 +08:00
/>
{ dropLoading && <p style={{textAlign:"center",color:"#999",padding:"10px"}}>文件加载中...</p>}
2020-06-03 09:56:07 +08:00
</Spin>
2020-05-29 17:00:42 +08:00
</div>
2020-06-03 09:56:07 +08:00
);
};