feat: add mindmap components from jarvis frontend

- Copied mindmap visualization components (ReactFlow-based interactive graph)
- Added MindmapViewer, ReactFlowEditor, MermaidViewer
- Included all node types: Concept, Task, Idea, Project
- Added controls: NodeCreateModal, ExportButton
- Created mindmap route at /mindmap
- Added useGraphData hook for knowledge graph API
- Copied auth-client and api utilities (dependencies)

Note: Requires better-auth packages to be installed for full compilation
This commit is contained in:
Jason Woltje
2026-01-29 21:45:56 -06:00
parent af8f5df111
commit aa267b56d8
15 changed files with 1758 additions and 0 deletions

View File

@@ -0,0 +1,212 @@
'use client';
import { useState, useRef, useEffect } from 'react';
import { GraphData, MermaidData } from '../hooks/useGraphData';
interface ExportButtonProps {
graph: GraphData | null;
mermaid: MermaidData | null;
}
type ExportFormat = 'json' | 'mermaid' | 'png' | 'svg';
export function ExportButton({ graph, mermaid }: ExportButtonProps) {
const [isOpen, setIsOpen] = useState(false);
const [isExporting, setIsExporting] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);
const downloadFile = (content: string, filename: string, mimeType: string) => {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
const exportAsJson = () => {
if (!graph) return;
const content = JSON.stringify(graph, null, 2);
downloadFile(content, 'knowledge-graph.json', 'application/json');
};
const exportAsMermaid = () => {
if (!mermaid) return;
downloadFile(mermaid.diagram, 'knowledge-graph.mmd', 'text/plain');
};
const exportAsPng = async () => {
const svgElement = document.querySelector('.mermaid-container svg') as SVGElement;
if (!svgElement) {
alert('Please switch to Diagram view first');
return;
}
setIsExporting(true);
try {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) return;
const svgData = new XMLSerializer().serializeToString(svgElement);
const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const url = URL.createObjectURL(svgBlob);
const img = new Image();
img.onload = () => {
canvas.width = img.width * 2;
canvas.height = img.height * 2;
ctx.scale(2, 2);
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0);
URL.revokeObjectURL(url);
canvas.toBlob((blob) => {
if (blob) {
const pngUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = pngUrl;
link.download = 'knowledge-graph.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(pngUrl);
}
setIsExporting(false);
}, 'image/png');
};
img.onerror = () => {
setIsExporting(false);
alert('Failed to export image');
};
img.src = url;
} catch (error) {
console.error('Export error:', error);
setIsExporting(false);
alert('Failed to export image');
}
};
const exportAsSvg = () => {
const svgElement = document.querySelector('.mermaid-container svg') as SVGElement;
if (!svgElement) {
alert('Please switch to Diagram view first');
return;
}
const svgData = new XMLSerializer().serializeToString(svgElement);
downloadFile(svgData, 'knowledge-graph.svg', 'image/svg+xml');
};
const handleExport = async (format: ExportFormat) => {
setIsOpen(false);
switch (format) {
case 'json':
exportAsJson();
break;
case 'mermaid':
exportAsMermaid();
break;
case 'png':
await exportAsPng();
break;
case 'svg':
exportAsSvg();
break;
}
};
return (
<div className="relative" ref={dropdownRef}>
<button
onClick={() => setIsOpen(!isOpen)}
disabled={isExporting}
className="px-3 py-1.5 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors disabled:opacity-50"
>
{isExporting ? (
<span className="flex items-center gap-2">
<svg className="animate-spin w-4 h-4" fill="none" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
</svg>
Exporting...
</span>
) : (
<span className="flex items-center gap-1">
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4" />
</svg>
Export
</span>
)}
</button>
{isOpen && (
<div className="absolute right-0 mt-2 w-48 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 py-1 z-50">
<button
onClick={() => handleExport('json')}
disabled={!graph}
className="w-full px-4 py-2 text-left text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
<span className="flex items-center gap-2">
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
Export as JSON
</span>
</button>
<button
onClick={() => handleExport('mermaid')}
disabled={!mermaid}
className="w-full px-4 py-2 text-left text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
<span className="flex items-center gap-2">
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4" />
</svg>
Export as Mermaid
</span>
</button>
<hr className="my-1 border-gray-200 dark:border-gray-700" />
<button
onClick={() => handleExport('svg')}
className="w-full px-4 py-2 text-left text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
>
<span className="flex items-center gap-2">
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
Export as SVG
</span>
</button>
<button
onClick={() => handleExport('png')}
className="w-full px-4 py-2 text-left text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
>
<span className="flex items-center gap-2">
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
Export as PNG
</span>
</button>
</div>
)}
</div>
);
}

View File

@@ -0,0 +1,168 @@
'use client';
import { useState } from 'react';
import { NodeCreateInput } from '../hooks/useGraphData';
const NODE_TYPES = [
{ value: 'concept', label: 'Concept', color: '#6366f1' },
{ value: 'idea', label: 'Idea', color: '#f59e0b' },
{ value: 'task', label: 'Task', color: '#10b981' },
{ value: 'project', label: 'Project', color: '#3b82f6' },
{ value: 'person', label: 'Person', color: '#ec4899' },
{ value: 'note', label: 'Note', color: '#8b5cf6' },
{ value: 'question', label: 'Question', color: '#f97316' },
];
interface NodeCreateModalProps {
onClose: () => void;
onCreate: (node: NodeCreateInput) => void;
}
export function NodeCreateModal({ onClose, onCreate }: NodeCreateModalProps) {
const [title, setTitle] = useState('');
const [nodeType, setNodeType] = useState('concept');
const [content, setContent] = useState('');
const [tags, setTags] = useState('');
const [domain, setDomain] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!title.trim()) return;
setIsSubmitting(true);
try {
await onCreate({
title: title.trim(),
node_type: nodeType,
content: content.trim() || null,
tags: tags
.split(',')
.map((t) => t.trim())
.filter(Boolean),
domain: domain.trim() || null,
metadata: {},
});
} finally {
setIsSubmitting(false);
}
};
return (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-md mx-4">
<div className="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700">
<h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100">
Create Node
</h2>
<button
onClick={onClose}
className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<form onSubmit={handleSubmit} className="p-4 space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Title *
</label>
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Enter node title"
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent"
required
autoFocus
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Type
</label>
<div className="grid grid-cols-4 gap-2">
{NODE_TYPES.map((type) => (
<button
key={type.value}
type="button"
onClick={() => setNodeType(type.value)}
className={`px-2 py-1.5 text-xs font-medium rounded border transition-colors ${
nodeType === type.value
? 'border-transparent text-white'
: 'border-gray-200 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'
}`}
style={{
backgroundColor: nodeType === type.value ? type.color : undefined,
}}
>
{type.label}
</button>
))}
</div>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Content
</label>
<textarea
value={content}
onChange={(e) => setContent(e.target.value)}
placeholder="Optional description or notes"
rows={3}
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Tags
</label>
<input
type="text"
value={tags}
onChange={(e) => setTags(e.target.value)}
placeholder="Comma-separated tags"
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Domain
</label>
<input
type="text"
value={domain}
onChange={(e) => setDomain(e.target.value)}
placeholder="e.g., Work, Personal, Project Name"
className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div className="flex justify-end gap-3 pt-4">
<button
type="button"
onClick={onClose}
className="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors"
>
Cancel
</button>
<button
type="submit"
disabled={!title.trim() || isSubmitting}
className="px-4 py-2 text-sm font-medium bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
{isSubmitting ? 'Creating...' : 'Create'}
</button>
</div>
</form>
</div>
</div>
);
}