'use client' import { useState } from 'react' import { trpc } from '@/lib/trpc/client' import { Switch } from '@/components/ui/switch' import { Label } from '@/components/ui/label' import { Button } from '@/components/ui/button' import { Skeleton } from '@/components/ui/skeleton' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { toast } from 'sonner' import { Users, Scale, GraduationCap, Eye, Shield } from 'lucide-react' // Category icons and labels const CATEGORIES = { team: { label: 'Team / Applicant', icon: Users }, jury: { label: 'Jury Members', icon: Scale }, mentor: { label: 'Mentors', icon: GraduationCap }, observer: { label: 'Observers', icon: Eye }, admin: { label: 'Administrators', icon: Shield }, } type NotificationSetting = { id: string notificationType: string category: string label: string description: string | null sendEmail: boolean } export function NotificationSettingsForm() { const { data: settings, isLoading, refetch } = trpc.notification.getEmailSettings.useQuery() const updateMutation = trpc.notification.updateEmailSetting.useMutation({ onSuccess: () => { toast.success('Notification setting updated') refetch() }, onError: (error) => { toast.error(`Failed to update: ${error.message}`) }, }) const handleToggle = (notificationType: string, sendEmail: boolean) => { updateMutation.mutate({ notificationType, sendEmail }) } if (isLoading) { return (
{[...Array(5)].map((_, i) => ( ))}
) } // Group settings by category const groupedSettings = (settings || []).reduce( (acc, setting) => { const category = setting.category || 'other' if (!acc[category]) acc[category] = [] acc[category].push(setting) return acc }, {} as Record ) if (Object.keys(groupedSettings).length === 0) { return (

No notification types configured yet. Notification settings will appear here once the system is seeded.

) } return (

Toggle which notifications should also send email notifications to users. Users can still disable email notifications in their personal preferences.

{Object.entries(CATEGORIES).map(([categoryKey, { label, icon: Icon }]) => { const categorySettings = groupedSettings[categoryKey] if (!categorySettings || categorySettings.length === 0) return null return ( {label} {categorySettings.filter(s => s.sendEmail).length}/{categorySettings.length} enabled {categorySettings.map((setting) => (
{setting.description && (

{setting.description}

)}
handleToggle(setting.notificationType, checked) } disabled={updateMutation.isPending} />
))}
) })}
) }