Skip to content

Commit

Permalink
Add account selector to manage dapps and persist when offline
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrkhalil committed Aug 29, 2022
1 parent c56ebf7 commit 0ebb126
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
47 changes: 36 additions & 11 deletions src/status_im/ui/screens/wallet/manage_connections/views.cljs
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
(ns status-im.ui.screens.wallet.manage-connections.views
(:require [re-frame.core :as re-frame]
(:require [quo.core :as quo]
[quo.design-system.colors :as colors]
[quo.react-native :as rn]
[re-frame.core :as re-frame]
[reagent.core :as reagent]
[status-im.constants :as constants]
[status-im.ui.components.icons.icons :as icons]
[status-im.ui.components.react :as react]
[status-im.ui.screens.wallet-connect.session-proposal.views :refer [account-selector]]
[status-im.ui.screens.wallet.manage-connections.styles :as styles]
[quo.core :as quo]
[quo.design-system.colors :as colors]
[status-im.ui.components.icons.icons :as icons]
[status-im.utils.utils :as utils]))

(defn print-session-info [session]
(defn account-selector-sheet [{:keys [wc-version] :as session} visible-accounts show-account-selector active-account-atom]
(let [{:keys [topic]} (when-not (= wc-version constants/wallet-connect-version-1) session)
update-fn #(re-frame/dispatch (if (= wc-version constants/wallet-connect-version-1)
[:wallet-connect-legacy/change-session-account session @active-account-atom]
[:wallet-connect/change-session-account topic @active-account-atom]))]
(when @show-account-selector
[rn/view
[account-selector visible-accounts active-account-atom update-fn]])))

(defn print-session-info [session visible-accounts show-account-selector]
(let [peer-meta (get-in session [:params 0 :peerMeta])
peer-id (get-in session [:params 0 :peerId])
name (get peer-meta :name)
url (get peer-meta :url)
account (get-in session [:params 0 :accounts 0])
account (get-in session [:params 0 :accounts 0])
icons (get peer-meta :icons)
dapp-name (get-in session [:params 0 :peerMeta :name])
dapp-url (get-in session [:params 0 :peerMeta :url])
_ (re-frame/dispatch [:wallet-connect-legacy/save-session {:peer-id peer-id
:connector (:connector session)
:dapp-name dapp-name
:dapp-url dapp-url}])
icon-uri (first (status-im.utils.utils/exclude-svg-resources icons))
visible-accounts @(re-frame/subscribe [:visible-accounts-without-watch-only])
selected-account (first (filter
#(= account
(:address %))
Expand All @@ -41,12 +58,20 @@
:height 20
:container-style {:elevation 3}
:color colors/red}]]

(when selected-account ;; The account might not be available in theory, if deleted
[rn/view {:style (styles/selected-account-container (:color selected-account))}
[rn/touchable-opacity {:style (styles/selected-account-container (:color selected-account))
:on-press #(swap! show-account-selector not)}
[rn/text {:style styles/selected-account} (:name selected-account)]])]]]]))

(defn views []
(let [legacy-sessions @(re-frame/subscribe [:wallet-connect-legacy/sessions])]
(let [legacy-sessions @(re-frame/subscribe [:wallet-connect-legacy/sessions])
visible-accounts @(re-frame/subscribe [:visible-accounts-without-watch-only])]
[rn/view {:margin-top 10}
(doall (map print-session-info legacy-sessions))]))
(doall (map-indexed (fn [idx session]
(let [account-addr (get-in session [:params 0 :accounts 0])
active-account-atom (reagent/atom @(re-frame/subscribe [:account-by-address account-addr]))
show-account-selector (reagent/atom false)]
[rn/view {:key idx}
[print-session-info session visible-accounts show-account-selector]
[account-selector-sheet session visible-accounts show-account-selector active-account-atom]]))
legacy-sessions))]))
26 changes: 18 additions & 8 deletions src/status_im/wallet_connect_legacy/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,27 @@
(:wallet-connect-legacy/sessions db))]
{:wc-1-clean-up-sessions connectors}))

(fx/defn save-session
{:events [:wallet-connect-legacy/save-session]}
[{:keys [db]} {:keys [peer-id dapp-name dapp-url connector]}]
(let [info (.stringify js/JSON (.-session connector))]
{:db db
::json-rpc/call [{:method "wakuext_addWalletConnectSession"
:params [{:id peer-id
:info info
;; info will be the updated value
:dappName dapp-name
:dappUrl dapp-url}]
:on-success #(log/info "wakuext_addWalletConnectSession success call back , data =>" %)
:on-error #(log/error "wakuext_addWalletConnectSession error call back , data =>" %)}]}))

(fx/defn session-connected
{:events [:wallet-connect-legacy/created]}
[{:keys [db]} session]
(let [connector (get db :wallet-connect-legacy/proposal-connector)
session (assoc (types/js->clj session)
:wc-version constants/wallet-connect-version-1
:connector connector)
info (.stringify js/JSON (.-session connector))
peer-id (get-in session [:params 0 :peerId])
dapp-name (get-in session [:params 0 :peerMeta :name])
dapp-url (get-in session [:params 0 :peerMeta :url])]
Expand All @@ -159,13 +172,10 @@
(update :wallet-connect-legacy/sessions
conj
session))
::json-rpc/call [{:method "wakuext_addWalletConnectSession"
:params [{:id peer-id
:info info
:dappName dapp-name
:dappUrl dapp-url}]
:on-success #(log/info "wakuext_addWalletConnectSession success call back , data =>" %)
:on-error #(log/error "wakuext_addWalletConnectSession error call back , data =>" %)}]}))
::save-session {:peer-id peer-id
:dapp-name dapp-name
:connector connector
:dapp-url dapp-url}}))

(fx/defn manage-app
{:events [:wallet-connect-legacy/manage-app]}
Expand Down

0 comments on commit 0ebb126

Please sign in to comment.