import React from 'react'; import PropTypes from 'prop-types'; import { navigate } from '@gatsbyjs/reach-router'; import { Modal, ModalBody, ModalHeader } from 'reactstrap'; import { Utils } from '../../../utils/utils'; import { gettext, siteRoot, enableUserCleanTrash, username } from '../../../utils/constants'; import { seafileAPI } from '../../../utils/seafile-api'; import { repoTrashAPI } from '../../../utils/repo-trash-api'; import ModalPortal from '../../modal-portal'; import Table from './table'; import CleanTrash from '../clean-trash'; import Paginator from '../../paginator'; import Loading from '../../loading'; import EmptyTip from '../../empty-tip'; import '../../../css/toolbar.css'; import '../../../css/search.css'; import './index.css'; class TrashDialog extends React.Component { constructor(props) { super(props); this.state = { isLoading: true, errorMsg: '', items: [], scanStat: null, more: false, isCleanTrashDialogOpen: false, trashType: 0, isOldTrashDialogOpen: false, currentPage: 1, perPage: 100, hasNextPage: false }; } componentDidMount() { this.getFolderTrash(); } getFolderTrash = (page) => { repoTrashAPI.getRepoFolderTrash(this.props.repoID, page, this.state.perPage).then((res) => { const { items, total_count } = res.data; if (!page) { page = 1; } this.setState({ currentPage: page, hasNextPage: total_count - page * this.state.perPage > 0, isLoading: false, items: items, more: false }); }); }; onSearchedClick = (selectedItem) => { if (selectedItem.is_dir === true) { let url = siteRoot + 'library/' + selectedItem.repo_id + '/' + selectedItem.repo_name + selectedItem.path; navigate(url, { replace: true }); } else { let url = siteRoot + 'lib/' + selectedItem.repo_id + '/file' + Utils.encodePath(selectedItem.path); let newWindow = window.open('about:blank'); newWindow.location.href = url; } }; getPreviousPage = () => { this.getFolderTrash(this.state.currentPage - 1); }; getNextPage = () => { this.getFolderTrash(this.state.currentPage + 1); }; resetPerPage = (perPage) => { this.setState({ perPage: perPage }, () => { this.getFolderTrash(1); }); }; cleanTrash = () => { this.toggleCleanTrashDialog(); }; toggleCleanTrashDialog = () => { this.setState({ isCleanTrashDialogOpen: !this.state.isCleanTrashDialogOpen }); }; refreshTrash2 = () => { this.setState({ isLoading: true, errorMsg: '', items: [], scanStat: null, more: false, showFolder: false }); this.getFolderTrash(); }; renderFolder = (commitID, baseDir, folderPath) => { this.setState({ showFolder: true, commitID: commitID, baseDir: baseDir, folderPath: folderPath, folderItems: [], isLoading: true }); seafileAPI.listCommitDir(this.props.repoID, commitID, `${baseDir.substr(0, baseDir.length - 1)}${folderPath}`).then((res) => { this.setState({ isLoading: false, folderItems: res.data.dirent_list }); }).catch((error) => { if (error.response) { if (error.response.status == 403) { this.setState({ isLoading: false, errorMsg: gettext('Permission denied') }); } else { this.setState({ isLoading: false, errorMsg: gettext('Error') }); } } else { this.setState({ isLoading: false, errorMsg: gettext('Please check the network.') }); } }); }; clickFolderPath = (folderPath, e) => { e.preventDefault(); const { commitID, baseDir } = this.state; this.renderFolder(commitID, baseDir, folderPath); }; clickRoot = (e) => { e.preventDefault(); this.refreshTrash2(); }; renderFolderPath = () => { const pathList = this.state.folderPath.split('/'); const repoFolderName = this.props.currentRepoInfo.repo_name; return ( {repoFolderName} / {pathList.map((item, index) => { if (index > 0 && index != pathList.length - 1) { return ( {pathList[index]} / ); } return null; } )} {pathList[pathList.length - 1]} ); }; render() { const { showTrashDialog, toggleTrashDialog, repoID } = this.props; const { isCleanTrashDialogOpen, showFolder, isLoading, items, perPage, currentPage, hasNextPage } = this.state; const isRepoAdmin = this.props.currentRepoInfo.owner_email === username || this.props.currentRepoInfo.is_admin; const repoFolderName = this.props.currentRepoInfo.repo_name; const oldTrashUrl = siteRoot + 'repo/' + this.props.repoID + '/trash/'; let title = gettext('{placeholder} Trash'); title = title.replace('{placeholder}', '' + Utils.HTMLescape(repoFolderName) + ''); return ( <> {gettext('Visit old version page')} {(enableUserCleanTrash && !showFolder && isRepoAdmin) && } } >
{isLoading && } {!isLoading && items.length === 0 && } {!isLoading && items.length > 0 &&
{gettext('Current path: ')} {showFolder ? this.renderFolderPath() : {repoFolderName} }
} {isCleanTrashDialogOpen && ( )} ); } } TrashDialog.propTypes = { repoID: PropTypes.string.isRequired, currentRepoInfo: PropTypes.object.isRequired, showTrashDialog: PropTypes.bool.isRequired, toggleTrashDialog: PropTypes.func.isRequired }; export default TrashDialog;