Skip to content

Commit

Permalink
Update CSV to mindmap app (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggasimzada-miro authored May 23, 2023
1 parent 39bac5d commit 2fbe748
Showing 1 changed file with 24 additions and 45 deletions.
69 changes: 24 additions & 45 deletions examples/csv-to-mindmap/src/mindmap.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
import { DSVRowArray } from "d3-dsv";

interface Node {
nodeView: { content: string };
children: Node[];
}

/**
* Create graph from CSV rows
*
* @param contents CSV rows
* @returns Graph representation using adjacency list
* @returns Schema that can be directly passed to createMindmapNode
*/
const createGraph = (contents: DSVRowArray<string>) => {
const adjacencyList: Record<string, Set<string>> = {};
const root = contents[0][0]!;
let root: Node | undefined;

const visited: Record<string, Node> = {};

const cols = contents.columns.reverse();
for (const row of contents) {
// start from leaf
let child = undefined;
for (const col of cols) {
let parent = undefined;
for (const col of contents.columns) {
const value = row[col]!;

if (value === "") continue;
const key = `${col}-${value}`;

if (!adjacencyList[value]) {
adjacencyList[value] = new Set();
}
if (!visited[key]) {
const node = { nodeView: { content: value }, children: [] };
visited[key] = node;

if (child) {
adjacencyList[value].add(child);
if (parent) {
parent.children.push(visited[key]);
} else {
root = node;
}
}

child = value;
parent = visited[key];
}
}

return { adjacencyList, root };
return root;
};

/**
Expand All @@ -40,35 +47,7 @@ const createGraph = (contents: DSVRowArray<string>) => {
* @param contents CSV rows
*/
export const createMindmap = async (contents: DSVRowArray<string>) => {
const visited: Record<string, any> = [];
const graph = createGraph(contents);

// Create all nodes
visited[graph.root] = await miro.board.experimental.createMindmapNode({
nodeView: { content: graph.root },
});

const nodes = Object.keys(graph.adjacencyList);
for (const node of nodes) {
if (!visited[node]) {
visited[node] = await miro.board.experimental.createMindmapNode({
nodeView: {
content: node,
},
});
}
}

// Create parent-child relations
const createRelations = async (parent: string) => {
const parentItem = visited[parent];

for (const adjacent of graph.adjacencyList[parent]) {
const childItem = visited[adjacent];
await parentItem.add(childItem);
await createRelations(adjacent);
}
};
const root = createGraph(contents);

await createRelations(graph.root);
await miro.board.experimental.createMindmapNode(root);
};

2 comments on commit 2fbe748

@vercel
Copy link

@vercel vercel bot commented on 2fbe748 May 23, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

webhooks-manager – ./examples/webhooks-manager

webhooks-manager-miro-web.vercel.app
webhooks-manager-git-main-miro-web.vercel.app
webhooks-manager-sepia.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 2fbe748 May 23, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

app-examples-wordle – ./examples/wordle

app-examples-wordle-git-main-anthonyroux.vercel.app
app-examples-wordle.vercel.app
app-examples-wordle-anthonyroux.vercel.app

Please sign in to comment.