import React from 'react';
import PropTypes from 'prop-types';
import { Link } from '@reach/router';
import { Badge } from 'reactstrap';
import { gettext, siteRoot, canPublishRepo, canAddRepo, canGenerateShareLink, canGenerateUploadLink, canInvitePeople, dtableWebServer, lang } 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, customNavItems } from '../utils/constants';
const propTypes = {
currentTab: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
tabItemClick: PropTypes.func.isRequired,
draftCounts: PropTypes.number,
};
class MainSideNav extends React.Component {
constructor(props) {
super(props);
this.state = {
groupsExtended: false,
sharedExtended: false,
closeSideBar:false,
groupItems: [],
};
this.listHeight = 24; //for caculate tabheight
this.groupsHeight = 0;
this.adminHeight = 0;
}
grpsExtend = () => {
this.setState({
groupsExtended: !this.state.groupsExtended,
});
this.loadGroups();
}
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.groupsHeight = (groupList.length + 1) * _this.listHeight;
_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);
}
onDTableClick = () => {
let url = dtableWebServer;
window.open(url);
}
getActiveClass = (tab) => {
return this.props.currentTab === tab ? 'active' : '';
}
renderSharedGroups() {
let style = {height: 0};
if (this.state.groupsExtended) {
style = {height: this.groupsHeight};
}
return (
this.tabItemClick(e, 'groups')}>
#
{gettext('All Groups')}
{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) => {
// if no item.lang, show
// if has item.lang && system.lang in item.lang, show
// other case hide
if (item.hasOwnProperty('lang') && item.lang.indexOf(lang) == -1) return null;
if (item.type === 'heading') {
return (
{item.desc}
);
} else {
return (
{
item.map((nav, idx) => {
if (nav.hasOwnProperty('lang') && nav.lang.indexOf(lang) == -1) return null;
return (
{nav.desc}
);
})
}
);
}
})
);
}
render() {
let showActivity = isDocs || isPro;
return (
{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')}
}
{gettext('Shared with groups')}
{this.renderSharedGroups()}
{gettext('Tools')}
this.tabItemClick(e, 'starred')}>
{gettext('Favorites')}
{showActivity &&
this.tabItemClick(e, 'dashboard')}>
{gettext('Activities')}
}
{canPublishRepo &&
this.tabItemClick(e, 'published')}>
{gettext('Published Libraries')}
}
{isDocs &&
this.tabItemClick(e, 'drafts')}>
{gettext('Drafts')}
{this.props.draftCounts === 0 ? '' : {this.props.draftCounts} }
}
this.tabItemClick(e, 'linked-devices')}>
{gettext('Linked Devices')}
{canInvitePeople &&
this.tabItemClick(e, 'invitations')}>
{gettext('Invite Guest')}
}
{gettext('Share Admin')}
{this.renderSharedAdmin()}
{customNavItems && this.renderCustomNavItems()}
{dtableWebServer &&
SeaTable
}
);
}
}
MainSideNav.propTypes = propTypes;
export default MainSideNav;