1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-06 01:23:56 +00:00
seahub/frontend/src/pages/dashboard/files-activities.js

254 lines
7.8 KiB
JavaScript
Raw Normal View History

2018-11-29 09:55:14 +00:00
import React, { Component, Fragment } from 'react';
2018-10-16 10:19:51 +00:00
import PropTypes from 'prop-types';
2018-09-21 06:16:15 +00:00
import { seafileAPI } from '../../utils/seafile-api';
import { gettext, siteRoot } from '../../utils/constants';
2018-12-24 04:24:16 +00:00
import { Utils } from '../../utils/utils';
2018-08-30 07:10:52 +00:00
2018-10-16 10:19:51 +00:00
const contentPropTypes = {
data: PropTypes.object.isRequired,
};
2018-08-30 07:10:52 +00:00
class FileActivitiesContent extends Component {
render() {
let {loading, error_msg, items, has_more} = this.props.data;
2018-08-30 07:10:52 +00:00
if (loading) {
return <span className="loading-icon loading-tip"></span>;
} else if (error_msg) {
return <p className="error text-center">{error_msg}</p>;
} else {
return (
2018-11-29 09:55:14 +00:00
<Fragment>
2018-10-25 06:42:53 +00:00
<table className="table table-hover table-vcenter">
2018-08-30 07:10:52 +00:00
<thead>
<tr>
<th width="8%">{/* avatar */}</th>
<th width="10%">{gettext('User')}</th>
<th width="25%">{gettext('Operation')}</th>
<th width="37%">{gettext('File')} / {gettext('Library')}</th>
<th width="20%">{gettext('Time')}</th>
2018-08-30 07:10:52 +00:00
</tr>
</thead>
<TableBody items={items} />
2018-08-30 07:10:52 +00:00
</table>
{has_more ? <span className="loading-icon loading-tip"></span> : ''}
{error_msg ? <p className="error text-center">{error_msg}</p> : ''}
2018-11-29 09:55:14 +00:00
</Fragment>
2018-08-30 07:10:52 +00:00
);
}
}
}
2018-10-16 10:19:51 +00:00
FileActivitiesContent.propTypes = contentPropTypes;
const tablePropTypes = {
items: PropTypes.array.isRequired,
};
2018-08-30 07:10:52 +00:00
class TableBody extends Component {
render() {
let listFilesActivities = this.props.items.map(function(item, index) {
let op, details;
let userProfileURL = `${siteRoot}profile/${encodeURIComponent(item.author_email)}/`;
2018-12-24 04:24:16 +00:00
let libURL = siteRoot + 'library/' + item.repo_id + '/' + encodeURIComponent(item.repo_name) + '/';
2018-08-30 07:10:52 +00:00
let libLink = <a href={libURL}>{item.repo_name}</a>;
let smallLibLink = <a className="small text-secondary" href={libURL}>{item.repo_name}</a>;
if (item.obj_type == 'repo') {
switch(item.op_type) {
2018-10-16 10:19:51 +00:00
case 'create':
op = gettext('Created library');
details = <td>{libLink}</td>;
break;
case 'rename':
op = gettext('Renamed library');
details = <td>{item.old_repo_name} => {libLink}</td>;
break;
case 'delete':
op = gettext('Deleted library');
details = <td>{item.repo_name}</td>;
break;
case 'recover':
op = gettext('Restored library');
details = <td>{libLink}</td>;
break;
case 'clean-up-trash':
if (item.days == 0) {
op = gettext('Removed all items from trash.');
} else {
op = gettext('Removed items older than {n} days from trash.').replace('{n}', item.days);
}
details = <td>{libLink}</td>;
break;
2018-08-30 07:10:52 +00:00
}
} else if (item.obj_type == 'file') {
2018-12-24 04:24:16 +00:00
let fileURL = `${siteRoot}lib/${item.repo_id}/file${Utils.encodePath(item.path)}`;
2018-08-30 07:10:52 +00:00
let fileLink = <a href={fileURL}>{item.name}</a>;
switch(item.op_type) {
2018-10-16 10:19:51 +00:00
case 'create':
op = gettext('Created file');
details = <td>{fileLink}<br />{smallLibLink}</td>;
break;
case 'delete':
op = gettext('Deleted file');
details = <td>{item.name}<br />{smallLibLink}</td>;
break;
case 'recover':
op = gettext('Restored file');
details = <td>{fileLink}<br />{smallLibLink}</td>;
break;
case 'rename':
op = gettext('Renamed file');
details = <td>{item.old_name} => {fileLink}<br />{smallLibLink}</td>;
break;
case 'move':
var filePathLink = <a href={fileURL}>{item.path}</a>;
op = gettext('Moved file');
details = <td>{item.old_path} => {filePathLink}<br />{smallLibLink}</td>;
break;
case 'edit': // update
op = gettext('Updated file');
details = <td>{fileLink}<br />{smallLibLink}</td>;
break;
2018-08-30 07:10:52 +00:00
}
} else { // dir
2018-12-24 04:24:16 +00:00
let dirURL = siteRoot + 'library/' + item.repo_id + '/' + encodeURIComponent(item.repo_name) + Utils.encodePath(item.path);
2018-08-30 07:10:52 +00:00
let dirLink = <a href={dirURL}>{item.name}</a>;
switch(item.op_type) {
2018-10-16 10:19:51 +00:00
case 'create':
op = gettext('Created folder');
details = <td>{dirLink}<br />{smallLibLink}</td>;
break;
case 'delete':
op = gettext('Deleted folder');
details = <td>{item.name}<br />{smallLibLink}</td>;
break;
case 'recover':
op = gettext('Restored folder');
details = <td>{dirLink}<br />{smallLibLink}</td>;
break;
case 'rename':
op = gettext('Renamed folder');
details = <td>{item.old_name} => {dirLink}<br />{smallLibLink}</td>;
break;
case 'move':
var dirPathLink = <a href={dirURL}>{item.path}</a>;
op = gettext('Moved folder');
details = <td>{item.old_path} => {dirPathLink}<br />{smallLibLink}</td>;
break;
2018-08-30 07:10:52 +00:00
}
}
return (
<tr key={index}>
<td className="text-center">
2018-09-20 05:23:24 +00:00
<img src={item.avatar_url} alt="" width="36px" height="36px" className="avatar" />
2018-08-30 07:10:52 +00:00
</td>
<td>
<a href={userProfileURL}>{item.author_name}</a>
</td>
<td><span className="activity-op">{op}</span></td>
{details}
<td className="text-secondary" dangerouslySetInnerHTML={{__html:item.time_relative}}></td>
</tr>
);
}, this);
return (
<tbody>{listFilesActivities}</tbody>
);
}
}
2018-10-16 10:19:51 +00:00
TableBody.propTypes = tablePropTypes;
2018-08-30 07:10:52 +00:00
class FilesActivities extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
error_msg: '',
events: {},
items: [],
page: 1,
has_more: false
2018-08-30 07:10:52 +00:00
};
this.handleScroll = this.handleScroll.bind(this);
}
componentDidMount() {
2018-09-20 05:23:24 +00:00
const pageNum = 1;
const avatarSize = 72;
2018-10-16 10:19:51 +00:00
seafileAPI.listActivities(pageNum, avatarSize).then(res => {
2018-08-30 07:10:52 +00:00
// not logged in
if (res.status == 403) {
this.setState({
loading: false,
2018-10-16 10:19:51 +00:00
error_msg: gettext('Permission denied')
2018-08-30 07:10:52 +00:00
});
} else {
// {"events":[...]}
this.setState({
loading: false,
items: res.data.events,
has_more: res.data.events.length == '0' ? false : true
2018-08-30 07:10:52 +00:00
});
}
});
}
getMore() {
const pageNum = this.state.page + 1;
this.setState({
page: pageNum
});
seafileAPI.listActivities(pageNum)
.then(res => {
if (res.status == 403) {
this.setState({
loading: false,
error_msg: gettext('Permission denied')
});
} else {
// {"events":[...]}
this.setState({
loading: false,
items: [...this.state.items, ...res.data.events],
has_more: res.data.events.length == '0' ? false : true
});
2018-08-30 07:10:52 +00:00
}
});
}
handleScroll(event) {
const clientHeight = event.target.clientHeight;
const scrollHeight = event.target.scrollHeight;
const scrollTop = event.target.scrollTop;
const isBottom = (clientHeight + scrollTop + 1 >= scrollHeight);
if (this.state.has_more && isBottom) { // scroll to the bottom
2018-08-30 07:10:52 +00:00
this.getMore();
}
}
render() {
return (
2018-11-26 09:53:18 +00:00
<div className="main-panel-center">
<div className="cur-view-container" id="activities">
<div className="cur-view-path">
<h3 className="sf-heading">{gettext('Activities')}</h3>
</div>
<div className="cur-view-content" onScroll={this.handleScroll}>
<FileActivitiesContent data={this.state} />
2018-11-26 06:00:32 +00:00
</div>
2018-08-30 07:10:52 +00:00
</div>
2018-11-26 09:53:18 +00:00
</div>
2018-08-30 07:10:52 +00:00
);
}
}
export default FilesActivities;