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
2 changes: 1 addition & 1 deletion apps/navigator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"react": "^19.3.0",
"react-dom": "^19.3.0",
"structured-context": "^0.26.0",
"structured-context": "^0.28.1",
"structured-context-map-views": "workspace:*"
},
"devDependencies": {
Expand Down
20 changes: 11 additions & 9 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/map-views/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@vitejs/plugin-react": "^6.1.1",
"react": "^19.3.0",
"react-dom": "^19.3.0",
"structured-context": "^0.26.0",
"structured-context": "^0.28.1",
"tailwindcss": "^4.3.3",
"vite": "^8.3.2"
}
Expand Down
26 changes: 20 additions & 6 deletions packages/map-views/src/core/layouts/landscape/projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,20 @@ type Filter = ValueFilter | NodeFilter;
* way a level is joined to the node level above, so `via` applies there too.
*/
export function buildLandscape(graph: SpaceGraph, regionId: string, layout: LayoutConfig<'landscape'>): Landscape {
const order = new Map([...graph.nodes.keys()].map((title, i) => [title, i]));
const spaceOrder = new Map([...graph.nodes.keys()].map((title, i) => [title, i]));
const nodeOrder = (node: SpaceNode): number | undefined => {
const value = (node.schemaData as Record<string, unknown> | undefined)?.order;
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
};
/** Sort nodes by numeric `order` ascending (matching SpaceGraph), falling back to space order. */
const compareNodes = (a: SpaceNode, b: SpaceNode): number => {
const oa = nodeOrder(a);
const ob = nodeOrder(b);
if (oa !== undefined && ob !== undefined && oa !== ob) return oa - ob;
if (oa !== undefined && ob === undefined) return -1;
if (oa === undefined && ob !== undefined) return 1;
return spaceOrder.get(a.id)! - spaceOrder.get(b.id)!;
};
// The first occurrence of each node, across strips, for marking repeats.
const first = new Map<string, string>();

Expand Down Expand Up @@ -156,9 +169,10 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo
return reached.filter(({ node }) => values.every((f) => valuesOf(node, f.field).includes(f.value)));
};

const ofType = (type: string) => [...graph.nodes.values()].filter((node) => node.resolvedType === type);
const ofType = (type: string) =>
[...graph.nodes.values()].filter((node) => node.resolvedType === type).sort(compareNodes);

/** Neighbours of a node with the given type, across edges in either direction, in space order. */
/** Neighbours of a node with the given type, across edges in either direction, in `order` then space order. */
const neighbours = (title: SpaceNode['id'], type: string, field: string | undefined): SpaceNode[] => {
const titles = new Set([
...childEdges(graph, title)
Expand All @@ -171,7 +185,7 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo
return [...titles]
.map((t) => graph.nodes.get(t)!)
.filter((node) => node.resolvedType === type)
.sort((a, b) => order.get(a.id)! - order.get(b.id)!);
.sort(compareNodes);
};

/**
Expand All @@ -196,7 +210,7 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo
byTitle.set(next.id, candidate);
}
}
return [...byTitle.values()].sort((a, b) => order.get(a.node.id)! - order.get(b.node.id)!);
return [...byTitle.values()].sort((a, b) => compareNodes(a.node, b.node));
};

/**
Expand All @@ -205,7 +219,7 @@ export function buildLandscape(graph: SpaceGraph, regionId: string, layout: Layo
* types of the first node level of each strip it divides) have, so a value no node has is not a group.
* A node with no value, or joined to no node of the dimension, is in none of its groups; it
* is shown as ungrouped, the same for both kinds.
* PROTOTYPE: groups come in space order. A field's declared domain, or an `order` field, would give
* PROTOTYPE: field dimension groups come in first-seen order. A field's declared domain would give
* the order authors expect and let a field dimension show empty groups too.
*/
const dimensionGroups = (
Expand Down
16 changes: 5 additions & 11 deletions packages/map-views/src/core/layouts/tree/projection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SpaceGraph, SpaceNode } from 'structured-context/api';
import type { LayoutConfig } from '../../definition';
import { type Connector, childEdges, isHierarchyEdge, occurrenceKey, parentEdges } from '../helpers';
import { type Connector, childEdges, isHierarchyEdge, occurrenceKey } from '../helpers';

/** One rendered appearance of a node in a tree. */
export type TreeOccurrence = {
Expand Down Expand Up @@ -44,14 +44,10 @@ export type Tree = {
export function buildTree(graph: SpaceGraph, regionId: string, dag: LayoutConfig<'tree'>['dag'] = 'repeat'): Tree {
const { hierarchy } = graph;
const levelTypes = new Set(hierarchy?.levels.map((level) => level.type));
const rootType = hierarchy?.levels[0]?.type;
const primary = new Map<string, string>();
// Parent occurrence → child title, resolved to the child's primary occurrence afterwards.
const pendingCross: { from: string; child: string; field: string }[] = [];

const hierarchyParents = (title: SpaceNode['id']) =>
parentEdges(graph, title).filter((edge) => isHierarchyEdge(edge, hierarchy));

const visit = (node: SpaceNode, path: readonly string[]): TreeOccurrence => {
const nodePath = [...path, node.id];
const key = occurrenceKey(regionId, nodePath);
Expand Down Expand Up @@ -79,14 +75,12 @@ export function buildTree(graph: SpaceGraph, regionId: string, dag: LayoutConfig
return { key, node, children };
};

const levelNodes = [...graph.nodes.values()].filter((node) => levelTypes.has(node.resolvedType));
const unparented = levelNodes.filter((node) => hierarchyParents(node.id).length === 0);
const roots = unparented.filter((node) => node.resolvedType === rootType).map((node) => visit(node, []));
const orphans = unparented.filter((node) => node.resolvedType !== rootType).map((node) => visit(node, []));
const roots = graph.hierarchyRoots.map((node) => visit(node, []));
const orphans = graph.orphans.map((node) => visit(node, []));
// Nodes only reachable through a cycle have a parent but no path from a root. Rather than
// drop them, enter each such cycle at its first node in space order.
for (const node of levelNodes) {
if (!primary.has(node.id)) orphans.push(visit(node, []));
for (const node of graph.nodes.values()) {
if (levelTypes.has(node.resolvedType) && !primary.has(node.id)) orphans.push(visit(node, []));
}

const crossConnectors = pendingCross.map(({ from, child, field }) => ({
Expand Down
18 changes: 18 additions & 0 deletions packages/map-views/tests/landscape.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, test } from 'bun:test';
import { buildSpaceGraph } from 'structured-context/api';
import type { LayoutConfig } from '../src/core/definition';
import { buildLandscape, type LandscapeOccurrence } from '../src/core/layouts/landscape/projection';
import { makeParentRef, makeSpaceNode } from './builders';
import { fixtureGraph } from './fixture-graph';
import sample from './fixtures/launch-playbook.json';

Expand Down Expand Up @@ -81,4 +83,20 @@ describe('sample landscape', () => {
);
expect(new Set(occurrences.map((o) => o.key)).size).toBe(occurrences.length);
});

test('orders type-level containers and their children by numeric order with unordered last', () => {
const orderedGraph = buildSpaceGraph(
[
makeSpaceNode('P-unordered', 'Phase'),
makeSpaceNode('P-2', 'Phase', { order: 2 }),
makeSpaceNode('P-1', 'Phase', { order: 1 }),
makeSpaceNode('A-2', 'Activity', { order: 20 }, undefined, [makeParentRef('P-1')]),
makeSpaceNode('A-1', 'Activity', { order: 10 }, undefined, [makeParentRef('P-1')]),
],
undefined,
);
const { groups } = plainLandscape(orderedGraph, 'main', layout([level('Phase'), level('Activity')]));
expect(groups.map(title)).toEqual(['P-1', 'P-2', 'P-unordered']);
expect(groups[0]!.children.map(title)).toEqual(['A-1', 'A-2']);
});
});
23 changes: 20 additions & 3 deletions packages/map-views/tests/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe('sample tree', () => {
const tree = buildTree(graph, 'main');
const all = occurrences([...tree.roots, ...tree.orphans]);

test('roots are the Phases', () => {
const phases = [...graph.nodes.values()].filter((n) => n.resolvedType === 'Phases');
expect(tree.roots.map((o) => o.node.id)).toEqual(phases.map((n) => n.id));
test('roots are the Phases, in graph order', () => {
expect(tree.roots.map((o) => o.node.id)).toEqual(graph.hierarchyRoots.map((n) => n.id));
expect(tree.roots.every((o) => o.node.resolvedType === 'Phases')).toBe(true);
});

test('occurrence keys are unique even though nodes repeat', () => {
Expand Down Expand Up @@ -106,4 +106,21 @@ describe('edge cases', () => {
const tree = buildTree(graph(nodes), 'r');
expect(tree.roots[0]!.children.map((o) => o.node.title)).toEqual(['O']);
});

test('roots, orphans, and children follow SpaceGraph numeric order', () => {
const ref = (parent: string) => makeParentRef(parent, { kind: 'hierarchy', hierarchy: hierarchy.name });
const nodes = [
makeSpaceNode('G-unordered', 'goal'),
makeSpaceNode('G-second', 'goal', { order: 2 }),
makeSpaceNode('G-first', 'goal', { order: 1 }),
makeSpaceNode('C-late', 'opportunity', { order: 20 }, undefined, [ref('G-first')]),
makeSpaceNode('C-early', 'opportunity', { order: 5 }, undefined, [ref('G-first')]),
makeSpaceNode('O-late', 'opportunity', { order: 2 }),
makeSpaceNode('O-early', 'opportunity', { order: 1 }),
];
const tree = buildTree(graph(nodes), 'r');
expect(tree.roots.map((o) => o.node.title)).toEqual(['G-first', 'G-second', 'G-unordered']);
expect(tree.roots[0]!.children.map((o) => o.node.title)).toEqual(['C-early', 'C-late']);
expect(tree.orphans.map((o) => o.node.title)).toEqual(['O-early', 'O-late']);
});
});
2 changes: 1 addition & 1 deletion packages/sctx-map-views/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"bundle": "bun run clean && bun scripts/build.ts && tsc -p tsconfig.build.json"
},
"devDependencies": {
"structured-context": "^0.26.0",
"structured-context": "^0.28.1",
"structured-context-map-views": "workspace:*"
}
}