diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 7776551a0c..03d152eed2 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -116,7 +116,6 @@ (when (and current-chat? (or (not cursor-clock-value) (<= cursor-clock-value clock-value))) - ;; Not in the current view, offload to db and update cursor if necessary (if (or (not @view.state/viewable-item) (not= current-chat-id (:chat-id @view.state/viewable-item)) @@ -124,10 +123,11 @@ clock-value)) (add-message cofx {:message message :current-chat? current-chat?}) + ;; Not in the current view, offload to db and update cursor if necessary (when (and (< clock-value cursor-clock-value) (= current-chat-id - (:chat-id (.-item view.state/viewable-item)))) + (:chat-id @view.state/viewable-item))) {:db (assoc-in db [:chats chat-id :cursor] (chat-loading/clock-value->cursor clock-value))}))))) (defn- add-to-chat? diff --git a/src/status_im/ui/screens/chat/views.cljs b/src/status_im/ui/screens/chat/views.cljs index a7b9c7f970..16bdaf5b36 100644 --- a/src/status_im/ui/screens/chat/views.cljs +++ b/src/status_im/ui/screens/chat/views.cljs @@ -305,13 +305,18 @@ (defonce messages-list-ref (atom nil)) (defn on-viewable-items-changed [e] - (reset! state/viewable-item - (let [element (->> (.-viewableItems e) - reverse - (filter (fn [e] - (= :message (:type (.-item e))))) - first)] - (when element (.-item element)))) + (when @messages-list-ref + (reset! state/viewable-item + (when-let [last-visible-element (aget (.-viewableItems e) (dec (.-length (.-viewableItems e))))] + (let [index (.-index last-visible-element) + ;; Get first not visible element, if it's a datemark/gap + ;; we might unnecessarely add messages on receiving as + ;; they do not have a clock value, but most of the times + ;; it will be a message + first-not-visible (aget (.-data (.-props @messages-list-ref)) (inc index))] + (when (and first-not-visible + (= :message (:type first-not-visible))) + first-not-visible))))) (debounce/debounce-and-dispatch [:chat.ui/message-visibility-changed e] 5000)) (defview messages-view