'use client' import { useState, useEffect } from 'react' import { ClusterVisualization } from '@/components/cluster-visualization' import { BridgeNotesDashboard } from '@/components/bridge-notes-dashboard' import { useRouter } from 'next/navigation' interface Cluster { clusterId: number name?: string noteIds: string[] } interface BridgeNote { noteId: string bridgeScore: number clustersConnected: number[] note?: { id: string title: string | null content: string } } export default function InsightsPage() { const router = useRouter() const [clusters, setClusters] = useState([]) const [bridgeNotes, setBridgeNotes] = useState([]) const [loading, setLoading] = useState(true) const [recalculating, setRecalculating] = useState(false) const [message, setMessage] = useState(null) useEffect(() => { loadClusters() }, []) const loadClusters = async () => { setLoading(true) try { const res = await fetch('/api/clusters') if (res.ok) { const data = await res.json() setClusters(data.clusters || []) if (data.message) { setMessage(data.message) } } } catch (error) { console.error('Error loading clusters:', error) setMessage('Failed to load clusters') } finally { setLoading(false) } } const recalculateClusters = async () => { setRecalculating(true) try { const res = await fetch('/api/clusters', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ force: true }) }) if (res.ok) { const data = await res.json() setClusters(data.clusters || []) setBridgeNotes(data.bridgeNotes || []) setMessage(data.message || 'Clusters recalculated successfully') } } catch (error) { console.error('Error recalculating clusters:', error) setMessage('Failed to recalculate clusters') } finally { setRecalculating(false) } } const handleNoteClick = (noteId: string, type: 'note' | 'cluster') => { if (type === 'note') { router.push(`/home?note=${noteId}`) } } return (
{/* Header */}

Insights

Discover thematic clusters and connections in your notes

{/* Stats Bar */}
Total Clusters
{clusters.length}
Bridge Notes
{bridgeNotes.length}
Notes Analyzed
{clusters.reduce((sum, c) => sum + c.noteIds.length, 0)}
{/* Message */} {message && (

{message}

)} {/* Loading State */} {loading && (

Analyzing your notes...

)} {/* Empty State */} {!loading && clusters.length === 0 && (

Not enough notes to analyze

Create at least 10 notes to start discovering clusters and connections

)} {/* Main Content */} {!loading && clusters.length > 0 && (
{/* Visualization */}

Cluster Visualization

{/* Dashboard */}
handleNoteClick(noteId, 'note')} />
)} {/* Cluster List */} {!loading && clusters.length > 0 && (

All Clusters

{clusters.map((cluster) => (

{cluster.name || `Cluster ${cluster.clusterId}`}

{cluster.noteIds.length} {cluster.noteIds.length === 1 ? 'note' : 'notes'}

))}
)}
) }