From 7b28880294865c7441ce8b4b3efc14b1417cb5e5 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Tue, 21 Jul 2015 15:54:47 -0400
Subject: Track caretCount as part of createPost's state so that we don't
 unnecessarily search for a thread to reply to when the user is typing

---
 web/react/components/create_post.jsx | 44 ++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 22 deletions(-)

(limited to 'web/react/components/create_post.jsx')

diff --git a/web/react/components/create_post.jsx b/web/react/components/create_post.jsx
index a3e354599..7b7e38ac2 100644
--- a/web/react/components/create_post.jsx
+++ b/web/react/components/create_post.jsx
@@ -102,7 +102,7 @@ module.exports = React.createClass({
         $(".post-list-holder-by-time").perfectScrollbar('update');
 
         if (this.state.rootId || this.state.parentId) {
-            this.setState({rootId: "", parentId: ""});
+            this.setState({rootId: "", parentId: "", caretCount: 0});
 
             // clear the active thread since we've now sent our message
             AppDispatcher.handleViewAction({
@@ -138,25 +138,30 @@ module.exports = React.createClass({
             // the number of carets indicates how many message threads back we're replying to
             var caretCount = replyMatch[0].length;
 
-            var posts = PostStore.getCurrentPosts();
+            // note that if someone else replies to this thread while a user is typing a reply, the message to which they're replying
+            // won't change unless they change the number of carets. this is probably the desired behaviour since we don't want the
+            // active message thread to change without the user noticing
+            if (caretCount != this.state.caretCount) {
+                this.setState({caretCount: caretCount});
 
-            var rootId = "";
+                var posts = PostStore.getCurrentPosts();
 
-            // find the nth most recent post that isn't a comment on another (ie it has no parent) where n is caretCount
-            for (var i = 0; i < posts.order.length; i++) {
-                var postId = posts.order[i];
+                var rootId = "";
 
-                if (posts.posts[postId].parent_id === "") {
-                    if (caretCount == 1) {
-                        rootId = postId;
-                        break;
-                    } else {
+                // find the nth most recent post that isn't a comment on another (ie it has no parent) where n is caretCount
+                for (var i = 0; i < posts.order.length; i++) {
+                    var postId = posts.order[i];
+
+                    if (posts.posts[postId].parent_id === "") {
                         caretCount -= 1;
+
+                        if (caretCount < 1) {
+                            rootId = postId;
+                            break;
+                        }
                     }
                 }
-            }
 
-            if (rootId) {
                 // only dispatch an event if something changed
                 if (rootId != this.state.rootId) {
                     // set the parent id to match the root id so that we're replying to the first post in the thread
@@ -169,16 +174,11 @@ module.exports = React.createClass({
                         parent_id: parentId
                     });
                 }
-            } else {
-                // we couldn't find a post to respond to so clear the active thread
-                AppDispatcher.handleViewAction({
-                    type: ActionTypes.RECEIVED_ACTIVE_THREAD_CHANGED,
-                    root_id: "",
-                    parent_id: ""
-                });
             }
         } else {
-            if (this.state.rootId || this.state.parentId) {
+            if (this.state.caretCount > 0) {
+                this.setState({caretCount: 0});
+
                 // clear the active thread since there no longer is one
                 AppDispatcher.handleViewAction({
                     type: ActionTypes.RECEIVED_ACTIVE_THREAD_CHANGED,
@@ -287,7 +287,7 @@ module.exports = React.createClass({
             previews = draft['previews'];
             messageText = draft['message'];
         }
-        return { channel_id: ChannelStore.getCurrentId(), messageText: messageText, uploadsInProgress: 0, previews: previews, submitting: false, initialText: messageText };
+        return { channel_id: ChannelStore.getCurrentId(), messageText: messageText, uploadsInProgress: 0, previews: previews, submitting: false, initialText: messageText, caretCount: 0 };
     },
     setUploads: function(val) {
         var oldInProgress = this.state.uploadsInProgress
-- 
cgit v1.2.3-1-g7c22

