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); // 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)); }, []); // 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); } }; // 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(); // 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); } }; // const triggerImport = () => { fileInputRef.current.click(); }; // 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); } // event.target.value = ''; }; reader.readAsText(file); }; // 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

{/* hidden file input, json only */}
{/* Export */} {/* Import */} {/* Clear */}
); } export default Customise;