import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import dayjs from 'dayjs'; import { Utils } from '../../utils/utils'; import { gettext, siteRoot, filePath, historyRepoID } from '../../utils/constants'; import URLDecorator from '../../utils/url-decorator'; import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; dayjs.locale(window.app.config.lang); const propTypes = { item: PropTypes.object.isRequired, index: PropTypes.number.isRequired, canDownload: PropTypes.bool.isRequired, canCompare: PropTypes.bool.isRequired, onItemRestore: PropTypes.func.isRequired, snapshotURL: PropTypes.string.isRequired, }; class HistoryItem extends React.Component { constructor(props) { super(props); this.state = { active: false, }; } onMouseEnter = () => { this.setState({ active: true }); }; onMouseLeave = () => { this.setState({ active: false }); }; onItemRestore = (e) => { e.preventDefault(); this.props.onItemRestore(this.props.item); }; render() { let item = this.props.item; let downloadUrl = URLDecorator.getUrl({ type: 'download_historic_file', filePath: filePath, objID: item.rev_file_id }); let userProfileURL = `${siteRoot}profile/${encodeURIComponent(item.creator_email)}/`; let viewUrl = `${siteRoot}repo/${historyRepoID}/history/files/?obj_id=${item.rev_file_id}&commit_id=${item.commit_id}&p=${Utils.encodePath(filePath)}`; let diffUrl = `${siteRoot}repo/text_diff/${historyRepoID}/?commit=${item.commit_id}&p=${Utils.encodePath(filePath)}`; const snapshotURL = `${siteRoot}repo/${historyRepoID}/snapshot/?commit_id=${item.commit_id}`; return ( {dayjs(item.ctime).format('YYYY-MM-DD HH:mm:ss')} {this.props.index === 0 && {gettext('(current version)')}} {' '} {item.creator_name} {Utils.bytesToSize(item.size)} {this.state.active && } ); } } HistoryItem.propTypes = propTypes; const MoreMenuPropTypes = { index: PropTypes.number.isRequired, downloadUrl: PropTypes.string.isRequired, viewUrl: PropTypes.string.isRequired, diffUrl: PropTypes.string.isRequired, onItemRestore: PropTypes.func.isRequired, canDownload: PropTypes.bool.isRequired, canCompare: PropTypes.bool.isRequired, snapshotURL: PropTypes.string.isRequired, }; class MoreMenu extends React.PureComponent { constructor(props) { super(props); this.state = { dropdownOpen: false }; } dropdownToggle = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen }); }; render() { const { index, downloadUrl, viewUrl, snapshotURL, onItemRestore, canDownload } = this.props; return ( {index !== 0 && {gettext('Restore')}} {canDownload && {gettext('Download')}} {gettext('View')} {/* canCompare && {gettext('Diff')}*/} {index != 0 && {gettext('View Related Snapshot')}} ); } } MoreMenu.propTypes = MoreMenuPropTypes; export default HistoryItem;