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
import '../../css/internal-link.css' ;
const propTypes = {
path : PropTypes . string . isRequired ,
repoID : PropTypes . string . isRequired ,
} ;
class InternalLinkDialog extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
isOpen : false ,
smartLink : '' ,
} ;
this . toggle = this . toggle . bind ( this ) ;
this . getInternalLink = this . getInternalLink . bind ( this ) ;
this . copyToClipBoard = this . copyToClipBoard . bind ( this ) ;
}
toggle ( ) {
this . setState ( {
isOpen : ! this . state . isOpen ,
} ) ;
}
getInternalLink ( ) {
let repoID = this . props . repoID ;
let path = this . props . path ;
seafileAPI . getInternalLink ( repoID , path ) . then ( res => {
this . setState ( {
isOpen : true ,
smartLink : res . data . smart _link
} ) ;
2019-07-22 10:29:59 +00:00
} ) . catch ( error => {
let errMessage = Utils . getErrorMsg ( error ) ;
toaster . danger ( errMessage ) ;
2018-12-14 04:19:24 +00:00
} ) ;
}
copyToClipBoard ( ) {
copy ( this . state . smartLink ) ;
this . setState ( {
isOpen : false
} ) ;
2019-03-01 07:14:45 +00:00
let message = gettext ( 'Internal link has been copied to clipboard' ) ;
2018-12-14 04:19:24 +00:00
toaster . success ( message ) , {
duration : 2
} ;
}
render ( ) {
return (
2021-09-29 09:44:53 +00:00
< Fragment >
< i title = { gettext ( 'Internal Link' ) } role = "button" tabIndex = "0" aria - label = { gettext ( 'Internal Link' ) } className = "file-internal-link fa fa-link" onClick = { this . getInternalLink } > < / i >
2018-12-14 04:19:24 +00:00
< Modal isOpen = { this . state . isOpen } toggle = { this . toggle } >
< ModalHeader toggle = { this . toggle } > { gettext ( 'Internal Link' ) } < / M o d a l H e a d e r >
< ModalBody >
< p className = "tip mb-1" >
2019-01-28 08:32:32 +00:00
{ 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
< / p >
< p >
< a target = "_blank" href = { this . state . smartLink } > { this . state . smartLink } < / a >
< / p >
< / M o d a l B o d y >
< ModalFooter >
< Button color = "secondary" onClick = { this . toggle } > { gettext ( 'Cancel' ) } < / B u t t o n >
2019-06-25 03:53:43 +00:00
< Button color = "primary" onClick = { this . copyToClipBoard } > { gettext ( 'Copy' ) } < / B u t t o n >
2018-12-14 04:19:24 +00:00
< / M o d a l F o o t e r >
< / M o d a l >
2021-09-29 09:44:53 +00:00
< / F r a g m e n t >
2018-12-14 04:19:24 +00:00
) ;
}
}
InternalLinkDialog . propTypes = propTypes ;
export default InternalLinkDialog ;