Repository navigation
feat(notifications): 概览当日待还总金额、主动通知增强、通知中心与全通知项开关(Web + iOS) #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e6285fd
fc9e3f4
ea75f83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||||||||||||||||||||||||||||
| "use client"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import { useEffect, useState, useCallback, useMemo } from "react"; | ||||||||||||||||||||||||||||||||||||
| import { useEffect, useState, useCallback, useMemo, useRef } from "react"; | ||||||||||||||||||||||||||||||||||||
| import { useRouter } from "next/navigation"; | ||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| getBillDashboardItems, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -10,13 +10,20 @@ import { | |||||||||||||||||||||||||||||||||||
| } from "@/lib/api/bills"; | ||||||||||||||||||||||||||||||||||||
| import { getHolders } from "@/lib/api/holders"; | ||||||||||||||||||||||||||||||||||||
| import { getCardsBasicByHolders } from "@/lib/api/cards"; | ||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| getRecurringPayables, | ||||||||||||||||||||||||||||||||||||
| payRecurringPayable, | ||||||||||||||||||||||||||||||||||||
| type PayableWithStatus, | ||||||||||||||||||||||||||||||||||||
| } from "@/lib/api/payables"; | ||||||||||||||||||||||||||||||||||||
| import { getUnreadNotificationCount } from "@/lib/api/notifications"; | ||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||
| type HolderFilter, | ||||||||||||||||||||||||||||||||||||
| type FutureRange, | ||||||||||||||||||||||||||||||||||||
| billCurrency, | ||||||||||||||||||||||||||||||||||||
| computeMetrics, | ||||||||||||||||||||||||||||||||||||
| countCardsByHolder, | ||||||||||||||||||||||||||||||||||||
| formatMoney, | ||||||||||||||||||||||||||||||||||||
| payableRemaining, | ||||||||||||||||||||||||||||||||||||
| } from "@/lib/dashboard-metrics"; | ||||||||||||||||||||||||||||||||||||
| import { useUserPreferences } from "@/lib/hooks/useUserPreferences"; | ||||||||||||||||||||||||||||||||||||
| import { fetchRates, type FxRates } from "@/lib/fx"; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -61,7 +68,10 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| const [error, setError] = useState<string | null>(null); | ||||||||||||||||||||||||||||||||||||
| const [holders, setHolders] = useState<CardHolder[]>([]); | ||||||||||||||||||||||||||||||||||||
| const [bills, setBills] = useState<BillDashboardItem[]>([]); | ||||||||||||||||||||||||||||||||||||
| const [payables, setPayables] = useState<PayableWithStatus[]>([]); | ||||||||||||||||||||||||||||||||||||
| const [unreadNotificationCount, setUnreadNotificationCount] = useState(0); | ||||||||||||||||||||||||||||||||||||
| const [cards, setCards] = useState<Array<{ id: string; holder_id: string }>>([]); | ||||||||||||||||||||||||||||||||||||
| const unreadSyncedRef = useRef(false); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // ---- 交互状态 ---- | ||||||||||||||||||||||||||||||||||||
| const [holderFilter, setHolderFilter] = useState<HolderFilter>("all"); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -88,6 +98,7 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| // 一键还款与 Toast 状态 | ||||||||||||||||||||||||||||||||||||
| const [payAllOpen, setPayAllOpen] = useState(false); | ||||||||||||||||||||||||||||||||||||
| const [paying, setPaying] = useState(false); | ||||||||||||||||||||||||||||||||||||
| const [payingPayableId, setPayingPayableId] = useState<string | null>(null); | ||||||||||||||||||||||||||||||||||||
| const [toast, setToast] = useState<{ | ||||||||||||||||||||||||||||||||||||
| open: boolean; | ||||||||||||||||||||||||||||||||||||
| message: string; | ||||||||||||||||||||||||||||||||||||
|
|
@@ -102,15 +113,39 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| const blurClass = privacyOn ? "privacy-blur" : "privacy-clear"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // ---- 数据加载 ---- | ||||||||||||||||||||||||||||||||||||
| const loadData = useCallback(async () => { | ||||||||||||||||||||||||||||||||||||
| const loadData = useCallback(async (opts?: { silent?: boolean }) => { | ||||||||||||||||||||||||||||||||||||
| if (!user?.id) return; | ||||||||||||||||||||||||||||||||||||
| setLoading(true); | ||||||||||||||||||||||||||||||||||||
| const silent = Boolean(opts?.silent); | ||||||||||||||||||||||||||||||||||||
| if (!silent) { | ||||||||||||||||||||||||||||||||||||
| setLoading(true); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| setError(null); | ||||||||||||||||||||||||||||||||||||
| const shouldSyncUnread = !unreadSyncedRef.current; | ||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| // 并行:持卡人 / 账单;卡片依赖持卡人 id,随后批量查询 | ||||||||||||||||||||||||||||||||||||
| const [holderList, allBills] = await Promise.all([ | ||||||||||||||||||||||||||||||||||||
| // 并行:持卡人 / 账单 / 固定支出 / 未读通知数(仅首载同步当日通知);卡片依赖持卡人 id | ||||||||||||||||||||||||||||||||||||
| const [holderList, allBills, payableList, unreadCount] = await Promise.all([ | ||||||||||||||||||||||||||||||||||||
| getHolders(user.id), | ||||||||||||||||||||||||||||||||||||
| getBillDashboardItems(200, user.id), | ||||||||||||||||||||||||||||||||||||
| getRecurringPayables(user.id).catch((err) => { | ||||||||||||||||||||||||||||||||||||
| console.warn("[dashboard] Failed to load recurring payables:", err); | ||||||||||||||||||||||||||||||||||||
| if (!silent) { | ||||||||||||||||||||||||||||||||||||
| setToast({ | ||||||||||||||||||||||||||||||||||||
| open: true, | ||||||||||||||||||||||||||||||||||||
| message: "固定支出加载失败,当日待还金额暂不含固定支出", | ||||||||||||||||||||||||||||||||||||
| severity: "info", | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return []; | ||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||
| getUnreadNotificationCount(shouldSyncUnread) | ||||||||||||||||||||||||||||||||||||
| .then((count) => { | ||||||||||||||||||||||||||||||||||||
| if (shouldSyncUnread) unreadSyncedRef.current = true; | ||||||||||||||||||||||||||||||||||||
| return count; | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| .catch((err) => { | ||||||||||||||||||||||||||||||||||||
| console.warn("[dashboard] Failed to load unread notification count:", err); | ||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||
| holderList.sort((a, b) => a.created_at.localeCompare(b.created_at)); | ||||||||||||||||||||||||||||||||||||
| const holderIds = holderList.map((h) => h.id); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -119,11 +154,15 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| setHolders(holderList); | ||||||||||||||||||||||||||||||||||||
| setCards(cardsRes); | ||||||||||||||||||||||||||||||||||||
| setBills(allBills); | ||||||||||||||||||||||||||||||||||||
| setPayables(payableList); | ||||||||||||||||||||||||||||||||||||
| setUnreadNotificationCount(unreadCount); | ||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||
| console.error("Dashboard load failed:", err); | ||||||||||||||||||||||||||||||||||||
| setError(err instanceof Error ? err.message : "加载失败,请下拉重试"); | ||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||
| setLoading(false); | ||||||||||||||||||||||||||||||||||||
| if (!silent) { | ||||||||||||||||||||||||||||||||||||
| setLoading(false); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
162
to
166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||||||||||||||||||||
| }, [user?.id]); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -202,6 +241,31 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const handlePayPayable = async (e: React.MouseEvent, payable: PayableWithStatus) => { | ||||||||||||||||||||||||||||||||||||
| e.stopPropagation(); | ||||||||||||||||||||||||||||||||||||
| if (payingPayableId === payable.id) return; | ||||||||||||||||||||||||||||||||||||
| const remaining = payableRemaining(payable); | ||||||||||||||||||||||||||||||||||||
| if (remaining <= 0) return; | ||||||||||||||||||||||||||||||||||||
| setPayingPayableId(payable.id); | ||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+244
to
+250
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||||||||||||||||||||
| await payRecurringPayable(payable.id, remaining); | ||||||||||||||||||||||||||||||||||||
| setToast({ | ||||||||||||||||||||||||||||||||||||
| open: true, | ||||||||||||||||||||||||||||||||||||
| message: `🎉 已登记 ${payable.name} 本期付款(${formatMoney(remaining, payable.currency || displayCurrency)})!`, | ||||||||||||||||||||||||||||||||||||
| severity: "success", | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| await loadData({ silent: true }); | ||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||
| setToast({ | ||||||||||||||||||||||||||||||||||||
| open: true, | ||||||||||||||||||||||||||||||||||||
| message: "登记付款失败: " + (err instanceof Error ? err.message : String(err)), | ||||||||||||||||||||||||||||||||||||
| severity: "error", | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||
| setPayingPayableId((curr) => (curr === payable.id ? null : curr)); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const handlePayAllOverdue = async () => { | ||||||||||||||||||||||||||||||||||||
| if (!user?.id) return; | ||||||||||||||||||||||||||||||||||||
| setPaying(true); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -240,22 +304,29 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| const closeToast = useCallback(() => setToast((prev) => ({ ...prev, open: false })), []); | ||||||||||||||||||||||||||||||||||||
| const goToBills = useCallback(() => router.push("/bills"), [router]); | ||||||||||||||||||||||||||||||||||||
| const goToPromos = useCallback(() => router.push("/promos"), [router]); | ||||||||||||||||||||||||||||||||||||
| const goToNotifications = useCallback(() => router.push("/notifications"), [router]); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // 当前选中持卡人下的账单 | ||||||||||||||||||||||||||||||||||||
| const visibleBills = useMemo(() => { | ||||||||||||||||||||||||||||||||||||
| if (holderFilter === "all") return bills; | ||||||||||||||||||||||||||||||||||||
| return bills.filter((b) => b.card?.holder_id === holderFilter); | ||||||||||||||||||||||||||||||||||||
| }, [bills, holderFilter]); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // 当前选中持卡人下的固定支出 | ||||||||||||||||||||||||||||||||||||
| const visiblePayables = useMemo(() => { | ||||||||||||||||||||||||||||||||||||
| if (holderFilter === "all") return payables; | ||||||||||||||||||||||||||||||||||||
| return payables.filter((p) => p.holder_id === holderFilter); | ||||||||||||||||||||||||||||||||||||
| }, [payables, holderFilter]); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // 当前选中持卡人下的卡片 | ||||||||||||||||||||||||||||||||||||
| const visibleCards = useMemo(() => { | ||||||||||||||||||||||||||||||||||||
| if (holderFilter === "all") return cards; | ||||||||||||||||||||||||||||||||||||
| return cards.filter((c) => c.holder_id === holderFilter); | ||||||||||||||||||||||||||||||||||||
| }, [cards, holderFilter]); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const metrics = useMemo( | ||||||||||||||||||||||||||||||||||||
| () => computeMetrics(visibleBills, futureRange, displayCurrency, fxRates), | ||||||||||||||||||||||||||||||||||||
| [visibleBills, futureRange, displayCurrency, fxRates], | ||||||||||||||||||||||||||||||||||||
| () => computeMetrics(visibleBills, futureRange, displayCurrency, fxRates, visiblePayables), | ||||||||||||||||||||||||||||||||||||
| [visibleBills, futureRange, displayCurrency, fxRates, visiblePayables], | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
327
to
330
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const cardsByHolder = useMemo(() => countCardsByHolder(cards), [cards]); | ||||||||||||||||||||||||||||||||||||
|
|
@@ -353,11 +424,14 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| onTogglePrivacy={togglePrivacyMode} | ||||||||||||||||||||||||||||||||||||
| amountClass={amountClass} | ||||||||||||||||||||||||||||||||||||
| displayCurrency={displayCurrency} | ||||||||||||||||||||||||||||||||||||
| visibleBills={visibleBills} | ||||||||||||||||||||||||||||||||||||
| onPaySingle={handlePaySingle} | ||||||||||||||||||||||||||||||||||||
| onPayPayable={handlePayPayable} | ||||||||||||||||||||||||||||||||||||
| payingPayableId={payingPayableId} | ||||||||||||||||||||||||||||||||||||
| onPayAllOpen={openPayAllConfirm} | ||||||||||||||||||||||||||||||||||||
| onNavBills={goToBills} | ||||||||||||||||||||||||||||||||||||
| onNavPromos={goToPromos} | ||||||||||||||||||||||||||||||||||||
| onNavNotifications={goToNotifications} | ||||||||||||||||||||||||||||||||||||
| unreadNotificationCount={unreadNotificationCount} | ||||||||||||||||||||||||||||||||||||
| totalCardCount={totalCardCount} | ||||||||||||||||||||||||||||||||||||
| holders={holders} | ||||||||||||||||||||||||||||||||||||
| cardsByHolder={cardsByHolder} | ||||||||||||||||||||||||||||||||||||
|
|
@@ -380,13 +454,13 @@ export default function DashboardPage() { | |||||||||||||||||||||||||||||||||||
| toast={toast} | ||||||||||||||||||||||||||||||||||||
| onToastClose={closeToast} | ||||||||||||||||||||||||||||||||||||
| error={error} | ||||||||||||||||||||||||||||||||||||
| onRetry={loadData} | ||||||||||||||||||||||||||||||||||||
| onRetry={() => void loadData()} | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| <QuickActionSheet | ||||||||||||||||||||||||||||||||||||
| open={quickSheetOpen} | ||||||||||||||||||||||||||||||||||||
| onClose={closeQuickSheet} | ||||||||||||||||||||||||||||||||||||
| onChanged={loadData} | ||||||||||||||||||||||||||||||||||||
| onChanged={() => void loadData({ silent: true })} | ||||||||||||||||||||||||||||||||||||
| initialAction={quickInitialAction} | ||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { NotificationCenterView } from '@/components/mobile/NotificationCenterView' | ||
|
|
||
| export default function NotificationsPage() { | ||
| return <NotificationCenterView /> | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,8 @@ import { DisplayPreferencesCard } from '@/components/settings/DisplayPreferences | |||||||||||
| import { ReminderPreferencesCard } from '@/components/settings/ReminderPreferencesCard' | ||||||||||||
| import { GmailConnectPanel } from '@/components/mobile/GmailConnectPanel' | ||||||||||||
| import { getGmailConnection, type GmailConnectionStatus } from '@/lib/api/gmail' | ||||||||||||
| import { getUnreadNotificationCount } from '@/lib/api/notifications' | ||||||||||||
| import { countEnabledNotificationItems } from '@/lib/notifications.shared' | ||||||||||||
| import { | ||||||||||||
| UserIcon, | ||||||||||||
| PromoIcon, | ||||||||||||
|
|
@@ -98,6 +100,7 @@ export default function SettingsPage() { | |||||||||||
| const [showDisplaySheet, setShowDisplaySheet] = useState(false) | ||||||||||||
| const [showLogoutDialog, setShowLogoutDialog] = useState(false) | ||||||||||||
| const [loggingOut, setLoggingOut] = useState(false) | ||||||||||||
| const [unreadNotifications, setUnreadNotifications] = useState<number | null>(null) | ||||||||||||
|
|
||||||||||||
| const [notice, setNotice] = useState<{ open: boolean; message: string; severity: 'success' | 'error' | 'info' }>({ | ||||||||||||
| open: false, | ||||||||||||
|
|
@@ -123,6 +126,47 @@ export default function SettingsPage() { | |||||||||||
| void loadGmail() | ||||||||||||
| }, [loadGmail]) | ||||||||||||
|
|
||||||||||||
| const refreshUnreadCount = useCallback(async () => { | ||||||||||||
| try { | ||||||||||||
| const count = await getUnreadNotificationCount(false) | ||||||||||||
| setUnreadNotifications(count) | ||||||||||||
| } catch (err) { | ||||||||||||
| console.warn('[settings] failed to fetch unread notification count:', err) | ||||||||||||
| setUnreadNotifications(null) | ||||||||||||
| } | ||||||||||||
| }, []) | ||||||||||||
|
|
||||||||||||
| useEffect(() => { | ||||||||||||
| void refreshUnreadCount() | ||||||||||||
| if (typeof window === 'undefined') return | ||||||||||||
| const onFocus = () => { | ||||||||||||
| void refreshUnreadCount() | ||||||||||||
| } | ||||||||||||
| const onVisibility = () => { | ||||||||||||
| if (document.visibilityState === 'visible') { | ||||||||||||
| void refreshUnreadCount() | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| window.addEventListener('focus', onFocus) | ||||||||||||
| document.addEventListener('visibilitychange', onVisibility) | ||||||||||||
|
Comment on lines
+150
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||
| return () => { | ||||||||||||
| window.removeEventListener('focus', onFocus) | ||||||||||||
| document.removeEventListener('visibilitychange', onVisibility) | ||||||||||||
| } | ||||||||||||
| }, [refreshUnreadCount]) | ||||||||||||
|
|
||||||||||||
| useEffect(() => { | ||||||||||||
| if (typeof window === 'undefined') return | ||||||||||||
| const params = new URLSearchParams(window.location.search) | ||||||||||||
| if (params.get('open') === 'reminders') { | ||||||||||||
| setShowReminderSheet(true) | ||||||||||||
| params.delete('open') | ||||||||||||
| const qs = params.toString() | ||||||||||||
| const cleanUrl = window.location.pathname + (qs ? `?${qs}` : '') | ||||||||||||
| window.history.replaceState(null, '', cleanUrl) | ||||||||||||
| } | ||||||||||||
| }, []) | ||||||||||||
|
|
||||||||||||
| const isAdmin = user?.role === 'admin' | ||||||||||||
| const forwardAddress = user ? 'bills+' + user.id + '@' + DOMAIN : null | ||||||||||||
|
|
||||||||||||
|
|
@@ -159,9 +203,9 @@ export default function SettingsPage() { | |||||||||||
| // 计算偏好摘要胶囊文案(映射表/函数化,去嵌套三元;完整展示已选节点) | ||||||||||||
| const gmailSummary = formatGmailSummary(gmailStatus, gmailLoading, gmailError) | ||||||||||||
| const billDays = reminderConfig?.remind_days_before ?? [3, 1, 0] | ||||||||||||
| const oppEnabled = reminderConfig?.opportunities?.enabled !== false | ||||||||||||
| const switchCounts = countEnabledNotificationItems(reminderConfig) | ||||||||||||
| const billDaysLabel = billDays.length > 0 ? `提前${billDays.join('/')}天` : '未设提醒' | ||||||||||||
| const reminderSummary = `${billDaysLabel} · 权益${oppEnabled ? '开' : '关'}` | ||||||||||||
| const reminderSummary = `${billDaysLabel} · ${switchCounts.enabled}/${switchCounts.total}项开` | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||
| const displaySummary = `${displayConfig?.default_currency || 'CNY'} · ${displayConfig?.privacy_mode ? '隐私开启' : '隐私关闭'}` | ||||||||||||
|
|
||||||||||||
| return ( | ||||||||||||
|
|
@@ -268,6 +312,29 @@ export default function SettingsPage() { | |||||||||||
| 数据同步与偏好设置 | ||||||||||||
| </p> | ||||||||||||
| <div className='overflow-hidden rounded-2xl border border-d-border bg-d-surface divide-y divide-d-border/60'> | ||||||||||||
| {/* 通知中心 */} | ||||||||||||
| <SettingsMenuRow | ||||||||||||
| icon={<BellIcon size={20} />} | ||||||||||||
| title='通知中心' | ||||||||||||
| subtitle='查看全部通知、标记已读与清理记录' | ||||||||||||
| onClick={() => router.push('/notifications')} | ||||||||||||
| badge={(() => { | ||||||||||||
| if (unreadNotifications === null) return null | ||||||||||||
| if (unreadNotifications > 0) { | ||||||||||||
| return ( | ||||||||||||
| <span className='mr-0.5 shrink-0 rounded-full border border-d-danger/40 bg-d-danger-soft px-2.5 py-0.5 text-[11px] font-semibold text-d-danger-ink'> | ||||||||||||
| {unreadNotifications > 99 ? '99+' : `${unreadNotifications} 未读`} | ||||||||||||
| </span> | ||||||||||||
| ) | ||||||||||||
| } | ||||||||||||
| return ( | ||||||||||||
| <span className='mr-0.5 shrink-0 rounded-full border border-d-border bg-d-subtle px-2.5 py-0.5 text-[11px] font-semibold text-d-muted'> | ||||||||||||
| 全部已读 | ||||||||||||
| </span> | ||||||||||||
| ) | ||||||||||||
| })()} | ||||||||||||
| /> | ||||||||||||
|
|
||||||||||||
| {/* 邮箱账单导入 */} | ||||||||||||
| <SettingsMenuRow | ||||||||||||
| icon={<ImportIcon size={20} />} | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ import webpush from 'web-push' | |||||||||||||||||
| import { getDb } from '@/lib/db' | ||||||||||||||||||
| import { getWorkerEnv } from '@/lib/db/client' | ||||||||||||||||||
| import { readSession } from '@/lib/session' | ||||||||||||||||||
| import { getLocalTodayString } from '@/lib/utils' | ||||||||||||||||||
|
|
||||||||||||||||||
| async function sessionUserId(request: Request): Promise<string | null> { | ||||||||||||||||||
| const session = await readSession(request) | ||||||||||||||||||
|
|
@@ -65,5 +66,40 @@ export async function POST(request: Request) { | |||||||||||||||||
| for (const id of staleIds) { | ||||||||||||||||||
| await db.prepare('DELETE FROM push_subscriptions WHERE id = ?').bind(id).run() | ||||||||||||||||||
| } | ||||||||||||||||||
| if (sent > 0) { | ||||||||||||||||||
| const ts = new Date().toISOString() | ||||||||||||||||||
| const todayStr = getLocalTodayString('Asia/Shanghai') | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| try { | ||||||||||||||||||
| await db | ||||||||||||||||||
| .prepare( | ||||||||||||||||||
| `INSERT INTO notification_history | ||||||||||||||||||
| (id, user_id, notification_key, category, title, body, target_url, metadata_json, sent_at, status, push_sent_at) | ||||||||||||||||||
| VALUES (?, ?, ?, 'system', ?, ?, '/notifications', ?, ?, 'sent', ?) | ||||||||||||||||||
| ON CONFLICT(user_id, notification_key) DO UPDATE SET | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||
| title = excluded.title, | ||||||||||||||||||
| body = excluded.body, | ||||||||||||||||||
| metadata_json = excluded.metadata_json, | ||||||||||||||||||
| sent_at = excluded.sent_at, | ||||||||||||||||||
| status = 'sent', | ||||||||||||||||||
| push_sent_at = excluded.push_sent_at, | ||||||||||||||||||
| -- 测试推送由用户在设置页显式触发,有意重置同日记录阅读/删除状态;勿复制到 Cron 路径 | ||||||||||||||||||
| read_at = NULL, | ||||||||||||||||||
| deleted_at = NULL` | ||||||||||||||||||
|
Comment on lines
+84
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||
| ) | ||||||||||||||||||
| .bind( | ||||||||||||||||||
| crypto.randomUUID(), | ||||||||||||||||||
| userId, | ||||||||||||||||||
| `test:${todayStr}`, | ||||||||||||||||||
| 'WorldCredit 测试推送', | ||||||||||||||||||
| '推送通道已就绪 ✅ 到期提醒将按时送达。', | ||||||||||||||||||
| JSON.stringify({ kind: 'system_test', sent }), | ||||||||||||||||||
| ts, | ||||||||||||||||||
| ts | ||||||||||||||||||
| ) | ||||||||||||||||||
| .run() | ||||||||||||||||||
| } catch (err) { | ||||||||||||||||||
| console.warn('[push/test] failed to record notification_history:', err) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| return Response.json({ data: { subscriptions: subs.length, sent, removed: staleIds.length, errors }, error: null }) | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
首载 getUnreadNotificationCount(shouldSyncUnread=true) 会触发服务端 ensureDailyNotificationsForUser → buildDailyNotificationCandidates(账单/固定支出/卡片/免息期/权益多表重查询),且嵌在首屏 Promise.all 关键路径上,可能拖慢首屏;而 NotificationCenterView 持有独立的 syncedTodayRef,用户随后打开通知中心会再次完整执行同一同步(INSERT OR IGNORE 幂等,但候选构建成本翻倍)。服务端 cron 已负责每日生成通知,建议仪表盘改为 syncToday=false,把同步职责留给通知中心首载或 cron。另注意 unreadSyncedRef 置 true 后永不重置,应用驻留跨天时客户端不再触发当日同步(有 cron 兜底,影响较小)。
Suggestion: