diff --git a/src/status_im/communities/core.cljs b/src/status_im/communities/core.cljs index ba914f983092..702cd3fd001b 100644 --- a/src/status_im/communities/core.cljs +++ b/src/status_im/communities/core.cljs @@ -61,6 +61,13 @@ {} categ)) +(defn <-featured-communities-rpc + [r] + (set/rename-keys r + {:contractCommunities :contract-communities + :contractFeaturedCommunities :contract-featured-communities + :unknownCommunities :unknown-communities})) + (defn <-rpc [c] (-> c @@ -84,6 +91,28 @@ {} (:communityTokensMetadata c))))) +(defn camel->kebab + [k] + (let [s (name k) + lower-cased (string/lower-case + (string/replace s #"([a-z])([A-Z])" "$1-$2")) + starts-with-digit? (re-matches #"^\d.*" s) + predicate? (some #(string/starts-with? lower-cased %) + ["can-" "is-"])] + (cond starts-with-digit? s + predicate? (keyword (str lower-cased "?")) + :else (keyword lower-cased)))) + +(defn rename-keys + [m] + (reduce (fn [acc [k v]] + (let [new-key (if (keyword? k) (camel->kebab k) k)] + (cond + (map? v) (assoc acc new-key (rename-keys v)) + :else (assoc acc new-key v)))) + {} + m)) + (defn- fetch-community-id-input [{:keys [db]}] (:communities/community-id-input db)) @@ -131,6 +160,17 @@ db communities)}) +(rf/defn handle-contract-communities + {:events [::fetched-contract-communities]} + [{:keys [db]} contract-communities] + (let [cc (rename-keys contract-communities) + featured (:contract-featured-communities cc) + other (remove (set featured) (:contract-communities cc))] + {:db (assoc db + :contract-communities + {:featured (select-keys (:communities cc) featured) + :other (select-keys (:communities cc) other)})})) + (rf/defn handle-my-pending-requests-to-join {:events [:communities/fetched-my-communities-requests-to-join]} [{:keys [db]} my-requests] @@ -307,6 +347,15 @@ (log/error "failed to fetch communities" %) (re-frame/dispatch [::failed-to-fetch %]))}]}) +(rf/defn fetch-contract-communities + [_] + {:json-rpc/call [{:method "wakuext_curatedCommunities" + :params [] + :on-success #(re-frame/dispatch [::fetched-contract-communities %]) + :on-error #(do + (log/error "failed to fetch contract communities" %) + (re-frame/dispatch [::failed-to-fetch %]))}]}) + (rf/defn chat-created {:events [::chat-created]} [_ community-id user-pk] diff --git a/src/status_im/multiaccounts/login/core.cljs b/src/status_im/multiaccounts/login/core.cljs index ade14df6175d..2dd994189b76 100644 --- a/src/status_im/multiaccounts/login/core.cljs +++ b/src/status_im/multiaccounts/login/core.cljs @@ -440,6 +440,7 @@ (initialize-wallet-connect) (get-node-config) (communities/fetch) + (communities/fetch-contract-communities) (communities/fetch-collapsed-community-categories) (communities/check-and-delete-pending-request-to-join) (logging/set-log-level (:log-level multiaccount)) diff --git a/src/status_im2/contexts/communities/discover/view.cljs b/src/status_im2/contexts/communities/discover/view.cljs index 595a13b1d8d4..82c357a7bbd1 100644 --- a/src/status_im2/contexts/communities/discover/view.cljs +++ b/src/status_im2/contexts/communities/discover/view.cljs @@ -88,7 +88,6 @@ :label (i18n/label :t/gated) :accessibility-label :gated-communities-tab}]}]]) - (defn featured-list [communities view-type] (let [view-size (reagent/atom 0)] @@ -158,7 +157,7 @@ [rn/view {:style {:flex 1}} (case @selected-tab :all - (other-communities-list {:communities (rf/sub [:communities/sorted-communities]) + (other-communities-list {:communities (rf/sub [:communities/other-contract-communities]) :view-type view-type}) :open @@ -227,9 +226,9 @@ (defn discover [] - (let [featured-communities (rf/sub [:communities/featured-communities])] + (let [fcc (rf/sub [:communities/featured-contract-communities])] [rn/view {:style (style/discover-screen-container (colors/theme-colors colors/white colors/neutral-95))} - [discover-screen-content featured-communities]])) + [discover-screen-content fcc]])) diff --git a/src/status_im2/subs/communities.cljs b/src/status_im2/subs/communities.cljs index 54a0db8181b3..12e979e7eb41 100644 --- a/src/status_im2/subs/communities.cljs +++ b/src/status_im2/subs/communities.cljs @@ -71,16 +71,16 @@ (sort-by #(visibility-status-utils/visibility-status-order (get % 0))))))) (re-frame/reg-sub - :communities/featured-communities - :<- [:communities] - (fn [communities] - (vals communities))) + :communities/featured-contract-communities + :<- [:contract-communities] + (fn [cc] + (sort-by :name (vals (:featured cc))))) (re-frame/reg-sub - :communities/sorted-communities - :<- [:communities] - (fn [communities] - (sort-by :name (vals communities)))) + :communities/other-contract-communities + :<- [:contract-communities] + (fn [cc] + (sort-by :name (vals (:other cc))))) (re-frame/reg-sub :communities/community-ids diff --git a/src/status_im2/subs/communities_test.cljs b/src/status_im2/subs/communities_test.cljs index ee18f146987c..ff5d08b49297 100644 --- a/src/status_im2/subs/communities_test.cljs +++ b/src/status_im2/subs/communities_test.cljs @@ -78,25 +78,6 @@ :unviewed-mentions-count 0} (rf/sub [sub-name community-id]))))) -(h/deftest-sub :communities/sorted-communities - [sub-name] - (testing "Empty communities list" - (swap! rf-db/app-db assoc - :communities - {}) - (is (= [] - (rf/sub [sub-name])))) - (testing "communities sorted by name" - (swap! rf-db/app-db assoc - :communities - {"0x1" {:id "0x1" :name "Civilized monkeys"} - "0x2" {:id "0x2" :name "Civilized rats"} - "0x3" {:id "0x3" :name "Civilized dolphins"}}) - (is (= [{:id "0x3" :name "Civilized dolphins"} - {:id "0x1" :name "Civilized monkeys"} - {:id "0x2" :name "Civilized rats"}] - (rf/sub [sub-name]))))) - (h/deftest-sub :communities/categorized-channels [sub-name] (testing "Channels with categories" diff --git a/src/status_im2/subs/root.cljs b/src/status_im2/subs/root.cljs index 8140239a315e..9a0c0de6e0b4 100644 --- a/src/status_im2/subs/root.cljs +++ b/src/status_im2/subs/root.cljs @@ -261,6 +261,8 @@ (reg-root-key-sub :communities/collapsed-categories :communities/collapsed-categories) (reg-root-key-sub :communities/selected-tab :communities/selected-tab) +(reg-root-key-sub :contract-communities :contract-communities) + (reg-root-key-sub :activity-center :activity-center) (reg-root-key-sub :bug-report/description-error :bug-report/description-error)