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 { useState } from 'react'; 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 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); } 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 */}
{/* Notification Settings */}
Notifications Manage notification preferences
{/* 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 Application preferences

User preferences will be available soon

{/* User Roles Tab (Admin Only) */} {/* System Configuration Tab (Admin Only) */} {/* Holiday Calendar Tab (Admin Only) */}
) : ( <> {/* Non-Admin User Settings Only */}
{/* Notification Settings */}
Notifications Manage notification preferences
{/* 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 Application preferences

User preferences will be available soon

)}
setShowNotificationModal(false)} success={notificationSuccess} message={notificationMessage} />
); }