mirror of
https://github.com/status-im/status-react.git
synced 2025-02-17 05:16:46 +00:00
[#6643] Allow chat command suggestions to select parameter
Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
parent
f1b457677c
commit
5dd345fe49
@ -44,22 +44,6 @@
|
|||||||
[{:keys [type]} command-message]
|
[{:keys [type]} command-message]
|
||||||
(protocol/preview type command-message))
|
(protocol/preview type command-message))
|
||||||
|
|
||||||
(defn- prepare-params
|
|
||||||
"Prepares parameters sequence of command by providing suggestion components with
|
|
||||||
selected-event injected with correct arg indexes and `last-arg?` flag."
|
|
||||||
[command]
|
|
||||||
(let [parameters (protocol/parameters command)
|
|
||||||
last-param-idx (dec (count parameters))]
|
|
||||||
(into []
|
|
||||||
(map-indexed (fn [idx {:keys [suggestions] :as param}]
|
|
||||||
(if suggestions
|
|
||||||
(update param :suggestions partial
|
|
||||||
(fn [value]
|
|
||||||
[:chat.ui/set-command-parameter
|
|
||||||
(= idx last-param-idx) idx value]))
|
|
||||||
param))
|
|
||||||
parameters))))
|
|
||||||
|
|
||||||
(defn- add-exclusive-choices [initial-scope exclusive-choices]
|
(defn- add-exclusive-choices [initial-scope exclusive-choices]
|
||||||
(reduce (fn [scopes-set exclusive-choices]
|
(reduce (fn [scopes-set exclusive-choices]
|
||||||
(reduce (fn [scopes-set scope]
|
(reduce (fn [scopes-set scope]
|
||||||
@ -83,7 +67,7 @@
|
|||||||
(let [id->command (reduce (fn [acc command]
|
(let [id->command (reduce (fn [acc command]
|
||||||
(assoc acc (command-id command)
|
(assoc acc (command-id command)
|
||||||
{:type command
|
{:type command
|
||||||
:params (prepare-params command)}))
|
:params (into [] (protocol/parameters command))}))
|
||||||
{}
|
{}
|
||||||
commands)
|
commands)
|
||||||
access-scope->command-id (reduce-kv (fn [acc command-id {:keys [type]}]
|
access-scope->command-id (reduce-kv (fn [acc command-id {:keys [type]}]
|
||||||
|
@ -31,10 +31,9 @@
|
|||||||
|
|
||||||
;; common `send/request` functionality
|
;; common `send/request` functionality
|
||||||
|
|
||||||
(defn- render-asset [selected-event-creator]
|
(defn- render-asset [{:keys [name symbol amount decimals] :as asset}]
|
||||||
(fn [{:keys [name symbol amount decimals] :as asset}]
|
|
||||||
[react/touchable-highlight
|
[react/touchable-highlight
|
||||||
{:on-press #(re-frame/dispatch (selected-event-creator (wallet.utils/display-symbol asset)))}
|
{:on-press #(re-frame/dispatch [:chat.ui/set-command-parameter (wallet.utils/display-symbol asset)])}
|
||||||
[react/view transactions-styles/asset-container
|
[react/view transactions-styles/asset-container
|
||||||
[react/view transactions-styles/asset-main
|
[react/view transactions-styles/asset-main
|
||||||
[react/image {:source (-> asset :icon :source)
|
[react/image {:source (-> asset :icon :source)
|
||||||
@ -45,12 +44,11 @@
|
|||||||
;;TODO(goranjovic) : temporarily disabled to fix https://github.com/status-im/status-react/issues/4963
|
;;TODO(goranjovic) : temporarily disabled to fix https://github.com/status-im/status-react/issues/4963
|
||||||
;;until the resolution of https://github.com/status-im/status-react/issues/4972
|
;;until the resolution of https://github.com/status-im/status-react/issues/4972
|
||||||
#_[react/text {:style transactions-styles/asset-balance}
|
#_[react/text {:style transactions-styles/asset-balance}
|
||||||
(str (money/internal->formatted amount symbol decimals))]]]))
|
(str (money/internal->formatted amount symbol decimals))]]])
|
||||||
|
|
||||||
(defn- render-nft-asset [selected-event-creator]
|
(defn- render-nft-asset [{:keys [name symbol amount] :as asset}]
|
||||||
(fn [{:keys [name symbol amount] :as asset}]
|
|
||||||
[react/touchable-highlight
|
[react/touchable-highlight
|
||||||
{:on-press #(re-frame/dispatch (selected-event-creator (clojure.core/name symbol)))}
|
{:on-press #(re-frame/dispatch [:chat.ui/set-command-parameter (clojure.core/name symbol)])}
|
||||||
[react/view transactions-styles/asset-container
|
[react/view transactions-styles/asset-container
|
||||||
[react/view transactions-styles/asset-main
|
[react/view transactions-styles/asset-main
|
||||||
[react/image {:source (-> asset :icon :source)
|
[react/image {:source (-> asset :icon :source)
|
||||||
@ -59,11 +57,11 @@
|
|||||||
[react/text {:style {:font-size 16
|
[react/text {:style {:font-size 16
|
||||||
:color colors/gray
|
:color colors/gray
|
||||||
:padding-right 14}}
|
:padding-right 14}}
|
||||||
(money/to-fixed amount)]]]))
|
(money/to-fixed amount)]]])
|
||||||
|
|
||||||
(def assets-separator [react/view transactions-styles/asset-separator])
|
(def assets-separator [react/view transactions-styles/asset-separator])
|
||||||
|
|
||||||
(defview choose-asset [nft? selected-event-creator]
|
(defview choose-asset [nft?]
|
||||||
(letsubs [assets [:wallet/visible-assets-with-amount]]
|
(letsubs [assets [:wallet/visible-assets-with-amount]]
|
||||||
[react/view
|
[react/view
|
||||||
[list/flat-list {:data (filter #(if nft?
|
[list/flat-list {:data (filter #(if nft?
|
||||||
@ -72,18 +70,15 @@
|
|||||||
assets)
|
assets)
|
||||||
:key-fn (comp name :symbol)
|
:key-fn (comp name :symbol)
|
||||||
:render-fn (if nft?
|
:render-fn (if nft?
|
||||||
(render-nft-asset selected-event-creator)
|
render-nft-asset
|
||||||
(render-asset selected-event-creator))
|
render-asset)
|
||||||
:enableEmptySections true
|
:enableEmptySections true
|
||||||
:separator assets-separator
|
:separator assets-separator
|
||||||
:keyboardShouldPersistTaps :always
|
:keyboardShouldPersistTaps :always
|
||||||
:bounces false}]]))
|
:bounces false}]]))
|
||||||
|
|
||||||
(defn choose-asset-suggestion [selected-event-creator]
|
(defn choose-asset-suggestion []
|
||||||
[choose-asset false selected-event-creator])
|
[choose-asset false])
|
||||||
|
|
||||||
(defn choose-nft-asset-suggestion [selected-event-creator]
|
|
||||||
[choose-asset true selected-event-creator])
|
|
||||||
|
|
||||||
(defn personal-send-request-short-preview
|
(defn personal-send-request-short-preview
|
||||||
[label-key {:keys [content]}]
|
[label-key {:keys [content]}]
|
||||||
@ -104,7 +99,7 @@
|
|||||||
:type :number
|
:type :number
|
||||||
:placeholder (i18n/label :t/send-request-amount)}])
|
:placeholder (i18n/label :t/send-request-amount)}])
|
||||||
|
|
||||||
(defview choose-nft-token [selected-event-creator]
|
(defview choose-nft-token []
|
||||||
(letsubs [{:keys [input-params]} [:chats/selected-chat-command]
|
(letsubs [{:keys [input-params]} [:chats/selected-chat-command]
|
||||||
collectibles [:collectibles]]
|
collectibles [:collectibles]]
|
||||||
(let [collectible-tokens (get collectibles (keyword (:symbol input-params)))]
|
(let [collectible-tokens (get collectibles (keyword (:symbol input-params)))]
|
||||||
@ -115,7 +110,7 @@
|
|||||||
(fn [[id {:keys [name image_url]}]]
|
(fn [[id {:keys [name image_url]}]]
|
||||||
[react/touchable-highlight
|
[react/touchable-highlight
|
||||||
{:key id
|
{:key id
|
||||||
:on-press #(re-frame/dispatch (selected-event-creator (str id)))}
|
:on-press #(re-frame/dispatch [:chat.ui/set-command-parameter (str id)])}
|
||||||
[react/view {:flex-direction :column
|
[react/view {:flex-direction :column
|
||||||
:align-items :center
|
:align-items :center
|
||||||
:margin-left 10
|
:margin-left 10
|
||||||
@ -130,9 +125,6 @@
|
|||||||
[react/text {} name]]])
|
[react/text {} name]]])
|
||||||
collectible-tokens)])))
|
collectible-tokens)])))
|
||||||
|
|
||||||
(defn choose-nft-token-suggestion [selected-event-creator]
|
|
||||||
[choose-nft-token selected-event-creator])
|
|
||||||
|
|
||||||
(defview nft-token [{{:keys [name image_url]} :token}]
|
(defview nft-token [{{:keys [name image_url]} :token}]
|
||||||
[react/view {:flex-direction :column
|
[react/view {:flex-direction :column
|
||||||
:align-items :center}
|
:align-items :center}
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
[status-im.utils.utils :as utils]
|
[status-im.utils.utils :as utils]
|
||||||
[taoensso.timbre :as log]
|
[taoensso.timbre :as log]
|
||||||
[status-im.utils.datetime :as time]
|
[status-im.utils.datetime :as time]
|
||||||
|
[status-im.chat.commands.core :as commands]
|
||||||
[status-im.chat.models.loading :as chat-loading]))
|
[status-im.chat.models.loading :as chat-loading]))
|
||||||
|
|
||||||
;; init module
|
;; init module
|
||||||
@ -679,16 +680,25 @@
|
|||||||
(fn [cofx [_ message-id]]
|
(fn [cofx [_ message-id]]
|
||||||
(chat.input/reply-to-message cofx message-id)))
|
(chat.input/reply-to-message cofx message-id)))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
|
||||||
:chat.ui/set-command-parameter
|
|
||||||
(fn [cofx [_ last-param? index value]]
|
|
||||||
(commands.input/set-command-parameter cofx last-param? index value)))
|
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:chat.ui/send-current-message
|
:chat.ui/send-current-message
|
||||||
(fn [cofx _]
|
(fn [cofx _]
|
||||||
(chat.input/send-current-message cofx)))
|
(chat.input/send-current-message cofx)))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
:chat.ui/set-command-parameter
|
||||||
|
(fn [{{:keys [chats current-chat-id chat-ui-props id->command access-scope->command-id]} :db :as cofx} [_ value]]
|
||||||
|
(let [current-chat (get chats current-chat-id)
|
||||||
|
selection (get-in chat-ui-props [current-chat-id :selection])
|
||||||
|
commands (commands/chat-commands id->command access-scope->command-id current-chat)
|
||||||
|
{:keys [current-param-position params]} (commands.input/selected-chat-command
|
||||||
|
(:input-text current-chat) selection commands)
|
||||||
|
last-param-idx (dec (count params))]
|
||||||
|
(commands.input/set-command-parameter cofx
|
||||||
|
(= current-param-position last-param-idx)
|
||||||
|
current-param-position
|
||||||
|
value))))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:chat/disable-cooldown
|
:chat/disable-cooldown
|
||||||
(fn [cofx _]
|
(fn [cofx _]
|
||||||
|
@ -134,6 +134,11 @@
|
|||||||
(when timeout
|
(when timeout
|
||||||
{:timeout-ms timeout}))}))
|
{:timeout-ms timeout}))}))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
:extensions.chat.command/set-parameter
|
||||||
|
(fn [_ [_ _ {:keys [value]}]]
|
||||||
|
{:dispatch [:chat.ui/set-command-parameter value]}))
|
||||||
|
|
||||||
(defn operation->fn [k]
|
(defn operation->fn [k]
|
||||||
(case k
|
(case k
|
||||||
:plus +
|
:plus +
|
||||||
@ -207,9 +212,7 @@
|
|||||||
;'list {:value list :properties {:data :vector :item-view :view}}
|
;'list {:value list :properties {:data :vector :item-view :view}}
|
||||||
'checkbox {:value checkbox :properties {:on-change :event :checked :boolean}}
|
'checkbox {:value checkbox :properties {:on-change :event :checked :boolean}}
|
||||||
'nft-token-viewer {:value transactions/nft-token :properties {:token :string}}
|
'nft-token-viewer {:value transactions/nft-token :properties {:token :string}}
|
||||||
'transaction-status {:value transactions/transaction-status :properties {:outgoing :string :tx-hash :string}}
|
'transaction-status {:value transactions/transaction-status :properties {:outgoing :string :tx-hash :string}}}
|
||||||
'asset-selector {:value transactions/choose-nft-asset-suggestion}
|
|
||||||
'token-selector {:value transactions/choose-nft-token-suggestion}}
|
|
||||||
:queries {'identity {:value :extensions/identity :arguments {:value :map}}
|
:queries {'identity {:value :extensions/identity :arguments {:value :map}}
|
||||||
'store/get {:value :store/get :arguments {:key :string}}
|
'store/get {:value :store/get :arguments {:key :string}}
|
||||||
'wallet/collectibles {:value :get-collectible-token :arguments {:token :string :symbol :string}}}
|
'wallet/collectibles {:value :get-collectible-token :arguments {:token :string :symbol :string}}}
|
||||||
@ -217,6 +220,10 @@
|
|||||||
{:permissions [:read]
|
{:permissions [:read]
|
||||||
:value :alert
|
:value :alert
|
||||||
:arguments {:value :string}}
|
:arguments {:value :string}}
|
||||||
|
'chat.command/set-parameter
|
||||||
|
{:permissions [:read]
|
||||||
|
:value :extensions.chat.command/set-parameter
|
||||||
|
:arguments {:value :string}}
|
||||||
'log
|
'log
|
||||||
{:permissions [:read]
|
{:permissions [:read]
|
||||||
:value :log
|
:value :log
|
||||||
|
@ -70,17 +70,6 @@
|
|||||||
(is (= TestCommandInstance
|
(is (= TestCommandInstance
|
||||||
(get-in fx [:db :id->command
|
(get-in fx [:db :id->command
|
||||||
(core/command-id TestCommandInstance) :type]))))
|
(core/command-id TestCommandInstance) :type]))))
|
||||||
(testing "Suggestions for parameters are injected with correct selection events"
|
|
||||||
(is (= [:chat.ui/set-command-parameter false 0 "first-value"]
|
|
||||||
((get-in fx [:db :id->command
|
|
||||||
(core/command-id TestCommandInstance) :params
|
|
||||||
0 :suggestions])
|
|
||||||
"first-value")))
|
|
||||||
(is (= [:chat.ui/set-command-parameter true 2 "last-value"]
|
|
||||||
((get-in fx [:db :id->command
|
|
||||||
(core/command-id TestCommandInstance) :params
|
|
||||||
2 :suggestions])
|
|
||||||
"last-value"))))
|
|
||||||
(testing "Access scope indexes are correctly created"
|
(testing "Access scope indexes are correctly created"
|
||||||
(is (contains? (get-in fx [:db :access-scope->command-id #{:personal-chats}])
|
(is (contains? (get-in fx [:db :access-scope->command-id #{:personal-chats}])
|
||||||
(core/command-id TestCommandInstance)))
|
(core/command-id TestCommandInstance)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user