From dfbc0eb435bde0a3033a8756ed77eac1976d6003 Mon Sep 17 00:00:00 2001 From: Vitaliy Vlasov Date: Fri, 21 Dec 2018 13:29:33 +0200 Subject: [PATCH] Add desktop msg limit Signed-off-by: Vitaliy Vlasov --- src/status_im/constants.cljs | 3 +- src/status_im/ui/screens/chat/utils.cljs | 38 +++++++++++-------- .../ui/screens/desktop/main/chat/views.cljs | 14 ++----- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/status_im/constants.cljs b/src/status_im/constants.cljs index edcaad07f7..e741c75c0d 100644 --- a/src/status_im/constants.cljs +++ b/src/status_im/constants.cljs @@ -221,6 +221,7 @@ (def ^:const lines-collapse-threshold 20) (def ^:const chars-collapse-threshold 600) +(def ^:const desktop-msg-chars-hard-limit 10000) (def ^:const dapp-permission-contact-code "contact-code") (def ^:const dapp-permission-web3 "web3") @@ -239,4 +240,4 @@ ;;ipfs (def ^:const ipfs-add-url "https://ipfs.infura.io:5001/api/v0/add") (def ^:const ipfs-add-param-name "extension.event.edn") -(def ^:const ipfs-cat-url "https://ipfs.infura.io/ipfs/") \ No newline at end of file +(def ^:const ipfs-cat-url "https://ipfs.infura.io/ipfs/") diff --git a/src/status_im/ui/screens/chat/utils.cljs b/src/status_im/ui/screens/chat/utils.cljs index fa3e6b4001..d611e4ce8e 100644 --- a/src/status_im/ui/screens/chat/utils.cljs +++ b/src/status_im/ui/screens/chat/utils.cljs @@ -4,6 +4,7 @@ [status-im.utils.platform :as platform] [status-im.i18n :as i18n] [status-im.constants :as constants] + [status-im.utils.core :as core-utils] [status-im.ui.components.react :as react] [status-im.ui.components.colors :as colors] [status-im.utils.http :as http])) @@ -51,18 +52,25 @@ text-chunk])) render-recipe)) -(defn render-chunks-desktop [render-recipe message] - "This fn is only need as a temporary hack - until rn-desktop supports text/number-of-lines property" - (seq (second - (reduce (fn [[total-length acc] [idx [text-chunk kind]]] - (if (< constants/chars-collapse-threshold total-length) - (reduced [total-length acc]) - [(+ total-length (count text-chunk)) - (conj acc - (if (= :text kind) - text-chunk - [react/text (into {:key idx} (lookup-props text-chunk message kind)) - text-chunk]))])) - [0 []] - (map vector (range) render-recipe))))) +(defn render-chunks-desktop [limit render-recipe message] + "This fn is only needed as a temporary hack + until rn-desktop supports text/number-of-lines property" + (->> render-recipe + (map vector (range)) + (reduce (fn [[total-length acc] [idx [text-chunk kind]]] + (if (<= limit total-length) + (reduced [total-length acc]) + (let [chunk-len (count text-chunk) + cut-chunk-len (min chunk-len (- limit total-length)) + cut-chunk (if (= chunk-len cut-chunk-len) + text-chunk + (core-utils/truncate-str text-chunk cut-chunk-len))] + [(+ total-length cut-chunk-len) + (conj acc + (if (= :text kind) + cut-chunk + [react/text (into {:key idx} (lookup-props text-chunk message kind)) + cut-chunk]))]))) + [0 []]) + second + seq)) diff --git a/src/status_im/ui/screens/desktop/main/chat/views.cljs b/src/status_im/ui/screens/desktop/main/chat/views.cljs index 200c0b3c76..17c9dec12f 100644 --- a/src/status_im/ui/screens/desktop/main/chat/views.cljs +++ b/src/status_im/ui/screens/desktop/main/chat/views.cljs @@ -110,9 +110,9 @@ :on-select #(when (message-sent? user-statuses current-public-key) (re-frame/dispatch [:chat.ui/reply-to-message message-id old-message-id]))}])))} (let [collapsible? (and (:should-collapse? content) group-chat) - message-text (cond-> (:text content) - (and collapsible? (not expanded?)) - (core-utils/truncate-str constants/chars-collapse-threshold))] + char-limit (if (and collapsible? (not expanded?)) + constants/chars-collapse-threshold constants/desktop-msg-chars-hard-limit) + message-text (core-utils/truncate-str (:text content) char-limit)] [react/view {:style styles/message-container} (when (:response-to content) [quoted-message (:response-to content) false current-public-key]) @@ -120,11 +120,7 @@ :selectable true :selection-color colors/blue-light} (if-let [render-recipe (:render-recipe content)] - (apply - (if (and collapsible? (not expanded?)) - chat-utils/render-chunks-desktop - chat-utils/render-chunks) - render-recipe message-text) + (chat-utils/render-chunks-desktop char-limit render-recipe message-text) message-text)] (when collapsible? [message/expand-button expanded? chat-id message-id])])]]) @@ -186,8 +182,6 @@ ^{:key (str "datemark" message-id)} [message.datemark/chat-datemark value] (when (contains? constants/desktop-content-types content-type) - (when (nil? message-id) - (log/debug "nil?" message)) (reagent.core/create-class {:component-did-mount #(when (and message-id