Spaces:
Running
Running
update src/ tutorial & data management
Browse files- src/components/FocusPageLocal.jsx +1 -1
- src/components/Records.jsx +36 -53
src/components/FocusPageLocal.jsx
CHANGED
|
@@ -564,7 +564,7 @@ function FocusPageLocal({ videoManager, sessionResult, setSessionResult, isActiv
|
|
| 564 |
},
|
| 565 |
{
|
| 566 |
title: 'Sync across devices',
|
| 567 |
-
text: 'Your progress is automatically saved to this browser. You can migrate your data anytime via the Data Management section at the
|
| 568 |
}
|
| 569 |
];
|
| 570 |
|
|
|
|
| 564 |
},
|
| 565 |
{
|
| 566 |
title: 'Sync across devices',
|
| 567 |
+
text: 'Your progress is automatically saved to this browser. You can migrate your data anytime via the Data Management section at the top of My Records.'
|
| 568 |
}
|
| 569 |
];
|
| 570 |
|
src/components/Records.jsx
CHANGED
|
@@ -14,7 +14,6 @@ function Records() {
|
|
| 14 |
|
| 15 |
const fileInputRef = useRef(null);
|
| 16 |
|
| 17 |
-
// Format a session duration.
|
| 18 |
const formatDuration = (seconds) => {
|
| 19 |
const safeSeconds = Math.max(0, Number(seconds) || 0);
|
| 20 |
const mins = Math.floor(safeSeconds / 60);
|
|
@@ -22,7 +21,6 @@ function Records() {
|
|
| 22 |
return `${mins}m ${secs}s`;
|
| 23 |
};
|
| 24 |
|
| 25 |
-
// Format a session timestamp for table display.
|
| 26 |
const formatDate = (dateString) => {
|
| 27 |
const date = new Date(dateString);
|
| 28 |
return date.toLocaleDateString('en-US', {
|
|
@@ -159,7 +157,6 @@ function Records() {
|
|
| 159 |
});
|
| 160 |
};
|
| 161 |
|
| 162 |
-
// Load session rows for the selected filter.
|
| 163 |
const loadSessions = async (filterType) => {
|
| 164 |
setLoading(true);
|
| 165 |
try {
|
|
@@ -174,7 +171,6 @@ function Records() {
|
|
| 174 |
}
|
| 175 |
};
|
| 176 |
|
| 177 |
-
// Draw the session score chart.
|
| 178 |
const drawChart = (data) => {
|
| 179 |
const canvas = chartRef.current;
|
| 180 |
if (!canvas) return;
|
|
@@ -183,7 +179,6 @@ function Records() {
|
|
| 183 |
const width = canvas.width = canvas.offsetWidth;
|
| 184 |
const height = canvas.height = 300;
|
| 185 |
|
| 186 |
-
// Clear the canvas before each redraw.
|
| 187 |
ctx.clearRect(0, 0, width, height);
|
| 188 |
|
| 189 |
if (data.length === 0) {
|
|
@@ -194,17 +189,14 @@ function Records() {
|
|
| 194 |
return;
|
| 195 |
}
|
| 196 |
|
| 197 |
-
// Use at most the latest 20 sessions in the chart.
|
| 198 |
const displayData = data.slice(0, 20).reverse();
|
| 199 |
const padding = 50;
|
| 200 |
const chartWidth = width - padding * 2;
|
| 201 |
const chartHeight = height - padding * 2;
|
| 202 |
const barWidth = chartWidth / displayData.length;
|
| 203 |
|
| 204 |
-
// Use a normalized max score for chart scaling.
|
| 205 |
const maxScore = 1.0;
|
| 206 |
|
| 207 |
-
// Draw the chart axes.
|
| 208 |
ctx.strokeStyle = '#E0E0E0';
|
| 209 |
ctx.lineWidth = 2;
|
| 210 |
ctx.beginPath();
|
|
@@ -213,7 +205,6 @@ function Records() {
|
|
| 213 |
ctx.lineTo(width - padding, height - padding);
|
| 214 |
ctx.stroke();
|
| 215 |
|
| 216 |
-
// Draw Y-axis labels.
|
| 217 |
ctx.fillStyle = '#666';
|
| 218 |
ctx.font = '12px Nunito';
|
| 219 |
ctx.textAlign = 'right';
|
|
@@ -222,7 +213,6 @@ function Records() {
|
|
| 222 |
const value = (maxScore * i / 4 * 100).toFixed(0);
|
| 223 |
ctx.fillText(value + '%', padding - 10, y + 4);
|
| 224 |
|
| 225 |
-
// Draw horizontal grid lines.
|
| 226 |
ctx.strokeStyle = '#F0F0F0';
|
| 227 |
ctx.lineWidth = 1;
|
| 228 |
ctx.beginPath();
|
|
@@ -231,14 +221,12 @@ function Records() {
|
|
| 231 |
ctx.stroke();
|
| 232 |
}
|
| 233 |
|
| 234 |
-
// Draw the bar chart.
|
| 235 |
displayData.forEach((session, index) => {
|
| 236 |
const barHeight = (session.focus_score / maxScore) * chartHeight;
|
| 237 |
const x = padding + index * barWidth + barWidth * 0.1;
|
| 238 |
const y = height - padding - barHeight;
|
| 239 |
const barActualWidth = barWidth * 0.8;
|
| 240 |
|
| 241 |
-
// Map each score to a blue-toned color band.
|
| 242 |
const score = session.focus_score;
|
| 243 |
let color;
|
| 244 |
if (score >= 0.8) color = '#4A90E2';
|
|
@@ -249,20 +237,17 @@ function Records() {
|
|
| 249 |
ctx.fillStyle = color;
|
| 250 |
ctx.fillRect(x, y, barActualWidth, barHeight);
|
| 251 |
|
| 252 |
-
// Draw a matching outline around each bar.
|
| 253 |
ctx.strokeStyle = color;
|
| 254 |
ctx.lineWidth = 1;
|
| 255 |
ctx.strokeRect(x, y, barActualWidth, barHeight);
|
| 256 |
});
|
| 257 |
|
| 258 |
-
// Draw the chart title.
|
| 259 |
ctx.textAlign = 'left';
|
| 260 |
ctx.font = 'bold 14px Nunito';
|
| 261 |
ctx.fillStyle = '#4A90E2';
|
| 262 |
ctx.fillText('Focus Score by Session', padding, 30);
|
| 263 |
};
|
| 264 |
|
| 265 |
-
// Initial load.
|
| 266 |
useEffect(() => {
|
| 267 |
loadSessions(filter);
|
| 268 |
}, [filter]);
|
|
@@ -287,12 +272,10 @@ function Records() {
|
|
| 287 |
};
|
| 288 |
}, [detailState.open]);
|
| 289 |
|
| 290 |
-
// Filter button handler.
|
| 291 |
const handleFilterClick = (filterType) => {
|
| 292 |
setFilter(filterType);
|
| 293 |
};
|
| 294 |
|
| 295 |
-
// Open the detail modal for one session.
|
| 296 |
const handleViewDetails = async (sessionId) => {
|
| 297 |
setDetailState({
|
| 298 |
open: true,
|
|
@@ -324,6 +307,7 @@ function Records() {
|
|
| 324 |
}
|
| 325 |
};
|
| 326 |
|
|
|
|
| 327 |
const handleExport = async () => {
|
| 328 |
try {
|
| 329 |
const response = await fetch('/api/sessions?filter=all');
|
|
@@ -411,7 +395,41 @@ function Records() {
|
|
| 411 |
|
| 412 |
return (
|
| 413 |
<main id="page-d" className="page">
|
| 414 |
-
<h1 className="page-title">My Records</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
| 416 |
<div className="records-controls" style={{ display: 'flex', justifyContent: 'center', gap: '10px', marginBottom: '30px' }}>
|
| 417 |
<button id="filter-today" onClick={() => handleFilterClick('today')} style={{ padding: '10px 30px', borderRadius: '8px', border: filter === 'today' ? 'none' : '2px solid #4A90E2', background: filter === 'today' ? '#4A90E2' : 'transparent', color: filter === 'today' ? 'white' : '#4A90E2', fontSize: '14px', fontWeight: '500', cursor: 'pointer', transition: 'all 0.3s' }}>Today</button>
|
|
@@ -460,41 +478,6 @@ function Records() {
|
|
| 460 |
)}
|
| 461 |
</div>
|
| 462 |
|
| 463 |
-
<div className="data-management-section" style={{
|
| 464 |
-
background: 'white',
|
| 465 |
-
padding: '25px',
|
| 466 |
-
borderRadius: '10px',
|
| 467 |
-
boxShadow: '0 2px 8px rgba(0,0,0,0.1)',
|
| 468 |
-
width: '80%',
|
| 469 |
-
margin: '0 auto',
|
| 470 |
-
textAlign: 'center'
|
| 471 |
-
}}>
|
| 472 |
-
<h2 style={{ color: '#333', marginBottom: '10px', fontSize: '18px', fontWeight: '600', textAlign: 'left' }}>Data Management</h2>
|
| 473 |
-
<p style={{ color: '#666', fontSize: '14px', marginBottom: '20px', textAlign: 'left' }}>
|
| 474 |
-
Export your focus history to a file, import previously saved history, or permanently clear all data.
|
| 475 |
-
</p>
|
| 476 |
-
|
| 477 |
-
<input
|
| 478 |
-
type="file"
|
| 479 |
-
ref={fileInputRef}
|
| 480 |
-
style={{ display: 'none' }}
|
| 481 |
-
accept=".json"
|
| 482 |
-
onChange={handleFileChange}
|
| 483 |
-
/>
|
| 484 |
-
|
| 485 |
-
<div style={{ display: 'flex', gap: '15px', justifyContent: 'center', flexWrap: 'wrap' }}>
|
| 486 |
-
<button className="action-btn blue" onClick={handleExport} style={{ width: '30%', minWidth: '140px', padding: '12px', borderRadius: '8px' }}>
|
| 487 |
-
Export Data
|
| 488 |
-
</button>
|
| 489 |
-
<button className="action-btn yellow" onClick={triggerImport} style={{ width: '30%', minWidth: '140px', padding: '12px', borderRadius: '8px', color: '#333' }}>
|
| 490 |
-
Import Data
|
| 491 |
-
</button>
|
| 492 |
-
<button className="action-btn red" onClick={handleClearHistory} style={{ width: '30%', minWidth: '140px', padding: '12px', borderRadius: '8px' }}>
|
| 493 |
-
Clear History
|
| 494 |
-
</button>
|
| 495 |
-
</div>
|
| 496 |
-
</div>
|
| 497 |
-
|
| 498 |
{detailState.open ? (
|
| 499 |
<div className="modal-overlay" onClick={closeDetails}>
|
| 500 |
<div className="modal-content records-detail-modal" onClick={(event) => event.stopPropagation()}>
|
|
|
|
| 14 |
|
| 15 |
const fileInputRef = useRef(null);
|
| 16 |
|
|
|
|
| 17 |
const formatDuration = (seconds) => {
|
| 18 |
const safeSeconds = Math.max(0, Number(seconds) || 0);
|
| 19 |
const mins = Math.floor(safeSeconds / 60);
|
|
|
|
| 21 |
return `${mins}m ${secs}s`;
|
| 22 |
};
|
| 23 |
|
|
|
|
| 24 |
const formatDate = (dateString) => {
|
| 25 |
const date = new Date(dateString);
|
| 26 |
return date.toLocaleDateString('en-US', {
|
|
|
|
| 157 |
});
|
| 158 |
};
|
| 159 |
|
|
|
|
| 160 |
const loadSessions = async (filterType) => {
|
| 161 |
setLoading(true);
|
| 162 |
try {
|
|
|
|
| 171 |
}
|
| 172 |
};
|
| 173 |
|
|
|
|
| 174 |
const drawChart = (data) => {
|
| 175 |
const canvas = chartRef.current;
|
| 176 |
if (!canvas) return;
|
|
|
|
| 179 |
const width = canvas.width = canvas.offsetWidth;
|
| 180 |
const height = canvas.height = 300;
|
| 181 |
|
|
|
|
| 182 |
ctx.clearRect(0, 0, width, height);
|
| 183 |
|
| 184 |
if (data.length === 0) {
|
|
|
|
| 189 |
return;
|
| 190 |
}
|
| 191 |
|
|
|
|
| 192 |
const displayData = data.slice(0, 20).reverse();
|
| 193 |
const padding = 50;
|
| 194 |
const chartWidth = width - padding * 2;
|
| 195 |
const chartHeight = height - padding * 2;
|
| 196 |
const barWidth = chartWidth / displayData.length;
|
| 197 |
|
|
|
|
| 198 |
const maxScore = 1.0;
|
| 199 |
|
|
|
|
| 200 |
ctx.strokeStyle = '#E0E0E0';
|
| 201 |
ctx.lineWidth = 2;
|
| 202 |
ctx.beginPath();
|
|
|
|
| 205 |
ctx.lineTo(width - padding, height - padding);
|
| 206 |
ctx.stroke();
|
| 207 |
|
|
|
|
| 208 |
ctx.fillStyle = '#666';
|
| 209 |
ctx.font = '12px Nunito';
|
| 210 |
ctx.textAlign = 'right';
|
|
|
|
| 213 |
const value = (maxScore * i / 4 * 100).toFixed(0);
|
| 214 |
ctx.fillText(value + '%', padding - 10, y + 4);
|
| 215 |
|
|
|
|
| 216 |
ctx.strokeStyle = '#F0F0F0';
|
| 217 |
ctx.lineWidth = 1;
|
| 218 |
ctx.beginPath();
|
|
|
|
| 221 |
ctx.stroke();
|
| 222 |
}
|
| 223 |
|
|
|
|
| 224 |
displayData.forEach((session, index) => {
|
| 225 |
const barHeight = (session.focus_score / maxScore) * chartHeight;
|
| 226 |
const x = padding + index * barWidth + barWidth * 0.1;
|
| 227 |
const y = height - padding - barHeight;
|
| 228 |
const barActualWidth = barWidth * 0.8;
|
| 229 |
|
|
|
|
| 230 |
const score = session.focus_score;
|
| 231 |
let color;
|
| 232 |
if (score >= 0.8) color = '#4A90E2';
|
|
|
|
| 237 |
ctx.fillStyle = color;
|
| 238 |
ctx.fillRect(x, y, barActualWidth, barHeight);
|
| 239 |
|
|
|
|
| 240 |
ctx.strokeStyle = color;
|
| 241 |
ctx.lineWidth = 1;
|
| 242 |
ctx.strokeRect(x, y, barActualWidth, barHeight);
|
| 243 |
});
|
| 244 |
|
|
|
|
| 245 |
ctx.textAlign = 'left';
|
| 246 |
ctx.font = 'bold 14px Nunito';
|
| 247 |
ctx.fillStyle = '#4A90E2';
|
| 248 |
ctx.fillText('Focus Score by Session', padding, 30);
|
| 249 |
};
|
| 250 |
|
|
|
|
| 251 |
useEffect(() => {
|
| 252 |
loadSessions(filter);
|
| 253 |
}, [filter]);
|
|
|
|
| 272 |
};
|
| 273 |
}, [detailState.open]);
|
| 274 |
|
|
|
|
| 275 |
const handleFilterClick = (filterType) => {
|
| 276 |
setFilter(filterType);
|
| 277 |
};
|
| 278 |
|
|
|
|
| 279 |
const handleViewDetails = async (sessionId) => {
|
| 280 |
setDetailState({
|
| 281 |
open: true,
|
|
|
|
| 307 |
}
|
| 308 |
};
|
| 309 |
|
| 310 |
+
// Data Management core function
|
| 311 |
const handleExport = async () => {
|
| 312 |
try {
|
| 313 |
const response = await fetch('/api/sessions?filter=all');
|
|
|
|
| 395 |
|
| 396 |
return (
|
| 397 |
<main id="page-d" className="page">
|
| 398 |
+
<h1 className="page-title" style={{ marginBottom: '10px' }}>My Records</h1>
|
| 399 |
+
|
| 400 |
+
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', gap: '15px', marginBottom: '25px' }}>
|
| 401 |
+
<input
|
| 402 |
+
type="file"
|
| 403 |
+
ref={fileInputRef}
|
| 404 |
+
style={{ display: 'none' }}
|
| 405 |
+
accept=".json"
|
| 406 |
+
onChange={handleFileChange}
|
| 407 |
+
/>
|
| 408 |
+
<button
|
| 409 |
+
onClick={handleExport}
|
| 410 |
+
style={{ background: '#eef3f8', border: '1px solid #d9eaff', color: '#4b5a6b', padding: '6px 16px', borderRadius: '20px', fontSize: '12px', fontWeight: '700', cursor: 'pointer', transition: 'all 0.2s' }}
|
| 411 |
+
onMouseOver={(e) => { e.target.style.background = '#e2eaf3'; }}
|
| 412 |
+
onMouseOut={(e) => { e.target.style.background = '#eef3f8'; }}
|
| 413 |
+
>
|
| 414 |
+
⬇️ Export
|
| 415 |
+
</button>
|
| 416 |
+
<button
|
| 417 |
+
onClick={triggerImport}
|
| 418 |
+
style={{ background: '#eef3f8', border: '1px solid #d9eaff', color: '#4b5a6b', padding: '6px 16px', borderRadius: '20px', fontSize: '12px', fontWeight: '700', cursor: 'pointer', transition: 'all 0.2s' }}
|
| 419 |
+
onMouseOver={(e) => { e.target.style.background = '#e2eaf3'; }}
|
| 420 |
+
onMouseOut={(e) => { e.target.style.background = '#eef3f8'; }}
|
| 421 |
+
>
|
| 422 |
+
⬆️ Import
|
| 423 |
+
</button>
|
| 424 |
+
<button
|
| 425 |
+
onClick={handleClearHistory}
|
| 426 |
+
style={{ background: '#fff1ee', border: '1px solid #f3c7c7', color: '#b54028', padding: '6px 16px', borderRadius: '20px', fontSize: '12px', fontWeight: '700', cursor: 'pointer', transition: 'all 0.2s' }}
|
| 427 |
+
onMouseOver={(e) => { e.target.style.background = '#fbe5e1'; }}
|
| 428 |
+
onMouseOut={(e) => { e.target.style.background = '#fff1ee'; }}
|
| 429 |
+
>
|
| 430 |
+
🗑️ Clear
|
| 431 |
+
</button>
|
| 432 |
+
</div>
|
| 433 |
|
| 434 |
<div className="records-controls" style={{ display: 'flex', justifyContent: 'center', gap: '10px', marginBottom: '30px' }}>
|
| 435 |
<button id="filter-today" onClick={() => handleFilterClick('today')} style={{ padding: '10px 30px', borderRadius: '8px', border: filter === 'today' ? 'none' : '2px solid #4A90E2', background: filter === 'today' ? '#4A90E2' : 'transparent', color: filter === 'today' ? 'white' : '#4A90E2', fontSize: '14px', fontWeight: '500', cursor: 'pointer', transition: 'all 0.3s' }}>Today</button>
|
|
|
|
| 478 |
)}
|
| 479 |
</div>
|
| 480 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
{detailState.open ? (
|
| 482 |
<div className="modal-overlay" onClick={closeDetails}>
|
| 483 |
<div className="modal-content records-detail-modal" onClick={(event) => event.stopPropagation()}>
|