Yingtao-Zheng commited on
Commit
dadceb2
·
1 Parent(s): 4a5bfab

Merge UI Manually (expect main)

Browse files
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,93 @@ 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); }
855
+ /* ================= 首页按钮 2x2 响应式网格 (终极修正版) ================= */
856
+ .home-button-grid {
857
+ display: grid;
858
+ grid-template-columns: 1fr 1fr; /* 强行平分两列,绝不妥协 */
859
+ gap: 20px; /* 按钮之间的间距 */
860
+ width: 100%;
861
+ max-width: 500px; /* 限制最大宽度,宽屏下也不会显得傻大黑粗 */
862
+ margin: 40px auto 0 auto; /* 上边距40px,左右auto保证绝对居中! */
863
+ }
864
+
865
+ .home-button-grid .btn-main {
866
+ width: 100%;
867
+ height: 60px; /* 统一高度,像一块块整齐的砖头 */
868
+ margin: 0; /* 清除默认外边距 */
869
+ padding: 10px;
870
+ font-size: 1rem;
871
+ display: flex;
872
+ justify-content: center;
873
+ align-items: center;
874
+ text-align: center;
875
+ box-sizing: border-box; /* 保证边框和内边距不会撑破格子 */
876
+ }
877
+
878
+ /* 📱 手机端专属适配 (屏幕宽度小于 600px 时自动缩放) */
879
+ @media (max-width: 600px) {
880
+ .home-button-grid {
881
+ gap: 15px;
882
+ max-width: 90%;
883
+ }
884
+
885
+ .home-button-grid .btn-main {
886
+ height: 50px;
887
+ font-size: 0.85rem;
888
+ }
889
+ }
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,96 +10,75 @@ 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}
@@ -116,4 +93,4 @@ function App() {
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
+ const [role, setRole] = useState('user');
18
 
19
+ // 刚进网页时,静默清空数据库,不弹窗!
20
  useEffect(() => {
21
+ fetch('/api/history', { method: 'DELETE' }).catch(err => console.error(err));
22
+
23
  const callbacks = {
24
  onSessionStart: () => {
25
  setIsSessionActive(true);
26
  setSessionResult(null);
27
  },
28
  onSessionEnd: (summary) => {
 
 
29
  setIsSessionActive(false);
30
+ if (summary) setSessionResult(summary);
 
 
 
 
 
31
  }
32
  };
 
33
  videoManagerRef.current = new VideoManagerLocal(callbacks);
34
 
 
35
  return () => {
36
+ if (videoManagerRef.current) videoManagerRef.current.stopStreaming();
 
 
37
  };
38
  }, []);
39
 
40
+ // 点击头像直接跳转到首页(Home)
41
+ const handleAvatarClick = () => {
42
+ setActiveTab('home');
43
+ };
44
 
45
  return (
46
  <div className="app-container">
 
47
  <nav id="top-menu">
48
+ <div className="avatar-container" onClick={handleAvatarClick} title="Back to Home">
49
+ <div className={`avatar-circle ${role}`}>
50
+ {role === 'admin' ? 'A' : 'U'}
51
+ </div>
52
+ </div>
53
+
54
+ <button className={`menu-btn ${activeTab === 'focus' ? 'active' : ''}`} onClick={() => setActiveTab('focus')}>
55
  Start Focus {isSessionActive && <span style={{marginLeft: '8px', color: '#00FF00'}}>●</span>}
56
  </button>
57
  <div className="separator"></div>
58
 
59
+ <button className={`menu-btn ${activeTab === 'achievement' ? 'active' : ''}`} onClick={() => setActiveTab('achievement')}>
 
 
 
60
  My Achievement
61
  </button>
62
  <div className="separator"></div>
63
 
64
+ <button className={`menu-btn ${activeTab === 'records' ? 'active' : ''}`} onClick={() => setActiveTab('records')}>
 
 
 
65
  My Records
66
  </button>
67
  <div className="separator"></div>
68
 
69
+ <button className={`menu-btn ${activeTab === 'customise' ? 'active' : ''}`} onClick={() => setActiveTab('customise')}>
 
 
 
70
  Customise
71
  </button>
72
  <div className="separator"></div>
73
 
74
+ <button className={`menu-btn ${activeTab === 'help' ? 'active' : ''}`} onClick={() => setActiveTab('help')}>
 
 
 
75
  Help
76
  </button>
77
  </nav>
78
 
79
+ {/* 把核心状态传给 Home 组件 */}
80
+ {activeTab === 'home' && <Home setActiveTab={setActiveTab} role={role} setRole={setRole} />}
81
+
 
 
 
82
  <FocusPageLocal
83
  videoManager={videoManagerRef.current}
84
  sessionResult={sessionResult}
 
93
  );
94
  }
95
 
96
+ 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
  // 创建临时下载链接
 
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
  // 创建临时下载链接
src/components/Home.jsx CHANGED
@@ -1,20 +1,124 @@
1
- import React from 'react';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- // receive setActiveTab to change page
4
- function Home({ setActiveTab }) {
5
  return (
6
  <main id="page-a" className="page">
7
  <h1>FocusGuard</h1>
8
  <p>Your productivity monitor assistant.</p>
9
 
10
- {/* click button change to 'focus' page */}
11
- <button
12
- id="start-button"
13
- className="btn-main"
14
- onClick={() => setActiveTab('focus')}
15
- >
16
- Start
17
- </button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  </main>
19
  );
20
  }
 
1
+ import React, { useRef } from 'react';
2
+
3
+ function Home({ setActiveTab, role, setRole }) {
4
+ const fileInputRef = useRef(null);
5
+
6
+ // 1. 开始新生活
7
+ const handleNewStart = async () => {
8
+ await fetch('/api/history', { method: 'DELETE' });
9
+ setActiveTab('focus');
10
+ };
11
+
12
+ // 2. 自动导入 (使用缓存魔术)
13
+ const handleAutoImport = async () => {
14
+ const backup = localStorage.getItem('focus_magic_backup');
15
+ if (backup) {
16
+ try {
17
+ const sessions = JSON.parse(backup);
18
+ const response = await fetch('/api/import', {
19
+ method: 'POST',
20
+ headers: { 'Content-Type': 'application/json' },
21
+ body: JSON.stringify(sessions)
22
+ });
23
+ if (response.ok) {
24
+ alert("Auto-recovery successful!");
25
+ } else {
26
+ alert("Auto-recovery failed.");
27
+ }
28
+ } catch (err) {
29
+ alert("Error: " + err.message);
30
+ }
31
+ } else {
32
+ alert("No previous backup found. Please use Manual Import.");
33
+ }
34
+ };
35
+
36
+ // 3. 手动导入
37
+ const handleFileChange = async (event) => {
38
+ const file = event.target.files[0];
39
+ if (!file) return;
40
+ const reader = new FileReader();
41
+ reader.onload = async (e) => {
42
+ try {
43
+ const sessions = JSON.parse(e.target.result);
44
+ const response = await fetch('/api/import', {
45
+ method: 'POST',
46
+ headers: { 'Content-Type': 'application/json' },
47
+ body: JSON.stringify(sessions)
48
+ });
49
+ if (response.ok) {
50
+ alert("Import successful!");
51
+ }
52
+ } catch (err) {
53
+ alert("Error: " + err.message);
54
+ }
55
+ event.target.value = '';
56
+ };
57
+ reader.readAsText(file);
58
+ };
59
+
60
+ // 4. 切换 Admin/User 模式
61
+ const handleAdminToggle = async () => {
62
+ if (role === 'admin') {
63
+ if (window.confirm("Switch back to User mode? Current data will be cleared.")) {
64
+ await fetch('/api/history', { method: 'DELETE' });
65
+ setRole('user');
66
+ alert("Switched to User mode.");
67
+ }
68
+ } else {
69
+ const pwd = window.prompt("Enter Admin Password:");
70
+ if (pwd === "123") {
71
+ try {
72
+ await fetch('/api/history', { method: 'DELETE' });
73
+ const res = await fetch('/test_data.json');
74
+ if (!res.ok) throw new Error("test_data.json not found");
75
+ const testData = await res.json();
76
+ const importRes = await fetch('/api/import', {
77
+ method: 'POST',
78
+ headers: { 'Content-Type': 'application/json' },
79
+ body: JSON.stringify(testData)
80
+ });
81
+ if (importRes.ok) {
82
+ setRole('admin');
83
+ alert("Admin mode activated!");
84
+ }
85
+ } catch (error) {
86
+ alert("Admin login failed: " + error.message);
87
+ }
88
+ } else if (pwd !== null) {
89
+ alert("Incorrect password!");
90
+ }
91
+ }
92
+ };
93
 
 
 
94
  return (
95
  <main id="page-a" className="page">
96
  <h1>FocusGuard</h1>
97
  <p>Your productivity monitor assistant.</p>
98
 
99
+ {/* 🔪 把隐藏的上传框放在网格外面,绝对不让它占格子! */}
100
+ <input type="file" ref={fileInputRef} style={{ display: 'none' }} accept=".json" onChange={handleFileChange} />
101
+
102
+ {/* 使用全新的 2x2 网格容器,里面只放 4 个纯净的按钮 */}
103
+ <div className="home-button-grid">
104
+
105
+ <button className="btn-main" onClick={handleNewStart}>
106
+ Start Focus
107
+ </button>
108
+
109
+ <button className="btn-main" onClick={handleAutoImport}>
110
+ Auto Import History
111
+ </button>
112
+
113
+ <button className="btn-main" onClick={() => fileInputRef.current.click()}>
114
+ Manual Import History
115
+ </button>
116
+
117
+ <button className="btn-main" onClick={handleAdminToggle}>
118
+ {role === 'admin' ? 'Switch to User Mode' : 'Admin Login'}
119
+ </button>
120
+
121
+ </div>
122
  </main>
123
  );
124
  }