From ef7d977299b70d750db54e945b097156624b678a Mon Sep 17 00:00:00 2001 From: Mohsen Date: Mon, 20 Jan 2025 19:41:40 +0300 Subject: [PATCH] [#21305] fix: sort assets by value and collectibles by name (#21908) * [#21305] fix: sort assets by value and collectibles by name * [#21305] fix: use seq to check name is nil or empty * [#21305] fix: normalize text to sort properly --- .../contexts/wallet/collectible/utils.cljs | 12 +++++- .../wallet/collectible/utils_test.cljs | 15 +++++++ .../wallet/common/asset_list/view.cljs | 14 ++----- .../contexts/wallet/common/utils.cljs | 10 +++-- .../contexts/wallet/common/utils_test.cljs | 30 +++++++++++++ src/status_im/subs/wallet/collectibles.cljs | 9 +++- src/status_im/subs/wallet/wallet.cljs | 42 +++++++++++-------- 7 files changed, 98 insertions(+), 34 deletions(-) diff --git a/src/status_im/contexts/wallet/collectible/utils.cljs b/src/status_im/contexts/wallet/collectible/utils.cljs index 3c4a6cc5f7..3de8eccdd0 100644 --- a/src/status_im/contexts/wallet/collectible/utils.cljs +++ b/src/status_im/contexts/wallet/collectible/utils.cljs @@ -1,5 +1,6 @@ (ns status-im.contexts.wallet.collectible.utils - (:require [status-im.config :as config] + (:require [clojure.string :as string] + [status-im.config :as config] [status-im.constants :as constants] [status-im.contexts.wallet.common.utils.networks :as network-utils] [taoensso.timbre :as log] @@ -79,3 +80,12 @@ acc))) {}) vals)) + +(defn sort-collectibles-by-name + [collectibles] + (sort-by (fn [collectible] + (let [name (-> collectible :collectible-data :name) + normalized-name (some-> name + string/lower-case)] + [(if (seq normalized-name) 0 1) normalized-name])) + collectibles)) diff --git a/src/status_im/contexts/wallet/collectible/utils_test.cljs b/src/status_im/contexts/wallet/collectible/utils_test.cljs index 2fad8fb3ab..b5fd443ab4 100644 --- a/src/status_im/contexts/wallet/collectible/utils_test.cljs +++ b/src/status_im/contexts/wallet/collectible/utils_test.cljs @@ -44,3 +44,18 @@ :token-id token-id :test-networks-enabled? true}) "https://testnets.opensea.io/assets/optimism-sepolia/0xC/0xT")))) + +(deftest sort-collectibles-by-name-test + (testing "Sorts collectibles by name, moving nil or empty names to the end" + (let [collectibles [{:collectible-data {:name "Alpha"}} + {:collectible-data {:name nil}} + {:collectible-data {:name "Beta"}} + {:collectible-data {:name ""}} + {:collectible-data {:name "Zeta"}}] + sorted-collectibles (utils/sort-collectibles-by-name collectibles) + expected [{:collectible-data {:name "Alpha"}} + {:collectible-data {:name "Beta"}} + {:collectible-data {:name "Zeta"}} + {:collectible-data {:name nil}} + {:collectible-data {:name ""}}]] + (is (= sorted-collectibles expected))))) diff --git a/src/status_im/contexts/wallet/common/asset_list/view.cljs b/src/status_im/contexts/wallet/common/asset_list/view.cljs index 9adf9e5726..7042e4f5fb 100644 --- a/src/status_im/contexts/wallet/common/asset_list/view.cljs +++ b/src/status_im/contexts/wallet/common/asset_list/view.cljs @@ -10,15 +10,11 @@ token-name :name total-balance :total-balance disabled? :bridge-disabled? + fiat-value :fiat-value :as token} _ _ - {:keys [currency currency-symbol on-token-press preselected-token-symbol prices-per-token]}] - (let [fiat-value (utils/calculate-token-fiat-value - {:currency currency - :balance total-balance - :token token - :prices-per-token prices-per-token}) - crypto-formatted (utils/get-standard-crypto-format token total-balance prices-per-token) + {:keys [currency-symbol on-token-press preselected-token-symbol prices-per-token]}] + (let [crypto-formatted (utils/get-standard-crypto-format token total-balance prices-per-token) fiat-formatted (utils/fiat-formatted-for-ui currency-symbol fiat-value)] [quo/token-network {:token token-symbol @@ -38,13 +34,11 @@ (let [filtered-tokens (rf/sub [:wallet/current-viewing-account-tokens-filtered {:query search-text :chain-ids chain-ids}]) - currency (rf/sub [:profile/currency]) currency-symbol (rf/sub [:profile/currency-symbol]) prices-per-token (rf/sub [:wallet/prices-per-token])] [gesture/flat-list {:data filtered-tokens - :render-data {:currency currency - :currency-symbol currency-symbol + :render-data {:currency-symbol currency-symbol :on-token-press on-token-press :preselected-token-symbol preselected-token-symbol :prices-per-token prices-per-token} diff --git a/src/status_im/contexts/wallet/common/utils.cljs b/src/status_im/contexts/wallet/common/utils.cljs index 496facc42d..18ecd88c7a 100644 --- a/src/status_im/contexts/wallet/common/utils.cljs +++ b/src/status_im/contexts/wallet/common/utils.cljs @@ -409,13 +409,15 @@ (defn sort-tokens [tokens] - (let [priority #(get constants/token-sort-priority (:symbol %) ##Inf)] - (sort-by (juxt (comp - :balance) priority) tokens))) + (sort-by (comp - :balance) tokens)) + +(defn sort-tokens-by-fiat-value + [tokens] + (sort-by (comp - :fiat-value) tokens)) (defn sort-tokens-by-name [tokens] - (let [priority #(get constants/token-sort-priority (:symbol %) ##Inf)] - (sort-by (juxt :symbol priority) tokens))) + (sort-by :symbol tokens)) (defn token-with-balance ([token networks] diff --git a/src/status_im/contexts/wallet/common/utils_test.cljs b/src/status_im/contexts/wallet/common/utils_test.cljs index 360f211b23..427f6130b3 100644 --- a/src/status_im/contexts/wallet/common/utils_test.cljs +++ b/src/status_im/contexts/wallet/common/utils_test.cljs @@ -345,3 +345,33 @@ expected []] (is (= (utils/get-accounts-with-token-balance accounts token) expected))))) + +(deftest sort-tokens-test + (testing "Sort tokens by balance in descending order" + (let [tokens [{:symbol "BTC" :balance 2} + {:symbol "ETH" :balance 5} + {:symbol "DAI" :balance 10}] + expected [{:symbol "DAI" :balance 10} + {:symbol "ETH" :balance 5} + {:symbol "BTC" :balance 2}]] + (is (= (utils/sort-tokens tokens) expected))))) + +(deftest sort-tokens-by-fiat-value-test + (testing "Sort tokens by fiat value in descending order" + (let [tokens [{:symbol "BTC" :fiat-value 50000} + {:symbol "ETH" :fiat-value 15000} + {:symbol "DAI" :fiat-value 1000}] + expected [{:symbol "BTC" :fiat-value 50000} + {:symbol "ETH" :fiat-value 15000} + {:symbol "DAI" :fiat-value 1000}]] + (is (= (utils/sort-tokens-by-fiat-value tokens) expected))))) + +(deftest sort-tokens-by-name-test + (testing "Sort tokens by symbol in ascending order" + (let [tokens [{:symbol "ETH"} + {:symbol "DAI"} + {:symbol "BTC"}] + expected [{:symbol "BTC"} + {:symbol "DAI"} + {:symbol "ETH"}]] + (is (= (utils/sort-tokens-by-name tokens) expected))))) diff --git a/src/status_im/subs/wallet/collectibles.cljs b/src/status_im/subs/wallet/collectibles.cljs index 11178d09c9..bd13294c07 100644 --- a/src/status_im/subs/wallet/collectibles.cljs +++ b/src/status_im/subs/wallet/collectibles.cljs @@ -2,6 +2,7 @@ (:require [clojure.string :as string] [re-frame.core :as re-frame] + [status-im.contexts.wallet.collectible.utils :as collectible-utils] [utils.collection])) (defn- filter-collectibles-in-chains @@ -46,7 +47,10 @@ :wallet/current-viewing-account-collectibles :<- [:wallet/current-viewing-account] (fn [current-account] - (-> current-account :collectibles add-collectibles-preview-url))) + (-> current-account + :collectibles + add-collectibles-preview-url + collectible-utils/sort-collectibles-by-name))) (re-frame/reg-sub :wallet/current-viewing-account-collectibles-in-selected-networks @@ -71,7 +75,8 @@ (apply interleave) (remove nil?) (utils.collection/distinct-by :id) - (add-collectibles-preview-url))))) + (add-collectibles-preview-url) + (collectible-utils/sort-collectibles-by-name))))) (re-frame/reg-sub :wallet/owned-collectibles-list-in-selected-networks diff --git a/src/status_im/subs/wallet/wallet.cljs b/src/status_im/subs/wallet/wallet.cljs index 84d40a9ef6..05cca505cd 100644 --- a/src/status_im/subs/wallet/wallet.cljs +++ b/src/status_im/subs/wallet/wallet.cljs @@ -505,30 +505,38 @@ :<- [:wallet/current-viewing-account-or-default] :<- [:wallet/network-details] :<- [:wallet/wallet-send] - (fn [[account networks send-data] [_ {:keys [query chain-ids hide-token-fn]}]] + :<- [:profile/currency] + :<- [:wallet/prices-per-token] + (fn [[account networks send-data currency price-per-token] [_ {:keys [query chain-ids hide-token-fn]}]] (let [tx-type (:tx-type send-data) tokens (->> (:tokens account) (map (fn [token] - (assoc token - :bridge-disabled? (and (= tx-type :tx/bridge) - (send-utils/bridge-disabled? (:symbol - token))) - :networks (cond->> - (network-utils/network-list-with-positive-balance - token - networks) - chain-ids - (filter #(some #{(:chain-id %)} chain-ids))) - :supported-networks (network-utils/network-list token networks) - :available-balance (utils/calculate-total-token-balance token) - :total-balance (utils/calculate-total-token-balance - token - chain-ids)))) + (let [total-balance (utils/calculate-total-token-balance + token + chain-ids)] + (assoc token + :bridge-disabled? (and (= tx-type :tx/bridge) + (send-utils/bridge-disabled? (:symbol + token))) + :networks (cond->> + (network-utils/network-list-with-positive-balance + token + networks) + chain-ids + (filter #(some #{(:chain-id %)} chain-ids))) + :supported-networks (network-utils/network-list token networks) + :available-balance (utils/calculate-total-token-balance token) + :total-balance total-balance + :fiat-value (utils/calculate-token-fiat-value + {:currency currency + :balance total-balance + :token token + :prices-per-token price-per-token}))))) (filter (fn [{:keys [networks]}] (pos? (count networks)))) (remove #(when hide-token-fn (hide-token-fn constants/swap-tokens-my %)))) - sorted-tokens (utils/sort-tokens tokens)] + sorted-tokens (utils/sort-tokens-by-fiat-value tokens)] (if query (let [query-string (string/lower-case query)] (filter #(or (string/starts-with? (string/lower-case (:name %)) query-string)