Kexin-251202 commited on
Commit
9def1ee
·
verified ·
1 Parent(s): a66ecb8

update user/admin, update data import options

Browse files
Files changed (4) hide show
  1. public/test_data.json +112 -0
  2. src/App.css +55 -0
  3. src/App.jsx +189 -104
  4. src/components/Customise.jsx +5 -2
public/test_data.json ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "id": 29,
4
+ "start_time": "2026-03-10T19:27:26.313545",
5
+ "end_time": "2026-03-10T19:27:59.454192",
6
+ "duration_seconds": 33,
7
+ "focus_score": 0.8848484848484849,
8
+ "total_frames": 165,
9
+ "focused_frames": 146,
10
+ "created_at": "2026-03-10 19:27:26"
11
+ },
12
+ {
13
+ "id": 27,
14
+ "start_time": "2026-03-10T15:09:42.264889",
15
+ "end_time": "2026-03-10T15:09:52.066905",
16
+ "duration_seconds": 9,
17
+ "focus_score": 0.575,
18
+ "total_frames": 120,
19
+ "focused_frames": 69,
20
+ "created_at": "2026-03-10 15:09:42"
21
+ },
22
+ {
23
+ "id": 20,
24
+ "start_time": "2026-03-10T12:10:08.838619",
25
+ "end_time": "2026-03-10T12:10:22.613099",
26
+ "duration_seconds": 13,
27
+ "focus_score": 0.3076923076923077,
28
+ "total_frames": 65,
29
+ "focused_frames": 20,
30
+ "created_at": "2026-03-10 12:10:08"
31
+ },
32
+ {
33
+ "id": 19,
34
+ "start_time": "2026-03-10T12:10:04.664410",
35
+ "end_time": "2026-03-10T12:21:08.528749",
36
+ "duration_seconds": 663,
37
+ "focus_score": 1,
38
+ "total_frames": 19,
39
+ "focused_frames": 19,
40
+ "created_at": "2026-03-10 12:10:04"
41
+ },
42
+ {
43
+ "id": 15,
44
+ "start_time": "2026-03-10T12:03:07.498863",
45
+ "end_time": "2026-03-10T12:03:19.454642",
46
+ "duration_seconds": 11,
47
+ "focus_score": 0.896551724137931,
48
+ "total_frames": 58,
49
+ "focused_frames": 52,
50
+ "created_at": "2026-03-10 12:03:07"
51
+ },
52
+ {
53
+ "id": 13,
54
+ "start_time": "2026-03-10T12:02:40.121044",
55
+ "end_time": "2026-03-10T12:03:03.700510",
56
+ "duration_seconds": 23,
57
+ "focus_score": 0.6923076923076923,
58
+ "total_frames": 117,
59
+ "focused_frames": 81,
60
+ "created_at": "2026-03-10 12:02:40"
61
+ },
62
+ {
63
+ "id": 12,
64
+ "start_time": "2026-03-10T12:02:36.741156",
65
+ "end_time": "2026-03-10T12:05:59.301680",
66
+ "duration_seconds": 202,
67
+ "focus_score": 0.13333333333333333,
68
+ "total_frames": 15,
69
+ "focused_frames": 2,
70
+ "created_at": "2026-03-10 12:02:36"
71
+ },
72
+ {
73
+ "id": 8,
74
+ "start_time": "2026-03-10T11:35:26.680264",
75
+ "end_time": "2026-03-10T11:36:07.574547",
76
+ "duration_seconds": 40,
77
+ "focus_score": 0.6225490196078431,
78
+ "total_frames": 204,
79
+ "focused_frames": 127,
80
+ "created_at": "2026-03-10 11:35:26"
81
+ },
82
+ {
83
+ "id": 7,
84
+ "start_time": "2026-03-10T11:35:16.587504",
85
+ "end_time": "2026-03-10T12:17:35.889266",
86
+ "duration_seconds": 2539,
87
+ "focus_score": 0.9387755102040817,
88
+ "total_frames": 49,
89
+ "focused_frames": 46,
90
+ "created_at": "2026-03-10 11:35:16"
91
+ },
92
+ {
93
+ "id": 4,
94
+ "start_time": "2026-03-10T11:16:31.204287",
95
+ "end_time": "2026-03-10T11:16:52.632759",
96
+ "duration_seconds": 21,
97
+ "focus_score": 0.75,
98
+ "total_frames": 104,
99
+ "focused_frames": 78,
100
+ "created_at": "2026-03-10 11:16:31"
101
+ },
102
+ {
103
+ "id": 2,
104
+ "start_time": "2026-03-10T11:15:37.543154",
105
+ "end_time": "2026-03-10T11:17:41.674593",
106
+ "duration_seconds": 124,
107
+ "focus_score": 0.9557522123893806,
108
+ "total_frames": 113,
109
+ "focused_frames": 108,
110
+ "created_at": "2026-03-10 11:15:37"
111
+ }
112
+ ]
src/App.css CHANGED
@@ -797,3 +797,58 @@ details p {
797
  from { opacity: 0; transform: scale(0.95); }
798
  to { opacity: 1; transform: scale(1); }
799
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
797
  from { opacity: 0; transform: scale(0.95); }
798
  to { opacity: 1; transform: scale(1); }
799
  }
800
+
801
+ /* ================= 迎宾弹窗样式 ================= */
802
+ .welcome-modal-overlay {
803
+ position: fixed;
804
+ top: 0; left: 0; right: 0; bottom: 0;
805
+ background-color: rgba(0, 0, 0, 0.7);
806
+ display: flex;
807
+ justify-content: center;
808
+ align-items: center;
809
+ z-index: 9999;
810
+ }
811
+
812
+ .welcome-modal {
813
+ background-color: #1e1e24;
814
+ padding: 40px;
815
+ border-radius: 15px;
816
+ text-align: center;
817
+ box-shadow: 0 10px 30px rgba(0,0,0,0.5);
818
+ border: 1px solid #333;
819
+ }
820
+
821
+ .welcome-modal h2 { margin-top: 0; color: #fff; }
822
+ .welcome-modal p { margin-bottom: 30px; color: #ccc; }
823
+ .welcome-buttons { display: flex; gap: 20px; justify-content: center; }
824
+
825
+ /* ================= 左上角头像样式 (修改版) ================= */
826
+ #top-menu {
827
+ position: relative;
828
+ display: flex;
829
+ justify-content: center;
830
+ align-items: center;
831
+ }
832
+
833
+ .avatar-container {
834
+ position: absolute;
835
+ left: 20px;
836
+ cursor: pointer;
837
+ }
838
+
839
+ .avatar-circle {
840
+ width: 40px;
841
+ height: 40px;
842
+ border-radius: 50%;
843
+ display: flex;
844
+ justify-content: center;
845
+ align-items: center;
846
+ font-weight: bold;
847
+ font-size: 1.2rem;
848
+ color: white;
849
+ transition: all 0.3s ease;
850
+ border: 2px solid transparent;
851
+ }
852
+
853
+ .avatar-circle.user { background-color: #555; }
854
+ .avatar-circle.admin { background-color: #ffaa00; border-color: #fff; box-shadow: 0 0 10px rgba(255, 170, 0, 0.5); }
src/App.jsx CHANGED
@@ -2,8 +2,6 @@ import React, { useState, useRef, useEffect } from 'react';
2
  import './App.css';
3
  import { VideoManagerLocal } from './utils/VideoManagerLocal';
4
 
5
- // 引入我们刚刚拆分好的组件
6
- // 注意:确保你的文件名和这里 import 的名字完全一致
7
  import Home from './components/Home';
8
  import FocusPageLocal from './components/FocusPageLocal';
9
  import Achievement from './components/Achievement';
@@ -12,108 +10,195 @@ import Customise from './components/Customise';
12
  import Help from './components/Help';
13
 
14
  function App() {
15
- // 定义状态:当前显示哪个页面
16
- const [activeTab, setActiveTab] = useState('home');
17
-
18
- // 全局VideoManagerLocal实例和session状态
19
- const videoManagerRef = useRef(null);
20
- const [isSessionActive, setIsSessionActive] = useState(false);
21
- const [sessionResult, setSessionResult] = useState(null);
22
-
23
- // 初始化VideoManagerLocal(只创建一次)
24
- useEffect(() => {
25
- const callbacks = {
26
- onSessionStart: () => {
27
- setIsSessionActive(true);
28
- setSessionResult(null);
29
- },
30
- onSessionEnd: (summary) => {
31
- console.log("App.jsx: onSessionEnd called");
32
- console.log("App.jsx: Session Summary:", summary);
33
- setIsSessionActive(false);
34
- if (summary) {
35
- console.log("App.jsx: Setting sessionResult with summary");
36
- setSessionResult(summary);
37
- } else {
38
- console.warn("App.jsx: Summary is null or undefined");
39
- }
40
- }
41
- };
42
-
43
- videoManagerRef.current = new VideoManagerLocal(callbacks);
44
-
45
- // 清理函数:只在整个App卸载时才清理
46
- return () => {
47
- if (videoManagerRef.current) {
48
- videoManagerRef.current.stopStreaming();
49
- }
50
- };
51
- }, []);
52
-
53
- // 页面切换时保持session连接,不自动断开
54
-
55
- return (
56
- <div className="app-container">
57
- {/* 顶部导航栏 (Top Menu) - 它是全局共用的,所以留在 App.jsx 里 */}
58
- <nav id="top-menu">
59
- <button
60
- className={`menu-btn ${activeTab === 'focus' ? 'active' : ''}`}
61
- onClick={() => setActiveTab('focus')}
62
- >
63
- Start Focus {isSessionActive && <span style={{marginLeft: '8px', color: '#00FF00'}}>●</span>}
64
- </button>
65
- <div className="separator"></div>
66
-
67
- <button
68
- className={`menu-btn ${activeTab === 'achievement' ? 'active' : ''}`}
69
- onClick={() => setActiveTab('achievement')}
70
- >
71
- My Achievement
72
- </button>
73
- <div className="separator"></div>
74
-
75
- <button
76
- className={`menu-btn ${activeTab === 'records' ? 'active' : ''}`}
77
- onClick={() => setActiveTab('records')}
78
- >
79
- My Records
80
- </button>
81
- <div className="separator"></div>
82
-
83
- <button
84
- className={`menu-btn ${activeTab === 'customise' ? 'active' : ''}`}
85
- onClick={() => setActiveTab('customise')}
86
- >
87
- Customise
88
- </button>
89
- <div className="separator"></div>
90
-
91
- <button
92
- className={`menu-btn ${activeTab === 'help' ? 'active' : ''}`}
93
- onClick={() => setActiveTab('help')}
94
- >
95
- Help
96
- </button>
97
- </nav>
98
-
99
- {/* 页面内容区域:根据 activeTab 的值,渲染对应的组件 */}
100
-
101
- {/* Home 页:我们需要把 setActiveTab 传给它,因为它的 'Start' 按钮要能跳转页面 */}
102
- {activeTab === 'home' && <Home setActiveTab={setActiveTab} />}
103
-
104
- {/* FocusPageLocal 保持常驻,避免切页中断视频/连接 - 使用本地处理,不依赖WebRTC */}
105
- <FocusPageLocal
106
- videoManager={videoManagerRef.current}
107
- sessionResult={sessionResult}
108
- setSessionResult={setSessionResult}
109
- isActive={activeTab === 'focus'}
110
- />
111
- {activeTab === 'achievement' && <Achievement />}
112
- {activeTab === 'records' && <Records />}
113
- {activeTab === 'customise' && <Customise />}
114
- {activeTab === 'help' && <Help />}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  </div>
116
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  }
118
 
119
- export default App;
 
2
  import './App.css';
3
  import { VideoManagerLocal } from './utils/VideoManagerLocal';
4
 
 
 
5
  import Home from './components/Home';
6
  import FocusPageLocal from './components/FocusPageLocal';
7
  import Achievement from './components/Achievement';
 
10
  import Help from './components/Help';
11
 
12
  function App() {
13
+ const [activeTab, setActiveTab] = useState('home');
14
+ const videoManagerRef = useRef(null);
15
+ const [isSessionActive, setIsSessionActive] = useState(false);
16
+ const [sessionResult, setSessionResult] = useState(null);
17
+
18
+ // 新增状态:控制身份和弹窗
19
+ const [role, setRole] = useState('user');
20
+ const [showWelcomePopup, setShowWelcomePopup] = useState(false);
21
+ const handleAutoImport = async () => {
22
+ const backup = localStorage.getItem('focus_magic_backup');
23
+ if (backup) {
24
+ try {
25
+ const sessions = JSON.parse(backup);
26
+ const response = await fetch('/api/import', {
27
+ method: 'POST',
28
+ headers: { 'Content-Type': 'application/json' },
29
+ body: JSON.stringify(sessions)
30
+ });
31
+ if (response.ok) {
32
+ alert("Auto-recovery successful! Data restored from local cache.");
33
+ setShowWelcomePopup(false);
34
+ } else {
35
+ alert("Auto-recovery failed on server.");
36
+ }
37
+ } catch (err) {
38
+ alert("Error reading cache: " + err.message);
39
+ }
40
+ } else {
41
+ alert("No previous automatic backup found. Please use 'Manual Import' to select your downloaded JSON file.");
42
+ }
43
+ };
44
+ const fileInputRef = useRef(null);
45
+
46
+ // 刚进网页时的初始化逻辑:先清空,后弹窗
47
+ useEffect(() => {
48
+ fetch('/api/history', { method: 'DELETE' })
49
+ .then(() => {
50
+ console.log("History cleared for new user.");
51
+ setShowWelcomePopup(true);
52
+ })
53
+ .catch(err => console.error("Failed to clear history on load", err));
54
+
55
+ const callbacks = {
56
+ onSessionStart: () => {
57
+ setIsSessionActive(true);
58
+ setSessionResult(null);
59
+ },
60
+ onSessionEnd: (summary) => {
61
+ setIsSessionActive(false);
62
+ if (summary) setSessionResult(summary);
63
+ }
64
+ };
65
+ videoManagerRef.current = new VideoManagerLocal(callbacks);
66
+
67
+ return () => {
68
+ if (videoManagerRef.current) videoManagerRef.current.stopStreaming();
69
+ };
70
+ }, []);
71
+
72
+ // 手动导入文件的处理逻辑 (借用 Customise 的逻辑)
73
+ const handleFileChange = async (event) => {
74
+ const file = event.target.files[0];
75
+ if (!file) return;
76
+ const reader = new FileReader();
77
+ reader.onload = async (e) => {
78
+ try {
79
+ const sessions = JSON.parse(e.target.result);
80
+ const response = await fetch('/api/import', {
81
+ method: 'POST',
82
+ headers: { 'Content-Type': 'application/json' },
83
+ body: JSON.stringify(sessions)
84
+ });
85
+ if (response.ok) {
86
+ alert("Import successful! You can check your records now.");
87
+ setShowWelcomePopup(false);
88
+ } else {
89
+ alert("Import failed on server side.");
90
+ }
91
+ } catch (err) {
92
+ alert("Error parsing file: " + err.message);
93
+ }
94
+ event.target.value = '';
95
+ };
96
+ reader.readAsText(file);
97
+ };
98
+
99
+ // 点击左上角头像切换 Admin/User
100
+ const handleAvatarClick = async () => {
101
+ if (role === 'admin') {
102
+ if (window.confirm("Switch back to User mode? This will clear current test data.")) {
103
+ await fetch('/api/history', { method: 'DELETE' });
104
+ setRole('user');
105
+ setShowWelcomePopup(true);
106
+ }
107
+ } else {
108
+ const pwd = window.prompt("Enter Admin Password:");
109
+ if (pwd === "123") { // 这里是你设定的测试密码
110
+ try {
111
+ // 1. 先清空当前数据
112
+ await fetch('/api/history', { method: 'DELETE' });
113
+ // 2. 读取 public 目录下的测试文件
114
+ const res = await fetch('/test_data.json');
115
+ if (!res.ok) throw new Error("test_data.json not found in public folder");
116
+ const testData = await res.json();
117
+ // 3. 导入测试数据
118
+ const importRes = await fetch('/api/import', {
119
+ method: 'POST',
120
+ headers: { 'Content-Type': 'application/json' },
121
+ body: JSON.stringify(testData)
122
+ });
123
+ if (importRes.ok) {
124
+ setRole('admin');
125
+ alert("Admin mode activated! Test data loaded successfully.");
126
+ }
127
+ } catch (error) {
128
+ alert("Admin login failed: " + error.message);
129
+ }
130
+ } else if (pwd !== null) {
131
+ alert("Incorrect password!");
132
+ }
133
+ }
134
+ };
135
+
136
+ return (
137
+ <div className="app-container">
138
+
139
+ {/* 迎宾弹窗 */}
140
+ {showWelcomePopup && (
141
+ <div className="welcome-modal-overlay">
142
+ <div className="welcome-modal">
143
+ <h2>Welcome to FocusGuard!</h2>
144
+ <p>Would you like to start fresh or import your previous local records?</p>
145
+ <div className="welcome-buttons">
146
+ <button className="btn-main" onClick={() => setShowWelcomePopup(false)}>New Start</button>
147
+ <button className="btn-main" style={{backgroundColor: '#4CAF50'}} onClick={handleAutoImport}>Auto Import</button>
148
+ <button className="btn-main yellow" onClick={() => fileInputRef.current.click()}>Manual Import</button>
149
+ </div>
150
+ {/* 隐藏的上传框 */}
151
+ <input type="file" ref={fileInputRef} style={{ display: 'none' }} accept=".json" onChange={handleFileChange} />
152
+ </div>
153
+ </div>
154
+ )}
155
+
156
+ <nav id="top-menu">
157
+ {/* 左上角头像区域 */}
158
+ <div className="avatar-container" onClick={handleAvatarClick} title="Click to switch User/Admin">
159
+ <div className={`avatar-circle ${role}`}>
160
+ {role === 'admin' ? 'A' : 'U'}
161
+ </div>
162
  </div>
163
+
164
+ <button className={`menu-btn ${activeTab === 'focus' ? 'active' : ''}`} onClick={() => setActiveTab('focus')}>
165
+ Start Focus {isSessionActive && <span style={{marginLeft: '8px', color: '#00FF00'}}>●</span>}
166
+ </button>
167
+ <div className="separator"></div>
168
+
169
+ <button className={`menu-btn ${activeTab === 'achievement' ? 'active' : ''}`} onClick={() => setActiveTab('achievement')}>
170
+ My Achievement
171
+ </button>
172
+ <div className="separator"></div>
173
+
174
+ <button className={`menu-btn ${activeTab === 'records' ? 'active' : ''}`} onClick={() => setActiveTab('records')}>
175
+ My Records
176
+ </button>
177
+ <div className="separator"></div>
178
+
179
+ <button className={`menu-btn ${activeTab === 'customise' ? 'active' : ''}`} onClick={() => setActiveTab('customise')}>
180
+ Customise
181
+ </button>
182
+ <div className="separator"></div>
183
+
184
+ <button className={`menu-btn ${activeTab === 'help' ? 'active' : ''}`} onClick={() => setActiveTab('help')}>
185
+ Help
186
+ </button>
187
+ </nav>
188
+
189
+ {activeTab === 'home' && <Home setActiveTab={setActiveTab} />}
190
+ <FocusPageLocal
191
+ videoManager={videoManagerRef.current}
192
+ sessionResult={sessionResult}
193
+ setSessionResult={setSessionResult}
194
+ isActive={activeTab === 'focus'}
195
+ />
196
+ {activeTab === 'achievement' && <Achievement />}
197
+ {activeTab === 'records' && <Records />}
198
+ {activeTab === 'customise' && <Customise />}
199
+ {activeTab === 'help' && <Help />}
200
+ </div>
201
+ );
202
  }
203
 
204
+ export default App;
src/components/Customise.jsx CHANGED
@@ -57,6 +57,9 @@ function Customise() {
57
 
58
  // 创建 JSON Blob
59
  const jsonString = JSON.stringify(data, null, 2);
 
 
 
60
  const blob = new Blob([jsonString], { type: 'application/json' });
61
 
62
  // 创建临时下载链接
@@ -142,7 +145,7 @@ function Customise() {
142
 
143
  return (
144
  <main id="page-e" className="page">
145
- <h1 className="page-title">Customise</h1>
146
 
147
  <div className="settings-container">
148
  {/* Detection Settings */}
@@ -217,4 +220,4 @@ function Customise() {
217
  );
218
  }
219
 
220
- export default Customise;
 
57
 
58
  // 创建 JSON Blob
59
  const jsonString = JSON.stringify(data, null, 2);
60
+ // 在浏览器缓存里存一份
61
+ localStorage.setItem('focus_magic_backup', jsonString);
62
+
63
  const blob = new Blob([jsonString], { type: 'application/json' });
64
 
65
  // 创建临时下载链接
 
145
 
146
  return (
147
  <main id="page-e" className="page">
148
+ <h1 className="page-title">Customise</h1>
149
 
150
  <div className="settings-container">
151
  {/* Detection Settings */}
 
220
  );
221
  }
222
 
223
+ export default Customise;