"use client" import { useState, useEffect } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Switch } from "@/components/ui/switch" import { Label } from "@/components/ui/label" import { Bell, BellOff, X, Check } from "lucide-react" import { useDialog } from "@/components/ui/alert-dialog-custom" import { requestNotificationPermission, checkUpcomingAnniversaries } from "@/lib/notifications" import { useFamily } from "@/context/family-context" import { Alert, AlertDescription } from "@/components/ui/alert" import { MemberNameWithStatus } from "@/components/member-name-with-status" export function NotificationSettings() { const { showAlert } = useDialog() const { treeData } = useFamily() const [notificationEnabled, setNotificationEnabled] = useState(false) const [permission, setPermission] = useState("default") const [isClient, setIsClient] = useState(false) const [upcomingEvents, setUpcomingEvents] = useState<{ birthdays: Array<{ member: any; daysUntil: number }> deathdays: Array<{ member: any; daysUntil: number }> }>({ birthdays: [], deathdays: [] }) useEffect(() => { // 标记为客户端 setIsClient(true) // 检查通知权限 if (typeof window !== "undefined" && "Notification" in window) { setPermission(Notification.permission) setNotificationEnabled(Notification.permission === "granted") } // 检查即将到来的纪念日 const members = Object.values(treeData.members) const upcoming = checkUpcomingAnniversaries(members, 7) setUpcomingEvents(upcoming) }, [treeData]) const handleEnableNotifications = async () => { const granted = await requestNotificationPermission() if (granted) { setNotificationEnabled(true) setPermission("granted") // 发送测试通知 new Notification("通知已启用", { body: "您将收到家族纪念日提醒", icon: "/icon.svg", }) } else { setPermission(Notification.permission) } } const handleDisableNotifications = async () => { setNotificationEnabled(false) await showAlert("请在浏览器设置中禁用此网站的通知权限", "提示") } return (
纪念日通知 接收家族成员生日和忌日的浏览器通知 {!isClient ? (
加载中...
) : !(typeof window !== "undefined" && "Notification" in window) ? ( 您的浏览器不支持通知功能 ) : ( <>

每天自动检查并提醒当日纪念日

{ if (checked) { handleEnableNotifications() } else { handleDisableNotifications() } }} />
{permission === "denied" && ( 通知权限已被拒绝。请在浏览器设置中允许此网站发送通知。 )} {permission === "granted" && ( 通知已启用。您将在每天首次访问时收到当日纪念日提醒。 )} {notificationEnabled && ( )} )}
{/* 即将到来的纪念日 */} {(upcomingEvents.birthdays.length > 0 || upcomingEvents.deathdays.length > 0) && ( 即将到来的纪念日(7天内) 提前准备,不错过重要日子
{upcomingEvents.birthdays.length > 0 && (

🎂 生日

{upcomingEvents.birthdays.map(({ member, daysUntil }) => (

{new Date(member.birthDate).toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' })}

{daysUntil === 1 ? '明天' : `${daysUntil}天后`}

))}
)} {upcomingEvents.deathdays.length > 0 && (

🕯️ 忌日

{upcomingEvents.deathdays.map(({ member, daysUntil }) => (

{new Date(member.deathDate!).toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' })}

{daysUntil === 1 ? '明天' : `${daysUntil}天后`}

))}
)}
)}
) }