0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-13 08:11:30 -05:00

🐛 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
: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]

View file

@ -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 [_ _ _]

View file

@ -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

View file

@ -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}]