Spaces:
Sleeping
Sleeping
File size: 6,246 Bytes
4fc8eae e85fa50 |
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 |
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import Layout from '../components/Layout/Layout';
import Card, { CardHeader, CardContent } from '../components/common/Card';
import Button from '../components/common/Button';
import { useAuth } from '../contexts/AuthContext';
const SettingsPage: React.FC = () => {
const navigate = useNavigate();
const { logout } = useAuth();
const [dataExported, setDataExported] = useState(false);
// 导出所有数据
const handleExportAllData = () => {
const promptGroups = localStorage.getItem('promptGroups');
const categories = localStorage.getItem('categories');
const allData = {
promptGroups: promptGroups ? JSON.parse(promptGroups) : [],
categories: categories ? JSON.parse(categories) : []
};
const jsonString = JSON.stringify(allData, null, 2);
const blob = new Blob([jsonString], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `prompt-manager-backup-${new Date().toISOString().split('T')[0]}.json`;
document.body.appendChild(a);
a.click();
// 清理
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
setDataExported(true);
// 重置状态
setTimeout(() => setDataExported(false), 3000);
}, 0);
};
// 导入数据文件
const handleImportData = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
try {
const data = JSON.parse(e.target?.result as string);
if (data.promptGroups && Array.isArray(data.promptGroups)) {
localStorage.setItem('promptGroups', JSON.stringify(data.promptGroups));
}
if (data.categories && Array.isArray(data.categories)) {
localStorage.setItem('categories', JSON.stringify(data.categories));
}
alert('数据导入成功,应用将刷新以加载新数据。');
window.location.reload();
} catch (error) {
alert('导入失败,文件格式不正确。');
console.error('导入错误:', error);
}
};
reader.readAsText(file);
};
// 清除所有数据
const handleClearAllData = () => {
if (window.confirm('确定要清除所有数据吗?此操作不可撤销。')) {
localStorage.removeItem('promptGroups');
localStorage.removeItem('categories');
alert('所有数据已清除,应用将刷新。');
window.location.reload();
}
};
// 退出登录
const handleLogout = () => {
if (window.confirm('确定要退出登录吗?')) {
logout();
navigate('/login');
}
};
return (
<Layout title="设置">
<div className="space-y-4">
{/* 账户管理卡片 */}
<Card>
<CardHeader title="账户管理" />
<CardContent>
<div className="space-y-4">
<Button
variant="danger"
fullWidth
onClick={handleLogout}
>
退出登录
</Button>
</div>
</CardContent>
</Card>
{/* 数据管理卡片 */}
<Card>
<CardHeader title="数据管理" />
<CardContent>
<div className="space-y-4">
<div>
<h3 className="font-medium mb-2">备份数据</h3>
<p className="text-gray-600 text-sm mb-2">
导出所有提示词组和分类数据为JSON文件,可用于备份或迁移。
</p>
<Button
variant="primary"
onClick={handleExportAllData}
>
{dataExported ? '✓ 导出成功' : '导出所有数据'}
</Button>
</div>
<div className="ios-divider"></div>
<div>
<h3 className="font-medium mb-2">导入数据</h3>
<p className="text-gray-600 text-sm mb-2">
从之前导出的JSON文件中恢复数据。将覆盖当前所有数据。
</p>
<input
type="file"
id="import-file"
accept=".json"
style={{ display: 'none' }}
onChange={handleImportData}
/>
<Button
variant="secondary"
onClick={() => document.getElementById('import-file')?.click()}
>
导入数据
</Button>
</div>
<div className="ios-divider"></div>
<div>
<h3 className="font-medium mb-2">清除数据</h3>
<p className="text-gray-600 text-sm mb-2">
删除所有存储的提示词组和分类数据。此操作不可撤销。
</p>
<Button
variant="danger"
onClick={handleClearAllData}
>
清除所有数据
</Button>
</div>
</div>
</CardContent>
</Card>
<Card>
<CardHeader title="关于" />
<CardContent>
<h3 className="font-medium mb-2">提示词管理应用</h3>
<p className="text-gray-600 text-sm mb-4">
版本 1.0.0
</p>
<p className="text-gray-600 text-sm">
这是一个用于管理提示词、工作流和DSL文件的本地应用。所有数据均存储在浏览器的本地存储中。
</p>
</CardContent>
</Card>
</div>
</Layout>
);
};
export default SettingsPage; |