diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 30421af407..0842fe5d3d 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -1821,8 +1821,30 @@ (d/index-by :id)))))) +(defn- library-loaded + "Stores `file`, a library of `file-id`, with its `:is-indirect` flag. A + library can be requested twice when links overlap, once because the + user linked it and once because another library brings it in. The + direct link wins whichever answer lands last. Only a flag recorded for + `file-id` counts: `:files` outlives the workspace, and a library can be + direct in the file visited before and indirect in this one." + [file-id file indirect?] + (ptk/reify ::library-loaded + ptk/UpdateEvent + (update [_ state] + (let [known (get-in state [:files (:id file)]) + known-direct? (and (= file-id (:library-of known)) + (false? (:is-indirect known)))] + (update state :files assoc (:id file) + (assoc file + :library-of file-id + :is-indirect (and indirect? (not known-direct?)))))))) + (defn- load-library-file - [file-id library-id] + "Loads a library of `file-id`. `indirect?` tells whether it is there + only because another library uses it, like the flag the libraries + fetched when the workspace opens carry." + [file-id library-id indirect?] (ptk/reify ::load-library-file ptk/WatchEvent (watch [_ state _] @@ -1832,7 +1854,7 @@ (rx/merge-map fpmap/resolve-file) (rx/mapcat (fn [file] (rx/of - (libraries-fetched file-id [file]))))) + (library-loaded file-id file indirect?))))) (->> (rp/cmd! :get-file-object-thumbnails {:file-id library-id :tag "component"}) (rx/map (fn [thumbnails] (fn [state] @@ -1867,7 +1889,7 @@ (as-> libraries-to-load $ (remove loaded-libraries $) (conj $ library-id) - (map #(load-library-file file-id %) $)))) + (map #(load-library-file file-id % (not= % library-id)) $)))) (rx/catch (fn [cause] (let [error (ex-data cause)] (if (= (:code error) :circular-library-reference) diff --git a/frontend/src/app/main/ui/workspace/libraries.cljs b/frontend/src/app/main/ui/workspace/libraries.cljs index 49f4758437..b2065ebbdd 100644 --- a/frontend/src/app/main/ui/workspace/libraries.cljs +++ b/frontend/src/app/main/ui/workspace/libraries.cljs @@ -40,6 +40,7 @@ [app.main.ui.ds.foundations.typography.text :refer [text*]] [app.main.ui.ds.layout.tab-switcher :refer [tab-switcher*]] [app.main.ui.ds.product.empty-state :refer [empty-state*]] + [app.main.ui.ds.tooltip.tooltip :refer [tooltip*]] [app.main.ui.hooks :as h] [app.main.ui.icons :as deprecated-icon] [app.main.ui.workspace.tokens.import-from-library] @@ -159,6 +160,83 @@ (reduce #(visit %1 %2 nil) $ (sort-by-name libraries)) (first $)))) +(defn- linked-users + [{:keys [connected-to]} libraries-by-id] + (into [] (keep libraries-by-id) connected-to)) + +(defn- removable-users + "The linked libraries to disconnect to release `library`: those that use + it, directly or through other libraries, and that either are linked to + the file directly or are used by no linked library. A directly linked + library stays until it is disconnected, even when another one uses it. + Empty when the libraries only use each other in a cycle." + [library libraries-by-id] + (loop [pending (linked-users library libraries-by-id) + seen #{(:id library)} + removable []] + (if-let [user (first pending)] + (if (contains? seen (:id user)) + (recur (rest pending) seen removable) + (let [users (linked-users user libraries-by-id)] + (recur (into (rest pending) users) + (conj seen (:id user)) + (cond-> removable + (or (empty? users) (false? (:is-indirect user))) + (conj user))))) + removable))) + +(defn library-disconnect-hint + "Why a linked library cannot be disconnected from this file, or nil when + it can. A library that another linked library uses stays connected as + long as that one does. Returns `{:reason :inherited}` for a nested + library, with the name of its parent, and `{:reason :used-by}` for a + direct one, with the name of the first library by name that uses it. + + Both carry `:removable`, the names of the libraries to disconnect to + release this one, sorted by name. The parent can be nested itself, a + library can have several parents, and a parent can be linked directly + as well. In a cycle nothing is removable, and `:removable` holds the + name given in `:library-name`. `libraries-by-id` indexes the linked + libraries by id." + [{:keys [nested? parent-name] :as library} libraries-by-id] + (let [users (linked-users library libraries-by-id)] + (when (seq users) + (let [library-name (if nested? + parent-name + (->> users (sort-by (comp str/lower :name)) first :name)) + removable (->> (removable-users library libraries-by-id) + (map :name) + (sort-by str/lower) + (vec))] + {:reason (if nested? :inherited :used-by) + :library-name library-name + :removable (if (seq removable) removable [library-name])})))) + +(defn disconnect-hint-segments + "Splits a translated disconnect hint template, which holds two `%s` + (the library that keeps this one, then the libraries to disconnect), + into `[:text s]` and `[:name s]` segments in reading order, so the UI + can highlight the library names. Each of `removable` is its own name + segment, separated by `[:text \", \"]`." + [template library-name removable] + (let [[before between after] (vec (.split ^string template "%s")) + names (into [] + (comp (map (fn [n] [:name n])) + (interpose [:text ", "])) + removable)] + (-> [[:text before] [:name library-name] [:text between]] + (into names) + (conj [:text after])))) + +(mf/defc disconnect-hint-text* + {::mf/private true} + [{:keys [template library-name removable]}] + [:* + (for [[idx [kind text]] (map-indexed vector (disconnect-hint-segments template library-name removable))] + (if (= kind :name) + [:span {:key idx :class (stl/css :library-name-highlight)} text] + [:span {:key idx} text]))]) + (defn- describe-library [components-count graphics-count colors-count typography-count] (let [all-zero? (and (zero? components-count) @@ -732,9 +810,9 @@ (map #(assoc % :connected-to (find-connected-to (:id %)))) (sort-linked-libraries))) - linked-libraries-ids + libraries-by-id (mf/with-memo [linked-libraries] - (into #{} d/xf:map-id linked-libraries)) + (d/index-by :id linked-libraries)) unlink-library (mf/use-fn @@ -865,8 +943,10 @@ :class (stl/css :title-spacing-lib) :title-class (stl/css :connected-libraries-title)}] (if (seq linked-libraries) - (for [{:keys [id name data connected-to nested? parent-name] :as library} linked-libraries] - (let [disabled? (some #(contains? linked-libraries-ids %) connected-to) + (for [{:keys [id name data nested? parent-name] :as library} linked-libraries] + (let [hint (library-disconnect-hint library libraries-by-id) + hint-library (:library-name hint) + hint-removable (str/join ", " (:removable hint)) can-import-tokens? (and (has-tokens? library) (cfo/editable-tokens? local-library) (contains? cf/flags :token-import-from-library))] @@ -904,13 +984,31 @@ :variant "secondary" :on-click import-tokens}])) - [:> icon-button* {:type "button" - :aria-label (tr "workspace.libraries.unlink-library-btn") - :icon i/detach - :data-library-id (dm/str id) - :variant "secondary" - :disabled disabled? - :on-click unlink-library}]]])) + (if (some? hint) + [:> tooltip* {:class (stl/css :library-locked) + :tab-index 0 + :data-testid "library-locked" + :aria-label (case (:reason hint) + :inherited (tr "workspace.libraries.connected-through-hint" + hint-library hint-removable) + :used-by (tr "workspace.libraries.used-by" + hint-library hint-removable)) + :content #(mf/html + [:> disconnect-hint-text* + {:template (case (:reason hint) + :inherited (tr "workspace.libraries.connected-through-hint") + :used-by (tr "workspace.libraries.used-by")) + :library-name hint-library + :removable (:removable hint)}])} + [:> icon* {:icon-id (case (:reason hint) + :inherited i/arrow-up + :used-by i/info)}]] + [:> icon-button* {:type "button" + :aria-label (tr "workspace.libraries.unlink-library-btn") + :icon i/detach + :data-library-id (dm/str id) + :variant "secondary" + :on-click unlink-library}])]])) [:div {:class (stl/css :shared-libraries-empty)} [:> text* {:class (stl/css :empty-libraries-text) :as "span" diff --git a/frontend/src/app/main/ui/workspace/libraries.scss b/frontend/src/app/main/ui/workspace/libraries.scss index 067c2b98fb..775e416009 100644 --- a/frontend/src/app/main/ui/workspace/libraries.scss +++ b/frontend/src/app/main/ui/workspace/libraries.scss @@ -308,6 +308,19 @@ gap: var(--sp-xs); } +.library-name-highlight { + color: var(--color-foreground-primary); +} + +.library-locked { + display: flex; + align-items: center; + justify-content: center; + inline-size: $sz-32; + block-size: $sz-32; + color: var(--color-foreground-secondary); +} + .element-count-legacy { white-space: nowrap; diff --git a/frontend/test/frontend_tests/data/workspace_libraries_link_test.cljs b/frontend/test/frontend_tests/data/workspace_libraries_link_test.cljs new file mode 100644 index 0000000000..59f81a03cd --- /dev/null +++ b/frontend/test/frontend_tests/data/workspace_libraries_link_test.cljs @@ -0,0 +1,109 @@ +;; 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) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.data.workspace-libraries-link-test + "The libraries a file links to while it is open, loaded after the + workspace was." + (:require + [app.common.test-helpers.files :as cthf] + [app.common.uuid :as uuid] + [app.main.data.workspace.libraries :as dwl] + [app.main.repo :as rp] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock] + [frontend-tests.helpers.state :as ths] + [potok.v2.core :as ptk])) + +(defn- library-cmd + "An RPC mock for linking a library whose dependencies are `dependencies`." + [dependencies] + (mock/stub + (fn [command params] + (case command + :link-file-to-library (rx/of dependencies) + :get-file (rx/of {:id (:id params) :name "Library" :data {}}) + :get-file-object-thumbnails (rx/of {}))))) + +(t/deftest ^:async linking-a-library-marks-it-direct-and-its-dependencies-indirect + ;; Only the flags fetched when the workspace opens say how a library is + ;; linked. The ones loaded after that must say it too: the Libraries + ;; modal reads it to tell which libraries the user can disconnect. + (let [file (cthf/sample-file :file1) + library (uuid/next) + dependency (uuid/next) + store (ths/setup-store file)] + (await + (mock/with-mocks* + {rp/cmd! (library-cmd [dependency])} + (ptk/emit! store (dwl/link-file-to-library (:id file) library)) + (await (async/wait-for + #(every? (fn [id] (some? (get-in @store [:files id :library-of]))) + [library dependency]) + "both libraries loaded")) + (t/is (false? (get-in @store [:files library :is-indirect]))) + (t/is (true? (get-in @store [:files dependency :is-indirect]))))))) + +(defn- deferred-cmd + "An RPC mock whose :get-file answers only when the test pushes the file + to the subject it records in `gets`, so the test decides the order." + [dependencies gets] + (mock/stub + (fn [command params] + (case command + :link-file-to-library (rx/of (get dependencies (:library-id params))) + :get-file (let [subject (rx/subject)] + (swap! gets conj {:id (:id params) :subject subject}) + subject) + :get-file-object-thumbnails (rx/of {}))))) + +(t/deftest ^:async a-library-loaded-twice-stays-direct + ;; Link a (which uses c), then b (which uses a) before a has loaded: a is + ;; requested twice, once because the user linked it and once because b + ;; brings it in. The user's link has to win whichever answer lands last. + (let [file (cthf/sample-file :file1) + a (uuid/next) + b (uuid/next) + c (uuid/next) + gets (atom []) + store (ths/setup-store file) + calls (fn [id] (filterv #(= id (:id %)) @gets)) + answer (fn [call] + (rx/push! (:subject call) {:id (:id call) :name "Library" :data {}}))] + (await + (mock/with-mocks* + {rp/cmd! (deferred-cmd {a [c] b [a c]} gets)} + (ptk/emit! store (dwl/link-file-to-library (:id file) a)) + (await (async/wait-for #(seq (calls a)) "a requested because it was linked")) + (ptk/emit! store (dwl/link-file-to-library (:id file) b)) + (await (async/wait-for #(= 2 (count (calls a))) "a requested again through b")) + (answer (first (calls a))) + (await (async/wait-for #(some? (get-in @store [:files a :library-of])) "a loaded")) + (answer (second (calls a))) + (await (async/settle)) + (t/is (false? (get-in @store [:files a :is-indirect]))))))) + +(t/deftest ^:async a-direct-flag-from-another-file-does-not-carry-over + ;; :files outlives a workspace, so a library can still be there as a + ;; direct link of the file visited before. In this file it is only + ;; brought in by b. + (let [file (cthf/sample-file :file1) + other (uuid/next) + a (uuid/next) + b (uuid/next) + store (ths/setup-store file {:libraries [{:id a + :library-of other + :is-indirect false}]})] + (await + (mock/with-mocks* + {rp/cmd! (library-cmd [a])} + (ptk/emit! store (dwl/link-file-to-library (:id file) b)) + (await (async/wait-for + #(= (:id file) (get-in @store [:files a :library-of])) + "a loaded for this file")) + (t/is (true? (get-in @store [:files a :is-indirect]))) + (t/is (false? (get-in @store [:files b :is-indirect]))))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index a4911c31d9..0af4b74729 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -44,6 +44,7 @@ [frontend-tests.data.workspace-focus-test] [frontend-tests.data.workspace-interactions-test] [frontend-tests.data.workspace-layout-other-page-test] + [frontend-tests.data.workspace-libraries-link-test] [frontend-tests.data.workspace-libraries-load-test] [frontend-tests.data.workspace-libraries-sync-test] [frontend-tests.data.workspace-mcp-test] @@ -276,6 +277,7 @@ 'frontend-tests.data.workspace-focus-test 'frontend-tests.data.workspace-interactions-test 'frontend-tests.data.workspace-layout-other-page-test + 'frontend-tests.data.workspace-libraries-link-test 'frontend-tests.data.workspace-libraries-load-test 'frontend-tests.data.workspace-libraries-sync-test 'frontend-tests.data.workspace-mcp-test diff --git a/frontend/test/frontend_tests/ui/workspace_libraries_test.cljs b/frontend/test/frontend_tests/ui/workspace_libraries_test.cljs index 1ed82c488f..5e266a2653 100644 --- a/frontend/test/frontend_tests/ui/workspace_libraries_test.cljs +++ b/frontend/test/frontend_tests/ui/workspace_libraries_test.cljs @@ -6,6 +6,7 @@ (ns frontend-tests.ui.workspace-libraries-test (:require + [app.common.data :as d] [app.main.ui.workspace.libraries :as wl] [cljs.test :as t :include-macros true])) @@ -99,3 +100,104 @@ (t/is (= [[:a false nil] [:b false nil]] (summarize (wl/sort-linked-libraries libraries)))))) + +(defn- disconnect-hints + "The disconnect hint of every library, by id, once sorted as the + Libraries modal shows them." + [libraries] + (let [sorted (wl/sort-linked-libraries libraries) + by-id (d/index-by :id sorted)] + (into {} (map (juxt :id #(wl/library-disconnect-hint % by-id))) sorted))) + +(t/deftest library-disconnect-hint-none-when-no-linked-library-uses-it + ;; Only the file uses it, so the user can disconnect it + (t/is (= {:a nil} + (disconnect-hints [(lib :a false [:file])])))) + +(t/deftest library-disconnect-hint-nested-library-is-inherited-from-its-parent + ;; file -> a -> b: b stays while a is connected + (t/is (= {:a nil + :b {:reason :inherited :library-name "a" :removable ["a"]}} + (disconnect-hints [(lib :a false [:file]) + (lib :b true [:a])])))) + +(t/deftest library-disconnect-hint-direct-library-is-used-by-another-one + ;; file -> a, b; a -> b: b is linked directly but a needs it + (t/is (= {:a nil + :b {:reason :used-by :library-name "a" :removable ["a"]}} + (disconnect-hints [(lib :a false [:file]) + (lib :b false [:file :a])])))) + +(t/deftest library-disconnect-hint-names-the-first-user-by-name + (t/is (= {:c {:reason :used-by :library-name "a" :removable ["a" "b"]} + :a nil + :b nil} + (disconnect-hints [(lib :c false [:file :b :a]) + (lib :b false [:file]) + (lib :a false [:file])])))) + +(t/deftest library-disconnect-hint-ignores-users-that-are-not-linked + ;; `other` uses a, but it is not one of the file's libraries + (t/is (= {:a nil} + (disconnect-hints [(lib :a false [:file :other])])))) + +(t/deftest library-disconnect-hint-shared-dependency-lists-every-removable-parent + ;; file -> a, b; a -> c; b -> c: disconnecting a alone leaves c linked + ;; through b, so both have to go + (t/is (= {:a nil + :b nil + :c {:reason :inherited :library-name "a" :removable ["a" "b"]}} + (disconnect-hints [(lib :a false [:file]) + (lib :b false [:file]) + (lib :c true [:a :b])])))) + +(t/deftest library-disconnect-hint-deep-chain-points-to-the-removable-library + ;; file -> a -> b -> c: b is nested too and has no button, so the + ;; removal step names a, the only library the user can disconnect + (t/is (= {:a nil + :b {:reason :inherited :library-name "a" :removable ["a"]} + :c {:reason :inherited :library-name "b" :removable ["a"]}} + (disconnect-hints [(lib :c true [:b]) + (lib :b true [:a]) + (lib :a false [:file])])))) + +(t/deftest library-disconnect-hint-lists-a-directly-linked-intermediate-library + ;; file -> a, b; a -> b -> c: disconnecting a leaves the direct link to b, + ;; which keeps c, so b has to go as well. b only becomes removable once a is gone. + (t/is (= {:a nil + :b {:reason :used-by :library-name "a" :removable ["a"]} + :c {:reason :inherited :library-name "b" :removable ["a" "b"]}} + (disconnect-hints [(lib :a false [:file]) + (lib :b false [:file :a]) + (lib :c true [:b])])))) + +(t/deftest library-disconnect-hint-cycle-falls-back-to-the-parent + ;; a <-> b, nothing removable: name the parent rather than nothing + (t/is (= {:a {:reason :used-by :library-name "b" :removable ["b"]} + :b {:reason :inherited :library-name "a" :removable ["a"]}} + (disconnect-hints [(lib :a true [:b]) + (lib :b true [:a])])))) + +(t/deftest disconnect-hint-segments-highlight-every-library-name + (t/is (= [[:text "Connected through "] + [:name "App"] + [:text ". To remove it, disconnect "] + [:name "App"] + [:text ", "] + [:name "Brand"] + [:text "."]] + (wl/disconnect-hint-segments + "Connected through %s. To remove it, disconnect %s." + "App" + ["App" "Brand"])))) + +(t/deftest disconnect-hint-segments-keep-the-text-after-the-last-name + (t/is (= [[:text "Used by "] + [:name "a"] + [:text ". To remove it, disconnect "] + [:name "b"] + [:text " first."]] + (wl/disconnect-hint-segments + "Used by %s. To remove it, disconnect %s first." + "a" + ["b"])))) diff --git a/frontend/translations/en.po b/frontend/translations/en.po index faffb0b35f..33b0f4f929 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -7175,6 +7175,10 @@ msgstr "Connected library" msgid "workspace.libraries.connected-through" msgstr "Connected through" +#: src/app/main/ui/workspace/libraries.cljs:934 +msgid "workspace.libraries.connected-through-hint" +msgstr "Connected through %s. To remove it, disconnect %s." + #: src/app/main/ui/workspace/libraries.cljs:507 msgid "workspace.libraries.connected-to" msgstr "Connected to" @@ -7321,6 +7325,10 @@ msgstr "see all changes" msgid "workspace.libraries.updates" msgstr "UPDATES" +#: src/app/main/ui/workspace/libraries.cljs:936 +msgid "workspace.libraries.used-by" +msgstr "Used by %s. To remove it, disconnect %s first." + #: src/app/main/ui/ds/notifications/shared/notification_pill.cljs:61 msgid "workspace.notification-pill.detail" msgstr "Details" diff --git a/frontend/translations/es.po b/frontend/translations/es.po index 4e5e230f54..15745cb8f6 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -7253,6 +7253,10 @@ msgstr "Biblioteca conectada" msgid "workspace.libraries.connected-through" msgstr "Conectada mediante" +#: src/app/main/ui/workspace/libraries.cljs:934 +msgid "workspace.libraries.connected-through-hint" +msgstr "Conectada mediante %s. Para quitar esta biblioteca, desconecta %s." + #: src/app/main/ui/workspace/libraries.cljs:507 msgid "workspace.libraries.connected-to" msgstr "Conectada con" @@ -7399,6 +7403,10 @@ msgstr "ver todos los cambios" msgid "workspace.libraries.updates" msgstr "ACTUALIZACIONES" +#: src/app/main/ui/workspace/libraries.cljs:936 +msgid "workspace.libraries.used-by" +msgstr "Usada por %s. Para quitar esta biblioteca, desconecta primero %s." + #: src/app/main/ui/ds/notifications/shared/notification_pill.cljs:61 msgid "workspace.notification-pill.detail" msgstr "Detalles"