import { useState, useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { Button } from '@/components/ui/button'; import { Label } from '@/components/ui/label'; import { Switch } from '@/components/ui/switch'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Separator } from '@/components/ui/separator'; import { useToast } from '@/hooks/use-toast'; import { useAuth } from '@/hooks/useAuth'; import { supabase } from '@/integrations/supabase/client'; import { Bell, Mail, Smartphone, Volume2 } from 'lucide-react'; interface EmailNotifications { review_replies: boolean; new_followers: boolean; system_announcements: boolean; weekly_digest: boolean; monthly_digest: boolean; } interface PushNotifications { browser_enabled: boolean; new_content: boolean; social_updates: boolean; } export function NotificationsTab() { const { user } = useAuth(); const { toast } = useToast(); const [loading, setLoading] = useState(false); const [emailNotifications, setEmailNotifications] = useState({ review_replies: true, new_followers: true, system_announcements: true, weekly_digest: false, monthly_digest: true }); const [pushNotifications, setPushNotifications] = useState({ browser_enabled: false, new_content: true, social_updates: true }); useEffect(() => { fetchNotificationPreferences(); }, [user]); const fetchNotificationPreferences = async () => { if (!user) return; try { const { data, error } = await supabase.from('user_preferences').select('email_notifications, push_notifications').eq('user_id', user.id).maybeSingle(); if (error && error.code !== 'PGRST116') { console.error('Error fetching notification preferences:', error); return; } if (data) { if (data.email_notifications) { setEmailNotifications(data.email_notifications as unknown as EmailNotifications); } if (data.push_notifications) { setPushNotifications(data.push_notifications as unknown as PushNotifications); } } else { // Initialize preferences if they don't exist await initializePreferences(); } } catch (error) { console.error('Error fetching notification preferences:', error); } }; const initializePreferences = async () => { if (!user) return; try { const { error } = await supabase.from('user_preferences').insert([{ user_id: user.id, email_notifications: emailNotifications as any, push_notifications: pushNotifications as any }]); if (error) throw error; } catch (error) { console.error('Error initializing preferences:', error); } }; const updateEmailNotification = (key: keyof EmailNotifications, value: boolean) => { setEmailNotifications(prev => ({ ...prev, [key]: value })); }; const updatePushNotification = (key: keyof PushNotifications, value: boolean) => { setPushNotifications(prev => ({ ...prev, [key]: value })); }; const saveNotificationPreferences = async () => { if (!user) return; setLoading(true); try { const { error } = await supabase.from('user_preferences').upsert([{ user_id: user.id, email_notifications: emailNotifications as any, push_notifications: pushNotifications as any, updated_at: new Date().toISOString() }]); if (error) throw error; toast({ title: 'Preferences saved', description: 'Your notification preferences have been updated.' }); } catch (error: any) { toast({ title: 'Error', description: error.message || 'Failed to save notification preferences', variant: 'destructive' }); } finally { setLoading(false); } }; const requestPushPermission = async () => { if ('Notification' in window) { const permission = await Notification.requestPermission(); if (permission === 'granted') { updatePushNotification('browser_enabled', true); toast({ title: 'Push notifications enabled', description: 'You will now receive browser push notifications.' }); } else { toast({ title: 'Permission denied', description: 'Push notifications require permission to work.', variant: 'destructive' }); } } }; return
{/* Email Notifications */}

Email Notifications

Choose which email notifications you'd like to receive.

Get notified when someone replies to your reviews

updateEmailNotification('review_replies', checked)} />

Get notified when someone follows you

updateEmailNotification('new_followers', checked)} />

Important updates and announcements from ThrillWiki

updateEmailNotification('system_announcements', checked)} />

Weekly summary of new parks, rides, and community activity

updateEmailNotification('weekly_digest', checked)} />

Monthly roundup of popular content and your activity stats

updateEmailNotification('monthly_digest', checked)} />
{/* Push Notifications */}

Push Notifications

Receive instant notifications in your browser when important events happen.

Enable push notifications in your browser

{!pushNotifications.browser_enabled && } { if (!checked) { updatePushNotification('browser_enabled', false); } else { requestPushPermission(); } }} />
{pushNotifications.browser_enabled && <>

Notifications about new parks, rides, and reviews

updatePushNotification('new_content', checked)} />

Notifications about followers, replies, and mentions

updatePushNotification('social_updates', checked)} />
}
{/* Sound Settings */} {/* Save Button */}
; }