|
| 1 | +import { useState, useCallback, useMemo } from 'react'; |
| 2 | +import { useNavigate } from 'react-router-dom'; |
| 3 | +import { useClusters } from '../hooks/useClusters'; |
| 4 | +import { ScatterPlot } from '../components/ScatterPlot'; |
| 5 | +import type { ProjectionPoint } from '../types'; |
| 6 | + |
| 7 | +const CLUSTER_COLORS = [ |
| 8 | + '#3b82f6', '#f59e0b', '#22c55e', '#a78bfa', '#f43f5e', |
| 9 | + '#6366f1', '#14b8a6', '#e879f9', '#fb923c', '#64748b', |
| 10 | +]; |
| 11 | + |
| 12 | +export function ClusterView() { |
| 13 | + const [threshold, setThreshold] = useState(0.6); |
| 14 | + const [minSize, setMinSize] = useState(2); |
| 15 | + const { data, isLoading, isError } = useClusters(threshold, minSize); |
| 16 | + const navigate = useNavigate(); |
| 17 | + |
| 18 | + const handlePointClick = useCallback((hash: string) => { |
| 19 | + navigate(`/memories/${hash}`); |
| 20 | + }, [navigate]); |
| 21 | + |
| 22 | + // Construire les points pour le ScatterPlot et la colorMap |
| 23 | + const { points, colorMap } = useMemo(() => { |
| 24 | + if (!data || data.clusters.length === 0) return { points: [], colorMap: {} }; |
| 25 | + |
| 26 | + const pts: ProjectionPoint[] = []; |
| 27 | + const cMap: Record<string, string> = {}; |
| 28 | + |
| 29 | + for (const cluster of data.clusters) { |
| 30 | + const color = CLUSTER_COLORS[cluster.id % CLUSTER_COLORS.length]; |
| 31 | + for (const mem of cluster.members) { |
| 32 | + pts.push({ |
| 33 | + content_hash: mem.content_hash, |
| 34 | + x: cluster.centroid.x + (Math.random() - 0.5) * 0.5, |
| 35 | + y: cluster.centroid.y + (Math.random() - 0.5) * 0.5, |
| 36 | + content: mem.content.length > 100 ? mem.content.substring(0, 100) + '...' : mem.content, |
| 37 | + memory_type: mem.memory_type, |
| 38 | + tags: mem.tags, |
| 39 | + created_at_iso: mem.created_at_iso, |
| 40 | + }); |
| 41 | + cMap[mem.content_hash] = color; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return { points: pts, colorMap: cMap }; |
| 46 | + }, [data]); |
| 47 | + |
| 48 | + if (isLoading) { |
| 49 | + return <p style={{ color: 'var(--text-muted)' }}>Chargement...</p>; |
| 50 | + } |
| 51 | + |
| 52 | + if (isError) { |
| 53 | + return <p style={{ color: 'var(--error)' }}>Erreur lors du chargement des clusters.</p>; |
| 54 | + } |
| 55 | + |
| 56 | + const clusters = data?.clusters ?? []; |
| 57 | + const totalClusters = data?.total_clusters ?? 0; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '16px' }}> |
| 62 | + <h2 style={{ margin: 0, fontSize: '16px', fontWeight: 600, color: 'var(--text-primary)' }}> |
| 63 | + Clusters semantiques |
| 64 | + </h2> |
| 65 | + <span style={{ fontSize: '12px', color: 'var(--text-muted)' }}> |
| 66 | + {totalClusters} clusters |
| 67 | + </span> |
| 68 | + </div> |
| 69 | + |
| 70 | + {/* Controles */} |
| 71 | + <div style={{ |
| 72 | + display: 'flex', |
| 73 | + gap: '24px', |
| 74 | + marginBottom: '16px', |
| 75 | + padding: '12px 16px', |
| 76 | + backgroundColor: 'var(--bg-surface)', |
| 77 | + borderRadius: 'var(--radius-md)', |
| 78 | + border: '1px solid var(--border-subtle)', |
| 79 | + alignItems: 'center', |
| 80 | + flexWrap: 'wrap', |
| 81 | + }}> |
| 82 | + <label style={{ display: 'flex', alignItems: 'center', gap: '8px', fontSize: '13px', color: 'var(--text-secondary)' }}> |
| 83 | + Seuil (threshold) |
| 84 | + <input |
| 85 | + type="range" |
| 86 | + min="0.3" |
| 87 | + max="0.9" |
| 88 | + step="0.05" |
| 89 | + value={threshold} |
| 90 | + onChange={e => setThreshold(parseFloat(e.target.value))} |
| 91 | + aria-label="Seuil" |
| 92 | + style={{ width: '100px' }} |
| 93 | + /> |
| 94 | + <span style={{ fontSize: '12px', color: 'var(--text-muted)', minWidth: '32px' }}>{threshold.toFixed(2)}</span> |
| 95 | + </label> |
| 96 | + |
| 97 | + <label style={{ display: 'flex', alignItems: 'center', gap: '8px', fontSize: '13px', color: 'var(--text-secondary)' }}> |
| 98 | + Taille min (min_size) |
| 99 | + <input |
| 100 | + type="range" |
| 101 | + min="2" |
| 102 | + max="10" |
| 103 | + step="1" |
| 104 | + value={minSize} |
| 105 | + onChange={e => setMinSize(parseInt(e.target.value))} |
| 106 | + aria-label="Taille minimale" |
| 107 | + style={{ width: '100px' }} |
| 108 | + /> |
| 109 | + <span style={{ fontSize: '12px', color: 'var(--text-muted)', minWidth: '24px' }}>{minSize}</span> |
| 110 | + </label> |
| 111 | + </div> |
| 112 | + |
| 113 | + {/* Layout : ScatterPlot + liste */} |
| 114 | + {clusters.length === 0 ? ( |
| 115 | + <p style={{ color: 'var(--text-muted)', textAlign: 'center', padding: '40px 0' }}> |
| 116 | + Aucun cluster trouve avec ces parametres. |
| 117 | + </p> |
| 118 | + ) : ( |
| 119 | + <div style={{ display: 'flex', gap: '16px' }}> |
| 120 | + {/* ScatterPlot a gauche (60%) */} |
| 121 | + <div style={{ flex: '0 0 60%' }}> |
| 122 | + <ScatterPlot |
| 123 | + points={points} |
| 124 | + onPointClick={handlePointClick} |
| 125 | + width={650} |
| 126 | + height={500} |
| 127 | + colorMap={colorMap} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + |
| 131 | + {/* Liste des clusters a droite (40%) */} |
| 132 | + <div style={{ flex: '1', overflow: 'auto', maxHeight: '500px' }}> |
| 133 | + {clusters.map(cluster => ( |
| 134 | + <div |
| 135 | + key={cluster.id} |
| 136 | + style={{ |
| 137 | + padding: '12px 16px', |
| 138 | + marginBottom: '8px', |
| 139 | + backgroundColor: 'var(--bg-surface)', |
| 140 | + borderRadius: 'var(--radius-md)', |
| 141 | + border: '1px solid var(--border-subtle)', |
| 142 | + }} |
| 143 | + > |
| 144 | + <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '6px' }}> |
| 145 | + <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}> |
| 146 | + <span style={{ |
| 147 | + width: '10px', |
| 148 | + height: '10px', |
| 149 | + borderRadius: '50%', |
| 150 | + backgroundColor: CLUSTER_COLORS[cluster.id % CLUSTER_COLORS.length], |
| 151 | + display: 'inline-block', |
| 152 | + }} /> |
| 153 | + <span style={{ fontSize: '13px', fontWeight: 600, color: 'var(--text-primary)' }}> |
| 154 | + {cluster.label} |
| 155 | + </span> |
| 156 | + </div> |
| 157 | + <span style={{ fontSize: '12px', color: 'var(--text-muted)' }}> |
| 158 | + {cluster.size} memoires |
| 159 | + </span> |
| 160 | + </div> |
| 161 | + <div style={{ fontSize: '11px', color: 'var(--text-secondary)' }}> |
| 162 | + Similarite : {cluster.avg_similarity.toFixed(2)} |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + ))} |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + )} |
| 169 | + </div> |
| 170 | + ); |
| 171 | +} |
0 commit comments