🐛 Fix thumbnail in shared library not updated

This commit is contained in:
Aitor 2024-02-01 13:47:07 +01:00 committed by Andrey Antukh
parent 564843b297
commit efddd6c35f
4 changed files with 43 additions and 35 deletions

View file

@ -640,6 +640,21 @@
:path-params path-params :path-params path-params
:query-params query-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 (defn ext-library-changed
[library-id modified-at revn changes] [library-id modified-at revn changes]
(dm/assert! (uuid? library-id)) (dm/assert! (uuid? library-id))
@ -654,11 +669,15 @@
ch/process-changes changes))) ch/process-changes changes)))
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ stream]
(->> (rp/cmd! :get-file-object-thumbnails {:file-id library-id :tag "component"}) (let [stopper-s (rx/filter (ptk/type? ::ext-library-changed) stream)]
(rx/map (fn [thumbnails] (->>
(fn [state] (rx/merge
(assoc-in state [:workspace-libraries library-id :thumbnails] thumbnails)))))))) (->> (rx/of library-id)
(rx/delay 5000)
(rx/map fetch-library-thumbnails)))
(rx/take-until stopper-s))))))
(defn reset-component (defn reset-component
"Cancels all modifications in the shape with the given id, and all its children, in "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"}) (->> (rp/cmd! :get-file-object-thumbnails {:file-id library-id :tag "component"})
(rx/map (fn [thumbnails] (rx/map (fn [thumbnails]
(fn [state] (fn [state]
(assoc-in state [:workspace-libraries library-id :thumbnails] thumbnails)))))))))) (update state :workspace-thumbnails merge thumbnails))))))))))
(defn unlink-file-from-library (defn unlink-file-from-library
[file-id library-id] [file-id library-id]

View file

@ -115,16 +115,10 @@
(ptk/reify ::assoc-thumbnail (ptk/reify ::assoc-thumbnail
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [prev-uri (dm/get-in state [:workspace-thumbnails object-id]) (let [prev-uri (dm/get-in state [:workspace-thumbnails object-id])]
current-file-id (:current-file-id state)]
(some->> prev-uri (vreset! prev-uri*)) (some->> prev-uri (vreset! prev-uri*))
(l/trc :hint "assoc thumbnail" :object-id object-id :uri uri) (l/trc :hint "assoc thumbnail" :object-id object-id :uri uri)
(update state :workspace-thumbnails assoc object-id 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)))))
ptk/EffectEvent ptk/EffectEvent
(effect [_ _ _] (effect [_ _ _]

View file

@ -490,6 +490,9 @@
(dm/get-in state [:viewer-local :zoom-type])) (dm/get-in state [:viewer-local :zoom-type]))
st/state)) st/state))
(def workspace-thumbnails
(l/derived :workspace-thumbnails st/state))
(defn workspace-thumbnail-by-id (defn workspace-thumbnail-by-id
[object-id] [object-id]
(l/derived (l/derived

View file

@ -8,7 +8,6 @@
(ns app.main.ui.workspace.sidebar.assets.common (ns app.main.ui.workspace.sidebar.assets.common
(:require-macros [app.main.style :as stl]) (:require-macros [app.main.style :as stl])
(:require (:require
[app.common.data.macros :as dm]
[app.common.files.helpers :as cfh] [app.common.files.helpers :as cfh]
[app.common.spec :as us] [app.common.spec :as us]
[app.common.thumbnails :as thc] [app.common.thumbnails :as thc]
@ -263,39 +262,32 @@
(:id target-asset) (:id target-asset)
(cfh/merge-path-item prefix (:name 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 (mf/defc component-item-thumbnail
"Component that renders the thumbnail image or the original SVG." "Component that renders the thumbnail image or the original SVG."
{::mf/wrap-props false} {::mf/wrap-props false}
[{:keys [file-id root-shape component container class]}] [{:keys [file-id root-shape component container class]}]
(let [retry (mf/use-state 0) (let [page-id (:main-instance-page component)
thumbnail-uri (get-component-thumbnail-uri file-id component) root-id (:main-instance-id component)
handle-error
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/use-fn
(mf/deps @retry) (mf/deps @retry)
(fn [] (fn []
(when (@retry < 3) (when (< @retry 3)
(inc retry))))] (inc retry))))]
(if (some? thumbnail-uri) (if (some? thumbnail-uri)
[:& component-svg-thumbnail [:& component-svg-thumbnail
{:thumbnail-uri thumbnail-uri {:thumbnail-uri thumbnail-uri
:class class :class class
:on-error handle-error :on-error on-error
:root-shape root-shape :root-shape root-shape
:objects (:objects container) :objects (:objects container)
:show-grids? true}] :show-grids? true}]