From 64c87412338f6c985e040a0c00e38b2530241124 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 20 Nov 2024 14:37:56 +0100 Subject: [PATCH] :bug: Make thumbnails independent of current public uri Mainly always resolve the public uri at frontend, making the PENPOT_PUBLIC_URI less necessary to be changed. This improves the experience of on-premise configuration. Also removes unnecesary calls for thumbnail generation on components. --- .../src/app/rpc/commands/files_thumbnails.clj | 16 +++++------ backend/test/backend_tests/rpc_file_test.clj | 3 +-- frontend/src/app/main/data/workspace.cljs | 27 ++++++++++--------- .../app/main/data/workspace/libraries.cljs | 12 ++++----- .../app/main/data/workspace/thumbnails.cljs | 2 +- frontend/src/app/main/refs.cljs | 4 ++- frontend/src/app/main/ui/shapes/frame.cljs | 4 +-- .../ui/workspace/sidebar/assets/common.cljs | 18 +++++++------ .../workspace/sidebar/assets/components.cljs | 9 ------- 9 files changed, 45 insertions(+), 50 deletions(-) diff --git a/backend/src/app/rpc/commands/files_thumbnails.clj b/backend/src/app/rpc/commands/files_thumbnails.clj index 92c8d16b0..eb7bf3c16 100644 --- a/backend/src/app/rpc/commands/files_thumbnails.clj +++ b/backend/src/app/rpc/commands/files_thumbnails.clj @@ -50,8 +50,7 @@ " where file_id=? and tag=? and deleted_at is null") res (db/exec! conn [sql file-id tag])] (->> res - (d/index-by :object-id (fn [row] - (files/resolve-public-uri (:media-id row)))) + (d/index-by :object-id :media-id) (d/without-nils)))) (defn- get-object-thumbnails @@ -62,8 +61,7 @@ " where file_id=? and deleted_at is null") res (db/exec! conn [sql file-id])] (->> res - (d/index-by :object-id (fn [row] - (files/resolve-public-uri (:media-id row)))) + (d/index-by :object-id :media-id) (d/without-nils)))) ([conn file-id object-ids] @@ -75,8 +73,7 @@ res (db/exec! conn [sql file-id ids])] (->> res - (d/index-by :object-id (fn [row] - (files/resolve-public-uri (:media-id row)))) + (d/index-by :object-id :media-id) (d/without-nils))))) (sv/defmethod ::get-file-object-thumbnails @@ -127,8 +124,11 @@ (if-let [frame (-> frames first)] (let [frame-id (:id frame) object-id (thc/fmt-object-id (:id file) page-id frame-id "frame") - frame (if-let [thumb (get thumbnails object-id)] - (assoc frame :thumbnail thumb :shapes []) + + frame (if-let [media-id (get thumbnails object-id)] + (-> frame + (assoc :thumbnail-id media-id) + (assoc :shapes [])) (dissoc frame :thumbnail)) children-ids diff --git a/backend/test/backend_tests/rpc_file_test.clj b/backend/test/backend_tests/rpc_file_test.clj index d6678be29..679d5221e 100644 --- a/backend/test/backend_tests/rpc_file_test.clj +++ b/backend/test/backend_tests/rpc_file_test.clj @@ -1090,8 +1090,7 @@ (t/is (contains? result :file-id)) (t/is (= (:id file) (:file-id result))) - (t/is (str/starts-with? (get-in result [:page :objects frame1-id :thumbnail]) - "http://localhost:3449/assets/by-id/")) + (t/is (uuid? (get-in result [:page :objects frame1-id :thumbnail-id]))) (t/is (= [] (get-in result [:page :objects frame1-id :shapes])))) ;; Delete thumbnail data diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 6b923dbb0..a64feda54 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -199,18 +199,21 @@ ;; Load libraries (->> (rp/cmd! :get-file-libraries {:file-id file-id}) - (rx/mapcat identity) - (rx/merge-map - (fn [{:keys [id synced-at]}] - (->> (rp/cmd! :get-file {:id id :features features}) - (rx/map #(assoc % :synced-at synced-at))))) - (rx/merge-map fpmap/resolve-file) - (rx/merge-map - (fn [{:keys [id] :as file}] - (->> (rp/cmd! :get-file-object-thumbnails {:file-id id :tag "component"}) - (rx/map #(assoc file :thumbnails %))))) - (rx/reduce conj []) - (rx/map libraries-fetched))) + (rx/mapcat (fn [libraries] + (rx/merge + (->> (rx/from libraries) + (rx/merge-map + (fn [{:keys [id synced-at]}] + (->> (rp/cmd! :get-file {:id id :features features}) + (rx/map #(assoc % :synced-at synced-at))))) + (rx/merge-map fpmap/resolve-file) + (rx/reduce conj []) + (rx/map libraries-fetched)) + (->> (rx/from libraries) + (rx/map :id) + (rx/mapcat (fn [file-id] + (rp/cmd! :get-file-object-thumbnails {:file-id file-id :tag "component"}))) + (rx/map dwl/library-thumbnails-fetched))))))) (rx/of (with-meta (workspace-initialized) {:file-id file-id}))) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index e04b4dfb2..5f4c56aa9 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -881,11 +881,9 @@ (rx/of (dwu/start-undo-transaction undo-id) (update-component shape-id undo-group) - (sync-file current-file-id file-id :components (:component-id shape) undo-group) - (update-component-thumbnail-sync state component-id file-id "frame") - (update-component-thumbnail-sync state component-id file-id "component") + (sync-file current-file-id file-id :components component-id undo-group) (when (not current-file?) - (sync-file file-id file-id :components (:component-id shape) undo-group)) + (sync-file file-id file-id :components component-id undo-group)) (dwu/commit-undo-transaction undo-id))))))) (defn launch-component-sync @@ -937,9 +935,9 @@ ;; in the grid creating new rows/columns to make space (let [file (wsh/get-file state file-id) libraries (wsh/get-libraries state) - page (wsh/lookup-page state) - objects (wsh/lookup-page-objects state) - parent (get objects (:parent-id shape)) + page (wsh/lookup-page state) + objects (wsh/lookup-page-objects state) + parent (get objects (:parent-id shape)) ;; If the target parent is a grid layout we need to pass the target cell target-cell (when (ctl/grid-layout? parent) diff --git a/frontend/src/app/main/data/workspace/thumbnails.cljs b/frontend/src/app/main/data/workspace/thumbnails.cljs index a043c3811..0d2ef2239 100644 --- a/frontend/src/app/main/data/workspace/thumbnails.cljs +++ b/frontend/src/app/main/data/workspace/thumbnails.cljs @@ -168,7 +168,7 @@ (.error js/console cause) (rx/empty))) - (rx/tap #(l/trc :hint "thumbnail updated" :elapsed (dm/str (tp) "ms"))) + (rx/tap #(l/dbg :hint "thumbnail updated" :elapsed (dm/str (tp) "ms"))) ;; We cancel all the stream if user starts editing while ;; thumbnail is generating diff --git a/frontend/src/app/main/refs.cljs b/frontend/src/app/main/refs.cljs index 104a504b5..7ebc8c772 100644 --- a/frontend/src/app/main/refs.cljs +++ b/frontend/src/app/main/refs.cljs @@ -13,6 +13,7 @@ [app.common.types.shape-tree :as ctt] [app.common.types.shape.layout :as ctl] [app.common.types.tokens-lib :as ctob] + [app.config :as cf] [app.main.data.workspace.state-helpers :as wsh] [app.main.store :as st] [app.main.ui.workspace.tokens.token-set :as wtts] @@ -582,7 +583,8 @@ [object-id] (l/derived (fn [state] - (dm/get-in state [:workspace-thumbnails object-id])) + (some-> (dm/get-in state [:workspace-thumbnails object-id]) + (cf/resolve-media))) st/state)) (def workspace-text-modifier diff --git a/frontend/src/app/main/ui/shapes/frame.cljs b/frontend/src/app/main/ui/shapes/frame.cljs index 3befcda74..b644b8def 100644 --- a/frontend/src/app/main/ui/shapes/frame.cljs +++ b/frontend/src/app/main/ui/shapes/frame.cljs @@ -135,7 +135,7 @@ bounds (mf/with-memo [bounds points] (or bounds (gsb/get-frame-bounds shape))) - thumb (:thumbnail shape) + thumb (cf/resolve-media (:thumbnail-id shape)) debug? (dbg/enabled? :thumbnails) safari? (cf/check-browser? :safari) @@ -171,7 +171,7 @@ {::mf/wrap-props false} [props] (let [shape (unchecked-get props "shape")] - (when ^boolean (:thumbnail shape) + (when ^boolean (:thumbnail-id shape) [:> frame-container props [:> frame-thumbnail-image props]]))) 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 578d08af2..b8bf2959f 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs @@ -269,17 +269,19 @@ (mf/defc component-item-thumbnail "Component that renders the thumbnail image or the original SVG." - {::mf/wrap-props false} + {::mf/props :obj} [{:keys [file-id root-shape component container class]}] - (let [page-id (:main-instance-page component) - root-id (:main-instance-id component) + (let [page-id (:main-instance-page component) + root-id (:main-instance-id component) + retry (mf/use-state 0) - 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/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*) + thumbnail-uri + (mf/deref thumbnail-uri*) on-error (mf/use-fn diff --git a/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs b/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs index 72a472841..8efffa844 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs @@ -57,15 +57,6 @@ component)] [root-shape container]))) - - ;; NOTE: We don't schedule the thumbnail generation on idle right now - ;; until we can queue and handle thumbnail batching properly. -#_(mf/with-effect [] - (when-not (some? thumbnail-uri) - (tm/schedule-on-idle - #(st/emit! (dwl/update-component-thumbnail (:id component) file-id))))) - - (mf/defc components-item {::mf/wrap-props false} [{:keys [component renaming listing-thumbs? selected