Skip to content

Commit

Permalink
Check chains
Browse files Browse the repository at this point in the history
  • Loading branch information
Rende11 committed Nov 9, 2023
1 parent 6d2fd2c commit 51fe6b5
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,70 +1,106 @@
(ns status-im2.contexts.wallet.common.sheets.network-preferences.view
(:require [quo.core :as quo]
(:require [clojure.string :as string]
[quo.core :as quo]
[quo.foundations.colors :as colors]
[quo.foundations.resources :as resources]
[quo.theme :as quo.theme]
[reagent.core :as reagent]
[status-im2.contexts.wallet.common.sheets.network-preferences.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))

(defn- mainnet
[account-color]
[{:title "Mainnet"
:image :icon-avatar
:image-props {:icon (resources/get-network :ethereum)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color account-color}}])

(defn- networks-list
[account-color]
[{:title "Optimism"
:image :icon-avatar
:image-props {:icon (resources/get-network :optimism)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color account-color}}
{:title "Arbitrum"
:image :icon-avatar
:image-props {:icon (resources/get-network :arbitrum)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color account-color}}])
(defn- make-network-item
[{:keys [network-name] :as _network}
{:keys [color on-change network-preferences] :as _options}]
{:key (name network-name)
:title (string/capitalize (name network-name))
:image :icon-avatar
:image-props {:icon (resources/get-network network-name)
:size :size-20}
:action :selector
:action-props {:type :checkbox
:customization-color color
:checked? (contains? network-preferences network-name)
:on-change on-change}})

(defn- view-internal
[{:keys [on-save theme]}]
(let [{:keys [color address]} (rf/sub [:wallet/current-viewing-account])]
[:<>
[quo/drawer-top
{:title (i18n/label :t/network-preferences)
:description (i18n/label :t/network-preferences-desc)}]
[quo/data-item
{:status :default
:size :default
:description :default
:label :none
:blur? false
:card? true
:title (i18n/label :t/address)
:subtitle address
:container-style (merge style/data-item
{:background-color (colors/theme-colors colors/neutral-2_5
colors/neutral-90
theme)})}]
[quo/category
{:list-type :settings
:data (mainnet color)}]
[quo/category
{:list-type :settings
:label (i18n/label :t/layer-2)
:data (networks-list color)}]
[quo/bottom-actions
{:button-one-label (i18n/label :t/update)
:button-one-props {:disabled? true
:on-press on-save
:customization-color color}}]]))
[]
(let [{:keys [color address network-preferences]} (rf/sub [:wallet/current-viewing-account])
network-preferences-state (reagent/atom network-preferences)
toggle-network (fn [network-name]
(if (contains? @network-preferences-state
network-name)
(swap! network-preferences-state disj
network-name)
(swap! network-preferences-state conj
network-name)))]
(fn [{:keys [on-save theme]}]
(let [network-details (rf/sub [:wallet/network-details])
mainnet (first network-details)
layer-2-networks (rest network-details)
current-networks (->> network-details
(filter
#(contains? @network-preferences-state (:network-name %))))]
[:<>
[quo/drawer-top
{:title (i18n/label :t/network-preferences)
:description (i18n/label :t/network-preferences-desc)}]
[quo/data-item
{:status :default
:size :default
:description :default
:label :none
:blur? false
:card? true
:title (i18n/label :t/address)
:custom-subtitle (fn []
[quo/address-text
{:networks current-networks
:address address
:format :long}])
:container-style (merge style/data-item
{:background-color (colors/theme-colors colors/neutral-2_5
colors/neutral-90
theme)})}]

[quo/category
{:list-type :settings
:data [(make-network-item mainnet
{:color color
:network-preferences @network-preferences-state
:on-change #(toggle-network (:network-name
mainnet))})]}]
[quo/category
{:list-type :settings
:label (i18n/label :t/layer-2)
:data (mapv #(make-network-item %
{:color color
:network-preferences @network-preferences-state
:on-change (fn [_]
(toggle-network (:network-name
%)))})
layer-2-networks)}]
[quo/bottom-actions
{:button-one-label (i18n/label :t/update)
:button-one-props {:disabled? true
:on-press on-save
:customization-color color}}]]))))

(def view (quo.theme/with-theme view-internal))


(comment
;; Temp
(rf/sub [:profile/profile])
(rf/sub [:wallet/accounts])
(rf/sub [:wallet])
(rf/sub [:wallet/network-details])
(rf/sub [:wallet/networks])
(rf/sub [:wallet/balances])
(rf/sub [:wallet/current-viewing-account])

(rf/sub [:wallet/account-networks])

(make-network-item (first (rf/sub [:wallet/network-details])) {})
)
28 changes: 20 additions & 8 deletions src/status_im2/subs/wallet/wallet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
token)]
result))

(defn- filter-networks
[chain-ids network-details]
(filter (fn [{:keys [chain-id]}]
(contains? chain-ids chain-id))
network-details))

(re-frame/reg-sub
:wallet/accounts
:<- [:wallet]
Expand All @@ -51,16 +57,22 @@
:wallet/current-viewing-account
:<- [:wallet]
:<- [:wallet/balances]
(fn [[{:keys [current-viewing-account-address] :as wallet} balances]]
(-> wallet
(get-in [:accounts current-viewing-account-address])
(assoc :balance (utils/get-balance-by-address balances current-viewing-account-address)))))
:<- [:wallet/network-details]
(fn [[{:keys [current-viewing-account-address] :as wallet} balances network-details]]
(let [current-viewing-account (get-in wallet [:accounts current-viewing-account-address])
preferred-chain-ids (:prod-preferred-chain-ids current-viewing-account)
network-preferences (->> network-details
(filter-networks preferred-chain-ids)
(map :network-name)
(set))]
(-> current-viewing-account
(assoc :balance (utils/get-balance-by-address balances current-viewing-account-address))
(assoc :network-preferences network-preferences)))))

(re-frame/reg-sub
:wallet/account-networks
:<- [:wallet/current-viewing-account]
:<- [:wallet/network-details]
(fn [[account networks] _]
(let [account-chains (:prod-preferred-chain-ids account)]
(filter (fn [{:keys [chain-id]}]
(contains? account-chains chain-id)) networks))))
(fn [[account network-details] _]
(let [preferred-chain-ids (:prod-preferred-chain-ids account)]
(filter-networks preferred-chain-ids network-details))))

0 comments on commit 51fe6b5

Please sign in to comment.