summaryrefslogtreecommitdiffstats
path: root/web/react/components/rhs_thread.jsx
blob: 2edcd8b37fa53f5af5e3363cde62946d63c3d476 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import PostStore from '../stores/post_store.jsx';
import UserStore from '../stores/user_store.jsx';
import PreferenceStore from '../stores/preference_store.jsx';
import * as Utils from '../utils/utils.jsx';
import SearchBox from './search_bar.jsx';
import CreateComment from './create_comment.jsx';
import RhsHeaderPost from './rhs_header_post.jsx';
import RootPost from './rhs_root_post.jsx';
import Comment from './rhs_comment.jsx';
import Constants from '../utils/constants.jsx';
import FileUploadOverlay from './file_upload_overlay.jsx';

export default class RhsThread extends React.Component {
    constructor(props) {
        super(props);

        this.onChange = this.onChange.bind(this);
        this.onChangeAll = this.onChangeAll.bind(this);
        this.forceUpdateInfo = this.forceUpdateInfo.bind(this);
        this.handleResize = this.handleResize.bind(this);

        const state = this.getStateFromStores();
        state.windowWidth = Utils.windowWidth();
        state.windowHeight = Utils.windowHeight();
        this.state = state;
    }
    getStateFromStores() {
        var postList = PostStore.getSelectedPost();
        if (!postList || postList.order.length < 1 || !postList.posts[postList.order[0]]) {
            return {postList: {}};
        }

        var channelId = postList.posts[postList.order[0]].channel_id;
        var pendingPostsList = PostStore.getPendingPosts(channelId);

        if (pendingPostsList) {
            for (var pid in pendingPostsList.posts) {
                if (pendingPostsList.posts.hasOwnProperty(pid)) {
                    postList.posts[pid] = pendingPostsList.posts[pid];
                }
            }
        }

        return {postList: postList};
    }
    componentDidMount() {
        PostStore.addSelectedPostChangeListener(this.onChange);
        PostStore.addChangeListener(this.onChangeAll);
        PreferenceStore.addChangeListener(this.forceUpdateInfo);
        this.resize();
        window.addEventListener('resize', this.handleResize);
    }
    componentDidUpdate() {
        if ($('.post-right__scroll')[0]) {
            $('.post-right__scroll').scrollTop($('.post-right__scroll')[0].scrollHeight);
        }
        this.resize();
    }
    componentWillUnmount() {
        PostStore.removeSelectedPostChangeListener(this.onChange);
        PostStore.removeChangeListener(this.onChangeAll);
        PreferenceStore.removeChangeListener(this.forceUpdateInfo);
        window.removeEventListener('resize', this.handleResize);
    }
    forceUpdateInfo() {
        if (this.state.postList) {
            for (var postId in this.state.postList.posts) {
                if (this.refs[postId]) {
                    this.refs[postId].forceUpdate();
                }
            }
        }
    }
    handleResize() {
        this.setState({
            windowWidth: Utils.windowWidth(),
            windowHeight: Utils.windowHeight()
        });
    }
    onChange() {
        var newState = this.getStateFromStores();
        if (!Utils.areObjectsEqual(newState, this.state)) {
            this.setState(newState);
        }
    }
    onChangeAll() {
        // if something was changed in the channel like adding a
        // comment or post then lets refresh the sidebar list
        var currentSelected = PostStore.getSelectedPost();
        if (!currentSelected || currentSelected.order.length === 0 || !currentSelected.posts[currentSelected.order[0]]) {
            return;
        }

        var currentPosts = PostStore.getVisiblePosts(currentSelected.posts[currentSelected.order[0]].channel_id);

        if (!currentPosts || currentPosts.order.length === 0) {
            return;
        }

        if (currentPosts.posts[currentPosts.order[0]].channel_id === currentSelected.posts[currentSelected.order[0]].channel_id) {
            for (var key in currentSelected.posts) {
                if (currentSelected.posts.hasOwnProperty(key)) {
                    var post = currentSelected.posts[key];
                    if (post.pending_post_id) {
                        Reflect.deleteProperty(currentSelected.posts, key);
                    }
                }
            }

            for (var postId in currentPosts.posts) {
                if (currentPosts.posts.hasOwnProperty(postId)) {
                    currentSelected.posts[postId] = currentPosts.posts[postId];
                }
            }

            PostStore.storeSelectedPost(currentSelected);
        }

        var newState = this.getStateFromStores();
        if (!Utils.areObjectsEqual(newState, this.state)) {
            this.setState(newState);
        }
    }
    resize() {
        $('.post-right__scroll').scrollTop(100000);
        if (this.state.windowWidth > 768) {
            $('.post-right__scroll').perfectScrollbar();
            $('.post-right__scroll').perfectScrollbar('update');
        }
    }
    render() {
        var postList = this.state.postList;

        if (postList == null || !postList.order) {
            return (
                <div></div>
            );
        }

        var selectedPost = postList.posts[postList.order[0]];
        var rootPost = null;

        if (selectedPost.root_id === '') {
            rootPost = selectedPost;
        } else {
            rootPost = postList.posts[selectedPost.root_id];
        }

        var postsArray = [];

        for (var postId in postList.posts) {
            if (postList.posts.hasOwnProperty(postId)) {
                var cpost = postList.posts[postId];
                if (cpost.root_id === rootPost.id) {
                    postsArray.push(cpost);
                }
            }
        }

        // sort failed posts to bottom, followed by pending, and then regular posts
        postsArray.sort(function postSort(a, b) {
            if ((a.state === Constants.POST_LOADING || a.state === Constants.POST_FAILED) && (b.state !== Constants.POST_LOADING && b.state !== Constants.POST_FAILED)) {
                return 1;
            }
            if ((a.state !== Constants.POST_LOADING && a.state !== Constants.POST_FAILED) && (b.state === Constants.POST_LOADING || b.state === Constants.POST_FAILED)) {
                return -1;
            }

            if (a.state === Constants.POST_LOADING && b.state === Constants.POST_FAILED) {
                return -1;
            }
            if (a.state === Constants.POST_FAILED && b.state === Constants.POST_LOADING) {
                return 1;
            }

            if (a.create_at < b.create_at) {
                return -1;
            }
            if (a.create_at > b.create_at) {
                return 1;
            }
            return 0;
        });

        var currentId = UserStore.getCurrentId();
        var searchForm;
        if (currentId != null) {
            searchForm = <SearchBox />;
        }

        return (
            <div className='post-right__container'>
                <FileUploadOverlay overlayType='right' />
                <div className='search-bar__container sidebar--right__search-header'>{searchForm}</div>
                <div className='sidebar-right__body'>
                    <RhsHeaderPost
                        fromSearch={this.props.fromSearch}
                        isMentionSearch={this.props.isMentionSearch}
                    />
                    <div className='post-right__scroll'>
                        <RootPost
                            ref={rootPost.id}
                            post={rootPost}
                            commentCount={postsArray.length}
                        />
                        <div className='post-right-comments-container'>
                        {postsArray.map(function mapPosts(comPost) {
                            return (
                                <Comment
                                    ref={comPost.id}
                                    key={comPost.id + 'commentKey'}
                                    post={comPost}
                                    selected={(comPost.id === selectedPost.id)}
                                />
                            );
                        })}
                        </div>
                        <div className='post-create__container'>
                            <CreateComment
                                channelId={rootPost.channel_id}
                                rootId={rootPost.id}
                            />
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

RhsThread.defaultProps = {
    fromSearch: '',
    isMentionSearch: false
};

RhsThread.propTypes = {
    fromSearch: React.PropTypes.string,
    isMentionSearch: React.PropTypes.bool
};