<commit_before>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns app.main.ui.workspace.libraries
  (:require
   [rumext.alpha :as mf]
   [cuerdas.core :as str]
   [app.util.dom :as dom]
   [app.util.i18n :as i18n :refer [t tr]]
   [app.util.data :refer [classnames matches-search]]
   [app.main.store :as st]
   [app.main.refs :as refs]
   [app.main.data.workspace :as dw]
   [app.main.ui.icons :as i]
   [app.main.ui.modal :as modal]))

(mf/defc libraries-tab
  [{:keys [file libraries shared-files] :as props}]
  (let [search-term (mf/use-state "")

        sorted-libraries (->> (vals libraries)
                              (sort-by #(str/lower (:name %))))

        filtered-files   (->> shared-files
                              (filter #(not= (:id %) (:id file)))
                              (filter #(nil? (get libraries (:id %))))
                              (filter #(matches-search (:name %) @search-term))
                              (sort-by #(str/lower (:name %))))

        on-search-term-change
        (mf/use-callback
         (fn [event]
           (let [value (-> (dom/get-target event)
                           (dom/get-value))]
             (reset! search-term value))))

        on-search-clear
        (mf/use-callback
         (fn [event]
           (reset! search-term "")))

        link-library
        (mf/use-callback (mf/deps file) #(st/emit! (dw/link-file-to-library (:id file) %)))

        unlink-library
        (mf/use-callback (mf/deps file) #(st/emit! (dw/unlink-file-from-library (:id file) %)))

        contents-str
        (fn [library graphics-count colors-count]
          ;; Include a &nbsp; so this block has always some content
          (str
           (str/join " · "
                     (cond-> []
                       (< 0 graphics-count)
                       (conj (tr "workspace.libraries.graphics" graphics-count))

                       (< 0 colors-count)
                       (conj (tr "workspace.libraries.colors" colors-count))))
           "\u00A0"))]
    [:*
     [:div.section
      [:div.section-title (tr "workspace.libraries.in-this-file")]
      [:div.section-list
       [:div.section-list-item
        [:div.item-name (tr "workspace.libraries.file-library")]
        [:div.item-contents (contents-str file
                                          (count (:media-objects file))
                                          (count (:colors file)))]]
       (for [library sorted-libraries]
         [:div.section-list-item {:key (:id library)}
          [:div.item-name (:name library)]
          [:div.item-contents (contents-str library
                                            (count (:media-objects library))
                                            (count (:colors library)))]
          [:input.item-button {:type "button"
                               :value (tr "workspace.libraries.remove")
                               :on-click #(unlink-library (:id library))}]])
       ]]
     [:div.section
      [:div.section-title (tr "workspace.libraries.shared-libraries")]
      [:div.libraries-search
       [:input.search-input
        {:placeholder (tr "workspace.libraries.search-shared-libraries")
         :type "text"
         :value @search-term
         :on-change on-search-term-change}]
       (if (str/empty? @search-term)
         [:div.search-icon
          i/search]
         [:div.search-icon.search-close
          {:on-click on-search-clear}
          i/close])]
      (if (> (count filtered-files) 0)
        [:div.section-list
         (for [file filtered-files]
           [:div.section-list-item {:key (:id file)}
            [:div.item-name (:name file)]
            [:div.item-contents (contents-str file
                                              (:graphics-count file)
                                              (:colors-count file))]
            [:input.item-button {:type "button"
                                 :value (tr "workspace.libraries.add")
                                 :on-click #(link-library (:id file))}]])]
        [:div.section-list-empty
         i/library
         (if (str/empty? @search-term)
           (tr "workspace.libraries.no-shared-libraries-available")
           (tr "workspace.libraries.no-matches-for" @search-term))])]]))


(mf/defc updates-tab
  []
  [:div])


(mf/defc libraries-dialog
  {::mf/register modal/components
   ::mf/register-as :libraries-dialog}
  [{:keys [] :as ctx}]
  (let [selected-tab (mf/use-state :libraries)

        locale       (mf/deref i18n/locale)
        project      (mf/deref refs/workspace-project)
        file         (mf/deref refs/workspace-file)
        libraries    (mf/deref refs/workspace-libraries)
        shared-files (mf/deref refs/workspace-shared-files)

        change-tab   #(reset! selected-tab %)
        close        #(modal/hide!)]
<commit_msg>:bug: Fixes counters in libraries popup<commit_after>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns app.main.ui.workspace.libraries
  (:require
   [rumext.alpha :as mf]
   [cuerdas.core :as str]
   [okulary.core :as l]
   [app.util.dom :as dom]
   [app.util.i18n :as i18n :refer [t tr]]
   [app.util.data :refer [classnames matches-search]]
   [app.main.store :as st]
   [app.main.refs :as refs]
   [app.main.data.workspace :as dw]
   [app.main.ui.icons :as i]
   [app.main.ui.modal :as modal]))

(def workspace-file
  (l/derived :workspace-file st/state))

(mf/defc libraries-tab
  [{:keys [file libraries shared-files] :as props}]
  (let [search-term (mf/use-state "")

        sorted-libraries (->> (vals libraries)
                              (sort-by #(str/lower (:name %))))

        filtered-files   (->> shared-files
                              (filter #(not= (:id %) (:id file)))
                              (filter #(nil? (get libraries (:id %))))
                              (filter #(matches-search (:name %) @search-term))
                              (sort-by #(str/lower (:name %))))

        on-search-term-change
        (mf/use-callback
         (fn [event]
           (let [value (-> (dom/get-target event)
                           (dom/get-value))]
             (reset! search-term value))))

        on-search-clear
        (mf/use-callback
         (fn [event]
           (reset! search-term "")))

        link-library
        (mf/use-callback (mf/deps file) #(st/emit! (dw/link-file-to-library (:id file) %)))

        unlink-library
        (mf/use-callback (mf/deps file) #(st/emit! (dw/unlink-file-from-library (:id file) %)))

        contents-str
        (fn [library]
          (let [graphics-count (count (get-in library [:data :media] []))
                colors-count (count (get-in library [:data :colors] []))]
            ;; Include a &nbsp; so this block has always some content
            (str
             (str/join " · "
                       (cond-> []
                         (< 0 graphics-count)
                         (conj (tr "workspace.libraries.graphics" graphics-count))

                         (< 0 colors-count)
                         (conj (tr "workspace.libraries.colors" colors-count))))
             "\u00A0")))]
    [:*
     [:div.section
      [:div.section-title (tr "workspace.libraries.in-this-file")]
      [:div.section-list
       [:div.section-list-item
        [:div.item-name (tr "workspace.libraries.file-library")]
        [:div.item-contents (contents-str file)]]
       (for [library sorted-libraries]
         [:div.section-list-item {:key (:id library)}
          [:div.item-name (:name library)]
          [:div.item-contents (contents-str library)]
          [:input.item-button {:type "button"
                               :value (tr "workspace.libraries.remove")
                               :on-click #(unlink-library (:id library))}]])
       ]]
     [:div.section
      [:div.section-title (tr "workspace.libraries.shared-libraries")]
      [:div.libraries-search
       [:input.search-input
        {:placeholder (tr "workspace.libraries.search-shared-libraries")
         :type "text"
         :value @search-term
         :on-change on-search-term-change}]
       (if (str/empty? @search-term)
         [:div.search-icon
          i/search]
         [:div.search-icon.search-close
          {:on-click on-search-clear}
          i/close])]
      (if (> (count filtered-files) 0)
        [:div.section-list
         (for [file filtered-files]
           [:div.section-list-item {:key (:id file)}
            [:div.item-name (:name file)]
            [:div.item-contents (contents-str file)]
            [:input.item-button {:type "button"
                                 :value (tr "workspace.libraries.add")
                                 :on-click #(link-library (:id file))}]])]
        [:div.section-list-empty
         i/library
         (if (str/empty? @search-term)
           (tr "workspace.libraries.no-shared-libraries-available")
           (tr "workspace.libraries.no-matches-for" @search-term))])]]))


(mf/defc updates-tab
  []
  [:div])


(mf/defc libraries-dialog
  {::mf/register modal/components
   ::mf/register-as :libraries-dialog}
  [{:keys [] :as ctx}]
  (let [selected-tab (mf/use-state :libraries)

        locale       (mf/deref i18n/locale)
        project      (mf/deref refs/workspace-project)
        file         (mf/deref workspace-file)
        libraries    (mf/deref refs/workspace-libraries)
        shared-files (mf/deref refs/workspace-shared-files)

        change-tab   #(reset! selected-tab %)
        close        #(modal/hide!)]

{'fraction_non_alphanumeric': 0.15237144283574638, 'fraction_numerical': 0.004792810783824264, 'mean_word_length': 1.6990029641606037, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219619', 'n_tokens_mistral': 3188, 'n_tokens_neox': 2997, 'n_words': 674}