"use client"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { BellRing, Loader2, Save } from "lucide-react"; import { http } from "@/lib/api"; import type { NotificationPrefs } from "@/lib/types"; import { NotificationsPanel } from "@/components/notifications-panel"; import { PageHeader } from "@/components/page-header"; import { Panel } from "@/components/panel"; import { Skeleton } from "@/components/ui/skeleton"; import { Button } from "@/components/ui/button"; import { Switch } from "@/components/ui/switch"; import { Label } from "@/components/ui/label"; const prefsMeta: { key: keyof NotificationPrefs; title: string; desc: string }[] = [ { key: "progress", title: "پیشرفت و باز شدن دروس", desc: "باز شدن آزمون، تکمیل درس، شاخه‌های جدید." }, { key: "gamification", title: "بازی‌سازی", desc: "امتیاز، زنجیرهٔ فعالیت، لیگ‌ها و هشدار قلب." }, { key: "mentor", title: "منتور و گفتگو", desc: "پاسخ منتور، پیام هم‌دوره‌ای، تایید رفع قفل." }, { key: "event", title: "رویدادها و اطلاعیه‌ها", desc: "تایید ثبت‌نام و یادآوری ۱۵ دقیقه قبل از شروع." }, ]; export default function NotificationsPage() { const qc = useQueryClient(); const { data, isLoading } = useQuery({ queryKey: ["prefs"], queryFn: () => http.get<{ prefs: NotificationPrefs }>("/api/notifications/preferences"), }); const [draft, setDraft] = useState(null); const [prefs, setPrefs] = useState<{ prefs: NotificationPrefs }>(); const prefsValue = draft ?? prefs?.prefs ?? data?.prefs; const save = useMutation({ mutationFn: (p: NotificationPrefs) => http.put<{ prefs: NotificationPrefs }>("/api/notifications/preferences", p), onSuccess: (res) => { setPrefs(res); setDraft(null); qc.invalidateQueries({ queryKey: ["prefs"] }); }, }); if (isLoading) { return (
{Array.from({ length: 4 }).map((_, i) => (
))}
{Array.from({ length: 3 }).map((_, i) => ( ))}
); } const toggle = (key: keyof NotificationPrefs) => { if (!prefsValue) return; setDraft({ ...prefsValue, [key]: !prefsValue[key] }); }; const dirty = draft != null; return (
} tint="bg-primary/10 text-primary" >
{prefsValue && prefsMeta.map((m) => (

{m.desc}

toggle(m.key)} />
))}
); }