 (ns braid.ui.views.pages.tags
   (:require [reagent.core :as r]
+            [reagent.ratom :include-macros true :refer-macros [reaction]]
             [chat.client.reagent-adapter :refer [subscribe]]
             [chat.client.store :as store]
             [chat.client.dispatcher :refer [dispatch!]]

  []
  (let [group-id (subscribe [:open-group-id])
        tags (subscribe [:tags])
        sorted-tags (->> @tags
                         (filter (fn [t] (= @group-id (t :group-id))))
                         (sort-by :threads-count)
                         reverse)
        subscribed-tag-ids (subscribe [:user-subscribed-tag-ids])
        subscribed-to-tag? (fn [tag-id]
                             (contains? @subscribed-tag-ids tag-id))]
    (fn []
      [:div.page.channels
       [:div.title "Tags"]

       [:div.content
        [new-tag-view {:group-id @group-id}]

          (when (seq @subscribed-tags)
            [:div
             [:h2 "Subscribed"]
             [:div.tags
              (doall
                (for [tag @subscribed-tags]
                  ^{:key (tag :id)}
                  [tag-info-view tag]))]])

          (when (seq @recommended-tags)
            [:div
             [:h2 "Recommended"]
             [:div.tags
              (doall
                (for [tag @recommended-tags]
                  ^{:key (tag :id)}
                  [tag-info-view tag]))]])]])))
