Skip to content

Commit

Permalink
feat: Track internal portals when indexing nodes (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Sep 13, 2024
1 parent 2dccab9 commit bea2192
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/models/session/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,43 @@ import { ComponentType } from "../../types";

export function sortFlow(flow: FlowGraph): OrderedFlow {
let sectionId: string | undefined;
let internalPortalId: string | undefined;

const nodes: IndexedNode[] = [];
const nodeIds = new Set<string>();

const searchNodeEdges = (id: string, parentId: string) => {
// skip already added nodes
if (nodes.map((n) => n.id).includes(id)) return;
// Skip already added nodes
if (nodeIds.has(id)) return;
nodeIds.add(id);

const foundNode = flow[id];
if (!foundNode) {
throw new Error(`Referenced node edge "${id}" not found`);
}
if (!foundNode.type) {
throw new Error(`Node is missing a type: "${JSON.stringify(foundNode)}"`);
}

sectionId = foundNode.type == ComponentType.Section ? id : sectionId;
nodes.push({

const nodeToAdd: IndexedNode = {
id,
parentId,
...(sectionId && { sectionId }),
type: foundNode.type!,
...(foundNode.edges && { edges: foundNode.edges }),
type: foundNode.type,
data: foundNode.data,
});
};

if (sectionId) nodeToAdd.sectionId = sectionId;
if (internalPortalId) nodeToAdd.internalPortalId = internalPortalId;
if (foundNode.edges) nodeToAdd.edges = foundNode.edges;

nodes.push(nodeToAdd);

// Subsequent nodes are within this internal portal
internalPortalId =
foundNode.type == ComponentType.InternalPortal ? id : internalPortalId;

foundNode.edges?.forEach((childEdgeId) => {
searchNodeEdges(childEdgeId, id);
});
Expand Down
1 change: 1 addition & 0 deletions src/types/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type IndexedNode = Node & {
type: ComponentType;
parentId: string;
sectionId?: string;
internalPortalId?: string;
};

export type IndexedFlowGraph = {
Expand Down

0 comments on commit bea2192

Please sign in to comment.