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

259 lines
8.2 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';
import moment from 'moment';
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-12-28 03:12:24 +00:00
import Loading from '../../components/loading';
2018-08-30 07:10:52 +00:00
moment.locale(window.app.config.lang);
2018-10-16 10:19:51 +00:00
const contentPropTypes = {
2018-12-28 03:12:24 +00:00
isLoadingMore: PropTypes.bool.isRequired,
items: PropTypes.array.isRequired,
2018-10-16 10:19:51 +00:00
};
2018-08-30 07:10:52 +00:00
class FileActivitiesContent extends Component {
render() {
2018-12-28 03:12:24 +00:00
let {items, isLoadingMore} = this.props;
return (
<Fragment>
<table className="table table-hover table-vcenter">
<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>
</tr>
</thead>
<TableBody items={items} />
</table>
{isLoadingMore ? <span className="loading-icon loading-tip"></span> : ''}
</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">
<time datetime={item.time} is="relative-time" title={moment(item.time).format('llll')}>{moment(item.time).fromNow()}</time>
</td>
2018-08-30 07:10:52 +00:00
</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 = {
2018-12-28 03:12:24 +00:00
errorMsg: '',
isFirstLoading: true,
isLoadingMore: false,
currentPage: 1,
hasMore: true,
items: [],
2018-08-30 07:10:52 +00:00
};
2019-01-08 06:51:13 +00:00
this.avatarSize = 72;
2018-08-30 07:10:52 +00:00
}
componentDidMount() {
2018-12-28 03:12:24 +00:00
let currentPage = this.state.currentPage;
2019-01-08 06:51:13 +00:00
seafileAPI.listActivities(currentPage, this.avatarSize).then(res => {
2018-12-28 03:12:24 +00:00
// {"events":[...]}
this.setState({
items: res.data.events,
currentPage: currentPage + 1,
isFirstLoading: false,
hasMore: true,
});
}).catch(error => {
if (error.response.status == 403) {
2018-08-30 07:10:52 +00:00
this.setState({
2018-12-28 03:12:24 +00:00
isFirstLoading: false,
errorMsg: gettext('Permission denied')
2018-08-30 07:10:52 +00:00
});
}
});
}
getMore() {
2018-12-28 03:12:24 +00:00
let currentPage = this.state.currentPage;
2019-01-08 06:51:13 +00:00
seafileAPI.listActivities(currentPage, this.avatarSize).then(res => {
2018-12-28 03:12:24 +00:00
// {"events":[...]}
this.setState({
isLoadingMore: false,
items: [...this.state.items, ...res.data.events],
currentPage: currentPage + 1,
hasMore: res.data.events.length === 0 ? false : true
});
}).catch(error => {
if (error.response.status == 403) {
2018-12-25 10:25:16 +00:00
this.setState({
2018-12-28 03:12:24 +00:00
isLoadingMore: false,
errorMsg: gettext('Permission denied')
2018-12-25 10:25:16 +00:00
});
}
});
2018-08-30 07:10:52 +00:00
}
2018-12-28 03:12:24 +00:00
handleScroll = (event) => {
2019-01-08 06:51:13 +00:00
if (!this.state.isLoadingMore && this.state.hasMore) {
const clientHeight = event.target.clientHeight;
const scrollHeight = event.target.scrollHeight;
const scrollTop = event.target.scrollTop;
const isBottom = (clientHeight + scrollTop + 1 >= scrollHeight);
if (isBottom) { // scroll to the bottom
this.setState({isLoadingMore: true}, () => {
this.getMore();
});
}
2018-08-30 07:10:52 +00:00
}
}
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}>
2018-12-28 03:12:24 +00:00
{this.state.isFirstLoading && <Loading />}
{(!this.state.isFirstLoading && this.state.errorMsg) &&
<p className="error text-center">{this.state.errorMsg}</p>
}
{!this.state.isFirstLoading &&
<FileActivitiesContent items={this.state.items} isLoadingMore={this.state.isLoadingMore}/>
}
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;