import { useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { Bell, BellOff } from 'lucide-react' import { disableCollaboratorChangePush, enableCollaboratorChangePush, fetchPushPrefs, getNotificationPermission, isPushSupported } from '../services/pushNotifications.js' import { isIosDevice, isRunningStandalone } from '../hooks/usePwaInstall.js' import { PlausibleEvents, trackPlausibleEvent } from '../services/analytics.js' import { useDialog } from './ModalDialog.tsx' export default function PushNotificationSettings() { const { t } = useTranslation() const { showAlert } = useDialog() const [enabled, setEnabled] = useState(false) const [loading, setLoading] = useState(true) const [toggling, setToggling] = useState(false) const supported = isPushSupported() const permission = getNotificationPermission() const iosNeedsInstall = isIosDevice() && !isRunningStandalone() const loadPrefs = useCallback(async () => { if (!supported) { setLoading(false) return } try { const prefs = await fetchPushPrefs() setEnabled(prefs.collaboratorChangesEnabled) } catch (err) { console.error('Failed to load push prefs:', err) } finally { setLoading(false) } }, [supported]) useEffect(() => { void loadPrefs() }, [loadPrefs]) const handleToggle = async (e: React.ChangeEvent) => { const next = e.target.checked setToggling(true) try { if (next) { await enableCollaboratorChangePush() setEnabled(true) trackPlausibleEvent(PlausibleEvents.PUSH_ENABLED) } else { await disableCollaboratorChangePush() setEnabled(false) trackPlausibleEvent(PlausibleEvents.PUSH_DISABLED) } } catch (err: unknown) { const message = err instanceof Error ? err.message : t('profile.push_error') showAlert(message) void loadPrefs() } finally { setToggling(false) } } if (!supported) { return (

{t('profile.push_title')}

{t('profile.push_unsupported')}

) } return (

{t('profile.push_title')}

{t('profile.push_desc')}

{iosNeedsInstall && (

{t('profile.push_ios_install_hint')}

)} {permission === 'denied' && (

{t('profile.push_denied_hint')}

)} {enabled && permission === 'granted' && (

{t('profile.push_active')}

)}
) }