Skip to content
Open
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
10 changes: 0 additions & 10 deletions package-lock.json

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

107 changes: 94 additions & 13 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 📦 Imports
import React, { useState, useRef } from 'react';
import React, { useState, useRef, useEffect } from 'react';
import ReactFlow, {
Background,
Controls,
Expand All @@ -13,9 +13,15 @@ import html2canvas from 'html2canvas';
import { saveAs } from 'file-saver';

// 🧠 Initial State & Helpers
const initialNodes = [];
const initialEdges = [];
let nodeId = 1;
const initialNodes = JSON.parse(localStorage.getItem('mindmap-nodes')) || [];
const initialEdges = JSON.parse(localStorage.getItem('mindmap-edges')) || [];

// 🆔 Initialize nodeId by finding the max ID in existing nodes
const getMaxId = (nodes) => {
const ids = nodes.map((n) => parseInt(n.id)).filter((id) => !isNaN(id));
return ids.length > 0 ? Math.max(...ids) : 0;
};
let nodeId = getMaxId(initialNodes) + 1;

const getColor = (type) => {
switch (type) {
Expand All @@ -27,9 +33,9 @@ const getColor = (type) => {
};

// 🤖 AI Logic with Domino Effect Thinking (Updated: Using Puter AI API)
const createAIChildren = async (text, parentId, setNodes, setEdges, stopGenerationRef) => {
const createAIChildren = async (text, parentId, setNodes, setEdges, stopGenerationRef, depth = 1) => {
try {
if (stopGenerationRef.current) return;
if (stopGenerationRef.current || depth < 0) return;
const res = await axios.post(
'https://api.puter.com/v1/chat/completions',
{
Expand Down Expand Up @@ -93,10 +99,12 @@ Return only 3 outputs: one of each type.`
setNodes((nds) => [...nds, ...newNodes]);
setEdges((eds) => [...eds, ...newEdges]);

// Recursively expand each child one level deep
for (const n of newNodes) {
if (stopGenerationRef.current) break;
await createAIChildren(n.data.label, n.id, setNodes, setEdges, stopGenerationRef);
// Recursively expand each child if depth permits
if (depth > 0) {
for (const n of newNodes) {
if (stopGenerationRef.current) break;
await createAIChildren(n.data.label, n.id, setNodes, setEdges, stopGenerationRef, depth - 1);
}
}

} catch (err) {
Expand All @@ -111,6 +119,16 @@ Return only 3 outputs: one of each type.`
export default function App() {
const [nodes, setNodes] = useState(initialNodes);
const [edges, setEdges] = useState(initialEdges);

// 💾 Persist state to localStorage
useEffect(() => {
localStorage.setItem('mindmap-nodes', JSON.stringify(nodes));
}, [nodes]);

useEffect(() => {
localStorage.setItem('mindmap-edges', JSON.stringify(edges));
}, [edges]);

const [prompt, setPrompt] = useState('');
const [loading, setLoading] = useState(false);
const stopGenerationRef = useRef(false);
Expand Down Expand Up @@ -150,7 +168,7 @@ export default function App() {
setNodes([rootNode]);
setEdges([]);

await createAIChildren(prompt, rootId, setNodes, setEdges, stopGenerationRef);
await createAIChildren(prompt, rootId, setNodes, setEdges, stopGenerationRef, 1);

setPrompt('');
setLoading(false);
Expand All @@ -161,10 +179,21 @@ export default function App() {
setLoading(false);
};

const handleClear = () => {
if (window.confirm('Are you sure you want to clear the entire mind map?')) {
setNodes([]);
setEdges([]);
localStorage.removeItem('mindmap-nodes');
localStorage.removeItem('mindmap-edges');
nodeId = 1;
}
};

const onNodeClick = async (_event, node) => {
stopGenerationRef.current = false;
setLoading(true);
await createAIChildren(node.data.label, node.id, setNodes, setEdges, stopGenerationRef);
// When clicking a node, we only want to generate its immediate children (depth 0)
await createAIChildren(node.data.label, node.id, setNodes, setEdges, stopGenerationRef, 0);
setLoading(false);
};

Expand Down Expand Up @@ -230,6 +259,22 @@ export default function App() {
>
⏹ Stop
</button>
<button
onClick={handleClear}
disabled={loading || nodes.length === 0}
style={{
padding: '10px 20px',
backgroundColor: '#f44336',
color: 'white',
border: 'none',
borderRadius: '8px',
fontWeight: 'bold',
cursor: loading || nodes.length === 0 ? 'not-allowed' : 'pointer',
opacity: loading || nodes.length === 0 ? 0.7 : 1,
}}
>
Clear All
</button>
<button
onClick={handleExport}
disabled={loading || nodes.length === 0}
Expand All @@ -250,7 +295,7 @@ export default function App() {
</div>

{/* 🧠 ReactFlow Canvas */}
<div style={{ flexGrow: 1 }} ref={reactFlowWrapper}>
<div style={{ flexGrow: 1, position: 'relative' }} ref={reactFlowWrapper}>
<ReactFlow
nodes={nodes}
edges={edges}
Expand All @@ -264,6 +309,42 @@ export default function App() {
<Controls />
<Background variant="dots" gap={12} size={1} />
</ReactFlow>

{/* 🎨 Color Legend */}
<div style={{
position: 'absolute',
bottom: '20px',
left: '20px',
backgroundColor: 'rgba(34, 34, 34, 0.9)',
padding: '12px',
borderRadius: '8px',
border: '1px solid #444',
color: '#fff',
zIndex: 10,
fontSize: '0.85rem',
display: 'flex',
flexDirection: 'column',
gap: '8px',
pointerEvents: 'none'
}}>
<div style={{ fontWeight: 'bold', borderBottom: '1px solid #444', paddingBottom: '4px', marginBottom: '4px' }}>Legend</div>
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<div style={{ width: '12px', height: '12px', borderRadius: '50%', backgroundColor: '#6200ea' }}></div>
<span>Root Event</span>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<div style={{ width: '12px', height: '12px', borderRadius: '50%', backgroundColor: '#00c853' }}></div>
<span>Positive Impact</span>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<div style={{ width: '12px', height: '12px', borderRadius: '50%', backgroundColor: '#ffd600' }}></div>
<span>Neutral Impact</span>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<div style={{ width: '12px', height: '12px', borderRadius: '50%', backgroundColor: '#d50000' }}></div>
<span>Negative Impact</span>
</div>
</div>
</div>
</div>
);
Expand Down