import React, { useState, useEffect, useRef } from 'react'; function Customise() { const [sensitivity, setSensitivity] = useState(6); const [frameRate, setFrameRate] = useState(30); const [notificationsEnabled, setNotificationsEnabled] = useState(true); const [threshold, setThreshold] = useState(30); // 引用隐藏的文件输入框 const fileInputRef = useRef(null); // 1. 加载设置 useEffect(() => { fetch('/api/settings') .then(res => res.json()) .then(data => { if (data) { if (data.sensitivity) setSensitivity(data.sensitivity); if (data.frame_rate) setFrameRate(data.frame_rate); if (data.notification_threshold) setThreshold(data.notification_threshold); if (data.notification_enabled !== undefined) setNotificationsEnabled(data.notification_enabled); } }) .catch(err => console.error("Failed to load settings", err)); }, []); // 2. 保存设置 const handleSave = async () => { const settings = { sensitivity: parseInt(sensitivity), frame_rate: parseInt(frameRate), notification_enabled: notificationsEnabled, notification_threshold: parseInt(threshold) }; try { const response = await fetch('/api/settings', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(settings) }); if (response.ok) alert("Settings saved successfully!"); else alert("Failed to save settings."); } catch (error) { alert("Error saving settings: " + error.message); } }; // 3. 导出数据 (Export) const handleExport = async () => { try { // 请求获取所有历史记录 const response = await fetch('/api/sessions?filter=all'); if (!response.ok) throw new Error("Failed to fetch data"); const data = await response.json(); // 创建 JSON Blob const jsonString = JSON.stringify(data, null, 2); // 在浏览器缓存里存一份 localStorage.setItem('focus_magic_backup', jsonString); const blob = new Blob([jsonString], { type: 'application/json' }); // 创建临时下载链接 const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; // 文件名包含当前日期 link.download = `focus-guard-backup-${new Date().toISOString().slice(0, 10)}.json`; // 触发下载 document.body.appendChild(link); link.click(); // 清理 document.body.removeChild(link); URL.revokeObjectURL(url); } catch (error) { console.error(error); alert("Export failed: " + error.message); } }; // 4. 触发导入文件选择 const triggerImport = () => { fileInputRef.current.click(); }; // 5. 处理文件导入 (Import) const handleFileChange = async (event) => { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = async (e) => { try { const content = e.target.result; const sessions = JSON.parse(content); // 简单的验证:确保它是一个数组 if (!Array.isArray(sessions)) { throw new Error("Invalid file format: Expected a list of sessions."); } // 发送给后端进行存储 const response = await fetch('/api/import', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(sessions) }); if (response.ok) { const result = await response.json(); alert(`Success! Imported ${result.count} sessions.`); } else { alert("Import failed on server side."); } } catch (err) { alert("Error parsing file: " + err.message); } // 清空 input,允许重复上传同一个文件 event.target.value = ''; }; reader.readAsText(file); }; // 6. 清除历史 (Clear History) const handleClearHistory = async () => { if (!window.confirm("Are you sure? This will delete ALL your session history permanently.")) { return; } try { const response = await fetch('/api/history', { method: 'DELETE' }); if (response.ok) { alert("All history has been cleared."); } else { alert("Failed to clear history."); } } catch (err) { alert("Error: " + err.message); } }; return (

Customise

{/* Detection Settings */}

Detection Settings

setSensitivity(e.target.value)} /> {sensitivity}

Higher values require stricter focus criteria

setFrameRate(e.target.value)} /> {frameRate} FPS
{/* Notifications */}

Notifications

setThreshold(e.target.value)} min="5" max="300" />
{/* Data Management */}

Data Management

{/* 隐藏的文件输入框,只接受 json */}
{/* Export 按钮 */} {/* Import 按钮 */} {/* Clear 按钮 */}
); } export default Customise;