Spaces:
Sleeping
Sleeping
Update src/pages/SettingsPage.tsx
Browse files- src/pages/SettingsPage.tsx +183 -156
src/pages/SettingsPage.tsx
CHANGED
@@ -1,157 +1,184 @@
|
|
1 |
-
import React, { useState } from 'react';
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
const
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
const
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
const
|
24 |
-
|
25 |
-
const
|
26 |
-
|
27 |
-
a
|
28 |
-
|
29 |
-
a.
|
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 |
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;
|