"use client"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useAuth } from "@/hooks/use-auth"; import { useTranslations } from "@/lib/i18n/context"; import { Switch } from "@/components/ui/switch"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import { PageHeader } from "@/components/ui/page-header"; import { Skeleton } from "@/components/ui/skeleton"; import { EmptyState } from "@/components/ui/empty-state"; import { QueryErrorCard } from "@/components/ui/query-error-card"; import Link from "next/link"; import { Bell, Settings, AlertCircle } from "lucide-react"; import { EVENT_DEFAULT_ENABLED, type EventType, } from "@/lib/notifications/types"; import { eventDescriptionTranslationKey, eventTranslationKey, } from "@/lib/notifications/event-labels"; import { queryKeys } from "@/lib/query-keys"; import { apiGet, apiPut } from "@/lib/api/api-fetch"; interface ChannelInfo { id: string; type: string; label: string; enabled: boolean; globallyEnabled: boolean; } interface Preference { channelId: string; eventType: string; enabled: boolean; } interface PreferencesData { channels: ChannelInfo[]; preferences: Preference[]; eventTypes: string[]; } export default function NotificationsPage() { const { isAuthenticated, isLoading: authLoading } = useAuth(); const { t } = useTranslations(); const queryClient = useQueryClient(); const { data, isLoading, isError, refetch } = useQuery({ queryKey: queryKeys.notificationsPreferences(), queryFn: async () => { return apiGet("/api/notifications/preferences"); }, enabled: isAuthenticated, }); const toggleMutation = useMutation({ mutationFn: async (params: { channelId: string; eventType: string; enabled: boolean; }) => { return apiPut("/api/notifications/preferences", params); }, onMutate: async ({ channelId, eventType, enabled }) => { await queryClient.cancelQueries({ queryKey: queryKeys.notificationsPreferences(), }); const prev = queryClient.getQueryData( queryKeys.notificationsPreferences(), ); if (prev) { const existing = prev.preferences.find( (p) => p.channelId === channelId && p.eventType === eventType, ); const newPreferences = existing ? prev.preferences.map((p) => p.channelId === channelId && p.eventType === eventType ? { ...p, enabled } : p, ) : [...prev.preferences, { channelId, eventType, enabled }]; queryClient.setQueryData( queryKeys.notificationsPreferences(), { ...prev, preferences: newPreferences }, ); } return { prev }; }, onError: (_err, _vars, context) => { if (context?.prev) { queryClient.setQueryData( queryKeys.notificationsPreferences(), context.prev, ); } }, onSettled: () => { queryClient.invalidateQueries({ queryKey: queryKeys.notificationsPreferences(), }); }, }); function isEnabled(channelId: string, eventType: string): boolean { const pref = data?.preferences?.find( (p) => p.channelId === channelId && p.eventType === eventType, ); if (pref) return pref.enabled; // v1.4.25 W16c — per-event default. Most events stay opt-out // (no row = ON); PERSONAL_RECORD flips to opt-in (no row = OFF) // so a backfill doesn't fire hundreds of pushes on first sync. return EVENT_DEFAULT_ENABLED[eventType as EventType] ?? true; } // Cross-links to the other two notification surfaces (reminder types + // delivery channels) so the matrix never reads as the only place to manage // notifications. Mirrored by back-links on those two settings screens. const headerActions = ( <> ); if (authLoading || isLoading) { return (
{/* Skeleton mirrors the event-row matrix so the auth-resolving frame reserves layout instead of flashing a lone spinner. */}
{[0, 1, 2, 3, 4].map((i) => (
))}
); } if (!isAuthenticated) { return (
); } const channels = data?.channels ?? []; const eventTypes = data?.eventTypes ?? []; const activeChannels = channels.filter((ch) => ch.globallyEnabled); if (isError) { return (
refetch()} />
); } // No channels configured if (activeChannels.length === 0) { return (
} title={t("notifications.noChannels")} /* Channel setup lives in Settings → Integrations since v1.25.7; point the CTA at that anchor, not the reminder-types screen. */ action={ } />
); } return (
{/* Desktop: table layout */}
{activeChannels.map((ch) => ( ))} {eventTypes.map((eventType, idx) => { const nameKey = eventTranslationKey(eventType); const descKey = eventDescriptionTranslationKey(eventType); return ( {activeChannels.map((ch) => { const enabled = isEnabled(ch.id, eventType); const disabled = !ch.enabled; const switchLabel = `${t(nameKey)} - ${ch.label}`; return ( ); })} ); })}
{t("notifications.eventColumn")} {/* Disabled state reads through the note + the disabled switches below — never through sub-AA opacity dimming on the label or the note itself. */} {ch.label} {!ch.enabled && ( {t("notifications.channelDisabled")} )}
{t(nameKey)}
{t(descKey)}
toggleMutation.mutate({ channelId: ch.id, eventType, enabled: checked, }) } />
{/* Mobile: stacked cards */}
{eventTypes.map((eventType) => { const nameKey = eventTranslationKey(eventType); const descKey = eventDescriptionTranslationKey(eventType); return (
{t(nameKey)}
{t(descKey)}
{activeChannels.map((ch) => { const enabled = isEnabled(ch.id, eventType); const disabled = !ch.enabled; const switchId = `notification-${eventType}-${ch.id}`; return (
toggleMutation.mutate({ channelId: ch.id, eventType, enabled: checked, }) } />
); })}
); })}
{/* Info hint about globally-disabled channels — a full sentence that explains the state, not the bare "(Disabled)" inline tag. */} {channels.some((ch) => !ch.globallyEnabled) && (

{t("notifications.someChannelsGloballyDisabled")}

)}
); }