import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Settings as SettingsIcon, Bell, Shield, Palette, Lock, Calendar, Sliders } from 'lucide-react'; import { setupPushNotifications } from '@/utils/pushNotifications'; import { useAuth, isAdmin as checkIsAdmin } from '@/contexts/AuthContext'; import { ConfigurationManager } from '@/components/admin/ConfigurationManager'; import { HolidayManager } from '@/components/admin/HolidayManager'; import { UserRoleManager } from '@/components/admin/UserRoleManager'; import { NotificationStatusModal } from '@/components/settings/NotificationStatusModal'; import { NotificationPreferencesModal } from '@/components/settings/NotificationPreferencesModal'; import { useState, useEffect } from 'react'; import { getUserSubscriptions } from '@/services/notificationApi'; export function Settings() { const { user } = useAuth(); const isAdmin = checkIsAdmin(user); const [showNotificationModal, setShowNotificationModal] = useState(false); const [notificationSuccess, setNotificationSuccess] = useState(false); const [notificationMessage, setNotificationMessage] = useState(); const [isEnablingNotifications, setIsEnablingNotifications] = useState(false); const [showPreferencesModal, setShowPreferencesModal] = useState(false); const [hasSubscription, setHasSubscription] = useState(false); const [checkingSubscription, setCheckingSubscription] = useState(true); useEffect(() => { checkSubscriptionStatus(); }, []); const checkSubscriptionStatus = async () => { try { setCheckingSubscription(true); const subscriptions = await getUserSubscriptions(); setHasSubscription(subscriptions.length > 0); } catch (error) { console.error('Failed to check subscription status:', error); setHasSubscription(false); } finally { setCheckingSubscription(false); } }; const handleEnableNotifications = async () => { setIsEnablingNotifications(true); setShowNotificationModal(false); try { // Check if notifications are supported if (!('Notification' in window)) { setNotificationSuccess(false); setNotificationMessage('Notifications are not supported in this browser. Please use a modern browser like Chrome, Firefox, or Edge.'); setShowNotificationModal(true); setIsEnablingNotifications(false); return; } // Check current permission status BEFORE attempting to enable let permission = Notification.permission; // If permission was previously denied, show user-friendly instructions if (permission === 'denied') { setNotificationSuccess(false); setNotificationMessage( 'Notification permission was previously denied. To enable notifications:\n\n' + '1. Click the lock icon (🔒) or info icon (â„šī¸) in your browser\'s address bar\n' + '2. Find "Notifications" in the permissions list\n' + '3. Change it from "Block" to "Allow"\n' + '4. Refresh this page and try again\n\n' + 'Alternatively, you can enable notifications in your browser\'s site settings.' ); setShowNotificationModal(true); setIsEnablingNotifications(false); return; } // If permission is 'default', request it first if (permission === 'default') { permission = await Notification.requestPermission(); // If user denied the permission request if (permission === 'denied') { setNotificationSuccess(false); setNotificationMessage( 'Notification permission was denied. To enable notifications:\n\n' + '1. Click the lock icon (🔒) or info icon (â„šī¸) in your browser\'s address bar\n' + '2. Find "Notifications" in the permissions list\n' + '3. Change it from "Block" to "Allow"\n' + '4. Refresh this page and try again' ); setShowNotificationModal(true); setIsEnablingNotifications(false); return; } } // Only proceed if permission is 'granted' if (permission !== 'granted') { setNotificationSuccess(false); setNotificationMessage('Notification permission is required to enable push notifications. Please grant permission and try again.'); setShowNotificationModal(true); setIsEnablingNotifications(false); return; } // Permission is granted, proceed with setup await setupPushNotifications(); setNotificationSuccess(true); setNotificationMessage('Push notifications have been successfully enabled! You will now receive notifications for workflow updates, approvals, and TAT alerts.'); setShowNotificationModal(true); // Recheck subscription status await checkSubscriptionStatus(); } catch (error: any) { console.error('[Settings] Error enabling notifications:', error); setNotificationSuccess(false); // Provide more specific error messages const errorMessage = error?.message || 'Unknown error occurred'; setNotificationMessage( errorMessage.includes('permission') ? 'Notification permission was denied. Please enable notifications in your browser settings and try again.' : errorMessage.includes('Service worker') ? 'Service worker registration failed. Please refresh the page and try again.' : errorMessage.includes('token') ? 'Authentication required. Please log in again and try enabling notifications.' : `Unable to enable push notifications: ${errorMessage}` ); setShowNotificationModal(true); } finally { setIsEnablingNotifications(false); } }; return (
{/* Header Card */}

Settings

Manage your account and preferences

{/* Tabs for Admin, Cards for Non-Admin */} {isAdmin ? ( User Settings User User Roles Roles Configuration Config Holidays Holidays {/* Fixed width container to prevent layout shifts */}
{/* User Settings Tab */}
{/* Enable Push Notifications Setup */}
Browser Push Setup Register this browser for push notifications
{checkingSubscription ? (

Checking registration status...

) : hasSubscription ? (

✓ This browser is already registered for push notifications.

) : (

Click below to register this browser for receiving push notifications. This needs to be done once per browser/device.

)}
{/* Security Settings */}
Security Password and security settings

Security settings will be available soon

{/* Appearance Settings */}
Appearance Theme and display preferences

Appearance settings will be available soon

{/* Preferences */}
Preferences Notification and application preferences
{/* User Roles Tab (Admin Only) */} {/* System Configuration Tab (Admin Only) */} {/* Holiday Calendar Tab (Admin Only) */}
) : ( <> {/* Non-Admin User Settings Only */}
{/* Enable Push Notifications Setup */}
Browser Push Setup Register this browser for push notifications
{checkingSubscription ? (

Checking registration status...

) : hasSubscription ? (

✓ This browser is already registered for push notifications.

) : (

Click below to register this browser for receiving push notifications. This needs to be done once per browser/device.

)}
{/* Security Settings */}
Security Password and security settings

Security settings will be available soon

{/* Appearance Settings */}
Appearance Theme and display preferences

Appearance settings will be available soon

{/* Preferences */}
Preferences Notification and application preferences
)}
setShowNotificationModal(false)} success={notificationSuccess} message={notificationMessage} /> setShowPreferencesModal(false)} />
); }