Files
chinese-family-tree-2/components/settings/notification-settings.tsx
T
freedakgmail 3d075c6076 0.0.8.5
2025-11-24 14:02:34 +08:00

232 lines
8.6 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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<NotificationPermission>("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 (
<div className="space-y-6">
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Bell className="h-5 w-5" />
</CardTitle>
<CardDescription>
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{!isClient ? (
<div className="text-center py-4 text-muted-foreground">
...
</div>
) : !(typeof window !== "undefined" && "Notification" in window) ? (
<Alert>
<AlertDescription>
</AlertDescription>
</Alert>
) : (
<>
<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label htmlFor="notifications"></Label>
<p className="text-sm text-muted-foreground">
</p>
</div>
<Switch
id="notifications"
checked={notificationEnabled}
onCheckedChange={(checked) => {
if (checked) {
handleEnableNotifications()
} else {
handleDisableNotifications()
}
}}
/>
</div>
{permission === "denied" && (
<Alert variant="destructive">
<X className="h-4 w-4" />
<AlertDescription>
</AlertDescription>
</Alert>
)}
{permission === "granted" && (
<Alert>
<Check className="h-4 w-4" />
<AlertDescription>
访
</AlertDescription>
</Alert>
)}
{notificationEnabled && (
<Button
variant="outline"
size="sm"
onClick={() => {
new Notification("测试通知", {
body: "这是一条测试通知",
icon: "/icon.svg",
})
}}
>
</Button>
)}
</>
)}
</CardContent>
</Card>
{/* 即将到来的纪念日 */}
{(upcomingEvents.birthdays.length > 0 || upcomingEvents.deathdays.length > 0) && (
<Card>
<CardHeader>
<CardTitle>7</CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-4">
{upcomingEvents.birthdays.length > 0 && (
<div>
<h4 className="font-medium mb-2 flex items-center gap-2">
🎂
</h4>
<div className="space-y-2">
{upcomingEvents.birthdays.map(({ member, daysUntil }) => (
<div
key={member.id}
className="flex items-center justify-between p-3 bg-muted/50 rounded-lg"
>
<div>
<p className="font-medium">
<MemberNameWithStatus
name={member.fullName}
isDead={!!member.deathDate}
/>
</p>
<p className="text-sm text-muted-foreground">
{new Date(member.birthDate).toLocaleDateString('zh-CN', {
month: 'long',
day: 'numeric'
})}
</p>
</div>
<div className="text-right">
<p className="text-sm font-medium text-primary">
{daysUntil === 1 ? '明天' : `${daysUntil}天后`}
</p>
</div>
</div>
))}
</div>
</div>
)}
{upcomingEvents.deathdays.length > 0 && (
<div>
<h4 className="font-medium mb-2 flex items-center gap-2">
🕯
</h4>
<div className="space-y-2">
{upcomingEvents.deathdays.map(({ member, daysUntil }) => (
<div
key={member.id}
className="flex items-center justify-between p-3 bg-muted/50 rounded-lg"
>
<div>
<p className="font-medium">
<MemberNameWithStatus
name={member.fullName}
isDead={!!member.deathDate}
/>
</p>
<p className="text-sm text-muted-foreground">
{new Date(member.deathDate!).toLocaleDateString('zh-CN', {
month: 'long',
day: 'numeric'
})}
</p>
</div>
<div className="text-right">
<p className="text-sm font-medium text-muted-foreground">
{daysUntil === 1 ? '明天' : `${daysUntil}天后`}
</p>
</div>
</div>
))}
</div>
</div>
)}
</div>
</CardContent>
</Card>
)}
</div>
)
}