import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { ButtonGroup, ButtonDropdown, Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; import IconButton from '../icon-button'; import { gettext, siteRoot } from '../../utils/constants'; import { Utils } from '../../utils/utils'; import ModalPortal from '../modal-portal'; import ShareDialog from '../dialog/share-dialog'; import { seafileAPI } from '../../utils/seafile-api'; import toaster from '../toast'; const propTypes = { isLocked: PropTypes.bool.isRequired, lockedByMe: PropTypes.bool.isRequired, onSave: PropTypes.func, isSaving: PropTypes.bool, needSave: PropTypes.bool, toggleLockFile: PropTypes.func.isRequired, toggleDetailsPanel: PropTypes.func.isRequired }; const { canLockUnlockFile, repoID, repoName, repoEncrypted, parentDir, filePerm, filePath, fileType, fileName, canEditFile, err, // fileEnc, // for 'edit', not undefined only for some kinds of files (e.g. text file) canDownloadFile, } = window.app.pageOptions; class FileToolbar extends React.Component { constructor(props) { super(props); this.state = { isLoading: true, dropdownOpen: false, moreDropdownOpen: false, isShareDialogOpen: false, }; } async componentDidMount() { if (filePerm && filePerm.startsWith('custom-')) { this.isCustomPermission = true; const permissionID = filePerm.split('-')[1]; try { const permissionRes = await seafileAPI.getCustomPermission(repoID, permissionID); this.customPermission = permissionRes.data.permission; // share dialog need a global custom_permission window.custom_permission = this.customPermission; this.setState({ isLoading: false }); } catch (error) { let errorMsg = Utils.getErrorMsg(error); toaster.danger(errorMsg); this.setState({ isLoading: false }); } } else { this.setState({ isLoading: false }); } } toggleShareDialog = () => { this.setState({ isShareDialogOpen: !this.state.isShareDialogOpen }); }; toggleMoreOpMenu = () => { this.setState({ moreDropdownOpen: !this.state.moreDropdownOpen }); }; toggle = () => { this.setState({ dropdownOpen: !this.state.dropdownOpen }); }; render() { if (this.state.isLoading) { return null; } const { isLocked, lockedByMe } = this.props; const { moreDropdownOpen } = this.state; let showLockUnlockBtn = false; let lockUnlockText; let lockUnlockIcon; if (canLockUnlockFile) { if (!isLocked) { showLockUnlockBtn = true; lockUnlockText = gettext('Lock'); lockUnlockIcon = 'sf3-font sf3-font-lock'; } else if (lockedByMe) { showLockUnlockBtn = true; lockUnlockText = gettext('Unlock'); lockUnlockIcon = 'sf3-font sf3-font-unlock'; } } let showShareBtn = false; if (repoEncrypted) { showShareBtn = true; // for internal link } else if (filePerm == 'rw' || filePerm == 'r') { showShareBtn = true; } const { isCustomPermission, customPermission } = this; if (isCustomPermission) { const { download_external_link } = customPermission.permission; showShareBtn = download_external_link; } return ( {fileType == 'PDF' && ( )} {showLockUnlockBtn && ( )} {showShareBtn && ( )} {(canEditFile && fileType != 'SDoc' && !err) && (this.props.isSaving ? : (this.props.needSave ? : ))} {canDownloadFile && ( )} {filePerm == 'rw' && ( )} {filePerm == 'rw' && ( {gettext('History')} )} {(canEditFile && fileType != 'SDoc' && !err) && (this.props.isSaving ? : (this.props.needSave ? : ))} {gettext('Open parent folder')} {showLockUnlockBtn && ( {lockUnlockText} )} {showShareBtn && ( {gettext('Share')} )} {filePerm == 'rw' && ( {gettext('History')} )} {canDownloadFile && ( {gettext('Download')} )} {gettext('Details')} {this.state.isShareDialogOpen && ( )} ); } } FileToolbar.propTypes = propTypes; export default FileToolbar;