-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.jsx
More file actions
247 lines (222 loc) · 7.92 KB
/
App.jsx
File metadata and controls
247 lines (222 loc) · 7.92 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
import { useState, useEffect } from 'react';
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js';
import { Doughnut } from 'react-chartjs-2';
// Register Chart.js components
ChartJS.register(ArcElement, Tooltip, Legend);
// API Configuration
// Create a .env file with: VITE_API_KEY=your-api-key-here
// Get a free key at: https://dashboard.apiverve.com
const API_KEY = import.meta.env.VITE_API_KEY;
const API_URL = 'https://api.apiverve.com/v1/sentimentanalysis';
function App() {
const [text, setText] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [history, setHistory] = useState([]);
// Load history from localStorage on mount
useEffect(() => {
const saved = localStorage.getItem('sentimentHistory');
if (saved) {
setHistory(JSON.parse(saved));
}
}, []);
// Save history to localStorage when it changes
useEffect(() => {
localStorage.setItem('sentimentHistory', JSON.stringify(history));
}, [history]);
// Analyze sentiment
const analyzeSentiment = async () => {
if (!text.trim()) return;
if (!API_KEY) {
setError('Add your API key to .env file (VITE_API_KEY=your-key)');
return;
}
setLoading(true);
setError('');
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY
},
body: JSON.stringify({ text })
});
const data = await response.json();
if (data.status === 'ok' && data.data) {
const result = {
id: Date.now(),
text: text.slice(0, 100) + (text.length > 100 ? '...' : ''),
sentiment: data.data.sentimentText || data.data.sentiment,
score: data.data.comparative || 0,
timestamp: new Date().toLocaleString()
};
setHistory(prev => [result, ...prev.slice(0, 49)]); // Keep last 50
setText('');
} else {
setError(data.error || 'Failed to analyze sentiment');
}
} catch (err) {
setError('API request failed. Check your API key.');
console.error('API Error:', err);
} finally {
setLoading(false);
}
};
// Calculate chart data from history
const getChartData = () => {
const counts = { positive: 0, negative: 0, neutral: 0 };
history.forEach(item => {
const sentiment = item.sentiment?.toLowerCase() || 'neutral';
if (sentiment.includes('positive')) counts.positive++;
else if (sentiment.includes('negative')) counts.negative++;
else counts.neutral++;
});
return {
labels: ['Positive', 'Negative', 'Neutral'],
datasets: [{
data: [counts.positive, counts.negative, counts.neutral],
backgroundColor: ['#10b981', '#ef4444', '#6b7280'],
borderWidth: 0
}]
};
};
// Get sentiment color
const getSentimentColor = (sentiment) => {
const s = sentiment?.toLowerCase() || '';
if (s.includes('positive')) return '#10b981';
if (s.includes('negative')) return '#ef4444';
return '#6b7280';
};
// Clear history
const clearHistory = () => {
setHistory([]);
localStorage.removeItem('sentimentHistory');
};
return (
<div className="app">
<header>
<h1>Sentiment Dashboard</h1>
<p className="subtitle">Analyze text sentiment with AI-powered insights</p>
</header>
<main>
{/* Input Section */}
<section className="input-section">
<textarea
value={text}
onChange={(e) => setText(e.target.value)}
placeholder="Enter text to analyze sentiment..."
rows={4}
/>
<button
onClick={analyzeSentiment}
disabled={loading || !text.trim()}
className="analyze-btn"
>
{loading ? 'Analyzing...' : 'Analyze Sentiment'}
</button>
{error && <div className="error">{error}</div>}
</section>
{/* Dashboard Grid */}
<div className="dashboard">
{/* Chart Section */}
<section className="chart-section">
<h2>Sentiment Overview</h2>
{history.length > 0 ? (
<div className="chart-container">
<Doughnut
data={getChartData()}
options={{
plugins: {
legend: { position: 'bottom' }
},
cutout: '60%'
}}
/>
</div>
) : (
<div className="empty-chart">
<p>No data yet</p>
<span>Analyze some text to see the chart</span>
</div>
)}
</section>
{/* Stats Section */}
<section className="stats-section">
<h2>Quick Stats</h2>
<div className="stats-grid">
<div className="stat-card">
<span className="stat-value">{history.length}</span>
<span className="stat-label">Total Analyzed</span>
</div>
<div className="stat-card positive">
<span className="stat-value">
{history.filter(h => h.sentiment?.toLowerCase().includes('positive')).length}
</span>
<span className="stat-label">Positive</span>
</div>
<div className="stat-card negative">
<span className="stat-value">
{history.filter(h => h.sentiment?.toLowerCase().includes('negative')).length}
</span>
<span className="stat-label">Negative</span>
</div>
<div className="stat-card neutral">
<span className="stat-value">
{history.filter(h => {
const s = h.sentiment?.toLowerCase() || '';
return !s.includes('positive') && !s.includes('negative');
}).length}
</span>
<span className="stat-label">Neutral</span>
</div>
</div>
</section>
</div>
{/* History Section */}
<section className="history-section">
<div className="history-header">
<h2>Analysis History</h2>
{history.length > 0 && (
<button onClick={clearHistory} className="clear-btn">
Clear All
</button>
)}
</div>
{history.length > 0 ? (
<div className="history-list">
{history.map(item => (
<div key={item.id} className="history-item">
<div
className="sentiment-badge"
style={{ backgroundColor: getSentimentColor(item.sentiment) }}
>
{item.sentiment}
</div>
<p className="history-text">{item.text}</p>
<span className="history-time">{item.timestamp}</span>
</div>
))}
</div>
) : (
<div className="empty-history">
<p>No analysis history yet</p>
<span>Start by analyzing some text above</span>
</div>
)}
</section>
</main>
<footer>
<p>
Powered by{' '}
<a href="https://apiverve.com/marketplace/sentimentanalysis?utm_source=github&utm_medium=tutorial&utm_campaign=sentiment-dashboard-react-tutorial"
target="_blank"
rel="noopener noreferrer">
APIVerve Sentiment Analysis API
</a>
</p>
</footer>
</div>
);
}
export default App;