"use client"; import { useSession } from "next-auth/react"; import { useRouter } from "next/navigation"; import { useState, useEffect } from "react"; import { useTheme } from "@/lib/contexts/theme-context"; import { notificationManager } from "@/lib/utils/notifications"; import Header from "@/components/Header"; import LoadingSpinner from "@/components/LoadingSpinner"; interface UserSettings { defaultQuality: string; publicProfile: boolean; allowDownload: boolean; } export default function SettingsPage() { const { data: session, status } = useSession(); const router = useRouter(); const { theme, setTheme } = useTheme(); const [defaultQuality, setDefaultQuality] = useState("medium"); const [publicProfile, setPublicProfile] = useState(false); const [allowDownload, setAllowDownload] = useState(true); const [isSaving, setIsSaving] = useState(false); const [isLoading, setIsLoading] = useState(true); const [isExporting, setIsExporting] = useState(false); // 获取用户设置 const fetchUserSettings = async () => { try { const response = await fetch("/api/user/settings"); if (response.ok) { const result = await response.json(); if (result.success && result.data) { const data: UserSettings = result.data; setDefaultQuality(data.defaultQuality); setPublicProfile(data.publicProfile); setAllowDownload(data.allowDownload); } else { console.error("API 返回数据格式错误:", result); } } } catch (error) { console.error("获取用户设置失败:", error); } finally { setIsLoading(false); } }; useEffect(() => { if (status === "authenticated") { fetchUserSettings(); } }, [status]); const handleSaveSettings = async () => { setIsSaving(true); try { const response = await fetch("/api/user/settings", { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ defaultQuality, publicProfile, allowDownload, }), }); if (response.ok) { console.log("设置已保存"); notificationManager.success("设置已保存", "您的设置已成功保存"); } else { throw new Error("保存设置失败"); } } catch (error) { console.error("保存设置失败:", error); notificationManager.error("保存失败", "设置保存失败,请重试"); } finally { setIsSaving(false); } }; const handleExportData = async () => { setIsExporting(true); try { const response = await fetch("/api/user/export"); if (response.ok) { const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `recorder-export-${Date.now()}.json`; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); notificationManager.success("导出成功", "数据已成功导出"); } else { throw new Error("导出失败"); } } catch (error) { console.error("导出失败:", error); notificationManager.error("导出失败", "数据导出失败,请重试"); } finally { setIsExporting(false); } }; const handleResetSettings = async () => { if (!confirm("确定要重置所有设置为默认值吗?")) return; try { const response = await fetch("/api/user/settings", { method: "DELETE", }); if (response.ok) { await fetchUserSettings(); notificationManager.success("重置成功", "设置已重置为默认值"); } else { throw new Error("重置失败"); } } catch (error) { console.error("重置设置失败:", error); notificationManager.error("重置失败", "设置重置失败,请重试"); } }; if (status === "loading" || isLoading) { return (
); } if (status === "unauthenticated") { router.push("/login"); return null; } if (!session?.user) { return null; } return (

设置

自定义你的应用体验

{/* 录音设置 */}

录音设置

默认音频质量

选择默认的录音质量

{/* 外观设置 */}

外观设置

主题模式

选择你喜欢的主题

{/* 隐私设置 */}

隐私设置

公开个人资料

允许其他用户查看你的个人资料

允许下载

允许其他用户下载你的录音

{/* 数据管理 */}

数据管理

导出数据

导出你的所有录音数据

重置设置

将所有设置重置为默认值

删除账户

永久删除你的账户和所有录音

{/* 保存按钮 */}
); }