import React, { Fragment } from 'react'; import { createRoot } from 'react-dom/client'; import { Button } from 'reactstrap'; import { Utils } from './utils/utils'; import { seafileAPI } from './utils/seafile-api'; import { gettext, PER_PAGE, filePath, fileName, historyRepoID, useNewAPI, canDownload, canCompare } from './utils/constants'; import editUtilities from './utils/editor-utilities'; import Loading from './components/loading'; import Logo from './components/logo'; import CommonToolbar from './components/toolbar/common-toolbar'; import HistoryItem from './pages/file-history-old/history-item'; import './css/layout.css'; import './css/toolbar.css'; import './css/search.css'; import './css/file-history-old.css'; class FileHistory extends React.Component { constructor(props) { super(props); this.state = { historyList: [], currentPage: 1, hasMore: false, nextCommit: undefined, filePath: '', oldFilePath: '', isLoading: true, isReloadingData: false, }; } componentDidMount() { if (useNewAPI) { this.listNewHistoryRecords(filePath, PER_PAGE); } else { this.listOldHistoryRecords(historyRepoID, filePath); } } listNewHistoryRecords = (filePath, PER_PAGE) => { editUtilities.listFileHistoryRecords(filePath, 1, PER_PAGE).then(res => { let historyData = res.data; if (!historyData) { this.setState({isLoading: false}); throw Error('There is an error in server.'); } this.initNewRecords(res.data); }); } listOldHistoryRecords = (repoID, filePath) => { seafileAPI.listOldFileHistoryRecords(repoID, filePath).then((res) => { let historyData = res.data; if (!historyData) { this.setState({isLoading: false}); throw Error('There is an error in server.'); } this.initOldRecords(res.data); }); } initNewRecords(result) { if (result.total_count < 5) { if (result.data.length) { let commitID = result.data[result.data.length-1].commit_id; let path = result.data[result.data.length-1].path; let oldPath = result.data[result.data.length-1].old_path; path = oldPath ? oldPath : path; seafileAPI.listOldFileHistoryRecords(historyRepoID, path, commitID).then((res) => { if (!res.data) { this.setState({isLoading: false}); throw Error('There is an error in server.'); } this.setState({ historyList: result.data.concat(res.data.data.slice(1, res.data.data.length)), isLoading: false, }); }); } else { seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath).then((res) => { if (!res.data) { this.setState({isLoading: false}); throw Error('There is an error in server.'); } this.setState({ historyList: res.data.data, isLoading: false, }); }); } } else { this.setState({ historyList: result.data, currentPage: result.page, hasMore: result.total_count > (PER_PAGE * this.state.currentPage), isLoading: false, }); } } initOldRecords(result) { if (result.data.length) { this.setState({ historyList: result.data, nextCommit: result.next_start_commit, filePath: result.data[result.data.length-1].path, oldFilePath: result.data[result.data.length-1].rev_renamed_old_path, isLoading: false, }); } else { this.setState({nextCommit: result.next_start_commit,}); if (this.state.nextCommit) { seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath, this.state.nextCommit).then((res) => { this.initOldRecords(res.data); }); } else { this.setState({isLoading: false}); } } } onScrollHandler = (event) => { const clientHeight = event.target.clientHeight; const scrollHeight = event.target.scrollHeight; const scrollTop = event.target.scrollTop; const isBottom = (clientHeight + scrollTop + 1 >= scrollHeight); let hasMore = this.state.hasMore; if (isBottom && hasMore) { this.reloadMore(); } } reloadMore = () => { if (!this.state.isReloadingData) { if (useNewAPI) { let currentPage = this.state.currentPage + 1; this.setState({ currentPage: currentPage, isReloadingData: true, }); editUtilities.listFileHistoryRecords(filePath, currentPage, PER_PAGE).then(res => { this.updateNewRecords(res.data); }); } else { let commitID = this.state.nextCommit; let filePath = this.state.filePath; let oldFilePath = this.state.oldFilePath; this.setState({isReloadingData: true}); if (oldFilePath) { seafileAPI.listOldFileHistoryRecords(historyRepoID, oldFilePath, commitID).then((res) => { this.updateOldRecords(res.data, oldFilePath); }); } else { seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath, commitID).then((res) => { this.updateOldRecords(res.data, filePath); }); } } } } updateNewRecords(result) { this.setState({ historyList: [...this.state.historyList, ...result.data], currentPage: result.page, hasMore: result.total_count > (PER_PAGE * this.state.currentPage), isReloadingData: false, }); } updateOldRecords(result, filePath) { if (result.data.length) { this.setState({ historyList: [...this.state.historyList, ...result.data], nextCommit: result.next_start_commit, filePath: result.data[result.data.length-1].path, oldFilePath: result.data[result.data.length-1].rev_renamed_old_path, isReloadingData: false, }); } else { this.setState({nextCommit: result.next_start_commit,}); if (this.state.nextCommit) { seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath, this.state.nextCommit).then((res) => { this.updateOldRecords(res.data, filePath); }); } } } onItemRestore = (item) => { let commitId = item.commit_id; let filePath = item.path; editUtilities.revertFile(filePath, commitId).then(res => { if (res.data.success) { this.setState({isLoading: true}); this.refershFileList(); } }); } refershFileList() { if (useNewAPI) { editUtilities.listFileHistoryRecords(filePath, 1, PER_PAGE).then((res) => { this.initNewRecords(res.data); }); } else { seafileAPI.listOldFileHistoryRecords(historyRepoID, filePath).then((res) => { this.initOldRecords(res.data); }); } } onSearchedClick = (searchedItem) => { Utils.handleSearchedItemClick(searchedItem); } render() { return (

{fileName}{' '}{gettext('History Versions')}

{!this.state.isLoading && {this.state.historyList.map((item, index) => { return ( ); })} }
{gettext('Time')} {gettext('Modifier')} {gettext('Size')}
{(this.state.isReloadingData || this.state.isLoading) && } {this.state.nextCommit && !this.state.isLoading && !this.state.isReloadingData && }
); } } const root = createRoot(document.getElementById('wrapper')); root.render();