1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-18 17:22:05 +00:00
seahub/frontend/src/repo-snapshot.js

323 lines
11 KiB
JavaScript
Raw Normal View History

2019-06-15 07:11:35 +00:00
import React from 'react';
import ReactDom from 'react-dom';
import PropTypes from 'prop-types';
import { navigate } from '@gatsbyjs/reach-router';
2019-06-15 07:11:35 +00:00
import { Utils } from './utils/utils';
import { gettext, siteRoot, mediaUrl, logoPath, logoWidth, logoHeight, siteTitle } from './utils/constants';
2019-06-15 07:11:35 +00:00
import { seafileAPI } from './utils/seafile-api';
import Loading from './components/loading';
import ModalPortal from './components/modal-portal';
import toaster from './components/toast';
import CommonToolbar from './components/toolbar/common-toolbar';
import ConfirmRestoreRepo from './components/dialog/confirm-restore-repo';
import './css/toolbar.css';
import './css/search.css';
import './css/repo-snapshot.css';
const {
repoID, repoName, canRestoreRepo,
2019-06-15 07:11:35 +00:00
commitID, commitTime, commitDesc, commitRelativeTime,
showAuthor, authorAvatarURL, authorName, authorNickName
} = window.app.pageOptions;
class RepoSnapshot extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
errorMsg: '',
folderPath: '/',
folderItems: [],
isConfirmDialogOpen: false
};
}
componentDidMount() {
this.renderFolder(this.state.folderPath);
}
toggleDialog = () => {
this.setState({
isConfirmDialogOpen: !this.state.isConfirmDialogOpen
});
};
2019-06-15 07:11:35 +00:00
onSearchedClick = (selectedItem) => {
if (selectedItem.is_dir === true) {
let url = siteRoot + 'library/' + selectedItem.repo_id + '/' + selectedItem.repo_name + selectedItem.path;
navigate(url, { replace: true });
2019-06-15 07:11:35 +00:00
} else {
let url = siteRoot + 'lib/' + selectedItem.repo_id + '/file' + Utils.encodePath(selectedItem.path);
let newWindow = window.open('about:blank');
newWindow.location.href = url;
}
};
2019-06-15 07:11:35 +00:00
goBack = (e) => {
e.preventDefault();
window.history.back();
};
2019-06-15 07:11:35 +00:00
renderFolder = (folderPath) => {
this.setState({
folderPath: folderPath,
folderItems: [],
isLoading: true
});
seafileAPI.listCommitDir(repoID, commitID, folderPath).then((res) => {
this.setState({
isLoading: false,
folderItems: res.data.dirent_list
});
}).catch((error) => {
this.setState({
isLoading: false,
errorMsg: Utils.getErrorMsg(error, true) // true: show login tip if 403
});
2019-06-15 07:11:35 +00:00
});
};
2019-06-15 07:11:35 +00:00
clickFolderPath = (folderPath, e) => {
e.preventDefault();
this.renderFolder(folderPath);
};
2019-06-15 07:11:35 +00:00
renderPath = () => {
const path = this.state.folderPath;
const pathList = path.split('/');
if (path == '/') {
return <span className="text-truncate" title={repoName}>{repoName}</span>;
2019-06-15 07:11:35 +00:00
}
return (
<React.Fragment>
<a href="#" onClick={this.clickFolderPath.bind(this, '/')} className="text-truncate" title={repoName}>{repoName}</a>
<span className="mx-1">/</span>
2019-06-15 07:11:35 +00:00
{pathList.map((item, index) => {
if (index > 0 && index != pathList.length - 1) {
return (
<React.Fragment key={index}>
2024-07-18 03:58:42 +00:00
<a href="#" onClick={this.clickFolderPath.bind(this, pathList.slice(0, index + 1).join('/'))} className="text-truncate" title={pathList[index]}>{pathList[index]}</a>
<span className="mx-1">/</span>
2019-06-15 07:11:35 +00:00
</React.Fragment>
);
2019-06-15 07:11:35 +00:00
}
return null;
2019-06-15 07:11:35 +00:00
}
)}
<span className="text-truncate" title={pathList[pathList.length - 1]}>{pathList[pathList.length - 1]}</span>
2019-06-15 07:11:35 +00:00
</React.Fragment>
);
};
2019-06-15 07:11:35 +00:00
restoreRepo = () => {
seafileAPI.revertRepo(repoID, commitID).then((res) => {
this.toggleDialog();
toaster.success(gettext('Successfully restored the library.'));
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
2019-06-15 07:11:35 +00:00
this.toggleDialog();
toaster.danger(errorMsg);
});
};
2019-06-15 07:11:35 +00:00
render() {
const { isConfirmDialogOpen, folderPath } = this.state;
let title = gettext('{placeholder} Snapshot');
title = title.replace('{placeholder}', '<span class="op-target text-truncate mx-1">' + Utils.HTMLescape(repoName) + '</span>');
2019-06-15 07:11:35 +00:00
return (
<React.Fragment>
<div className="h-100 d-flex flex-column">
<div className="top-header d-flex justify-content-between">
<a href={siteRoot}>
<img src={mediaUrl + logoPath} height={logoHeight} width={logoWidth} title={siteTitle} alt="logo" />
</a>
<CommonToolbar onSearchedClick={this.onSearchedClick} />
</div>
<div className="flex-auto container-fluid pt-4 pb-6 o-auto">
<div className="row">
<div className="col-md-10 offset-md-1">
<h2>
2024-07-18 03:58:42 +00:00
<span dangerouslySetInnerHTML={{ __html: title }} className="d-flex mw-100"></span>
<span className="heading-commit-time ml-1">({commitTime})</span>
</h2>
<a href="#" className="go-back" title={gettext('Back')} role="button" aria-label={gettext('Back')} onClick={this.goBack}>
<span className="sf3-font sf3-font-down rotate-90 d-inline-block"></span>
2019-06-15 07:11:35 +00:00
</a>
{folderPath == '/' && (
<div className="d-flex mb-2 align-items-center">
<p className="m-0 text-truncate" title={commitDesc}>{commitDesc}</p>
<div className="ml-4 border-left pl-4 d-flex align-items-center flex-shrink-0">
2019-06-15 07:11:35 +00:00
{showAuthor ? (
<React.Fragment>
<img src={authorAvatarURL} width="20" height="20" alt="" className="rounded mr-1" />
<a href={`${siteRoot}profile/${encodeURIComponent(authorName)}/`}>{authorNickName}</a>
</React.Fragment>
) : <span>{gettext('Unknown')}</span>}
2024-07-18 03:58:42 +00:00
<p className="m-0 ml-2" dangerouslySetInnerHTML={{ __html: commitRelativeTime }}></p>
2019-06-15 07:11:35 +00:00
</div>
</div>
)}
<div className="d-flex justify-content-between align-items-center op-bar">
<p className="m-0 text-truncate d-flex"><span className="mr-1">{gettext('Current path: ')}</span>{this.renderPath()}</p>
{(folderPath == '/' && canRestoreRepo) &&
<button className="btn btn-secondary op-bar-btn flex-shrink-0 ml-4" onClick={this.toggleDialog}>{gettext('Restore')}</button>
2019-06-15 07:11:35 +00:00
}
</div>
<Content
2019-06-15 07:11:35 +00:00
data={this.state}
renderFolder={this.renderFolder}
/>
</div>
</div>
</div>
</div>
{isConfirmDialogOpen &&
<ModalPortal>
<ConfirmRestoreRepo
restoreRepo={this.restoreRepo}
toggle={this.toggleDialog}
/>
</ModalPortal>
}
</React.Fragment>
);
}
}
class Content extends React.Component {
constructor(props) {
super(props);
this.theadData = [
2024-07-18 03:58:42 +00:00
{ width: '5%', text: '' },
{ width: '55%', text: gettext('Name') },
{ width: '20%', text: gettext('Size') },
{ width: '20%', text: '' }
2019-06-15 07:11:35 +00:00
];
}
2019-06-15 07:11:35 +00:00
render() {
const { isLoading, errorMsg, folderPath, folderItems } = this.props.data;
if (isLoading) {
return <Loading />;
}
if (errorMsg) {
return <p className="error mt-6 text-center">{errorMsg}</p>;
}
return (
2019-06-21 05:59:17 +00:00
<table className="table-hover">
<thead>
<tr>
{this.theadData.map((item, index) => {
return <th key={index} width={item.width}>{item.text}</th>;
})}
</tr>
</thead>
<tbody>
{folderItems.map((item, index) => {
return <FolderItem
2019-06-21 05:59:17 +00:00
key={index}
item={item}
2019-06-21 05:59:17 +00:00
folderPath={folderPath}
renderFolder={this.props.renderFolder}
/>;
})
}
</tbody>
</table>
2019-06-15 07:11:35 +00:00
);
}
}
Content.propTypes = {
data: PropTypes.object.isRequired,
renderFolder: PropTypes.func.isRequired,
};
2019-06-15 07:11:35 +00:00
class FolderItem extends React.Component {
constructor(props) {
super(props);
this.state = {
isIconShown: false
};
}
handleMouseOver = () => {
2024-07-18 03:58:42 +00:00
this.setState({ isIconShown: true });
};
2019-06-15 07:11:35 +00:00
handleMouseOut = () => {
2024-07-18 03:58:42 +00:00
this.setState({ isIconShown: false });
};
2019-06-15 07:11:35 +00:00
restoreItem = (e) => {
e.preventDefault();
const item = this.props.item;
const path = Utils.joinPath(this.props.folderPath, item.name);
const request = item.type == 'dir' ?
2024-07-18 03:58:42 +00:00
seafileAPI.revertFolder(repoID, path, commitID) :
2019-06-15 07:11:35 +00:00
seafileAPI.revertFile(repoID, path, commitID);
request.then((res) => {
toaster.success(gettext('Restored 1 item'));
2019-06-15 07:11:35 +00:00
}).catch((error) => {
let errorMsg = Utils.getErrorMsg(error);
2019-06-15 07:11:35 +00:00
toaster.danger(errorMsg);
});
};
2019-06-15 07:11:35 +00:00
renderFolder = (e) => {
e.preventDefault();
const item = this.props.item;
const { folderPath } = this.props;
this.props.renderFolder(Utils.joinPath(folderPath, item.name));
};
2019-06-15 07:11:35 +00:00
render() {
const item = this.props.item;
const { isIconShown } = this.state;
const { folderPath } = this.props;
return item.type == 'dir' ? (
<tr onMouseOver={this.handleMouseOver} onMouseOut={this.handleMouseOut} onFocus={this.handleMouseOver}>
<td className="text-center"><img src={Utils.getFolderIconUrl()} alt={gettext('Folder')} width="24" /></td>
2019-06-15 07:11:35 +00:00
<td><a href="#" onClick={this.renderFolder}>{item.name}</a></td>
<td></td>
<td>
2024-07-18 03:58:42 +00:00
<a href="#" className={`action-icon sf2-icon-reply ${isIconShown ? '' : 'invisible'}`} onClick={this.restoreItem} title={gettext('Restore')} aria-label={gettext('Restore')} role="button"></a>
2019-06-15 07:11:35 +00:00
</td>
</tr>
) : (
<tr onMouseOver={this.handleMouseOver} onMouseOut={this.handleMouseOut} onFocus={this.handleMouseOver}>
2019-06-15 07:11:35 +00:00
<td className="text-center"><img src={Utils.getFileIconUrl(item.name)} alt={gettext('File')} width="24" /></td>
<td><a href={`${siteRoot}repo/${repoID}/snapshot/files/?obj_id=${item.obj_id}&commit_id=${commitID}&p=${encodeURIComponent(Utils.joinPath(folderPath, item.name))}`} target="_blank" rel="noreferrer">{item.name}</a></td>
2019-06-15 07:11:35 +00:00
<td>{Utils.bytesToSize(item.size)}</td>
<td>
2024-07-18 03:58:42 +00:00
<a href="#" className={`action-icon sf2-icon-reply ${isIconShown ? '' : 'invisible'}`} onClick={this.restoreItem} title={gettext('Restore')} aria-label={gettext('Restore')} role="button"></a>
<a href={`${siteRoot}repo/${repoID}/${item.obj_id}/download/?file_name=${encodeURIComponent(item.name)}&p=${encodeURIComponent(Utils.joinPath(folderPath, item.name))}`} className={`action-icon sf2-icon-download ${isIconShown ? '' : 'invisible'}`} title={gettext('Download')}></a>
2019-06-15 07:11:35 +00:00
</td>
</tr>
);
}
}
FolderItem.propTypes = {
item: PropTypes.object.isRequired,
folderPath: PropTypes.string.isRequired,
renderFolder: PropTypes.func.isRequired,
};
ReactDom.render(<RepoSnapshot />, document.getElementById('wrapper'));