Unable to enter/clear smoothly the data into contacts field #19384 (#19385)

This commit is contained in:
flexsurfer 2024-03-25 15:17:08 +01:00 committed by GitHub
parent 32a4bb1153
commit 06dc9ea1d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 60 additions and 52 deletions

View File

@ -3,7 +3,6 @@
[quo.core :as quo] [quo.core :as quo]
[react-native.clipboard :as clipboard] [react-native.clipboard :as clipboard]
[react-native.core :as rn] [react-native.core :as rn]
[reagent.core :as reagent]
[status-im.contexts.chat.home.add-new-contact.style :as style] [status-im.contexts.chat.home.add-new-contact.style :as style]
[utils.address :as address] [utils.address :as address]
[utils.debounce :as debounce] [utils.debounce :as debounce]
@ -51,24 +50,40 @@
(defn- search-input (defn- search-input
[] []
(reagent/with-let [input-value (reagent/atom nil) (let [[input-value set-input-value] (rn/use-state nil)
input-ref (atom nil) input-ref (rn/use-ref-atom nil)
clear-input (fn [] on-ref (rn/use-callback #(reset! input-ref %))
(reset! input-value nil) {:keys [scanned]
(rf/dispatch [:contacts/clear-new-identity])) contact-public-key :id} (rf/sub [:contacts/new-identity])
paste-on-input #(clipboard/get-string
(fn [clipboard-text]
(reset! input-value clipboard-text)
(rf/dispatch [:contacts/set-new-identity
{:input clipboard-text}])))]
(let [{:keys [scanned] contact-public-key :id} (rf/sub [:contacts/new-identity])
contact-public-key-or-scanned (or contact-public-key scanned) contact-public-key-or-scanned (or contact-public-key scanned)
empty-input? (and (string/blank? @input-value) empty-input? (and (string/blank? input-value)
(string/blank? contact-public-key-or-scanned))] (string/blank? contact-public-key-or-scanned))
clear-input (rn/use-callback
(fn []
(set-input-value nil)
(rf/dispatch [:contacts/clear-new-identity])))
paste-on-input (rn/use-callback
(fn []
(clipboard/get-string
(fn [clipboard-text]
(set-input-value clipboard-text)
(rf/dispatch [:contacts/set-new-identity
{:input clipboard-text}])))))
on-change-text (rn/use-callback
(fn [new-text]
(set-input-value new-text)
(if (string/blank? contact-public-key-or-scanned)
(debounce/debounce-and-dispatch [:contacts/set-new-identity
{:input new-text}]
600)
(rf/dispatch-sync [:contacts/set-new-identity
{:input new-text}])))
[contact-public-key-or-scanned])]
(rn/use-unmount #(rf/dispatch [:contacts/clear-new-identity]))
[rn/view {:style style/input-and-scan-container} [rn/view {:style style/input-and-scan-container}
[quo/input [quo/input
{:accessibility-label :enter-contact-code-input {:accessibility-label :enter-contact-code-input
:ref #(reset! input-ref %) :ref on-ref
:container-style {:flex 1} :container-style {:flex 1}
:auto-capitalize :none :auto-capitalize :none
:multiline? true :multiline? true
@ -81,27 +96,20 @@
:button (when empty-input? :button (when empty-input?
{:on-press paste-on-input {:on-press paste-on-input
:text (i18n/label :t/paste)}) :text (i18n/label :t/paste)})
;; NOTE: `contact-public-key-or-scanned` has priority over `@input-value`, ;; NOTE: `contact-public-key-or-scanned` has priority over `input-value`,
;; we clean it when the input is updated so that it's `nil` and ;; we clean it when the input is updated so that it's `nil` and
;; `@input-value`is shown. To fastly clean it, we use `dispatch-sync`. ;; `input-value`is shown. To fastly clean it, we use `dispatch-sync`.
;; This call could be avoided if `::qr-scanner/scan-code` were able to ;; This call could be avoided if `::qr-scanner/scan-code` were able to
;; receive a callback function, not only a re-frame event as callback. ;; receive a callback function, not only a re-frame event as callback.
:value (or contact-public-key-or-scanned @input-value) :value (or contact-public-key-or-scanned input-value)
:on-change-text (fn [new-text] :on-change-text on-change-text}]
(reset! input-value new-text)
(as-> [:contacts/set-new-identity {:input new-text}] $
(if (string/blank? contact-public-key-or-scanned)
(debounce/debounce-and-dispatch $ 600)
(rf/dispatch-sync $))))}]
[rn/view {:style style/scan-button-container} [rn/view {:style style/scan-button-container}
[quo/button [quo/button
{:type :outline {:type :outline
:icon-only? true :icon-only? true
:size 40 :size 40
:on-press #(rf/dispatch [:open-modal :scan-profile-qr-code])} :on-press #(rf/dispatch [:open-modal :scan-profile-qr-code])}
:i/scan]]]) :i/scan]]]))
(finally
(rf/dispatch [:contacts/clear-new-identity]))))
(defn- invalid-text (defn- invalid-text
[message] [message]