forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permalink_view.jsx
90 lines (75 loc) · 2.4 KB
/
permalink_view.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import Constants from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';
export default class PermalinkView extends React.PureComponent {
static propTypes = {
channelId: PropTypes.string,
/*
* Object from react-router
*/
match: PropTypes.shape({
params: PropTypes.shape({
postid: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
returnTo: PropTypes.string.isRequired,
teamName: PropTypes.string,
actions: PropTypes.shape({
focusPost: PropTypes.func.isRequired,
}).isRequired,
currentUserId: PropTypes.string.isRequired,
};
static getDerivedStateFromProps(props, state) {
let updatedState = {postid: props.match.params.postid};
if (state.postid !== props.match.params.postid) {
updatedState = {...updatedState, valid: false};
}
return updatedState;
}
constructor(props) {
super(props);
this.state = {valid: false};
}
componentDidMount() {
this.mounted = true;
this.doPermalinkEvent(this.props);
document.body.classList.add('app__body');
}
componentDidUpdate() {
if (!this.state.valid) {
this.doPermalinkEvent(this.props);
}
}
componentWillUnmount() {
this.mounted = false;
}
doPermalinkEvent = async (props) => {
const postId = props.match.params.postid;
await this.props.actions.focusPost(postId, this.props.returnTo, this.props.currentUserId);
if (this.mounted) {
this.setState({valid: true});
}
}
isStateValid = () => {
return this.state.valid && this.props.channelId && this.props.teamName;
}
onShortcutKeyDown = (e) => {
if (e.shiftKey && Utils.cmdOrCtrlPressed(e) && Utils.isKeyPressed(e, Constants.KeyCodes.L) && this.permalink.current) {
this.permalink.current.focus();
}
}
render() {
if (!this.isStateValid()) {
return (
<div
id='app-content'
className='app__content'
/>
);
}
return null;
}
}