Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class MoveFileDialogComponent {
const currentProject = this.currentProject();
if (currentProject) {
const rootParentId = currentProject.rootResourceId ?? currentProject.id;
this.actions.getComponentsTree(rootParentId, currentProject.id, ResourceType.Project);
this.actions.getComponentsTree(rootParentId, currentProject.id, ResourceType.Project, true);
}

effect(() => {
Expand Down
31 changes: 29 additions & 2 deletions src/app/shared/mappers/nodes/base-node.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ export class BaseNodeMapper {
};
}

static getNodesWithChildren(data: BaseNodeDataJsonApi[], parentId: string): NodeShortInfoModel[] {
return this.getAllDescendants(data, parentId).map((item) => ({
static getNodesWithChildren(
data: BaseNodeDataJsonApi[],
parentId: string,
includeAncestors = false
): NodeShortInfoModel[] {
const nodes = includeAncestors
? this.getAllAncestorsAndDescendants(data, parentId)
: this.getAllDescendants(data, parentId);

return nodes.map((item) => ({
id: item.id,
title: replaceBadEncodedChars(item.attributes.title),
isPublic: item.attributes.public,
Expand Down Expand Up @@ -82,4 +90,23 @@ export class BaseNodeMapper {

return [parent, ...descendants];
}

static getAllAncestors(allNodes: BaseNodeDataJsonApi[], nodeId: string): BaseNodeDataJsonApi[] {
const node = allNodes.find((n) => n.id === nodeId);
if (!node) return [];

const parentId = node.relationships.parent?.data?.id;
if (!parentId) return [node];

const ancestors = this.getAllAncestors(allNodes, parentId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an API heavy way to do this. To get all nodes in a hierarchy, you can just /v2/nodes/?filter[root]={project.root}. You might have to sort them in JS, but it will make far fewer calls.

If need be, we can make a technical improvement ticket to fix this that isn't a hotfix, but it'd be nice to not have this kind of thing be in the codebase for too long.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked on Slack, and this won't be necessary.


return [node, ...ancestors];
}

static getAllAncestorsAndDescendants(allNodes: BaseNodeDataJsonApi[], nodeId: string): BaseNodeDataJsonApi[] {
const ancestors = this.getAllAncestors(allNodes, nodeId);
const descendants = this.getAllDescendants(allNodes, nodeId).slice(1);

return [...ancestors, ...descendants];
}
}
5 changes: 3 additions & 2 deletions src/app/shared/services/resource.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ export class ResourceGuidService {
getResourceWithChildren(
rootParentId: string,
resourceId: string,
resourceType: ResourceType
resourceType: ResourceType,
includeAncestors = false
): Observable<NodeShortInfoModel[]> {
const resourcePath = this.urlMap.get(resourceType);

return this.jsonApiService
.get<NodesResponseJsonApi>(`${this.apiUrl}/${resourcePath}/?filter[root]=${rootParentId}&page[size]=100`)
.pipe(map((response) => BaseNodeMapper.getNodesWithChildren(response.data, resourceId)));
.pipe(map((response) => BaseNodeMapper.getNodesWithChildren(response.data, resourceId, includeAncestors)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class GetResourceWithChildren {
constructor(
public rootParentId: string,
public resourceId: string,
public resourceType: ResourceType
public resourceType: ResourceType,
public includeAncestors = false
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class CurrentResourceState {
});

return this.resourceService
.getResourceWithChildren(action.rootParentId, action.resourceId, action.resourceType)
.getResourceWithChildren(action.rootParentId, action.resourceId, action.resourceType, action.includeAncestors)
.pipe(
tap((children) => {
ctx.patchState({
Expand Down