chore: small refactor to remove some uses of status-im in status-im2 (#16358)

This commit is contained in:
Jamie Caprani 2023-06-27 15:11:07 +01:00 committed by GitHub
parent cebc50d409
commit e0e693791f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 64 deletions

View File

@ -11,17 +11,18 @@
[status-im2.contexts.chat.composer.link-preview.events :as link-preview] [status-im2.contexts.chat.composer.link-preview.events :as link-preview]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.i18n :as i18n] [utils.i18n :as i18n]
[utils.re-frame :as rf])) [utils.re-frame :as rf]
[utils.string :as utils.string]))
(defn text->emoji (defn text->emoji
"Replaces emojis in a specified `text`" "Replaces emojis in a specified `text`"
[text] [text]
(utils/safe-replace text (utils.string/safe-replace text
#":([a-z_\-+0-9]*):" #":([a-z_\-+0-9]*):"
(fn [[original emoji-id]] (fn [[original emoji-id]]
(if-let [emoji-map (object/get (.-lib emojis) emoji-id)] (if-let [emoji-map (object/get (.-lib emojis) emoji-id)]
(.-char ^js emoji-map) (.-char ^js emoji-map)
original)))) original))))
;; effects ;; effects
(re-frame/reg-fx (re-frame/reg-fx
@ -169,7 +170,7 @@
{:chat-id chat-id {:chat-id chat-id
:album-id album-id :album-id album-id
:content-type constants/content-type-image :content-type constants/content-type-image
:image-path (utils/safe-replace resized-uri #"file://" "") :image-path (utils.string/safe-replace resized-uri #"file://" "")
:image-width width :image-width width
:image-height height :image-height height
:text input-text :text input-text

View File

@ -16,7 +16,8 @@
[status-im.ui.components.topbar :as topbar] [status-im.ui.components.topbar :as topbar]
[status-im.ui.screens.wallet.components.views :as components] [status-im.ui.screens.wallet.components.views :as components]
[status-im.utils.utils :as utils] [status-im.utils.utils :as utils]
[utils.debounce :as debounce]) [utils.debounce :as debounce]
[utils.string :as utils.string])
(:require-macros [status-im.utils.views :as views])) (:require-macros [status-im.utils.views :as views]))
(defn- recipient-topbar (defn- recipient-topbar
@ -304,7 +305,7 @@
(re-frame/dispatch [:set-in [:wallet/recipient :searching] (re-frame/dispatch [:set-in [:wallet/recipient :searching]
:searching]) :searching])
(debounce/debounce-and-dispatch [:wallet.recipient/address-changed (debounce/debounce-and-dispatch [:wallet.recipient/address-changed
(utils/safe-trim %)] (utils.string/safe-trim %)]
600)) 600))
:accessibility-label :recipient-address-input}]] :accessibility-label :recipient-address-input}]]
[react/view {:align-items :center :height 30 :padding-bottom 8} [react/view {:align-items :center :height 30 :padding-bottom 8}

View File

@ -116,16 +116,6 @@
(gstring/format (str "%." places "f") amount) (gstring/format (str "%." places "f") amount)
(or (str amount) 0)))) (or (str amount) 0))))
(defn safe-trim
[s]
(when (string? s)
(string/trim s)))
(defn safe-replace
[s m r]
(when (string? s)
(string/replace s m r)))
(defn safe-nth (defn safe-nth
[coll index] [coll index]
(when (number? index) (when (number? index)

View File

@ -11,7 +11,8 @@
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.utils.utils :as utils] [status-im.utils.utils :as utils]
[status-im2.common.json-rpc.events :as json-rpc] [status-im2.common.json-rpc.events :as json-rpc]
[status-im2.navigation.events :as navigation])) [status-im2.navigation.events :as navigation]
[utils.string :as utils.string]))
;;NOTE we want to handle only last resolve ;;NOTE we want to handle only last resolve
(def resolve-last-id (atom nil)) (def resolve-last-id (atom nil))
@ -37,7 +38,7 @@
[{:keys [db] :as cofx} raw-recipient id] [{:keys [db] :as cofx} raw-recipient id]
(when (or (not id) (= id @resolve-last-id)) (when (or (not id) (= id @resolve-last-id))
(reset! resolve-last-id nil) (reset! resolve-last-id nil)
(let [recipient (utils/safe-trim raw-recipient)] (let [recipient (utils.string/safe-trim raw-recipient)]
(cond (cond
(ethereum/address? recipient) (ethereum/address? recipient)
(let [checksum (eip55/address->checksum recipient)] (let [checksum (eip55/address->checksum recipient)]

View File

@ -4,28 +4,10 @@
[react-native.gesture :as gesture] [react-native.gesture :as gesture]
[react-native.reanimated :as reanimated] [react-native.reanimated :as reanimated]
[reagent.core :as reagent] [reagent.core :as reagent]
[status-im.utils.utils :as utils.utils] [react-native.background-timer :as background-timer]
[status-im2.common.toasts.style :as style] [status-im2.common.toasts.style :as style]
[utils.re-frame :as rf])) [utils.re-frame :as rf]))
;; (def ^:private slide-out-up-animation
;; (-> ^js reanimated/slide-out-up-animation
;; .springify
;; (.damping 20)
;; (.stiffness 300)))
;; (def ^:private slide-in-up-animation
;; (-> ^js reanimated/slide-in-up-animation
;; .springify
;; (.damping 20)
;; (.stiffness 300)))
;; (def ^:private linear-transition
;; (-> ^js reanimated/linear-transition
;; .springify
;; (.damping 20)
;; (.stiffness 300)))
(defn toast (defn toast
[id] [id]
(let [{:keys [type] :as toast-opts} (rf/sub [:toasts/toast id])] (let [{:keys [type] :as toast-opts} (rf/sub [:toasts/toast id])]
@ -33,42 +15,47 @@
[quo/notification toast-opts] [quo/notification toast-opts]
[quo/toast toast-opts]))) [quo/toast toast-opts])))
(defn reset-translate-y
[translate-y]
(reanimated/animate-shared-value-with-spring translate-y
0
{:mass 1
:damping 20
:stiffness 300}))
(defn dismiss
[translate-y dismissed-locally? close!]
(reanimated/animate-shared-value-with-spring
translate-y
-500
{:mass 1 :damping 20 :stiffness 300})
(reset! dismissed-locally? true)
(close!))
(defn f-container (defn f-container
[id] [id]
(let [dismissed-locally? (reagent/atom false) (let [dismissed-locally? (reagent/atom false)
close! #(rf/dispatch [:toasts/close id]) close! #(rf/dispatch [:toasts/close id])
timer (reagent/atom nil) timer (reagent/atom nil)
clear-timer #(utils.utils/clear-timeout @timer)] clear-timer #(background-timer/clear-timeout @timer)]
(fn [] (fn []
(let [duration (or (rf/sub [:toasts/toast-cursor id :duration]) 3000) (let [duration (or (rf/sub [:toasts/toast-cursor id :duration]) 3000)
on-dismissed (or (rf/sub [:toasts/toast-cursor id :on-dismissed]) identity) on-dismissed (or (rf/sub [:toasts/toast-cursor id :on-dismissed]) identity)
create-timer (fn [] create-timer (fn []
(reset! timer (utils.utils/set-timeout close! duration))) (reset! timer (background-timer/set-timeout close! duration)))
translate-y (reanimated/use-shared-value 0) translate-y (reanimated/use-shared-value 0)
pan pan
(-> (->
(gesture/gesture-pan) (gesture/gesture-pan)
;; remove timer on pan start
(gesture/on-start clear-timer) (gesture/on-start clear-timer)
(gesture/on-update (gesture/on-update
(fn [^js evt] (fn [^js evt]
(let [evt-translation-y (.-translationY evt)] (let [evt-translation-y (.-translationY evt)
pan-down? (> evt-translation-y 100)
pan-up? (< evt-translation-y -30)]
(cond (cond
;; reset translate y on pan down pan-down? (reset-translate-y translate-y)
(> evt-translation-y 100) pan-up? (dismiss translate-y dismissed-locally? close!)
(reanimated/animate-shared-value-with-spring translate-y
0
{:mass 1
:damping 20
:stiffness 300})
;; dismiss on pan up
(< evt-translation-y -30)
(do (reanimated/animate-shared-value-with-spring
translate-y
-500
{:mass 1 :damping 20 :stiffness 300})
(reset! dismissed-locally? true)
(close!))
:else :else
(reanimated/set-shared-value translate-y (reanimated/set-shared-value translate-y
evt-translation-y))))) evt-translation-y)))))

View File

@ -10,8 +10,8 @@
[status-im2.navigation.events :as navigation] [status-im2.navigation.events :as navigation]
[utils.validators :as validators] [utils.validators :as validators]
[status-im2.contexts.contacts.events :as data-store.contacts] [status-im2.contexts.contacts.events :as data-store.contacts]
[status-im.utils.utils :as utils] [status-im2.constants :as constants]
[status-im2.constants :as constants])) [utils.string :as utils.string]))
(defn init-contact (defn init-contact
"Create a new contact (persisted to app-db as [:contacts/new-identity]). "Create a new contact (persisted to app-db as [:contacts/new-identity]).
@ -37,7 +37,7 @@
(defn ->id (defn ->id
[{:keys [input] :as contact}] [{:keys [input] :as contact}]
(let [trimmed-input (utils/safe-trim input)] (let [trimmed-input (utils.string/safe-trim input)]
(->> {:id (if (empty? trimmed-input) (->> {:id (if (empty? trimmed-input)
nil nil
(if-some [[_ id] (re-matches url-regex trimmed-input)] (if-some [[_ id] (re-matches url-regex trimmed-input)]

View File

@ -1,4 +1,5 @@
(ns utils.string) (ns utils.string
(:require [clojure.string :as string]))
(defn truncate-str-memo (defn truncate-str-memo
"Given string and max threshold, trims the string to threshold length with `...` "Given string and max threshold, trims the string to threshold length with `...`
@ -38,3 +39,13 @@
(defn at-least-n-chars? (defn at-least-n-chars?
[s n] [s n]
(>= (count s) n)) (>= (count s) n))
(defn safe-trim
[s]
(when (string? s)
(string/trim s)))
(defn safe-replace
[s m r]
(when (string? s)
(string/replace s m r)))