samlax12 commited on
Commit
66c1245
·
verified ·
1 Parent(s): e85fa50

Update src/pages/SettingsPage.tsx

Browse files
Files changed (1) hide show
  1. src/pages/SettingsPage.tsx +183 -156
src/pages/SettingsPage.tsx CHANGED
@@ -1,157 +1,184 @@
1
- import React, { useState } from 'react';
2
- import Layout from '../components/Layout/Layout';
3
- import Card, { CardHeader, CardContent } from '../components/common/Card';
4
- import Button from '../components/common/Button';
5
- import { useNavigate } from 'react-router-dom';
6
-
7
- const SettingsPage: React.FC = () => {
8
- const navigate = useNavigate();
9
- const [dataExported, setDataExported] = useState(false);
10
-
11
- // 导出所有数据
12
- const handleExportAllData = () => {
13
- const promptGroups = localStorage.getItem('promptGroups');
14
- const categories = localStorage.getItem('categories');
15
-
16
- const allData = {
17
- promptGroups: promptGroups ? JSON.parse(promptGroups) : [],
18
- categories: categories ? JSON.parse(categories) : []
19
- };
20
-
21
- const jsonString = JSON.stringify(allData, null, 2);
22
- const blob = new Blob([jsonString], { type: 'application/json' });
23
- const url = URL.createObjectURL(blob);
24
-
25
- const a = document.createElement('a');
26
- a.href = url;
27
- a.download = `prompt-manager-backup-${new Date().toISOString().split('T')[0]}.json`;
28
- document.body.appendChild(a);
29
- a.click();
30
-
31
- // 清理
32
- setTimeout(() => {
33
- document.body.removeChild(a);
34
- URL.revokeObjectURL(url);
35
- setDataExported(true);
36
-
37
- // 重置状态
38
- setTimeout(() => setDataExported(false), 3000);
39
- }, 0);
40
- };
41
-
42
- // 导入数据文件
43
- const handleImportData = (event: React.ChangeEvent<HTMLInputElement>) => {
44
- const file = event.target.files?.[0];
45
- if (!file) return;
46
-
47
- const reader = new FileReader();
48
- reader.onload = (e) => {
49
- try {
50
- const data = JSON.parse(e.target?.result as string);
51
-
52
- if (data.promptGroups && Array.isArray(data.promptGroups)) {
53
- localStorage.setItem('promptGroups', JSON.stringify(data.promptGroups));
54
- }
55
-
56
- if (data.categories && Array.isArray(data.categories)) {
57
- localStorage.setItem('categories', JSON.stringify(data.categories));
58
- }
59
-
60
- alert('数据导入成功,应用将刷新以加载新数据。');
61
- window.location.reload();
62
- } catch (error) {
63
- alert('导入失败,文件格式不正确。');
64
- console.error('导入错误:', error);
65
- }
66
- };
67
- reader.readAsText(file);
68
- };
69
-
70
- // 清除所有数据
71
- const handleClearAllData = () => {
72
- if (window.confirm('确定要清除所有数据吗?此操作不可撤销。')) {
73
- localStorage.removeItem('promptGroups');
74
- localStorage.removeItem('categories');
75
- alert('所有数据已清除,应用将刷新。');
76
- window.location.reload();
77
- }
78
- };
79
-
80
- return (
81
- <Layout title="设置">
82
- <div className="space-y-4">
83
- <Card>
84
- <CardHeader title="数据管理" />
85
- <CardContent>
86
- <div className="space-y-4">
87
- <div>
88
- <h3 className="font-medium mb-2">备份数据</h3>
89
- <p className="text-gray-600 text-sm mb-2">
90
- 导出所有提示词组和分类数据为JSON文件,可用于备份或迁移。
91
- </p>
92
- <Button
93
- variant="primary"
94
- onClick={handleExportAllData}
95
- >
96
- {dataExported ? '✓ 导出成功' : '导出所有数据'}
97
- </Button>
98
- </div>
99
-
100
- <div className="ios-divider"></div>
101
-
102
- <div>
103
- <h3 className="font-medium mb-2">导入数据</h3>
104
- <p className="text-gray-600 text-sm mb-2">
105
- 从之前导出的JSON文件中恢复数据。将覆盖当前所有数据。
106
- </p>
107
- <input
108
- type="file"
109
- id="import-file"
110
- accept=".json"
111
- style={{ display: 'none' }}
112
- onChange={handleImportData}
113
- />
114
- <Button
115
- variant="secondary"
116
- onClick={() => document.getElementById('import-file')?.click()}
117
- >
118
- 导入数据
119
- </Button>
120
- </div>
121
-
122
- <div className="ios-divider"></div>
123
-
124
- <div>
125
- <h3 className="font-medium mb-2">清除数据</h3>
126
- <p className="text-gray-600 text-sm mb-2">
127
- 删除所有存储的提示词组和分类数据。此操作不可撤销。
128
- </p>
129
- <Button
130
- variant="danger"
131
- onClick={handleClearAllData}
132
- >
133
- 清除所有数据
134
- </Button>
135
- </div>
136
- </div>
137
- </CardContent>
138
- </Card>
139
-
140
- <Card>
141
- <CardHeader title="关于" />
142
- <CardContent>
143
- <h3 className="font-medium mb-2">提示词管理应用</h3>
144
- <p className="text-gray-600 text-sm mb-4">
145
- 版本 1.0.0
146
- </p>
147
- <p className="text-gray-600 text-sm">
148
- 这是一个用于管理提示词、工作流和DSL文件的本地应用。所有数据均存储在浏览器的本地存储中。
149
- </p>
150
- </CardContent>
151
- </Card>
152
- </div>
153
- </Layout>
154
- );
155
- };
156
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  export default SettingsPage;
 
1
+ import React, { useState } from 'react';
2
+ import { useNavigate } from 'react-router-dom';
3
+ import Layout from '../components/Layout/Layout';
4
+ import Card, { CardHeader, CardContent } from '../components/common/Card';
5
+ import Button from '../components/common/Button';
6
+ import { useAuth } from '../contexts/AuthContext';
7
+
8
+ const SettingsPage: React.FC = () => {
9
+ const navigate = useNavigate();
10
+ const { logout } = useAuth();
11
+ const [dataExported, setDataExported] = useState(false);
12
+
13
+ // 导出所有数据
14
+ const handleExportAllData = () => {
15
+ const promptGroups = localStorage.getItem('promptGroups');
16
+ const categories = localStorage.getItem('categories');
17
+
18
+ const allData = {
19
+ promptGroups: promptGroups ? JSON.parse(promptGroups) : [],
20
+ categories: categories ? JSON.parse(categories) : []
21
+ };
22
+
23
+ const jsonString = JSON.stringify(allData, null, 2);
24
+ const blob = new Blob([jsonString], { type: 'application/json' });
25
+ const url = URL.createObjectURL(blob);
26
+
27
+ const a = document.createElement('a');
28
+ a.href = url;
29
+ a.download = `prompt-manager-backup-${new Date().toISOString().split('T')[0]}.json`;
30
+ document.body.appendChild(a);
31
+ a.click();
32
+
33
+ // 清理
34
+ setTimeout(() => {
35
+ document.body.removeChild(a);
36
+ URL.revokeObjectURL(url);
37
+ setDataExported(true);
38
+
39
+ // 重置状态
40
+ setTimeout(() => setDataExported(false), 3000);
41
+ }, 0);
42
+ };
43
+
44
+ // 导入数据文件
45
+ const handleImportData = (event: React.ChangeEvent<HTMLInputElement>) => {
46
+ const file = event.target.files?.[0];
47
+ if (!file) return;
48
+
49
+ const reader = new FileReader();
50
+ reader.onload = (e) => {
51
+ try {
52
+ const data = JSON.parse(e.target?.result as string);
53
+
54
+ if (data.promptGroups && Array.isArray(data.promptGroups)) {
55
+ localStorage.setItem('promptGroups', JSON.stringify(data.promptGroups));
56
+ }
57
+
58
+ if (data.categories && Array.isArray(data.categories)) {
59
+ localStorage.setItem('categories', JSON.stringify(data.categories));
60
+ }
61
+
62
+ alert('数据导入成功,应用将刷新以加载新数据。');
63
+ window.location.reload();
64
+ } catch (error) {
65
+ alert('导入失败,文件格式不正确。');
66
+ console.error('导入错误:', error);
67
+ }
68
+ };
69
+ reader.readAsText(file);
70
+ };
71
+
72
+ // 清除所有数据
73
+ const handleClearAllData = () => {
74
+ if (window.confirm('确定要清除所有数据吗?此操作不可撤销。')) {
75
+ localStorage.removeItem('promptGroups');
76
+ localStorage.removeItem('categories');
77
+ alert('所有数据已清除,应用将刷新。');
78
+ window.location.reload();
79
+ }
80
+ };
81
+
82
+ // 退出登录
83
+ const handleLogout = () => {
84
+ if (window.confirm('确定要退出登录吗?')) {
85
+ logout();
86
+ navigate('/login');
87
+ }
88
+ };
89
+
90
+ return (
91
+ <Layout title="设置">
92
+ <div className="space-y-4">
93
+ {/* 账户管理卡片 */}
94
+ <Card>
95
+ <CardHeader title="账户管理" />
96
+ <CardContent>
97
+ <div className="space-y-4">
98
+ <Button
99
+ variant="danger"
100
+ fullWidth
101
+ onClick={handleLogout}
102
+ >
103
+ 退出登录
104
+ </Button>
105
+ </div>
106
+ </CardContent>
107
+ </Card>
108
+
109
+ {/* 数据管理卡片 */}
110
+ <Card>
111
+ <CardHeader title="数据管理" />
112
+ <CardContent>
113
+ <div className="space-y-4">
114
+ <div>
115
+ <h3 className="font-medium mb-2">备份数据</h3>
116
+ <p className="text-gray-600 text-sm mb-2">
117
+ 导出所有提示词组和分类数据为JSON文件,可用于备份或迁移。
118
+ </p>
119
+ <Button
120
+ variant="primary"
121
+ onClick={handleExportAllData}
122
+ >
123
+ {dataExported ? '✓ 导出成功' : '导出所有数据'}
124
+ </Button>
125
+ </div>
126
+
127
+ <div className="ios-divider"></div>
128
+
129
+ <div>
130
+ <h3 className="font-medium mb-2">导入数据</h3>
131
+ <p className="text-gray-600 text-sm mb-2">
132
+ 从之前导出的JSON文件中恢复数据。将覆盖当前所有数据。
133
+ </p>
134
+ <input
135
+ type="file"
136
+ id="import-file"
137
+ accept=".json"
138
+ style={{ display: 'none' }}
139
+ onChange={handleImportData}
140
+ />
141
+ <Button
142
+ variant="secondary"
143
+ onClick={() => document.getElementById('import-file')?.click()}
144
+ >
145
+ 导入数据
146
+ </Button>
147
+ </div>
148
+
149
+ <div className="ios-divider"></div>
150
+
151
+ <div>
152
+ <h3 className="font-medium mb-2">清除数据</h3>
153
+ <p className="text-gray-600 text-sm mb-2">
154
+ 删除所有存储的提示词组和分类数据。此操作不可撤销。
155
+ </p>
156
+ <Button
157
+ variant="danger"
158
+ onClick={handleClearAllData}
159
+ >
160
+ 清除所有数据
161
+ </Button>
162
+ </div>
163
+ </div>
164
+ </CardContent>
165
+ </Card>
166
+
167
+ <Card>
168
+ <CardHeader title="关于" />
169
+ <CardContent>
170
+ <h3 className="font-medium mb-2">提示词管理应用</h3>
171
+ <p className="text-gray-600 text-sm mb-4">
172
+ 版本 1.0.0
173
+ </p>
174
+ <p className="text-gray-600 text-sm">
175
+ 这是一个用于管理提示词、工作流和DSL文件的本地应用。所有数据均存储在浏览器的本地存储中。
176
+ </p>
177
+ </CardContent>
178
+ </Card>
179
+ </div>
180
+ </Layout>
181
+ );
182
+ };
183
+
184
  export default SettingsPage;