import { useEffect, useState, useMemo } from 'react'; import { Container, Title, Text, Switch, Stack, Paper, Group, Button, Loader, Select, Divider, Checkbox, ScrollArea, } from '@mantine/core'; import { IconArrowLeft, IconSettings, IconBell, IconRefresh, IconBellCheck, IconChevronDown } from '@tabler/icons-react'; import { useNavigate } from 'react-router-dom'; import { useAppStore } from '../stores'; import { requestNotificationPermission, getNotificationPermission, isNotificationSupported } from '../services/notification'; import { syncRemindersToSW, triggerSWCheck } from '../services/swSync'; import { notifications } from '@mantine/notifications'; import { getBuiltInHolidays } from '../constants/holidays'; export function SettingsPage() { const navigate = useNavigate(); const settings = useAppStore((state) => state.settings); const updateSettings = useAppStore((state) => state.updateSettings); const [permissionStatus, setPermissionStatus] = useState<'granted' | 'denied' | 'default'>('default'); const [isRequesting, setIsRequesting] = useState(false); const [holidayFilterExpanded, setHolidayFilterExpanded] = useState(false); // 获取所有节假日列表 const allHolidays = useMemo(() => getBuiltInHolidays(), []); // 法定节假日 const statutoryHolidays = useMemo( () => allHolidays.filter((h) => h.isStatutory), [allHolidays] ); // 非法定节假日 const nonStatutoryHolidays = useMemo( () => allHolidays.filter((h) => !h.isStatutory), [allHolidays] ); // 处理节假日开关 const handleHolidayToggle = (checked: boolean) => { updateSettings({ showHolidays: checked }); if (!checked) { // 关闭时也可以收起筛选面板 setHolidayFilterExpanded(false); } }; // 处理显示数量变化 const handleDisplayCountChange = (value: string | null) => { updateSettings({ holidayDisplayCount: value ? parseInt(value, 10) : 3 }); }; // 处理仅显示法定节假日变化 const handleStatutoryOnlyChange = (checked: boolean) => { updateSettings({ showStatutoryOnly: checked }); }; // 处理特定节假日选择 const handleHolidaySelection = (holidayId: string, checked: boolean) => { const currentEnabled = settings.enabledHolidays || []; let newEnabled: string[]; if (checked) { newEnabled = [...currentEnabled, holidayId]; } else { newEnabled = currentEnabled.filter((id) => id !== holidayId); } updateSettings({ enabledHolidays: newEnabled }); }; // 检查节假日是否被选中 const isHolidaySelected = (holidayId: string) => { const enabled = settings.enabledHolidays || []; return enabled.includes(holidayId); }; // 全选/取消全选 const handleSelectAll = (checked: boolean) => { if (checked) { updateSettings({ enabledHolidays: allHolidays.map((h) => h.id) }); } else { updateSettings({ enabledHolidays: [] }); } }; // 选择所有法定节假日 const handleSelectStatutory = (checked: boolean) => { if (checked) { const statutoryIds = statutoryHolidays.map((h) => h.id); const currentEnabled = settings.enabledHolidays || []; // 合并现有选择和法定节假日 const newEnabled = [...new Set([...currentEnabled, ...statutoryIds])]; updateSettings({ enabledHolidays: newEnabled }); } else { // 移除所有法定节假日 const statutoryIds = new Set(statutoryHolidays.map((h) => h.id)); const newEnabled = (settings.enabledHolidays || []).filter( (id) => !statutoryIds.has(id) ); updateSettings({ enabledHolidays: newEnabled }); } }; // 页面加载时检查登录状态 useEffect(() => { const isAuthenticated = useAppStore.getState().isAuthenticated; if (!isAuthenticated) { navigate('/login', { replace: true }); } }, [navigate]); // 初始化权限状态 useEffect(() => { if (isNotificationSupported()) { setPermissionStatus(getNotificationPermission()); } }, []); // 处理浏览器通知开关 const handleBrowserNotificationToggle = async (enabled: boolean) => { if (!isNotificationSupported()) { notifications.show({ title: '不支持通知', message: '您的浏览器不支持通知功能', color: 'red', }); return; } if (enabled) { // 请求权限 setIsRequesting(true); const permission = await requestNotificationPermission(); setIsRequesting(false); setPermissionStatus(permission); if (permission === 'granted') { updateSettings({ browserNotifications: true }); // 同步所有提醒到 Service Worker await syncRemindersToSW(); notifications.show({ title: '通知已开启', message: '您将收到浏览器的提醒通知', color: 'green', }); } else { // 权限被拒绝 notifications.show({ title: '无法开启通知', message: '请在浏览器设置中允许通知权限', color: 'red', }); } } else { updateSettings({ browserNotifications: false }); } }; // 同步提醒到 SW const handleSyncReminders = async () => { await syncRemindersToSW(); notifications.show({ title: '同步完成', message: '提醒已同步到 Service Worker', color: 'green', }); }; // 手动触发 SW 检查 const handleTriggerCheck = async () => { await triggerSWCheck(); notifications.show({ title: '检查已触发', message: 'Service Worker 将立即检查提醒', color: 'blue', }); }; // 发送测试通知 const handleTestNotification = async () => { if (Notification.permission !== 'granted') { notifications.show({ title: '请先开启通知', message: '需要先允许通知权限', color: 'yellow', }); return; } new Notification('测试通知', { body: '这是一条测试通知,通知功能正常工作', icon: '/favicon.png', tag: 'test-notification', }); notifications.show({ title: '测试通知已发送', message: '请查看浏览器通知', color: 'blue', }); }; return (
{/* Header */} 设置 {/* 节假日设置 */} 显示节假日 在纪念日列表中显示即将到来的节假日 handleHolidayToggle(e.currentTarget.checked)} size="sm" color="#1a1a1a" /> {/* 节假日显示数量 */} {settings.showHolidays && ( 显示数量