24 lines
796 B
TypeScript
24 lines
796 B
TypeScript
import type { AnswerUiBlock } from "@/features/agent";
|
|
|
|
/** Include only reachable Chart nodes, preserving report props and their source. */
|
|
export function presentationChartBlocks(blocks: AnswerUiBlock[]): AnswerUiBlock[] {
|
|
return blocks.flatMap((block) => {
|
|
const charts: AnswerUiBlock[] = [];
|
|
const visited = new Set<string>();
|
|
const visit = (id: string) => {
|
|
if (visited.has(id)) return;
|
|
visited.add(id);
|
|
const element = block.spec.elements[id];
|
|
if (!element) return;
|
|
if (element.type === "Chart") charts.push({
|
|
...block,
|
|
block_id: `${block.block_id}:${id}`,
|
|
spec: { root: id, elements: { [id]: element } }
|
|
});
|
|
else element.children?.forEach(visit);
|
|
};
|
|
visit(block.spec.root);
|
|
return charts;
|
|
});
|
|
}
|