From e8844d31a93204449aea479f673031a16643992b Mon Sep 17 00:00:00 2001 From: John Obelenus Date: Tue, 3 Dec 2024 13:22:10 -0500 Subject: [PATCH] Unify view logic in one place --- app/web/src/store/components.store.ts | 33 ------------ app/web/src/store/views.store.ts | 52 +++++++++++-------- .../src/service/component/set_type.rs | 2 + 3 files changed, 33 insertions(+), 54 deletions(-) diff --git a/app/web/src/store/components.store.ts b/app/web/src/store/components.store.ts index 6240852ae6..e55ba3ca8c 100644 --- a/app/web/src/store/components.store.ts +++ b/app/web/src/store/components.store.ts @@ -43,7 +43,6 @@ import { useAssetStore } from "./asset.store"; import { useRealtimeStore } from "./realtime/realtime.store"; import { useWorkspacesStore } from "./workspaces.store"; import { useFeatureFlagsStore } from "./feature_flags.store"; -import { useViewsStore } from "./views.store"; export type ComponentNodeId = string; @@ -1253,38 +1252,6 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => { this.processAndStoreRawComponent(componentId); if (oldParent && !data.component.parentId) this.processAndStoreRawComponent(oldParent); - - // Updates without geometry may still change component type - // So we need to coerce the geometry to the correct array in the views store - if (!data.component.viewData) { - const viewsStore = useViewsStore(); - - for (const view of _.values(viewsStore.viewsById)) { - const groupGeo = view.groups[componentId]; - const componentGeo = view.components[componentId]; - - const geo = groupGeo ?? componentGeo; - if (!geo) continue; - - delete view.groups[componentId]; - delete view.components[componentId]; - - if ( - data.component.componentType === ComponentType.Component - ) { - const node = processRawComponent( - data.component, - this.rawComponentsById, - ) as DiagramNodeData; - geo.height = node.height; - geo.width = node.width; - - view.components[componentId] = geo; - } else { - view.groups[componentId] = geo; - } - } - } }, }, { diff --git a/app/web/src/store/views.store.ts b/app/web/src/store/views.store.ts index c7908d098a..dae3f17512 100644 --- a/app/web/src/store/views.store.ts +++ b/app/web/src/store/views.store.ts @@ -1622,28 +1622,38 @@ export const useViewsStore = (forceChangeSetId?: ChangeSetId) => { // don't update if (metadata.change_set_id !== changeSetId) return; const { viewId, geometry } = { ...data.component.viewData }; - if (!viewId || !geometry) - // this is expected in many situations - return; // but will be populated on changing componentType - const view = this.viewsById[viewId]; - if (!view) return; // FIXME later when we have full WsEvents - - delete view.components[data.component.id]; - delete view.groups[data.component.id]; - if (data.component.componentType === ComponentType.Component) { - const node = processRawComponent( - data.component, - componentsStore.rawComponentsById, - ) as DiagramNodeData; - geometry.height = node.height; - geometry.width = node.width; - view.components[data.component.id] = geometry as IRect; - } else { - if (!geometry.width) geometry.width = 500; - if (!geometry.height) geometry.height = 500; - view.groups[data.component.id] = geometry as IRect; - } + // changed component type means book-keeping for all views + // PSA: currently SDF does not change any geometry when you change component types + // if it starts to change geometry, it must return new geometry for every view + // the structure doesn't currently support that + Object.values(this.viewsById).forEach((view) => { + const groupGeo = view.groups[data.component.id]; + const componentGeo = view.components[data.component.id]; + const _geo = groupGeo ?? componentGeo; + // i don't exist in this view, and i am not being added to this view + if (!_geo && !viewId) return; + const finalGeo = geometry ?? _geo; + if (!finalGeo) return; + + delete view.components[data.component.id]; + delete view.groups[data.component.id]; + if ( + data.component.componentType === ComponentType.Component + ) { + const node = processRawComponent( + data.component, + componentsStore.rawComponentsById, + ) as DiagramNodeData; + finalGeo.height = node.height; + finalGeo.width = node.width; + view.components[data.component.id] = geometry as IRect; + } else { + if (!finalGeo.width) finalGeo.width = 500; + if (!finalGeo.height) finalGeo.height = 500; + view.groups[data.component.id] = finalGeo as IRect; + } + }); if (this.selectedComponentId === data.component.id) { if (data.component.changeStatus !== "deleted") diff --git a/lib/sdf-server/src/service/component/set_type.rs b/lib/sdf-server/src/service/component/set_type.rs index c3d11669f7..381ef11ed4 100644 --- a/lib/sdf-server/src/service/component/set_type.rs +++ b/lib/sdf-server/src/service/component/set_type.rs @@ -43,6 +43,8 @@ pub async fn set_type( let component = Component::get_by_id(&ctx, component_id).await?; let mut socket_map = HashMap::new(); + // PSA: when we call `set_type_by_id` we are not altering any geometries (e.g. turning a small component into a default 500x500 sized frame) + // if we do alter those geometries, we need to send multiple geometries back over the wire (currently, we only support sending one) let payload = component .into_frontend_type( &ctx,