Skip to content

Commit

Permalink
Merge pull request #5054 from systeminit/victor/allow-changing-types-…
Browse files Browse the repository at this point in the history
…on-views

Fix: Change component type on views other than the default
  • Loading branch information
vbustamante authored Dec 3, 2024
2 parents 9991cf4 + 28a551b commit 318231a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
70 changes: 55 additions & 15 deletions app/web/src/store/components.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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;

Expand Down Expand Up @@ -624,7 +625,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
const dEdge = processRawEdge(edge, this.allComponentsById);
if (dEdge) this.diagramEdgesById[dEdge.def.id] = dEdge;
},
processRawComponent(
processAndStoreRawComponent(
componentId: ComponentId,
processAncestors = true,
): void {
Expand All @@ -634,19 +635,25 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {

// data replacement here
this.allComponentsById[elm.def.id] = elm;

// if component changes type it should only be in one group
// so first remove
delete this.groupsById[elm.def.id];
delete this.nodesById[elm.def.id];
// and then add as appropriate
if (elm instanceof DiagramGroupData)
if (elm instanceof DiagramGroupData) {
this.groupsById[elm.def.id] = elm;
else this.nodesById[elm.def.id] = elm;
} else {
this.nodesById[elm.def.id] = elm;
}

// is false when iterating over the whole data set... no need to duplicate work
if (processAncestors) {
if (component.parentId) {
this.processRawComponent(component.parentId, processAncestors);
this.processAndStoreRawComponent(
component.parentId,
processAncestors,
);
}
}
},
Expand Down Expand Up @@ -712,7 +719,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
// this.nodesById = {};
// this.groupsById = {};
response.components.forEach((component) => {
this.processRawComponent(component.id, false);
this.processAndStoreRawComponent(component.id, false);
});

const edges =
Expand Down Expand Up @@ -1065,7 +1072,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
},
};

this.processRawComponent(componentId);
this.processAndStoreRawComponent(componentId);
}
}

Expand All @@ -1083,7 +1090,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
deletedInfo: undefined,
};

this.processRawComponent(componentId);
this.processAndStoreRawComponent(componentId);
}
}
};
Expand Down Expand Up @@ -1115,7 +1122,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
toDelete: false,
deletedInfo: undefined,
};
this.processRawComponent(componentId);
this.processAndStoreRawComponent(componentId);
}
}
},
Expand Down Expand Up @@ -1164,7 +1171,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
// don't update
if (data.changeSetId !== changeSetId) return;
this.rawComponentsById[data.component.id] = data.component;
this.processRawComponent(data.component.id);
this.processAndStoreRawComponent(data.component.id);
},
},
{
Expand Down Expand Up @@ -1238,13 +1245,46 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
// If the component that updated wasn't in this change set,
// don't update
if (metadata.change_set_id !== changeSetId) return;
const componentId = data.component.id;
const oldParent =
this.rawComponentsById[data.component.id]?.parentId;
this.rawComponentsById[componentId]?.parentId;

this.rawComponentsById[data.component.id] = data.component;
this.processRawComponent(data.component.id);
this.rawComponentsById[componentId] = data.component;
this.processAndStoreRawComponent(componentId);
if (oldParent && !data.component.parentId)
this.processRawComponent(oldParent);
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;
}
}
}
},
},
{
Expand Down Expand Up @@ -1287,7 +1327,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
delete this.nodesById[data.originalComponentId];
delete this.groupsById[data.originalComponentId];
this.rawComponentsById[data.component.id] = data.component;
this.processRawComponent(data.component.id);
this.processAndStoreRawComponent(data.component.id);
},
},
{
Expand All @@ -1297,7 +1337,7 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
// don't update
if (data.changeSetId !== changeSetId) return;
this.rawComponentsById[data.component.id] = data.component;
this.processRawComponent(data.component.id);
this.processAndStoreRawComponent(data.component.id);
this.refreshingStatus[data.component.id] = false;
},
},
Expand Down
10 changes: 6 additions & 4 deletions app/web/src/store/views.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,13 +1230,13 @@ export const useViewsStore = (forceChangeSetId?: ChangeSetId) => {
else {
component.parentId = undefined;
}
componentsStore.processRawComponent(componentId);
componentsStore.processAndStoreRawComponent(componentId);
});
// if we change to no parent, we have to follow up and re-process
Object.values(oldParentIds)
.filter(nonNullable)
.forEach((parentId) => {
componentsStore.processRawComponent(parentId);
componentsStore.processAndStoreRawComponent(parentId);
});
},
onFail: () => {
Expand All @@ -1245,9 +1245,11 @@ export const useViewsStore = (forceChangeSetId?: ChangeSetId) => {
componentsStore.rawComponentsById[componentId];
if (!component) return;
component.parentId = oldParentIds[componentId];
componentsStore.processRawComponent(componentId);
componentsStore.processAndStoreRawComponent(componentId);
if (component.parentId)
componentsStore.processRawComponent(component.parentId);
componentsStore.processAndStoreRawComponent(
component.parentId,
);
});
},
});
Expand Down
3 changes: 2 additions & 1 deletion lib/sdf-server/src/service/component/set_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ pub async fn set_type(
let component = Component::get_by_id(&ctx, component_id).await?;
let mut socket_map = HashMap::new();
let payload = component
.into_frontend_type_for_default_view(
.into_frontend_type(
&ctx,
None,
component.change_status(&ctx).await?,
&mut socket_map,
)
Expand Down

0 comments on commit 318231a

Please sign in to comment.