import React, { Component, Fragment } from 'react'; import moment from 'moment'; import { Link } from '@reach/router'; import { Dropdown, DropdownMenu, DropdownToggle, DropdownItem } from 'reactstrap'; import { seafileAPI } from '../../utils/seafile-api'; import { Utils } from '../../utils/utils'; import { gettext, siteRoot, loginUrl, isPro, storages, canGenerateShareLink, canGenerateUploadLink, folderPermEnabled, enableRepoSnapshotLabel } from '../../utils/constants'; import Loading from '../../components/loading'; import DeleteItemPopup from './popups/delete-item'; import CommonToolbar from '../../components/toolbar/common-toolbar'; import RepoViewToolbar from '../../components/toolbar/repo-view-toobar'; class Content extends Component { constructor(props) { super(props); this.state = { deleteItemPopupOpen: false }; this.toggleDeleteItemPopup = this.toggleDeleteItemPopup.bind(this); this.showDeleteItemPopup = this.showDeleteItemPopup.bind(this); this.operations = { showDeleteItemPopup: this.showDeleteItemPopup }; } toggleDeleteItemPopup() { this.setState({ deleteItemPopupOpen: !this.state.deleteItemPopupOpen }); } showDeleteItemPopup(data) { this.toggleDeleteItemPopup(); this.setState({ deleteItemPopupData: data }); } render() { const {loading, errorMsg, items} = this.props.data; if (loading) { return ; } else if (errorMsg) { return

{errorMsg}

; } else { const emptyTip = (

{gettext('You have not created any libraries')}

{gettext('You can create a library to organize your files. For example, you can create one for each of your projects. Each library can be synchronized and shared separately.')}

); // TODO: test 'storage backend' const showStorageBackend = storages.length > 0; // only for desktop const desktopThead = ( {gettext("Library Type")} {gettext("Name")}{/*TODO: sort*/} {gettext("Actions")} {gettext("Size")} {showStorageBackend ? {gettext('Storage backend')} : null} {gettext("Last Update")}{/*TODO: sort*/} ); const mobileThead = ( {gettext("Library Type")} {gettext("Sort:")} {/* TODO: sort */} {gettext("name")} {gettext("last update")} {gettext("Actions")} ); const table = ( {window.innerWidth >= 768 ? desktopThead : mobileThead}
); const nonEmpty = ( {table} ); return items.length ? nonEmpty : emptyTip; } } } class TableBody extends Component { constructor(props) { super(props); this.state = { items: this.props.items }; } render() { let listItems = this.state.items.map(function(item, index) { return ; }, this); return ( {listItems} ); } } class Item extends Component { constructor(props) { super(props); this.state = { showOpIcon: false, operationMenuOpen: false, deleted: false }; this.handleMouseOver = this.handleMouseOver.bind(this); this.handleMouseOut = this.handleMouseOut.bind(this); this.toggleOperationMenu = this.toggleOperationMenu.bind(this); this.clickOperationMenuToggle = this.clickOperationMenuToggle.bind(this); this.share = this.share.bind(this); this.showDeleteItemPopup = this.showDeleteItemPopup.bind(this); this.deleteItem = this.deleteItem.bind(this); this.rename = this.rename.bind(this); this.transfer = this.transfer.bind(this); this.historySetting = this.historySetting.bind(this); this.changePassword = this.changePassword.bind(this); this.showLinks = this.showLinks.bind(this); this.folderPerm = this.folderPerm.bind(this); this.showDetails = this.showDetails.bind(this); this.label = this.label.bind(this); } handleMouseOver() { if (this.state.operationMenuOpen) { return; } this.setState({ showOpIcon: true }); } handleMouseOut() { if (this.state.operationMenuOpen) { return; } this.setState({ showOpIcon: false }); } toggleOperationMenu() { this.setState({ operationMenuOpen: !this.state.operationMenuOpen }); } clickOperationMenuToggle(e) { e.preventDefault(); this.toggleOperationMenu(); } share(e) { e.preventDefault(); // TODO } showDeleteItemPopup(e) { e.preventDefault(); // for `` const data = this.props.data; this.props.operations.showDeleteItemPopup({ repoName: data.repo_name, yesCallback: this.deleteItem, _this: this }); } deleteItem() { const data = this.props.data; seafileAPI.deleteRepo(data.repo_id).then((res) => { this.setState({ deleted: true }); // TODO: show feedback msg }).catch((error) => { // TODO: show feedback msg }); } rename() { } transfer() { } historySetting() { } changePassword() { } showLinks() { } folderPerm() { } showDetails() { } label() { } render() { if (this.state.deleted) { return null; } const data = this.props.data; const permission = data.permission; let is_readonly = false; if (permission == 'r' || permission == 'preview') { is_readonly = true; } data.icon_url = Utils.getLibIconUrl({ is_encrypted: data.encrypted, is_readonly: is_readonly, size: Utils.isHiDPI() ? 48 : 24 }); data.icon_title = Utils.getLibIconTitle({ 'encrypted': data.encrypted, 'is_admin': data.is_admin, 'permission': permission }); data.url = `${siteRoot}#my-libs/lib/${data.repo_id}/`; let iconVisibility = this.state.showOpIcon ? '' : ' invisible'; let shareIconClassName = 'sf2-icon-share sf2-x op-icon' + iconVisibility; let deleteIconClassName = 'sf2-icon-delete sf2-x op-icon' + iconVisibility; let operationMenuToggleIconClassName = 'sf2-icon-caret-down item-operation-menu-toggle-icon op-icon'; if (window.innerWidth >= 768) { operationMenuToggleIconClassName += iconVisibility; } const showShareLinks = !data.encrypted && (canGenerateShareLink || canGenerateUploadLink); const commonToggle = ( ); const commonOperationsInMenu = ( {gettext('Rename')} {gettext('Transfer')} {gettext('History Setting')} {data.encrypted ? {gettext('Change Password')} : ''} {showShareLinks ? {gettext('Share Links')} : ''} {folderPermEnabled ? {gettext('Folder Permission')} : ''} {gettext('Details')} ); const desktopOperations = (
{commonToggle} {commonOperationsInMenu} {enableRepoSnapshotLabel ? {gettext('Label current state')} : ''}
); const mobileOperations = ( {commonToggle}
{gettext('Share')} {gettext('Delete')} {commonOperationsInMenu}
); const desktopItem = ( {data.icon_title} {data.repo_name ? {data.repo_name} : gettext('Broken (please contact your administrator to fix this library)')} {data.repo_name ? desktopOperations : ''} {Utils.formatSize({bytes: data.size})} {storages.length ? {data.storage_name} : null} {moment(data.last_modified).fromNow()} ); const mobileItem = ( {data.icon_title} {data.repo_name ? {data.repo_name} : gettext('Broken (please contact your administrator to fix this library)')}
{Utils.formatSize({bytes: data.size})} {moment(data.last_modified).fromNow()} {data.repo_name ? mobileOperations : ''} ); return window.innerWidth >= 768 ? desktopItem : mobileItem; } } class MyLibraries extends Component { constructor(props) { super(props); this.state = { loading: true, errorMsg: '', items: [] }; } componentDidMount() { seafileAPI.listRepos({type:'mine'}).then((res) => { // res: {data: {...}, status: 200, statusText: "OK", headers: {…}, config: {…}, …} this.setState({ loading: false, items: res.data.repos }); }).catch((error) => { if (error.response) { if (error.response.status == 403) { this.setState({ loading: false, errorMsg: gettext("Permission denied") }); location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`; } else { this.setState({ loading: false, errorMsg: gettext("Error") }); } } else { this.setState({ loading: false, errorMsg: gettext("Please check the network.") }); } }); } onCreateRepo = (repo) => { seafileAPI.createMineRepo(repo).then((res) => { //todo update repoList }); } render() { return (

{gettext("My Libraries")}

); } } export default MyLibraries;