forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite
Permalink
as functional component
- Loading branch information
1 parent
16014d4
commit 94be4f3
Showing
1 changed file
with
26 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,38 @@ | ||
import PropTypes from 'prop-types'; | ||
import { PureComponent } from 'react'; | ||
import { useCallback } from 'react'; | ||
|
||
import { withOptionalRouter, WithOptionalRouterPropTypes } from 'flavours/glitch/utils/react_router'; | ||
import { useAppHistory } from './router'; | ||
|
||
class Permalink extends PureComponent { | ||
const Permalink = ({ className, href, to, children, onInterceptClick, ...props }) => { | ||
const history = useAppHistory(); | ||
|
||
static propTypes = { | ||
className: PropTypes.string, | ||
href: PropTypes.string.isRequired, | ||
to: PropTypes.string.isRequired, | ||
children: PropTypes.node, | ||
onInterceptClick: PropTypes.func, | ||
...WithOptionalRouterPropTypes, | ||
}; | ||
|
||
handleClick = (e) => { | ||
const handleClick = useCallback((e) => { | ||
if (e.button === 0 && !(e.ctrlKey || e.metaKey)) { | ||
if (this.props.onInterceptClick && this.props.onInterceptClick()) { | ||
if (onInterceptClick && onInterceptClick()) { | ||
e.preventDefault(); | ||
return; | ||
} | ||
|
||
if (this.props.history) { | ||
if (history) { | ||
e.preventDefault(); | ||
this.props.history.push(this.props.to); | ||
history.push(to); | ||
} | ||
} | ||
}; | ||
|
||
render () { | ||
const { | ||
children, | ||
className, | ||
href, | ||
to, | ||
onInterceptClick, | ||
...other | ||
} = this.props; | ||
|
||
return ( | ||
<a target='_blank' href={href} onClick={this.handleClick} {...other} className={`permalink${className ? ' ' + className : ''}`}> | ||
{children} | ||
</a> | ||
); | ||
} | ||
|
||
} | ||
|
||
export default withOptionalRouter(Permalink); | ||
}, [onInterceptClick, history, to]); | ||
|
||
return ( | ||
<a target='_blank' href={href} onClick={handleClick} className={`permalink${className ? ' ' + className : ''}`} {...props}> | ||
{children} | ||
</a> | ||
); | ||
}; | ||
|
||
Permalink.propTypes = { | ||
className: PropTypes.string, | ||
href: PropTypes.string.isRequired, | ||
to: PropTypes.string.isRequired, | ||
children: PropTypes.node, | ||
onInterceptClick: PropTypes.func, | ||
}; | ||
|
||
export default Permalink; |