2018-12-27 10:24:34 +08:00
|
|
|
import React, { Component, Fragment } from 'react';
|
2018-11-19 11:53:44 +08:00
|
|
|
import { Link } from '@reach/router';
|
|
|
|
import moment from 'moment';
|
2019-02-20 10:40:54 +08:00
|
|
|
import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap';
|
2018-11-19 11:53:44 +08:00
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
|
|
|
import { Utils } from '../../utils/utils';
|
2018-12-26 16:07:22 +08:00
|
|
|
import { gettext, siteRoot, loginUrl, canGenerateUploadLink } from '../../utils/constants';
|
2018-12-27 10:24:34 +08:00
|
|
|
import SharedLinkInfo from '../../models/shared-link-info';
|
2019-02-20 10:40:54 +08:00
|
|
|
import copy from '@seafile/seafile-editor/dist//utils/copy-to-clipboard';
|
|
|
|
import toaster from '../../components/toast';
|
2018-11-19 11:53:44 +08:00
|
|
|
|
|
|
|
class Content extends Component {
|
2018-12-26 16:07:22 +08:00
|
|
|
|
2019-01-02 17:49:30 +08:00
|
|
|
sortByName = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const sortBy = 'name';
|
|
|
|
const sortOrder = this.props.sortOrder == 'asc' ? 'desc' : 'asc';
|
|
|
|
this.props.sortItems(sortBy, sortOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortByTime = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const sortBy = 'time';
|
|
|
|
const sortOrder = this.props.sortOrder == 'asc' ? 'desc' : 'asc';
|
|
|
|
this.props.sortItems(sortBy, sortOrder);
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:53:44 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
modalOpen: false,
|
|
|
|
modalContent: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// required by `Modal`, and can only set the 'open' state
|
2018-12-26 16:07:22 +08:00
|
|
|
toggleModal = () => {
|
2018-11-19 11:53:44 +08:00
|
|
|
this.setState({
|
|
|
|
modalOpen: !this.state.modalOpen
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:07:22 +08:00
|
|
|
showModal = (options) => {
|
2018-11-19 11:53:44 +08:00
|
|
|
this.toggleModal();
|
2018-12-27 10:24:34 +08:00
|
|
|
this.setState({modalContent: options.content});
|
2018-11-19 11:53:44 +08:00
|
|
|
}
|
|
|
|
|
2019-02-20 10:40:54 +08:00
|
|
|
copyToClipboard = () => {
|
|
|
|
copy(this.state.modalContent);
|
|
|
|
this.setState({
|
|
|
|
modalOpen: false
|
|
|
|
});
|
|
|
|
let message = gettext('Share link is copied to the clipboard.');
|
|
|
|
toaster.success(message), {
|
|
|
|
duration: 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:53:44 +08:00
|
|
|
render() {
|
2019-01-02 17:49:30 +08:00
|
|
|
const { loading, errorMsg, items, sortBy, sortOrder } = this.props;
|
2018-11-19 11:53:44 +08:00
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <span className="loading-icon loading-tip"></span>;
|
|
|
|
} else if (errorMsg) {
|
|
|
|
return <p className="error text-center">{errorMsg}</p>;
|
|
|
|
} else {
|
|
|
|
const emptyTip = (
|
|
|
|
<div className="empty-tip">
|
2019-01-31 17:37:02 +08:00
|
|
|
<h2>{gettext('You don\'t have any share links')}</h2>
|
|
|
|
<p>{gettext('You can generate a share link for a folder or a file. Anyone who receives this link can view the folder or the file online.')}</p>
|
2018-11-19 11:53:44 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2019-01-02 17:49:30 +08:00
|
|
|
// sort
|
|
|
|
const sortByName = sortBy == 'name';
|
|
|
|
const sortByTime = sortBy == 'time';
|
|
|
|
const sortIcon = sortOrder == 'asc' ? <span className="fas fa-caret-up"></span> : <span className="fas fa-caret-down"></span>;
|
|
|
|
|
2018-11-19 11:53:44 +08:00
|
|
|
const table = (
|
|
|
|
<React.Fragment>
|
2018-12-26 16:07:22 +08:00
|
|
|
<table className="table-hover">
|
2018-11-19 11:53:44 +08:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2018-12-26 16:07:22 +08:00
|
|
|
<th width="4%">{/*icon*/}</th>
|
2019-04-13 15:06:23 +08:00
|
|
|
<th width="31%"><a className="d-block table-sort-op" href="#" onClick={this.sortByName}>{gettext('Name')} {sortByName && sortIcon}</a></th>
|
|
|
|
<th width="14%">{gettext('Library')}</th>
|
|
|
|
<th width="20%">{gettext('Permission')}</th>
|
|
|
|
<th width="7%">{gettext('Visits')}</th>
|
2019-01-02 17:49:30 +08:00
|
|
|
<th width="14%"><a className="d-block table-sort-op" href="#" onClick={this.sortByTime}>{gettext('Expiration')} {sortByTime && sortIcon}</a></th>
|
2018-11-19 11:53:44 +08:00
|
|
|
<th width="10%">{/*Operations*/}</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
2018-12-27 10:24:34 +08:00
|
|
|
<tbody>
|
|
|
|
{items.map((item, index) => {
|
|
|
|
return (<Item key={index} item={item} showModal={this.showModal} onRemoveLink={this.props.onRemoveLink}/>);
|
|
|
|
})}
|
|
|
|
</tbody>
|
2018-11-19 11:53:44 +08:00
|
|
|
</table>
|
|
|
|
<Modal isOpen={this.state.modalOpen} toggle={this.toggleModal} centered={true}>
|
|
|
|
<ModalHeader toggle={this.toggleModal}>{gettext('Link')}</ModalHeader>
|
|
|
|
<ModalBody>
|
2019-02-20 10:40:54 +08:00
|
|
|
<a href={this.state.modalContent}>{this.state.modalContent}</a>
|
2018-11-19 11:53:44 +08:00
|
|
|
</ModalBody>
|
2019-02-20 10:40:54 +08:00
|
|
|
<ModalFooter>
|
|
|
|
<Button color="primary" onClick={this.copyToClipboard}>{gettext('Copy')}</Button>{' '}
|
|
|
|
<Button color="secondary" onClick={this.toggleModal}>{gettext('Close')}</Button>
|
|
|
|
</ModalFooter>
|
2018-11-19 11:53:44 +08:00
|
|
|
</Modal>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
return items.length ? table : emptyTip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Item extends Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
showOpIcon: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:07:22 +08:00
|
|
|
handleMouseOver = () => {
|
2018-12-27 10:24:34 +08:00
|
|
|
this.setState({showOpIcon: true});
|
2018-11-19 11:53:44 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 16:07:22 +08:00
|
|
|
handleMouseOut = () => {
|
2018-12-27 10:24:34 +08:00
|
|
|
this.setState({showOpIcon: false});
|
2018-11-19 11:53:44 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 16:07:22 +08:00
|
|
|
viewLink = (e) => {
|
2018-11-19 11:53:44 +08:00
|
|
|
e.preventDefault();
|
2018-12-27 10:24:34 +08:00
|
|
|
this.props.showModal({content: this.props.item.link});
|
2018-11-19 11:53:44 +08:00
|
|
|
}
|
2018-12-27 10:24:34 +08:00
|
|
|
|
2018-12-26 16:07:22 +08:00
|
|
|
removeLink = (e) => {
|
2018-11-19 11:53:44 +08:00
|
|
|
e.preventDefault();
|
2018-12-27 10:24:34 +08:00
|
|
|
this.props.onRemoveLink(this.props.item);
|
2018-11-19 11:53:44 +08:00
|
|
|
}
|
|
|
|
|
2018-12-27 10:24:34 +08:00
|
|
|
getLinkParams = () => {
|
|
|
|
let item = this.props.item;
|
|
|
|
let iconUrl = '';
|
|
|
|
let linkUrl = '';
|
|
|
|
if (item.is_dir) {
|
2019-01-29 15:22:02 +08:00
|
|
|
iconUrl = Utils.getFolderIconUrl(false);
|
2018-12-27 10:24:34 +08:00
|
|
|
linkUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}${Utils.encodePath(item.path)}`;
|
2018-11-19 11:53:44 +08:00
|
|
|
} else {
|
2019-01-29 15:22:02 +08:00
|
|
|
iconUrl = Utils.getFileIconUrl(item.obj_name);
|
2018-12-27 10:24:34 +08:00
|
|
|
linkUrl = `${siteRoot}lib/${item.repo_id}/file${Utils.encodePath(item.path)}`;
|
2018-11-19 11:53:44 +08:00
|
|
|
}
|
2018-12-27 10:24:34 +08:00
|
|
|
|
|
|
|
return { iconUrl, linkUrl };
|
|
|
|
}
|
|
|
|
|
|
|
|
renderExpriedData = () => {
|
|
|
|
let item = this.props.item;
|
|
|
|
if (!item.expire_date) {
|
|
|
|
return (
|
|
|
|
<Fragment>--</Fragment>
|
|
|
|
);
|
2018-11-19 11:53:44 +08:00
|
|
|
}
|
2018-12-27 10:24:34 +08:00
|
|
|
let expire_date = moment(item.expire_date).format('YYYY-MM-DD');
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{item.is_expired ?
|
|
|
|
<span className="error">{expire_date}</span> :
|
|
|
|
expire_date
|
|
|
|
}
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const item = this.props.item;
|
|
|
|
let { iconUrl, linkUrl } = this.getLinkParams();
|
2018-11-19 11:53:44 +08:00
|
|
|
|
|
|
|
let iconVisibility = this.state.showOpIcon ? '' : ' invisible';
|
2018-12-28 11:12:24 +08:00
|
|
|
let linkIconClassName = 'sf2-icon-link action-icon' + iconVisibility;
|
|
|
|
let deleteIconClassName = 'sf2-icon-delete action-icon' + iconVisibility;
|
2018-12-27 10:24:34 +08:00
|
|
|
return (
|
2018-11-19 11:53:44 +08:00
|
|
|
<tr onMouseOver={this.handleMouseOver} onMouseOut={this.handleMouseOut}>
|
2019-01-11 15:37:02 +08:00
|
|
|
<td><img src={iconUrl} width="24" /></td>
|
2019-01-02 11:52:44 +08:00
|
|
|
<td>
|
|
|
|
{item.is_dir ?
|
|
|
|
<Link to={linkUrl}>{item.obj_name}</Link> :
|
|
|
|
<a href={linkUrl} target="_blank">{item.obj_name}</a>
|
|
|
|
}
|
|
|
|
</td>
|
2018-12-27 10:24:34 +08:00
|
|
|
<td><Link to={`${siteRoot}library/${item.repo_id}/${item.repo_name}/`}>{item.repo_name}</Link></td>
|
2019-04-13 15:06:23 +08:00
|
|
|
{ item.permissions.can_download ?
|
2019-04-13 16:52:14 +08:00
|
|
|
<td>{gettext('Preview and download')}</td> :
|
|
|
|
<td>{gettext('Preview only')}</td>
|
2019-04-13 15:06:23 +08:00
|
|
|
}
|
2018-12-27 10:24:34 +08:00
|
|
|
<td>{item.view_cnt}</td>
|
|
|
|
<td>{this.renderExpriedData()}</td>
|
2018-11-19 11:53:44 +08:00
|
|
|
<td>
|
|
|
|
<a href="#" className={linkIconClassName} title={gettext('View')} onClick={this.viewLink}></a>
|
|
|
|
<a href="#" className={deleteIconClassName} title={gettext('Remove')} onClick={this.removeLink}></a>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShareAdminShareLinks extends Component {
|
2018-12-26 16:07:22 +08:00
|
|
|
|
2018-11-19 11:53:44 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
loading: true,
|
|
|
|
errorMsg: '',
|
2019-01-02 17:49:30 +08:00
|
|
|
items: [],
|
|
|
|
sortBy: 'name', // 'name' or 'time'
|
|
|
|
sortOrder: 'asc' // 'asc' or 'desc'
|
2018-11-19 11:53:44 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:49:30 +08:00
|
|
|
_sortItems = (items, sortBy, sortOrder) => {
|
|
|
|
let comparator;
|
|
|
|
|
|
|
|
switch (`${sortBy}-${sortOrder}`) {
|
|
|
|
case 'name-asc':
|
|
|
|
comparator = function(a, b) {
|
|
|
|
var result = Utils.compareTwoWord(a.obj_name, b.obj_name);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'name-desc':
|
|
|
|
comparator = function(a, b) {
|
|
|
|
var result = Utils.compareTwoWord(a.obj_name, b.obj_name);
|
|
|
|
return -result;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'time-asc':
|
|
|
|
comparator = function(a, b) {
|
|
|
|
return a.expire_date < b.expire_date ? -1 : 1;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'time-desc':
|
|
|
|
comparator = function(a, b) {
|
|
|
|
return a.expire_date < b.expire_date ? 1 : -1;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
items.sort((a, b) => {
|
|
|
|
if (a.is_dir && !b.is_dir) {
|
|
|
|
return -1;
|
|
|
|
} else if (!a.is_dir && b.is_dir) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return comparator(a, b);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
sortItems = (sortBy, sortOrder) => {
|
|
|
|
this.setState({
|
|
|
|
sortBy: sortBy,
|
|
|
|
sortOrder: sortOrder,
|
|
|
|
items: this._sortItems(this.state.items, sortBy, sortOrder)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:53:44 +08:00
|
|
|
componentDidMount() {
|
|
|
|
seafileAPI.listShareLinks().then((res) => {
|
|
|
|
// res: {data: Array(2), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
|
2018-12-27 10:24:34 +08:00
|
|
|
let items = res.data.map(item => {
|
|
|
|
return new SharedLinkInfo(item);
|
|
|
|
});
|
2018-11-19 11:53:44 +08:00
|
|
|
this.setState({
|
|
|
|
loading: false,
|
2019-01-02 17:49:30 +08:00
|
|
|
items: this._sortItems(items, this.state.sortBy, this.state.sortOrder)
|
2018-11-19 11:53:44 +08:00
|
|
|
});
|
|
|
|
}).catch((error) => {
|
|
|
|
if (error.response) {
|
|
|
|
if (error.response.status == 403) {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
2019-01-31 17:37:02 +08:00
|
|
|
errorMsg: gettext('Permission denied')
|
2018-11-19 11:53:44 +08:00
|
|
|
});
|
|
|
|
location.href = `${loginUrl}?next=${encodeURIComponent(location.href)}`;
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
2019-01-31 17:37:02 +08:00
|
|
|
errorMsg: gettext('Error')
|
2018-11-19 11:53:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
2019-01-31 17:37:02 +08:00
|
|
|
errorMsg: gettext('Please check the network.')
|
2018-11-19 11:53:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-27 10:24:34 +08:00
|
|
|
onRemoveLink = (item) => {
|
2019-01-03 11:56:32 +08:00
|
|
|
seafileAPI.deleteShareLink(item.token).then(() => {
|
2018-12-27 10:24:34 +08:00
|
|
|
let items = this.state.items.filter(uploadItem => {
|
|
|
|
return uploadItem.token !== item.token;
|
|
|
|
});
|
|
|
|
this.setState({items: items});
|
|
|
|
// TODO: show feedback msg
|
|
|
|
// gettext("Successfully deleted 1 item")
|
|
|
|
}).catch((error) => {
|
|
|
|
// TODO: show feedback msg
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:53:44 +08:00
|
|
|
render() {
|
|
|
|
return (
|
2019-01-10 15:09:47 +08:00
|
|
|
<div className="main-panel-center">
|
2018-11-26 17:53:18 +08:00
|
|
|
<div className="cur-view-container">
|
2019-02-20 13:11:13 +08:00
|
|
|
<div className="cur-view-path share-upload-nav">
|
2018-11-26 17:53:18 +08:00
|
|
|
<ul className="nav">
|
|
|
|
<li className="nav-item">
|
|
|
|
<Link to={`${siteRoot}share-admin-share-links/`} className="nav-link active">{gettext('Share Links')}</Link>
|
|
|
|
</li>
|
2018-12-27 10:24:34 +08:00
|
|
|
{canGenerateUploadLink && (
|
2018-11-26 17:53:18 +08:00
|
|
|
<li className="nav-item"><Link to={`${siteRoot}share-admin-upload-links/`} className="nav-link">{gettext('Upload Links')}</Link></li>
|
2018-12-27 10:24:34 +08:00
|
|
|
)}
|
2018-11-26 17:53:18 +08:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<div className="cur-view-content">
|
2018-12-27 10:24:34 +08:00
|
|
|
<Content
|
2019-01-02 17:49:30 +08:00
|
|
|
loading={this.state.loading}
|
2018-12-27 10:24:34 +08:00
|
|
|
errorMsg={this.state.errorMsg}
|
|
|
|
items={this.state.items}
|
2019-01-02 17:49:30 +08:00
|
|
|
sortBy={this.state.sortBy}
|
|
|
|
sortOrder={this.state.sortOrder}
|
|
|
|
sortItems={this.sortItems}
|
2018-12-27 10:24:34 +08:00
|
|
|
onRemoveLink={this.onRemoveLink}
|
|
|
|
/>
|
2018-11-26 14:00:32 +08:00
|
|
|
</div>
|
2018-11-19 11:53:44 +08:00
|
|
|
</div>
|
2018-11-26 17:53:18 +08:00
|
|
|
</div>
|
2018-11-19 11:53:44 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ShareAdminShareLinks;
|