forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
285 lines (257 loc) · 11.2 KB
/
Copy pathpage.tsx
File metadata and controls
285 lines (257 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'use client';
import { useMemo, useState } from 'react';
import WordCloud, { type WordEntry } from '@/components/charts/WordCloud';
type Sentiment = 'positive' | 'negative' | 'neutral';
const STOP_WORDS = new Set([
'the', 'a', 'an', 'is', 'it', 'in', 'on', 'at', 'to', 'for', 'of', 'and',
'or', 'but', 'with', 'this', 'that', 'are', 'was', 'be', 'as', 'by', 'from',
]);
const RAW_WORDS: WordEntry[] = [
{ text: 'amazing', count: 42, sentiment: 'positive' },
{ text: 'location', count: 38, sentiment: 'neutral' },
{ text: 'gist', count: 35, sentiment: 'neutral' },
{ text: 'broken', count: 30, sentiment: 'negative' },
{ text: 'helpful', count: 28, sentiment: 'positive' },
{ text: 'map', count: 26, sentiment: 'neutral' },
{ text: 'slow', count: 24, sentiment: 'negative' },
{ text: 'great', count: 22, sentiment: 'positive' },
{ text: 'community', count: 20, sentiment: 'neutral' },
{ text: 'error', count: 18, sentiment: 'negative' },
{ text: 'love', count: 17, sentiment: 'positive' },
{ text: 'pin', count: 16, sentiment: 'neutral' },
{ text: 'crash', count: 15, sentiment: 'negative' },
{ text: 'fast', count: 14, sentiment: 'positive' },
{ text: 'nearby', count: 13, sentiment: 'neutral' },
{ text: 'useful', count: 12, sentiment: 'positive' },
{ text: 'confusing', count: 11, sentiment: 'negative' },
{ text: 'stellar', count: 10, sentiment: 'positive' },
{ text: 'update', count: 9, sentiment: 'neutral' },
{ text: 'bug', count: 8, sentiment: 'negative' },
{ text: 'discover', count: 7, sentiment: 'positive' },
{ text: 'anonymous', count: 6, sentiment: 'neutral' },
{ text: 'missing', count: 5, sentiment: 'negative' },
{ text: 'explore', count: 5, sentiment: 'positive' },
{ text: 'local', count: 4, sentiment: 'neutral' },
];
const SENTIMENT_BG: Record<Sentiment, string> = {
positive: 'bg-green-100 text-green-700 dark:bg-green-900/40 dark:text-green-400',
negative: 'bg-red-100 text-red-700 dark:bg-red-900/40 dark:text-red-400',
neutral: 'bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-300',
};
export default function WordCloudPage() {
const [activeFilter, setActiveFilter] = useState<Sentiment | 'all'>('all');
const [selectedWord, setSelectedWord] = useState<string | null>(null);
const words = useMemo(() => RAW_WORDS.filter((w) => !STOP_WORDS.has(w.text)), []);
const filtered = useMemo(
() => (activeFilter === 'all' ? words : words.filter((w) => w.sentiment === activeFilter)),
[words, activeFilter],
);
const filteredGists = useMemo(() => {
if (!selectedWord) return [];
return [
`Gist near Market St: "This spot is ${selectedWord}, check it out!"`,
`Gist in Downtown: "Noticed something ${selectedWord} here today."`,
`Gist at Central Park: "The vibe is ${selectedWord} around this area."`,
];
}, [selectedWord]);
const handleWordClick = (word: string) => setSelectedWord((prev) => (prev === word ? null : word));
return (
<div className="space-y-6">
{/* Sentiment filter */}
<div className="flex flex-wrap gap-2">
{(['all', 'positive', 'negative', 'neutral'] as const).map((s) => (
<button
key={s}
onClick={() => setActiveFilter(s)}
className={`rounded-full px-3 py-1 text-xs font-medium capitalize transition-colors ${
activeFilter === s
? 'bg-indigo-600 text-white'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700'
}`}
>
{s === 'all' ? 'All' : s}
</button>
))}
</div>
{/* Word cloud */}
<div className="rounded-xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-gray-900">
<WordCloud words={filtered} selectedWord={selectedWord} onWordClick={handleWordClick} />
</div>
{/* Legend */}
<div className="flex flex-wrap gap-3">
{(['positive', 'negative', 'neutral'] as Sentiment[]).map((s) => (
<span key={s} className={`rounded-full px-3 py-1 text-xs font-medium capitalize ${SENTIMENT_BG[s]}`}>
● {s}
</span>
))}
<span className="text-xs text-gray-400 self-center">· Word size = frequency · Click to filter gists</span>
</div>
{/* Filtered gists */}
{selectedWord && (
<div className="rounded-xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-gray-900">
<h2 className="text-sm font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400 mb-3">
Gists containing “{selectedWord}”
</h2>
<ul className="space-y-2">
{filteredGists.map((g, i) => (
<li key={i} className="text-sm text-gray-700 dark:text-gray-300 border-l-2 border-indigo-400 pl-3">
{g}
</li>
))}
</ul>
<button
onClick={() => setSelectedWord(null)}
className="mt-3 text-xs text-gray-400 hover:text-gray-600 dark:hover:text-gray-200"
>
Clear filter
</button>
</div>
)}
</div>
);
}
interface WordEntry {
text: string;
count: number;
sentiment: Sentiment;
}
const STOP_WORDS = new Set([
'the', 'a', 'an', 'is', 'it', 'in', 'on', 'at', 'to', 'for', 'of', 'and',
'or', 'but', 'with', 'this', 'that', 'are', 'was', 'be', 'as', 'by', 'from',
]);
const RAW_WORDS: WordEntry[] = [
{ text: 'amazing', count: 42, sentiment: 'positive' },
{ text: 'location', count: 38, sentiment: 'neutral' },
{ text: 'gist', count: 35, sentiment: 'neutral' },
{ text: 'broken', count: 30, sentiment: 'negative' },
{ text: 'helpful', count: 28, sentiment: 'positive' },
{ text: 'map', count: 26, sentiment: 'neutral' },
{ text: 'slow', count: 24, sentiment: 'negative' },
{ text: 'great', count: 22, sentiment: 'positive' },
{ text: 'community', count: 20, sentiment: 'neutral' },
{ text: 'error', count: 18, sentiment: 'negative' },
{ text: 'love', count: 17, sentiment: 'positive' },
{ text: 'pin', count: 16, sentiment: 'neutral' },
{ text: 'crash', count: 15, sentiment: 'negative' },
{ text: 'fast', count: 14, sentiment: 'positive' },
{ text: 'nearby', count: 13, sentiment: 'neutral' },
{ text: 'useful', count: 12, sentiment: 'positive' },
{ text: 'confusing', count: 11, sentiment: 'negative' },
{ text: 'stellar', count: 10, sentiment: 'positive' },
{ text: 'update', count: 9, sentiment: 'neutral' },
{ text: 'bug', count: 8, sentiment: 'negative' },
{ text: 'discover', count: 7, sentiment: 'positive' },
{ text: 'anonymous', count: 6, sentiment: 'neutral' },
{ text: 'missing', count: 5, sentiment: 'negative' },
{ text: 'explore', count: 5, sentiment: 'positive' },
{ text: 'local', count: 4, sentiment: 'neutral' },
];
const SENTIMENT_COLORS: Record<Sentiment, string> = {
positive: '#16a34a',
negative: '#dc2626',
neutral: '#6b7280',
};
const SENTIMENT_BG: Record<Sentiment, string> = {
positive: 'bg-green-100 text-green-700 dark:bg-green-900/40 dark:text-green-400',
negative: 'bg-red-100 text-red-700 dark:bg-red-900/40 dark:text-red-400',
neutral: 'bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-300',
};
export default function WordCloudPage() {
const [activeFilter, setActiveFilter] = useState<Sentiment | 'all'>('all');
const [selectedWord, setSelectedWord] = useState<string | null>(null);
const words = useMemo(
() => RAW_WORDS.filter((w) => !STOP_WORDS.has(w.text)),
[],
);
const filtered = useMemo(
() => (activeFilter === 'all' ? words : words.filter((w) => w.sentiment === activeFilter)),
[words, activeFilter],
);
const maxCount = useMemo(() => Math.max(...words.map((w) => w.count)), [words]);
const filteredGists = useMemo(() => {
if (!selectedWord) return [];
return [
`Gist near Market St: "This spot is ${selectedWord}, check it out!"`,
`Gist in Downtown: "Noticed something ${selectedWord} here today."`,
`Gist at Central Park: "The vibe is ${selectedWord} around this area."`,
];
}, [selectedWord]);
return (
<div className="space-y-6">
{/* Sentiment filter */}
<div className="flex flex-wrap gap-2">
{(['all', 'positive', 'negative', 'neutral'] as const).map((s) => (
<button
key={s}
onClick={() => setActiveFilter(s)}
className={`rounded-full px-3 py-1 text-xs font-medium capitalize transition-colors ${
activeFilter === s
? 'bg-indigo-600 text-white'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700'
}`}
>
{s === 'all' ? 'All' : s}
</button>
))}
</div>
{/* Word cloud */}
<div className="rounded-xl border border-gray-200 bg-white p-6 dark:border-gray-800 dark:bg-gray-900 min-h-64">
<div className="flex flex-wrap gap-3 items-end justify-center">
{filtered.map((word) => {
const ratio = word.count / maxCount;
const fontSize = 12 + ratio * 32; // 12px – 44px
const isSelected = selectedWord === word.text;
return (
<button
key={word.text}
onClick={() => setSelectedWord(isSelected ? null : word.text)}
style={{
fontSize,
color: SENTIMENT_COLORS[word.sentiment],
fontWeight: ratio > 0.6 ? 700 : ratio > 0.3 ? 600 : 400,
opacity: selectedWord && !isSelected ? 0.4 : 1,
outline: isSelected ? `2px solid ${SENTIMENT_COLORS[word.sentiment]}` : 'none',
outlineOffset: 3,
}}
className="rounded px-1 transition-opacity hover:opacity-100"
title={`${word.text}: ${word.count} occurrences (${word.sentiment})`}
>
{word.text}
</button>
);
})}
</div>
</div>
{/* Legend */}
<div className="flex flex-wrap gap-3">
{(['positive', 'negative', 'neutral'] as Sentiment[]).map((s) => (
<span key={s} className={`rounded-full px-3 py-1 text-xs font-medium capitalize ${SENTIMENT_BG[s]}`}>
● {s}
</span>
))}
<span className="text-xs text-gray-400 self-center">· Word size = frequency · Click to filter gists</span>
</div>
{/* Filtered gists */}
{selectedWord && (
<div className="rounded-xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-gray-900">
<h2 className="text-sm font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400 mb-3">
Gists containing “{selectedWord}”
</h2>
<ul className="space-y-2">
{filteredGists.map((g, i) => (
<li key={i} className="text-sm text-gray-700 dark:text-gray-300 border-l-2 border-indigo-400 pl-3">
{g}
</li>
))}
</ul>
<button
onClick={() => setSelectedWord(null)}
className="mt-3 text-xs text-gray-400 hover:text-gray-600 dark:hover:text-gray-200"
>
Clear filter
</button>
</div>
)}
</div>
);
}