import { useState, useEffect } from "react"; import { useNavigate } from "react-router"; import { motion } from "motion/react"; import { ChevronLeft, User, Calendar, Heart, Phone, Mail, MapPin, Edit2, Check, X, Camera, Baby, Activity, AlertCircle, Shield, Bell, LogOut, } from "lucide-react"; import { Button } from "../components/ui/button"; import { Card } from "../components/ui/card"; import { Badge } from "../components/ui/badge"; import { useUser, getPregnancyStage } from "../context/UserContext"; import { isDebug, getDebugLogs } from "../services/debug"; interface UserInfo { name: string; age: number; phone: string; email: string; address: string; avatar_url: string | null; lastPeriod: string; dueDate: string; currentWeek: number; currentDay: number; height: number; prePregnancyWeight: number; currentWeight: number; bloodType: string; allergies: string[]; medicalHistory: string[]; emergencyContact: { name: string; relation: string; phone: string; }; } export function ProfilePage() { const navigate = useNavigate(); const { user, updateUserRemote, logout } = useUser(); const [isEditing, setIsEditing] = useState(null); const [userInfo, setUserInfo] = useState({ name: user.name || "未设置", age: user.age || 0, phone: user.phone || "未设置", email: "", address: "", avatar_url: user.avatar_url || null, lastPeriod: user.lastPeriodDate || "", dueDate: user.dueDate || "", currentWeek: user.currentWeek, currentDay: user.currentDay, height: user.height || 0, prePregnancyWeight: user.prePregnancyWeight || 0, currentWeight: user.currentWeight || 0, bloodType: user.bloodType || "未设置", allergies: user.allergies.length ? user.allergies : ["无"], medicalHistory: user.medicalHistory.length ? user.medicalHistory : ["无重大病史"], emergencyContact: { name: user.emergencyContact.name || "未设置", relation: user.emergencyContact.relation || "", phone: user.emergencyContact.phone || "", }, }); const [editValues, setEditValues] = useState>({}); const handleEdit = (field: string) => { setIsEditing(field); setEditValues({ [field]: (userInfo as any)[field] }); }; const handleSave = (field: string) => { const updated = { ...userInfo, ...(editValues as any) }; setUserInfo(updated); setIsEditing(null); setEditValues({}); // Sync back to global context + backend updateUserRemote({ name: updated.name, age: updated.age, phone: updated.phone, lastPeriodDate: updated.lastPeriod, height: updated.height, prePregnancyWeight: updated.prePregnancyWeight, currentWeight: updated.currentWeight, bloodType: updated.bloodType, allergies: updated.allergies, medicalHistory: updated.medicalHistory, emergencyContact: updated.emergencyContact, }); }; const handleCancel = () => { setIsEditing(null); setEditValues({}); }; const calculateBMI = () => { const heightInMeters = userInfo.height / 100; const bmi = userInfo.currentWeight / (heightInMeters * heightInMeters); return bmi.toFixed(1); }; const calculateWeightGain = () => { return (userInfo.currentWeight - userInfo.prePregnancyWeight).toFixed(1); }; const InfoItem = ({ label, value, field, editable = true, type = "text", icon: Icon, }: { label: string; value: string | number; field: string; editable?: boolean; type?: string; icon?: any; }) => (
{Icon && (
)}
{label}
{isEditing === field ? ( setEditValues({ ...editValues, [field]: e.target.value }) } className="mt-1 px-2 py-1 border border-morandi-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-morandi-500" autoFocus /> ) : (
{value}
)}
{editable && (
{isEditing === field ? ( <> ) : ( )}
)}
); // 🐞 调试面板(VITE_DEBUG=true 时显示) const [debugLog] = useState(() => isDebug() ? getDebugLogs() : []); const [showDebug, setShowDebug] = useState(false); return (
{/* 🐞 调试面板 */} {debugLog.length > 0 && (
{showDebug && debugLog.map((line, i) => (
{line}
))}
)} {/* 顶部导航 */}

个人中心

管理您的个人信息

{/* 个人头像卡片 */}
{userInfo.avatar_url ? ( 头像 ) : (
)}

{userInfo.name}

{userInfo.age}岁 · {userInfo.bloodType}

孕{userInfo.currentWeek}周+{userInfo.currentDay}天

{/* 基本信息 */}

基本信息

{/* 孕期信息 */}

孕期信息

当前孕周
{userInfo.currentWeek}周+{userInfo.currentDay}天
{getPregnancyStage(userInfo.currentWeek)}
{/* 健康数据 */}

健康数据

当前BMI
{calculateBMI()}
正常范围
体重增长
+{calculateWeightGain()} kg
符合预期
{/* 联系方式 */}

联系方式

{/* 医疗信息 */}

医疗信息

血型
{userInfo.bloodType}
药物过敏史
{userInfo.allergies.map((allergy, index) => ( {allergy} ))}
既往病史
{userInfo.medicalHistory.map((history, index) => ( {history} ))}
{/* 紧急联系人 */}

紧急联系人

姓名
{userInfo.emergencyContact.name}
{userInfo.emergencyContact.relation}
联系电话
{userInfo.emergencyContact.phone}
{/* 退出登录 */}
); }