2023-05-13 15:42:54 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Loading from '../../components/loading';
|
2023-06-19 17:35:30 +08:00
|
|
|
import { gettext, historyRepoID, PER_PAGE } from '../../utils/constants';
|
2023-05-13 15:42:54 +08:00
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
|
|
|
import { Utils } from '../../utils/utils';
|
|
|
|
import editUtilities from '../../utils/editor-utilities';
|
|
|
|
import toaster from '../../components/toast';
|
|
|
|
import HistoryVersion from './history-version';
|
|
|
|
import Switch from '../../components/common/switch';
|
|
|
|
|
2023-06-19 17:35:30 +08:00
|
|
|
const { docUuid } = window.fileHistory.pageOptions;
|
|
|
|
|
2023-05-13 15:42:54 +08:00
|
|
|
class SidePanel extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
|
|
|
historyVersions: [],
|
|
|
|
errorMessage: '',
|
2023-06-19 17:35:30 +08:00
|
|
|
currentPage: 1,
|
|
|
|
hasMore: false,
|
|
|
|
fileOwner: '',
|
|
|
|
isReloadingData: false,
|
2023-05-13 15:42:54 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2023-06-19 17:35:30 +08:00
|
|
|
seafileAPI.listSdocHistory(docUuid, 1, PER_PAGE).then(res => {
|
|
|
|
let historyList = res.data;
|
|
|
|
if (historyList.length === 0) {
|
|
|
|
this.setState({isLoading: false});
|
|
|
|
throw Error('there has an error in server');
|
|
|
|
}
|
|
|
|
this.initResultState(res.data);
|
2023-05-13 15:42:54 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-19 17:35:30 +08:00
|
|
|
refershFileList() {
|
|
|
|
seafileAPI.listSdocHistory(docUuid, 1, PER_PAGE).then(res => {
|
|
|
|
this.initResultState(res.data);
|
|
|
|
});
|
2023-05-13 15:42:54 +08:00
|
|
|
}
|
|
|
|
|
2023-06-19 17:35:30 +08:00
|
|
|
initResultState(result) {
|
|
|
|
if (result.histories.length) {
|
|
|
|
this.setState({
|
|
|
|
historyVersions: result.histories,
|
|
|
|
currentPage: result.page,
|
|
|
|
hasMore: result.total_count > (PER_PAGE * this.state.currentPage),
|
|
|
|
isLoading: false,
|
|
|
|
fileOwner: result.histories[0].creator_email,
|
|
|
|
});
|
|
|
|
this.props.onSelectHistoryVersion(result.histories[0], result.histories[1]);
|
|
|
|
}
|
2023-05-13 15:42:54 +08:00
|
|
|
}
|
|
|
|
|
2023-06-19 17:35:30 +08:00
|
|
|
updateResultState(result) {
|
|
|
|
if (result.histories.length) {
|
|
|
|
this.setState({
|
|
|
|
historyVersions: [...this.state.historyVersions, ...result.histories],
|
|
|
|
currentPage: result.page,
|
|
|
|
hasMore: result.total_count > (PER_PAGE * this.state.currentPage),
|
|
|
|
isLoading: false,
|
|
|
|
fileOwner: result.histories[0].creator_email
|
2023-05-13 15:42:54 +08:00
|
|
|
});
|
|
|
|
}
|
2023-06-19 17:35:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
loadMore = () => {
|
|
|
|
if (!this.state.isReloadingData) {
|
|
|
|
let currentPage = this.state.currentPage + 1;
|
|
|
|
this.setState({
|
|
|
|
currentPage: currentPage,
|
|
|
|
isReloadingData: true,
|
|
|
|
});
|
|
|
|
seafileAPI.listSdocHistory(docUuid, currentPage, PER_PAGE).then(res => {
|
|
|
|
this.updateResultState(res.data);
|
|
|
|
this.setState({isReloadingData: false});
|
|
|
|
});
|
2023-05-13 15:42:54 +08:00
|
|
|
}
|
2023-06-19 17:35:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renameHistoryVersion = (objID, newName) => {
|
|
|
|
seafileAPI.renameSdocHistory(docUuid, objID, newName).then((res) => {
|
|
|
|
this.setState({
|
|
|
|
historyVersions: this.state.historyVersions.map(item => {
|
|
|
|
if (item.obj_id == objID) {
|
|
|
|
item.name = newName;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}).catch(error => {
|
|
|
|
const errorMessage = Utils.getErrorMsg(error, true);
|
|
|
|
this.setState({ isLoading: false, errorMessage: errorMessage });
|
|
|
|
});
|
2023-05-13 15:42:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onScrollHandler = (event) => {
|
|
|
|
const clientHeight = event.target.clientHeight;
|
|
|
|
const scrollHeight = event.target.scrollHeight;
|
|
|
|
const scrollTop = event.target.scrollTop;
|
|
|
|
const isBottom = (clientHeight + scrollTop + 1 >= scrollHeight);
|
2023-06-19 17:35:30 +08:00
|
|
|
if (isBottom && this.state.hasMore) {
|
2023-05-13 15:42:54 +08:00
|
|
|
this.loadMore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 17:35:30 +08:00
|
|
|
restoreVersion = (currentItem) => {
|
|
|
|
const { commit_id, path } = currentItem;
|
|
|
|
editUtilities.revertFile(path, commit_id).then(res => {
|
2023-05-13 15:42:54 +08:00
|
|
|
if (res.data.success) {
|
2023-06-19 17:35:30 +08:00
|
|
|
this.setState({isLoading: true});
|
|
|
|
this.refershFileList();
|
2023-05-13 15:42:54 +08:00
|
|
|
}
|
2023-06-19 17:35:30 +08:00
|
|
|
let message = gettext('Successfully restored.');
|
|
|
|
toaster.success(message);
|
2023-05-13 15:42:54 +08:00
|
|
|
}).catch(error => {
|
|
|
|
const errorMessage = Utils.getErrorMsg(error, true);
|
|
|
|
toaster.danger(gettext(errorMessage));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelectHistoryVersion = (historyVersion) => {
|
|
|
|
const { isShowChanges } = this.props;
|
|
|
|
if (!isShowChanges) {
|
|
|
|
this.props.onSelectHistoryVersion(historyVersion);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { historyVersions } = this.state;
|
2023-06-19 17:35:30 +08:00
|
|
|
const historyVersionIndex = historyVersions.findIndex(item => item.commit_id === historyVersion.commit_id);
|
2023-05-13 15:42:54 +08:00
|
|
|
this.props.onSelectHistoryVersion(historyVersion, historyVersions[historyVersionIndex + 1]);
|
|
|
|
}
|
|
|
|
|
2023-06-15 16:27:24 +08:00
|
|
|
copyHistoryFile = (historyVersion) => {
|
2023-06-19 17:35:30 +08:00
|
|
|
const { path, obj_id, ctime_format } = historyVersion;
|
|
|
|
seafileAPI.sdocCopyHistoryFile(historyRepoID, path, obj_id, ctime_format).then(res => {
|
2023-06-15 16:27:24 +08:00
|
|
|
let message = gettext('Successfully copied %(name)s.');
|
|
|
|
let filename = res.data.file_name;
|
|
|
|
message = message.replace('%(name)s', filename);
|
|
|
|
toaster.success(message);
|
|
|
|
}).catch(error => {
|
|
|
|
const errorMessage = Utils.getErrorMsg(error, true);
|
|
|
|
toaster.danger(gettext(errorMessage));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-13 15:42:54 +08:00
|
|
|
renderHistoryVersions = () => {
|
|
|
|
const { isLoading, historyVersions, errorMessage } = this.state;
|
|
|
|
if (historyVersions.length === 0) {
|
|
|
|
if (isLoading) {
|
|
|
|
return (
|
|
|
|
<div className="h-100 w-100 d-flex align-items-center justify-content-center">
|
|
|
|
<Loading />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (errorMessage) {
|
|
|
|
return (
|
|
|
|
<div className="h-100 w-100 d-flex align-items-center justify-content-center error-message">
|
|
|
|
{gettext(errorMessage)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className="h-100 w-100 d-flex align-items-center justify-content-center empty-tip-color">
|
|
|
|
{gettext('No_historical_versions')}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{historyVersions.map((historyVersion, index) => {
|
|
|
|
return (
|
|
|
|
<HistoryVersion
|
2023-06-19 17:35:30 +08:00
|
|
|
key={historyVersion.commit_id}
|
2023-05-13 15:42:54 +08:00
|
|
|
index={index}
|
|
|
|
currentVersion={this.props.currentVersion}
|
|
|
|
historyVersion={historyVersion}
|
|
|
|
onSelectHistoryVersion={this.onSelectHistoryVersion}
|
|
|
|
onRestore={this.restoreVersion}
|
2023-06-15 16:27:24 +08:00
|
|
|
onCopy={this.copyHistoryFile}
|
2023-06-19 17:35:30 +08:00
|
|
|
renameHistoryVersion={this.renameHistoryVersion}
|
2023-05-13 15:42:54 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{isLoading && (
|
|
|
|
<div className="loading-more d-flex align-items-center justify-content-center w-100">
|
|
|
|
<Loading />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onShowChanges = () => {
|
2023-05-18 13:52:49 +08:00
|
|
|
const { isShowChanges, currentVersion } = this.props;
|
|
|
|
const { historyVersions } = this.state;
|
2023-06-19 17:35:30 +08:00
|
|
|
const historyVersionIndex = historyVersions.findIndex(item => item.commit_id === currentVersion.commit_id);
|
2023-05-18 13:52:49 +08:00
|
|
|
const lastVersion = historyVersions[historyVersionIndex + 1];
|
|
|
|
this.props.onShowChanges(!isShowChanges, lastVersion);
|
2023-05-13 15:42:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { historyVersions } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="sdoc-file-history-panel h-100 o-hidden d-flex flex-column">
|
|
|
|
<div className="sdoc-file-history-select-range">
|
|
|
|
<div className="sdoc-file-history-select-range-title">
|
|
|
|
{gettext('History Versions')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classnames('sdoc-file-history-versions', { 'o-hidden': historyVersions.length === 0 } )}
|
|
|
|
onScroll={this.onScrollHandler}
|
|
|
|
>
|
|
|
|
{this.renderHistoryVersions()}
|
|
|
|
</div>
|
|
|
|
<div className="sdoc-file-history-diff-switch d-flex align-items-center">
|
|
|
|
<Switch
|
|
|
|
checked={this.props.isShowChanges}
|
|
|
|
placeholder={gettext('Show changes')}
|
|
|
|
className="sdoc-history-show-changes w-100"
|
|
|
|
size="small"
|
|
|
|
onChange={this.onShowChanges}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SidePanel.propTypes = {
|
|
|
|
isShowChanges: PropTypes.bool,
|
|
|
|
currentVersion: PropTypes.object,
|
|
|
|
onSelectHistoryVersion: PropTypes.func,
|
|
|
|
onShowChanges: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SidePanel;
|