From efddd6c35f52d815dff629e4a8fce0a61a5d65dc Mon Sep 17 00:00:00 2001 From: Aitor Date: Thu, 1 Feb 2024 13:47:07 +0100 Subject: [PATCH] :bug: Fix thumbnail in shared library not updated --- .../app/main/data/workspace/libraries.cljs | 31 +++++++++++++---- .../app/main/data/workspace/thumbnails.cljs | 10 ++---- frontend/src/app/main/refs.cljs | 3 ++ .../ui/workspace/sidebar/assets/common.cljs | 34 +++++++------------ 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 59864afbb..269701c78 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -640,6 +640,21 @@ :path-params path-params :query-params query-params})))))) +(defn library-thumbnails-fetched + [thumbnails] + (ptk/reify ::library-thumbnails-fetched + ptk/UpdateEvent + (update [_ state] + (update state :workspace-thumbnails merge thumbnails)))) + +(defn fetch-library-thumbnails + [library-id] + (ptk/reify ::fetch-library-thumbnails + ptk/WatchEvent + (watch [_ _ _] + (->> (rp/cmd! :get-file-object-thumbnails {:file-id library-id :tag "component"}) + (rx/map library-thumbnails-fetched))))) + (defn ext-library-changed [library-id modified-at revn changes] (dm/assert! (uuid? library-id)) @@ -654,11 +669,15 @@ ch/process-changes changes))) ptk/WatchEvent - (watch [_ _ _] - (->> (rp/cmd! :get-file-object-thumbnails {:file-id library-id :tag "component"}) - (rx/map (fn [thumbnails] - (fn [state] - (assoc-in state [:workspace-libraries library-id :thumbnails] thumbnails)))))))) + (watch [_ _ stream] + (let [stopper-s (rx/filter (ptk/type? ::ext-library-changed) stream)] + (->> + (rx/merge + (->> (rx/of library-id) + (rx/delay 5000) + (rx/map fetch-library-thumbnails))) + + (rx/take-until stopper-s)))))) (defn reset-component "Cancels all modifications in the shape with the given id, and all its children, in @@ -1261,7 +1280,7 @@ (->> (rp/cmd! :get-file-object-thumbnails {:file-id library-id :tag "component"}) (rx/map (fn [thumbnails] (fn [state] - (assoc-in state [:workspace-libraries library-id :thumbnails] thumbnails)))))))))) + (update state :workspace-thumbnails merge thumbnails)))))))))) (defn unlink-file-from-library [file-id library-id] diff --git a/frontend/src/app/main/data/workspace/thumbnails.cljs b/frontend/src/app/main/data/workspace/thumbnails.cljs index 6582a01c8..bc24e69e0 100644 --- a/frontend/src/app/main/data/workspace/thumbnails.cljs +++ b/frontend/src/app/main/data/workspace/thumbnails.cljs @@ -115,16 +115,10 @@ (ptk/reify ::assoc-thumbnail ptk/UpdateEvent (update [_ state] - (let [prev-uri (dm/get-in state [:workspace-thumbnails object-id]) - current-file-id (:current-file-id state)] + (let [prev-uri (dm/get-in state [:workspace-thumbnails object-id])] (some->> prev-uri (vreset! prev-uri*)) (l/trc :hint "assoc thumbnail" :object-id object-id :uri uri) - - #_(update state :workspace-thumbnails assoc object-id uri) - (if (thc/file-id? object-id current-file-id) - (update state :workspace-thumbnails assoc object-id uri) - (let [file-id (thc/get-file-id object-id)] - (update-in state [:workspace-libraries file-id :thumbnails] assoc object-id uri))))) + (update state :workspace-thumbnails assoc object-id uri))) ptk/EffectEvent (effect [_ _ _] diff --git a/frontend/src/app/main/refs.cljs b/frontend/src/app/main/refs.cljs index 46c1ca493..7d3ee4fec 100644 --- a/frontend/src/app/main/refs.cljs +++ b/frontend/src/app/main/refs.cljs @@ -490,6 +490,9 @@ (dm/get-in state [:viewer-local :zoom-type])) st/state)) +(def workspace-thumbnails + (l/derived :workspace-thumbnails st/state)) + (defn workspace-thumbnail-by-id [object-id] (l/derived diff --git a/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs b/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs index b46e6a809..58b4a1c0f 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs @@ -8,7 +8,6 @@ (ns app.main.ui.workspace.sidebar.assets.common (:require-macros [app.main.style :as stl]) (:require - [app.common.data.macros :as dm] [app.common.files.helpers :as cfh] [app.common.spec :as us] [app.common.thumbnails :as thc] @@ -263,39 +262,32 @@ (:id target-asset) (cfh/merge-path-item prefix (:name target-asset)))))))) - -(defn- get-component-thumbnail-uri - "Returns the component thumbnail uri" - [file-id component] - (let [page-id (:main-instance-page component) - root-id (:main-instance-id component) - object-id (thc/fmt-object-id file-id page-id root-id "component") - current-file? (= file-id (:id @refs/workspace-file))] - - (if current-file? - (mf/deref (refs/workspace-thumbnail-by-id object-id)) - (let [libraries @refs/workspace-libraries - thumbnail (dm/get-in libraries [file-id :thumbnails object-id])] - thumbnail)))) - (mf/defc component-item-thumbnail "Component that renders the thumbnail image or the original SVG." {::mf/wrap-props false} [{:keys [file-id root-shape component container class]}] - (let [retry (mf/use-state 0) - thumbnail-uri (get-component-thumbnail-uri file-id component) - handle-error + (let [page-id (:main-instance-page component) + root-id (:main-instance-id component) + + retry (mf/use-state 0) + + thumbnail-uri* (mf/with-memo [file-id page-id root-id] + (let [object-id (thc/fmt-object-id file-id page-id root-id "component")] + (refs/workspace-thumbnail-by-id object-id))) + thumbnail-uri (mf/deref thumbnail-uri*) + + on-error (mf/use-fn (mf/deps @retry) (fn [] - (when (@retry < 3) + (when (< @retry 3) (inc retry))))] (if (some? thumbnail-uri) [:& component-svg-thumbnail {:thumbnail-uri thumbnail-uri :class class - :on-error handle-error + :on-error on-error :root-shape root-shape :objects (:objects container) :show-grids? true}]