import React from 'react'; import PropTypes from 'prop-types'; import dayjs from 'dayjs'; import { Modal, ModalBody } from 'reactstrap'; import { Utils } from '../../utils/utils'; import { gettext, siteRoot } from '../../utils/constants'; import { fileAccessLogAPI } from '../../utils/file-access-log-api'; import toaster from '../toast'; import Loading from '../loading'; import EmptyTip from '../empty-tip'; import SeahubModalHeader from '@/components/common/seahub-modal-header'; import '../../css/file-access-log.css'; dayjs.locale(window.app.config.lang); const propTypes = { repoID: PropTypes.string.isRequired, filePath: PropTypes.string.isRequired, fileName: PropTypes.string.isRequired, toggleDialog: PropTypes.func.isRequired }; class FileAccessLog extends React.Component { constructor(props) { super(props); this.state = { isLoading: true, // first loading isLoadingMore: false, items: [], page: 1, perPage: 100, hasNextPage: false }; } componentDidMount() { this.listFileAccessLog(this.state.page); } listFileAccessLog = (page) => { const { repoID, filePath } = this.props; const { perPage, items } = this.state; fileAccessLogAPI.listFileAccessLog(repoID, filePath, page, perPage).then((res) => { const { data: newItems } = res.data; this.setState({ isLoading: false, isLoadingMore: false, page: page, hasNextPage: newItems.length < perPage ? false : true, items: items.concat(newItems) }); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); this.setState({ isLoading: false, isLoadingMore: false, hasNextPage: false }); }); }; handleScroll = (event) => { // isLoadingMore: to avoid repeated request const { page, hasNextPage, isLoadingMore } = this.state; if (hasNextPage && !isLoadingMore) { const clientHeight = event.target.clientHeight; const scrollHeight = event.target.scrollHeight; const scrollTop = event.target.scrollTop; const isBottom = (clientHeight + scrollTop + 1 >= scrollHeight); if (isBottom) { // scroll to the bottom this.setState({ isLoadingMore: true }, () => { this.listFileAccessLog(page + 1); }); } } }; render() { const { isLoading, hasNextPage, items } = this.state; const { fileName } = this.props; let title = gettext('{placeholder} Access Log'); title = title.replace('{placeholder}', '' + Utils.HTMLescape(fileName) + ''); return ( {isLoading ? : ( <> {items.length > 0 ? ( <> {items.map((item, index) => { return ( ); })}
{gettext('User')} {gettext('Type')} {gettext('IP')} / {gettext('Device Name')} {gettext('Date')}
{item.email ? {item.name} : {gettext('Anonymous User')}} {item.etype} {`${item.ip}${item.device ? '/' + item.device : ''}`} {dayjs(item.time).format('YYYY-MM-DD HH:mm:ss')}
{hasNextPage && } ) : ( )} )}
); } } FileAccessLog.propTypes = propTypes; export default FileAccessLog;