2021-09-29 09:44:53 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2018-12-14 04:19:24 +00:00
|
|
|
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import toaster from '../toast';
|
2020-02-03 08:43:49 +00:00
|
|
|
import copy from '../copy-to-clipboard';
|
2018-12-14 04:19:24 +00:00
|
|
|
import { gettext } from '../../utils/constants';
|
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
2019-07-16 02:01:09 +00:00
|
|
|
import { Utils } from '../../utils/utils';
|
2020-02-03 08:43:49 +00:00
|
|
|
|
2018-12-14 04:19:24 +00:00
|
|
|
const propTypes = {
|
|
|
|
path: PropTypes.string.isRequired,
|
|
|
|
repoID: PropTypes.string.isRequired,
|
2023-12-25 08:06:36 +00:00
|
|
|
internalLink: PropTypes.string,
|
2023-05-12 03:24:34 +00:00
|
|
|
onInternalLinkDialogToggle: PropTypes.func.isRequired,
|
2018-12-14 04:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class InternalLinkDialog extends React.Component {
|
2023-05-12 03:24:34 +00:00
|
|
|
|
2018-12-14 04:19:24 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
smartLink: '',
|
2023-05-12 03:24:34 +00:00
|
|
|
isLoading: true,
|
2018-12-14 04:19:24 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-12 03:24:34 +00:00
|
|
|
componentDidMount() {
|
2023-12-25 08:06:36 +00:00
|
|
|
if (this.props.internalLink) {
|
|
|
|
this.setState({smartLink: this.props.internalLink});
|
|
|
|
return;
|
|
|
|
}
|
2023-05-12 03:24:34 +00:00
|
|
|
this.getInternalLink();
|
2018-12-14 04:19:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 03:24:34 +00:00
|
|
|
getInternalLink = () => {
|
2018-12-14 04:19:24 +00:00
|
|
|
let repoID = this.props.repoID;
|
|
|
|
let path = this.props.path;
|
|
|
|
seafileAPI.getInternalLink(repoID, path).then(res => {
|
2023-05-12 03:24:34 +00:00
|
|
|
const { smart_link } = res.data;
|
2018-12-14 04:19:24 +00:00
|
|
|
this.setState({
|
2023-05-12 03:24:34 +00:00
|
|
|
isLoading: false,
|
|
|
|
smartLink: smart_link,
|
2018-12-14 04:19:24 +00:00
|
|
|
});
|
2019-07-22 10:29:59 +00:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2023-05-12 03:24:34 +00:00
|
|
|
this.setState({isLoading: false});
|
2018-12-14 04:19:24 +00:00
|
|
|
});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-12-14 04:19:24 +00:00
|
|
|
|
2023-05-12 03:24:34 +00:00
|
|
|
copyToClipBoard = () => {
|
2018-12-14 04:19:24 +00:00
|
|
|
copy(this.state.smartLink);
|
2023-05-12 03:24:34 +00:00
|
|
|
const message = gettext('Internal link has been copied to clipboard');
|
|
|
|
toaster.success(message, {duration: 2});
|
|
|
|
this.toggle();
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-05-12 03:24:34 +00:00
|
|
|
|
|
|
|
toggle = () => {
|
2023-12-25 08:06:36 +00:00
|
|
|
this.props.onInternalLinkDialogToggle({internalLink: ''});
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2018-12-14 04:19:24 +00:00
|
|
|
|
|
|
|
render() {
|
2023-05-12 03:24:34 +00:00
|
|
|
const tipMessage = gettext('An internal link is a link to a file or folder that can be accessed by users with read permission to the file or folder.');
|
2018-12-14 04:19:24 +00:00
|
|
|
return (
|
2021-09-29 09:44:53 +00:00
|
|
|
<Fragment>
|
2023-05-12 03:24:34 +00:00
|
|
|
<Modal isOpen={true} toggle={this.toggle}>
|
2018-12-14 04:19:24 +00:00
|
|
|
<ModalHeader toggle={this.toggle}>{gettext('Internal Link')}</ModalHeader>
|
|
|
|
<ModalBody>
|
2023-05-12 03:24:34 +00:00
|
|
|
<p className="tip mb-1">{tipMessage}</p>
|
2018-12-14 04:19:24 +00:00
|
|
|
<p>
|
2023-05-12 03:24:34 +00:00
|
|
|
<a target="_blank" href={this.state.smartLink} rel='noreferrer'>{this.state.smartLink}</a>
|
2018-12-14 04:19:24 +00:00
|
|
|
</p>
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<Button color="secondary" onClick={this.toggle}>{gettext('Cancel')}</Button>
|
2019-06-25 03:53:43 +00:00
|
|
|
<Button color="primary" onClick={this.copyToClipBoard}>{gettext('Copy')}</Button>
|
2018-12-14 04:19:24 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
2021-09-29 09:44:53 +00:00
|
|
|
</Fragment>
|
2018-12-14 04:19:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InternalLinkDialog.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default InternalLinkDialog;
|