import React from 'react';
import PropTypes from 'prop-types';
import { Link } from '@gatsbyjs/reach-router';
import { gettext, siteRoot, canAddRepo, canGenerateShareLink, canGenerateUploadLink, canInvitePeople } from '../utils/constants';
import { seafileAPI } from '../utils/seafile-api';
import { Utils } from '../utils/utils';
import toaster from './toast';
import Group from '../models/group';
import { canViewOrg, isDocs, isPro, isDBSqlite3, customNavItems } from '../utils/constants';
const propTypes = {
currentTab: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
tabItemClick: PropTypes.func.isRequired,
};
class MainSideNav extends React.Component {
constructor(props) {
super(props);
this.state = {
FilesNavUnfolded: false,
sharedExtended: false,
closeSideBar:false,
groupItems: [],
};
this.listHeight = 24; //for caculate tabheight
this.adminHeight = 0;
}
shExtend = () => {
this.setState({
sharedExtended: !this.state.sharedExtended,
});
};
loadGroups = () => {
let _this = this;
seafileAPI.listGroups().then(res =>{
let groupList = res.data.map(item => {
let group = new Group(item);
return group;
});
_this.setState({
groupItems: groupList.sort((a, b) => {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
})
});
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
};
tabItemClick = (e, param, id) => {
if (window.uploader &&
window.uploader.isUploadProgressDialogShow &&
window.uploader.totalProgress !== 100) {
if (!window.confirm(gettext('A file is being uploaded. Are you sure you want to leave this page?'))) {
e.preventDefault();
return false;
}
window.uploader.isUploadProgressDialogShow = false;
}
this.props.tabItemClick(param, id);
};
getActiveClass = (tab) => {
return this.props.currentTab === tab ? 'active' : '';
};
renderSharedGroups() {
return (
{this.state.groupItems.map(item => {
return (
-
this.tabItemClick(e, item.name, item.id)}>
{item.name}
);
})}
);
}
renderSharedAdmin() {
let height = 0;
if (this.state.sharedExtended) {
if (!this.adminHeight) {
this.adminHeight = 3 * this.listHeight;
}
height = this.adminHeight;
}
let style = {height: height};
let linksNavItem = null;
if (canGenerateShareLink) {
linksNavItem = (
this.tabItemClick(e, 'share-admin-share-links')}>
#
{gettext('Links')}
);
} else if (canGenerateUploadLink) {
linksNavItem = (
this.tabItemClick(e, 'share-admin-upload-links')}>
#
{gettext('Links')}
);
}
return (
{canAddRepo && (
-
this.tabItemClick(e, 'share-admin-libs')}>
#
{gettext('Libraries')}
)}
-
this.tabItemClick(e, 'share-admin-folders')}>
#
{gettext('Folders')}
{linksNavItem}
);
}
renderCustomNavItems() {
return (
customNavItems.map((item, idx) => {
return (
{item.desc}
);
})
);
}
toggleFilesNav = (e) => {
e.preventDefault();
e.stopPropagation();
this.setState({
FilesNavUnfolded: !this.state.FilesNavUnfolded
}, () => {
if (this.state.FilesNavUnfolded) {
this.loadGroups();
}
});
};
render() {
let showActivity = isDocs || isPro || !isDBSqlite3;
const { FilesNavUnfolded } = this.state;
return (
-
this.tabItemClick(e, 'libraries')}>
{gettext('Files')}
{canAddRepo && (
-
this.tabItemClick(e, 'my-libs')}>
{gettext('My Libraries')}
)}
-
this.tabItemClick(e, 'shared-libs')}>
{gettext('Shared with me')}
{canViewOrg &&
- this.tabItemClick(e, 'org')}>
{gettext('Shared with all')}
}
-
{this.renderSharedGroups()}
-
this.tabItemClick(e, 'starred')}>
{gettext('Favorites')}
{showActivity &&
-
this.tabItemClick(e, 'dashboard')}>
{gettext('Activities')}
}
-
this.tabItemClick(e, 'published')}>
{gettext('Wikis')}
-
this.tabItemClick(e, 'linked-devices')}>
{gettext('Linked Devices')}
{canInvitePeople &&
-
this.tabItemClick(e, 'invitations')}>
{gettext('Invite Guest')}
}
-
{gettext('Share Admin')}
{this.renderSharedAdmin()}
{customNavItems && this.renderCustomNavItems()}
);
}
}
MainSideNav.propTypes = propTypes;
export default MainSideNav;