♻️ Refactor state locality

The main purpose of this refactor is reduce
a custom state from different pages and unify
them under common access patterns
This commit is contained in:
Andrey Antukh 2025-01-16 11:00:14 +01:00
parent f62aecb383
commit 85746e7cb2
110 changed files with 2254 additions and 2125 deletions

View file

@ -9,7 +9,7 @@
[app.common.files.helpers :as cfh]
[app.common.types.container :as ctn]
[app.common.types.file :as ctf]
[app.main.data.workspace.state-helpers :as wsh]
[app.main.data.helpers :as dsh]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.pages :as thp]))
@ -111,7 +111,7 @@
root-inst (ctn/get-shape page root-inst-id)
main-instance? (:main-instance root-inst)
libs (wsh/get-libraries state)
libs (dsh/lookup-libraries state)
component (ctf/get-component libs (:component-file root-inst) (:component-id root-inst))
library (ctf/get-component-library libs root-inst)
@ -151,7 +151,7 @@
(let [page (thp/current-page state)
root-inst (ctn/get-shape page root-inst-id)
libs (wsh/get-libraries state)
libs (dsh/lookup-libraries state)
component (ctf/get-component libs (:component-file root-inst) (:component-id root-inst))
library (ctf/get-component-library libs root-inst)
@ -166,7 +166,7 @@
(defn resolve-component
"Get the component with the given id and all its shapes."
[state component-file component-id]
(let [libs (wsh/get-libraries state)
(let [libs (dsh/lookup-libraries state)
component (ctf/get-component libs component-file component-id)
library (ctf/get-component-library libs component)
shapes-main (ctf/get-component-shapes (:data library) component)]

View file

@ -20,9 +20,9 @@
[app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst]
[app.common.uuid :as uuid]
[app.main.data.helpers :as dsh]
[app.main.data.workspace.groups :as dwg]
[app.main.data.workspace.layout :as layout]
[app.main.data.workspace.state-helpers :as wsh]))
[app.main.data.workspace.layout :as layout]))
;; ---- Helpers to manage pages and objects
@ -33,12 +33,15 @@
:current-page-id nil
:workspace-layout layout/default-layout
:workspace-global layout/default-global
:workspace-data {:id current-file-id
:options {:components-v2 true}
:components {}
:pages []
:pages-index {}}
:workspace-libraries {}
:files
{current-file-id
{:id current-file-id
:data {:id current-file-id
:options {:components-v2 true}
:components {}
:pages []
:pages-index {}}}}
:features-team #{"components/v2"}})
(def ^:private idmap (atom {}))
@ -48,8 +51,9 @@
(defn current-page
[state]
(let [page-id (:current-page-id state)]
(get-in state [:workspace-data :pages-index page-id])))
(let [page-id (:current-page-id state)
file-id (:current-file-id state)]
(get-in state [:files file-id :data :pages-index page-id])))
(defn id
[label]
@ -65,20 +69,22 @@
(let [page (current-page state)]
(cfh/get-children (:objects page) (id label))))
(defn apply-changes
[state changes]
(let [file-id (:current-file-id state)]
(update-in state [:files file-id :data] cp/process-changes changes)))
(defn sample-page
([state] (sample-page state {}))
([state {:keys [id name] :as props
:or {id (uuid/next)
name "page1"}}]
(swap! idmap assoc :page id)
(-> state
(assoc :current-page-id id)
(update :workspace-data
cp/process-changes
[{:type :add-page
:id id
:name name}]))))
(apply-changes [{:type :add-page
:id id
:name name}]))))
(defn sample-shape
([state label type] (sample-shape state type {}))
@ -87,13 +93,12 @@
frame (cfh/get-frame (:objects page))
shape (cts/setup-shape (merge {:type type :x 0 :y 0 :width 1 :height 1} props))]
(swap! idmap assoc label (:id shape))
(update state :workspace-data
cp/process-changes
[{:type :add-obj
:id (:id shape)
:page-id (:id page)
:frame-id (:id frame)
:obj shape}]))))
(apply-changes state
[{:type :add-obj
:id (:id shape)
:page-id (:id page)
:frame-id (:id frame)
:obj shape}]))))
(defn group-shapes
([state label ids] (group-shapes state label ids "Group"))
@ -106,8 +111,7 @@
(dwg/prepare-create-group (pcb/empty-changes) nil (:objects page) (:id page) shapes prefix true)]
(swap! idmap assoc label (:id group))
(update state :workspace-data
cp/process-changes (:redo-changes changes)))))))
(apply-changes state (:redo-changes changes)))))))
(defn frame-shapes
([state label ids] (frame-shapes state label ids "Board"))
@ -128,13 +132,12 @@
true)]
(swap! idmap assoc label (:id frame))
(update state :workspace-data
cp/process-changes (:redo-changes changes)))))))
(apply-changes state (:redo-changes changes)))))))
(defn make-component
[state instance-label component-label shape-ids]
(let [page (current-page state)
objects (wsh/lookup-page-objects state (:id page))
objects (dsh/lookup-page-objects state (:id page))
shapes (dwg/shapes-for-grouping objects shape-ids)
[group component-id changes]
@ -149,15 +152,14 @@
(swap! idmap assoc instance-label (:id group)
component-label component-id)
(update state :workspace-data
cp/process-changes (:redo-changes changes))))
(apply-changes state (:redo-changes changes))))
(defn instantiate-component
([state label component-id]
(instantiate-component state label component-id current-file-id))
([state label component-id file-id]
(let [page (current-page state)
libraries (wsh/get-libraries state)
libraries (dsh/lookup-libraries state)
objects (:objects page)
changes (-> (pcb/empty-changes nil (:id page))
@ -173,26 +175,27 @@
libraries)]
(swap! idmap assoc label (:id new-shape))
(update state :workspace-data
cp/process-changes (:redo-changes changes)))))
(apply-changes state (:redo-changes changes)))))
(defn move-to-library
[state label name]
(let [library-id (uuid/next)
data (get state :workspace-data)]
file-id (:current-file-id state)
data (get-in state [:files file-id :data])]
(swap! idmap assoc label library-id)
(-> state
(update :workspace-libraries
assoc library-id {:id library-id
:name name
:data {:id library-id
:options (:options data)
:pages (:pages data)
:pages-index (:pages-index data)
:components (:components data)}})
(update :workspace-data
assoc :components {} :pages [] :pages-index {}))))
(update :files assoc library-id
{:id library-id
:name name
:data {:id library-id
:options (:options data)
:pages (:pages data)
:pages-index (:pages-index data)
:components (:components data)}})
(update-in [:files file-id :data] assoc
:components {}
:pages []
:pages-index {}))))
(defn simulate-copy-shape
[selected objects libraries page file features version]

View file

@ -18,8 +18,6 @@
:workspace-global layout/default-global
:current-file-id nil
:current-page-id nil
:workspace-data nil
:workspace-libraries {}
:features-team #{"components/v2"}})
(defn- on-error
@ -34,8 +32,7 @@
(assoc :current-file-id (:id file)
:current-page-id (cthf/current-page-id file)
:permissions {:can-edit true}
:workspace-file (dissoc file :data)
:workspace-data (:data file)))
:files {(:id file) file}))
store (ptk/store {:state state :on-error on-error})]
store))
@ -64,7 +61,7 @@
(ptk/emit! store :the/end))))
(defn get-file-from-store
[store]
(-> (:workspace-file store)
(assoc :data (:workspace-data store))))
(defn get-file-from-state
[state]
(let [file-id (:current-file-id state)]
(get-in state [:files file-id])))