File size: 8,006 Bytes
c86c45b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9def1ee
 
 
c86c45b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9def1ee
c86c45b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9def1ee
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
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 (
    <main id="page-e" className="page">

      <h1 className="page-title">Customise</h1>



      <div className="settings-container">

        {/* Detection Settings */}

        <div className="setting-group">

          <h2>Detection Settings</h2>

          <div className="setting-item">

            <label htmlFor="sensitivity-slider">Detection Sensitivity</label>

            <div className="slider-group">

              <input type="range" id="sensitivity-slider" min="1" max="10" value={sensitivity} onChange={(e) => setSensitivity(e.target.value)} />

              <span id="sensitivity-value">{sensitivity}</span>

            </div>

            <p className="setting-description">Higher values require stricter focus criteria</p>

          </div>

          <div className="setting-item">

            <label htmlFor="default-framerate">Default Frame Rate</label>

            <div className="slider-group">

              <input type="range" id="default-framerate" min="5" max="60" value={frameRate} onChange={(e) => setFrameRate(e.target.value)} />

              <span id="framerate-value">{frameRate}</span> FPS

            </div>

          </div>

        </div>



        {/* Notifications */}

        <div className="setting-group">

          <h2>Notifications</h2>

          <div className="setting-item">

            <label>

              <input type="checkbox" id="enable-notifications" checked={notificationsEnabled} onChange={(e) => setNotificationsEnabled(e.target.checked)} />

              Enable distraction notifications

            </label>

          </div>

          <div className="setting-item">

            <label htmlFor="notification-threshold">Alert after (seconds)</label>

            <input type="number" id="notification-threshold" value={threshold} onChange={(e) => setThreshold(e.target.value)} min="5" max="300" />

          </div>

        </div>



        {/* Data Management */}

        <div className="setting-group">

          <h2>Data Management</h2>

          

          {/* 隐藏的文件输入框,只接受 json */}

          <input 

            type="file" 

            ref={fileInputRef} 

            style={{ display: 'none' }} 

            accept=".json" 

            onChange={handleFileChange} 

          />



          <div style={{ display: 'flex', gap: '10px', justifyContent: 'center', flexWrap: 'wrap' }}>

            {/* Export 按钮 */}

            <button id="export-data" className="action-btn blue" onClick={handleExport} style={{ width: '30%', minWidth: '120px' }}>

                Export Data

            </button>

            

            {/* Import 按钮 */}

            <button id="import-data" className="action-btn yellow" onClick={triggerImport} style={{ width: '30%', minWidth: '120px' }}>

                Import Data

            </button>

            

            {/* Clear 按钮 */}

            <button id="clear-history" className="action-btn red" onClick={handleClearHistory} style={{ width: '30%', minWidth: '120px' }}>

                Clear History

            </button>

          </div>

        </div>



        <button id="save-settings" className="btn-main" onClick={handleSave}>Save Settings</button>

      </div>

    </main>
  );
}

export default Customise;