Skip to content

Commit 6940ccd

Browse files
committed
fix: composer mention key & edit re-enter issues
1 parent 6397f3e commit 6940ccd

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/status_im2/contexts/chat/composer/effects.cljs

+21-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[react-native.core :as rn]
77
[react-native.platform :as platform]
88
[react-native.reanimated :as reanimated]
9+
[reagent.core :as reagent]
910
[status-im2.contexts.chat.composer.constants :as constants]
1011
[status-im2.contexts.chat.composer.keyboard :as kb]
1112
[status-im2.contexts.chat.composer.utils :as utils]
@@ -105,29 +106,34 @@
105106

106107
(defn use-edit
107108
[{:keys [input-ref]}
108-
{:keys [text-value saved-cursor-position]}
109+
{:keys [text-value saved-cursor-position cursor-position]}
109110
{:keys [edit input-with-mentions]}
110111
messages-list-on-layout-finished?]
111-
(let [mention? (some #(= :mention (first %)) (seq input-with-mentions))]
112+
(let [mention? (some #(= :mention (first %)) (seq input-with-mentions))
113+
composer-just-opened? (not @messages-list-on-layout-finished?)]
112114
(rn/use-effect
113115
(fn []
114116
(let [mention-text (reduce (fn [acc item]
115117
(str acc (second item)))
116118
""
117119
input-with-mentions)
118-
edit-text (if mention? mention-text (get-in edit [:content :text]))
119-
text-value-count (count @text-value)
120+
edit-text (cond
121+
mention? mention-text
122+
;; NOTE: using text-value for cases when the user
123+
;; leaves the app with an unfinished edit and re-opens
124+
;; the chat.
125+
(and (seq @text-value) composer-just-opened?)
126+
@text-value
127+
:else (get-in edit [:content :text]))
128+
selection-pos (count edit-text)
120129
inject-edit-text (fn []
121130
(reset! text-value edit-text)
122-
(reset! saved-cursor-position (if (seq edit-text)
123-
(count edit-text)
124-
text-value-count))
131+
(reset! cursor-position selection-pos)
132+
(reset! saved-cursor-position selection-pos)
125133
(when @input-ref
126-
(let [selection-pos (count edit-text)]
127-
(.setNativeProps ^js @input-ref
128-
(clj->js {:text edit-text
129-
:selection {:start selection-pos
130-
:end selection-pos}})))))]
134+
(.setNativeProps ^js @input-ref
135+
(clj->js {:text edit-text}))))]
136+
131137
(when (and edit @input-ref)
132138
;; NOTE: A small setTimeout is necessary to ensure the focus is enqueued and is executed
133139
;; ASAP. Check https://github.com/software-mansion/react-native-screens/issues/472
@@ -139,9 +145,9 @@
139145
;; `on-content-size-change` will animate the height of the input based on the injected
140146
;; text.
141147
(js/setTimeout #(do (when @messages-list-on-layout-finished? (.focus ^js @input-ref))
142-
(js/setTimeout inject-edit-text 250))
143-
100))))
144-
[(:message-id edit) mention?])))
148+
(reagent/next-tick inject-edit-text))
149+
600))))
150+
[(:message-id edit)])))
145151

146152
(defn use-reply
147153
[{:keys [input-ref]}

src/status_im2/contexts/chat/messages/list/view.cljs

+9-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,15 @@
412412
;;TODO(rasom) https://github.com/facebook/react-native/issues/30034
413413
:inverted (when platform/ios? true)
414414
:on-layout (fn [e]
415-
(reset! messages-list-on-layout-finished? true)
415+
;; FIXME: the 1s timeout is to assure all effects with
416+
;; timeouts that depend on the value are considered.
417+
;; Hacky, but we're heavily relying on timeouts in the
418+
;; composer and need to react to differently (e.g.
419+
;; inside effects/use-edit) when the chat has just
420+
;; opened and the subsequent times.
421+
(js/setTimeout #(reset! messages-list-on-layout-finished?
422+
true)
423+
1000)
416424
(let [layout-height (oops/oget e
417425
"nativeEvent.layout.height")]
418426
(reset! messages-view-height layout-height)))

0 commit comments

Comments
 (0)