1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-07 09:51:26 +00:00
Files
seahub/frontend/src/pages/share-admin/share-links.js

260 lines
7.6 KiB
JavaScript
Raw Normal View History

2018-12-27 10:24:34 +08:00
import React, { Component, Fragment } from 'react';
import { Link } from '@reach/router';
import moment from 'moment';
import { Modal, ModalHeader, ModalBody } from 'reactstrap';
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';
class Content extends Component {
2018-12-26 16:07:22 +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 = () => {
this.setState({
modalOpen: !this.state.modalOpen
});
}
2018-12-26 16:07:22 +08:00
showModal = (options) => {
this.toggleModal();
2018-12-27 10:24:34 +08:00
this.setState({modalContent: options.content});
}
render() {
2018-12-27 10:24:34 +08:00
const { loading, errorMsg, items } = this.props;
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">
<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>
</div>
);
const table = (
<React.Fragment>
2018-12-26 16:07:22 +08:00
<table className="table-hover">
<thead>
<tr>
2018-12-26 16:07:22 +08:00
<th width="4%">{/*icon*/}</th>
<th width="36%">{gettext("Name")}<a className="table-sort-op by-name" href="#"> <span className="sort-icon icon-caret-up"></span></a></th>{/* TODO:sort */}
<th width="24%">{gettext("Library")}</th>
<th width="12%">{gettext("Visits")}</th>
<th width="14%">{gettext("Expiration")}<a className="table-sort-op by-time" href="#"> <span className="sort-icon icon-caret-down hide" aria-hidden="true"></span></a></th>{/*TODO:sort*/}
<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>
</table>
<Modal isOpen={this.state.modalOpen} toggle={this.toggleModal} centered={true}>
<ModalHeader toggle={this.toggleModal}>{gettext('Link')}</ModalHeader>
<ModalBody>
{this.state.modalContent}
</ModalBody>
</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-12-26 16:07:22 +08:00
handleMouseOut = () => {
2018-12-27 10:24:34 +08:00
this.setState({showOpIcon: false});
}
2018-12-26 16:07:22 +08:00
viewLink = (e) => {
e.preventDefault();
2018-12-27 10:24:34 +08:00
this.props.showModal({content: this.props.item.link});
}
2018-12-27 10:24:34 +08:00
2018-12-26 16:07:22 +08:00
removeLink = (e) => {
e.preventDefault();
2018-12-27 10:24:34 +08:00
this.props.onRemoveLink(this.props.item);
}
2018-12-27 10:24:34 +08:00
getLinkParams = () => {
let item = this.props.item;
let icon_size = Utils.isHiDPI() ? 48 : 24;
let iconUrl = '';
let linkUrl = '';
if (item.is_dir) {
iconUrl = Utils.getFolderIconUrl({
is_readonly: false,
size: icon_size
2018-12-27 10:24:34 +08:00
});
linkUrl = `${siteRoot}library/${item.repo_id}/${item.repo_name}${Utils.encodePath(item.path)}`;
} else {
2018-12-27 10:24:34 +08:00
iconUrl = Utils.getFileIconUrl(item.obj_name, icon_size);
linkUrl = `${siteRoot}lib/${item.repo_id}/file${Utils.encodePath(item.path)}`;
}
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-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();
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 (
<tr onMouseOver={this.handleMouseOver} onMouseOut={this.handleMouseOut}>
2018-12-27 10:24:34 +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>
<td>{item.view_cnt}</td>
<td>{this.renderExpriedData()}</td>
<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
constructor(props) {
super(props);
this.state = {
loading: true,
errorMsg: '',
items: []
};
}
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);
});
this.setState({
loading: false,
2018-12-27 10:24:34 +08:00
items: items
});
}).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.")
});
}
});
}
2018-12-27 10:24:34 +08:00
onRemoveLink = (item) => {
seafileAPI.deleteUploadLink(item.token).then(() => {
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
});
}
render() {
return (
2018-11-26 17:53:18 +08:00
<div className="main-panel-cneter">
<div className="cur-view-container">
<div className="cur-view-path">
<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
errorMsg={this.state.errorMsg}
items={this.state.items}
loading={this.state.loading}
onRemoveLink={this.onRemoveLink}
/>
2018-11-26 14:00:32 +08:00
</div>
</div>
2018-11-26 17:53:18 +08:00
</div>
);
}
}
export default ShareAdminShareLinks;