import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { Modal, ModalHeader, ModalBody, TabContent, TabPane, Nav, NavItem, NavLink } from 'reactstrap'; import { gettext, username, canGenerateShareLink, canGenerateUploadLink, canInvitePeople, additionalShareDialogNote, enableOCM, isPro, canShareRepo } from '../../utils/constants'; import ShareLinkPanel from '../share-link-panel'; import GenerateUploadLink from './generate-upload-link'; import ShareToUser from './share-to-user'; import ShareToGroup from './share-to-group'; import ShareToInvitePeople from './share-to-invite-people'; import ShareToOtherServer from './share-to-other-server'; import InternalLink from './internal-link'; import { seafileAPI } from '../../utils/seafile-api'; import { Utils } from '../../utils/utils'; import Loading from '../loading'; import toaster from '../toast'; import CustomPermissionManager from './custom-permission/custom-permission-manager'; import '../../css/share-link-dialog.css'; const propTypes = { isGroupOwnedRepo: PropTypes.bool, itemType: PropTypes.string.isRequired, // there will be three choose: ['library', 'dir', 'file'] itemName: PropTypes.string.isRequired, itemPath: PropTypes.string.isRequired, toggleDialog: PropTypes.func.isRequired, repoID: PropTypes.string.isRequired, repoEncrypted: PropTypes.bool, userPerm: PropTypes.string, enableDirPrivateShare: PropTypes.bool, }; class ShareDialog extends React.Component { constructor(props) { super(props); this.state = { activeTab: this.getInitialActiveTab(), isRepoJudgemented: false, isRepoOwner: false, }; } componentDidMount() { let repoID = this.props.repoID; seafileAPI.getRepoInfo(repoID).then(res => { let isRepoOwner = res.data.owner_email === username; this.setState({ isRepoJudgemented: true, isRepoOwner: isRepoOwner, }); }).catch(error => { let errMessage = Utils.getErrorMsg(error); toaster.danger(errMessage); }); } getInitialActiveTab = () => { let { repoEncrypted, userPerm, enableDirPrivateShare, itemType } = this.props; const enableShareLink = !repoEncrypted && canGenerateShareLink; const enableUploadLink = !repoEncrypted && canGenerateUploadLink && (userPerm == 'rw' || userPerm == 'admin'); // for encrypted repo, 'dir private share' is only enabled for the repo itself, // not for the folders in it. if (repoEncrypted) { enableDirPrivateShare = itemType == 'library'; } if (enableShareLink) { return 'shareLink'; } else if (enableUploadLink) { return 'uploadLink'; } else if (itemType == 'file' || itemType == 'dir') { return 'internalLink'; } else if (enableDirPrivateShare) { return 'shareToUser'; } }; toggle = (tab) => { if (this.state.activeTab !== tab) { this.setState({ activeTab: tab }); } }; onAddCustomPermissionToggle = () => { this.toggle('customSharePermission'); }; renderDirContent = () => { if (!this.state.isRepoJudgemented) { return ; } let activeTab = this.state.activeTab; let { repoEncrypted, userPerm, enableDirPrivateShare, itemType } = this.props; const enableShareLink = !repoEncrypted && canGenerateShareLink; const enableUploadLink = !repoEncrypted && canGenerateUploadLink && (userPerm == 'rw' || userPerm == 'admin'); // for encrypted repo, 'dir private share' is only enabled for the repo itself, // not for the folders in it. if (repoEncrypted) { enableDirPrivateShare = itemType == 'library'; } const { isCustomPermission } = Utils.getUserPermission(userPerm); return (
{(enableShareLink && activeTab === 'shareLink') && } {(enableUploadLink && activeTab === 'uploadLink') && } {(itemType === 'dir' && activeTab === 'internalLink') && } {enableDirPrivateShare && {activeTab === 'shareToUser' && } {activeTab === 'shareToGroup' && } {isPro && activeTab === 'customSharePermission' && ( )} {(canInvitePeople && activeTab === 'invitePeople') && } } {enableOCM && itemType === 'library' && activeTab === 'shareToOtherServer' && }
); }; onTabKeyDown = (e) => { if (e.key == 'Enter' || e.key == 'Space') { e.target.click(); } }; renderFileContent = () => { let activeTab = this.state.activeTab; const { itemType, repoEncrypted, userPerm } = this.props; const enableShareLink = !repoEncrypted && canGenerateShareLink; return (
{enableShareLink && activeTab === 'shareLink' && } {activeTab === 'internalLink' && }
); }; renderExternalShareMessage = () => { if (additionalShareDialogNote && (typeof additionalShareDialogNote) === 'object') { return (
{additionalShareDialogNote.title}

{additionalShareDialogNote.content}

); } return null; }; render() { const { itemType, itemName } = this.props; return (
{gettext('Share')} {itemName}
{this.renderExternalShareMessage()}
{(itemType === 'library' || itemType === 'dir') && this.renderDirContent()} {itemType === 'file' && this.renderFileContent()}
); } } ShareDialog.propTypes = propTypes; export default ShareDialog;