[#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]
|
||||
(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]
|
||||
(reduce (fn [scopes-set exclusive-choices]
|
||||
(reduce (fn [scopes-set scope]
|
||||
|
@ -83,7 +67,7 @@
|
|||
(let [id->command (reduce (fn [acc command]
|
||||
(assoc acc (command-id command)
|
||||
{:type command
|
||||
:params (prepare-params command)}))
|
||||
:params (into [] (protocol/parameters command))}))
|
||||
{}
|
||||
commands)
|
||||
access-scope->command-id (reduce-kv (fn [acc command-id {:keys [type]}]
|
||||
|
|
|
@ -31,39 +31,37 @@
|
|||
|
||||
;; common `send/request` functionality
|
||||
|
||||
(defn- render-asset [selected-event-creator]
|
||||
(fn [{:keys [name symbol amount decimals] :as asset}]
|
||||
[react/touchable-highlight
|
||||
{:on-press #(re-frame/dispatch (selected-event-creator (wallet.utils/display-symbol asset)))}
|
||||
[react/view transactions-styles/asset-container
|
||||
[react/view transactions-styles/asset-main
|
||||
[react/image {:source (-> asset :icon :source)
|
||||
:style transactions-styles/asset-icon}]
|
||||
[react/text {:style transactions-styles/asset-symbol}
|
||||
(wallet.utils/display-symbol asset)]
|
||||
[react/text {:style transactions-styles/asset-name} name]]
|
||||
;;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
|
||||
#_[react/text {:style transactions-styles/asset-balance}
|
||||
(str (money/internal->formatted amount symbol decimals))]]]))
|
||||
(defn- render-asset [{:keys [name symbol amount decimals] :as asset}]
|
||||
[react/touchable-highlight
|
||||
{: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-main
|
||||
[react/image {:source (-> asset :icon :source)
|
||||
:style transactions-styles/asset-icon}]
|
||||
[react/text {:style transactions-styles/asset-symbol}
|
||||
(wallet.utils/display-symbol asset)]
|
||||
[react/text {:style transactions-styles/asset-name} name]]
|
||||
;;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
|
||||
#_[react/text {:style transactions-styles/asset-balance}
|
||||
(str (money/internal->formatted amount symbol decimals))]]])
|
||||
|
||||
(defn- render-nft-asset [selected-event-creator]
|
||||
(fn [{:keys [name symbol amount] :as asset}]
|
||||
[react/touchable-highlight
|
||||
{:on-press #(re-frame/dispatch (selected-event-creator (clojure.core/name symbol)))}
|
||||
[react/view transactions-styles/asset-container
|
||||
[react/view transactions-styles/asset-main
|
||||
[react/image {:source (-> asset :icon :source)
|
||||
:style transactions-styles/asset-icon}]
|
||||
[react/text {:style transactions-styles/asset-symbol} name]]
|
||||
[react/text {:style {:font-size 16
|
||||
:color colors/gray
|
||||
:padding-right 14}}
|
||||
(money/to-fixed amount)]]]))
|
||||
(defn- render-nft-asset [{:keys [name symbol amount] :as asset}]
|
||||
[react/touchable-highlight
|
||||
{: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-main
|
||||
[react/image {:source (-> asset :icon :source)
|
||||
:style transactions-styles/asset-icon}]
|
||||
[react/text {:style transactions-styles/asset-symbol} name]]
|
||||
[react/text {:style {:font-size 16
|
||||
:color colors/gray
|
||||
:padding-right 14}}
|
||||
(money/to-fixed amount)]]])
|
||||
|
||||
(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]]
|
||||
[react/view
|
||||
[list/flat-list {:data (filter #(if nft?
|
||||
|
@ -72,18 +70,15 @@
|
|||
assets)
|
||||
:key-fn (comp name :symbol)
|
||||
:render-fn (if nft?
|
||||
(render-nft-asset selected-event-creator)
|
||||
(render-asset selected-event-creator))
|
||||
render-nft-asset
|
||||
render-asset)
|
||||
:enableEmptySections true
|
||||
:separator assets-separator
|
||||
:keyboardShouldPersistTaps :always
|
||||
:bounces false}]]))
|
||||
|
||||
(defn choose-asset-suggestion [selected-event-creator]
|
||||
[choose-asset false selected-event-creator])
|
||||
|
||||
(defn choose-nft-asset-suggestion [selected-event-creator]
|
||||
[choose-asset true selected-event-creator])
|
||||
(defn choose-asset-suggestion []
|
||||
[choose-asset false])
|
||||
|
||||
(defn personal-send-request-short-preview
|
||||
[label-key {:keys [content]}]
|
||||
|
@ -104,7 +99,7 @@
|
|||
:type :number
|
||||
: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]
|
||||
collectibles [:collectibles]]
|
||||
(let [collectible-tokens (get collectibles (keyword (:symbol input-params)))]
|
||||
|
@ -115,7 +110,7 @@
|
|||
(fn [[id {:keys [name image_url]}]]
|
||||
[react/touchable-highlight
|
||||
{: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
|
||||
:align-items :center
|
||||
:margin-left 10
|
||||
|
@ -130,9 +125,6 @@
|
|||
[react/text {} name]]])
|
||||
collectible-tokens)])))
|
||||
|
||||
(defn choose-nft-token-suggestion [selected-event-creator]
|
||||
[choose-nft-token selected-event-creator])
|
||||
|
||||
(defview nft-token [{{:keys [name image_url]} :token}]
|
||||
[react/view {:flex-direction :column
|
||||
:align-items :center}
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
[status-im.utils.utils :as utils]
|
||||
[taoensso.timbre :as log]
|
||||
[status-im.utils.datetime :as time]
|
||||
[status-im.chat.commands.core :as commands]
|
||||
[status-im.chat.models.loading :as chat-loading]))
|
||||
|
||||
;; init module
|
||||
|
@ -679,16 +680,25 @@
|
|||
(fn [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
|
||||
:chat.ui/send-current-message
|
||||
(fn [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
|
||||
:chat/disable-cooldown
|
||||
(fn [cofx _]
|
||||
|
|
|
@ -134,6 +134,11 @@
|
|||
(when 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]
|
||||
(case k
|
||||
:plus +
|
||||
|
@ -207,9 +212,7 @@
|
|||
;'list {:value list :properties {:data :vector :item-view :view}}
|
||||
'checkbox {:value checkbox :properties {:on-change :event :checked :boolean}}
|
||||
'nft-token-viewer {:value transactions/nft-token :properties {:token :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}}
|
||||
'transaction-status {:value transactions/transaction-status :properties {:outgoing :string :tx-hash :string}}}
|
||||
:queries {'identity {:value :extensions/identity :arguments {:value :map}}
|
||||
'store/get {:value :store/get :arguments {:key :string}}
|
||||
'wallet/collectibles {:value :get-collectible-token :arguments {:token :string :symbol :string}}}
|
||||
|
@ -217,6 +220,10 @@
|
|||
{:permissions [:read]
|
||||
:value :alert
|
||||
:arguments {:value :string}}
|
||||
'chat.command/set-parameter
|
||||
{:permissions [:read]
|
||||
:value :extensions.chat.command/set-parameter
|
||||
:arguments {:value :string}}
|
||||
'log
|
||||
{:permissions [:read]
|
||||
:value :log
|
||||
|
|
|
@ -70,17 +70,6 @@
|
|||
(is (= TestCommandInstance
|
||||
(get-in fx [:db :id->command
|
||||
(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"
|
||||
(is (contains? (get-in fx [:db :access-scope->command-id #{:personal-chats}])
|
||||
(core/command-id TestCommandInstance)))
|
||||
|
|
Loading…
Reference in New Issue