push notification permissions fixed
This commit is contained in:
parent
28ec63e443
commit
c94a919304
@ -222,6 +222,7 @@ export default function AccountSettingsPage() {
|
||||
</h2>
|
||||
<p className="text-sm text-slate-400 mt-1 mb-4">
|
||||
Get notified in your browser when you receive new messages and updates.
|
||||
When you click Enable, your browser will show a permission prompt—choose <strong>Allow</strong>.
|
||||
</p>
|
||||
<div className="bg-white/5 rounded-xl p-4 border border-white/10">
|
||||
{pushLoading ? (
|
||||
@ -262,10 +263,11 @@ export default function AccountSettingsPage() {
|
||||
type="button"
|
||||
onClick={async () => {
|
||||
setPushActionLoading(true)
|
||||
const ok = await subscribe()
|
||||
setError("")
|
||||
const result = await subscribe()
|
||||
setPushActionLoading(false)
|
||||
if (!ok) {
|
||||
setError("Could not enable notifications. Check that the site has permission.")
|
||||
if (!result.success) {
|
||||
setError(result.error ?? "Could not enable notifications. Try again or check browser settings.")
|
||||
}
|
||||
}}
|
||||
disabled={pushActionLoading}
|
||||
|
||||
@ -232,6 +232,7 @@ export default function SupplierSettingsPage() {
|
||||
</h2>
|
||||
<p className="text-sm text-slate-400 mb-4">
|
||||
Get notified in your browser when you receive new messages and updates.
|
||||
When you click Enable, your browser will show a permission prompt—choose <strong>Allow</strong>.
|
||||
</p>
|
||||
<div className="bg-white/5 rounded-xl p-4 border border-white/10">
|
||||
{pushLoading ? (
|
||||
@ -272,9 +273,10 @@ export default function SupplierSettingsPage() {
|
||||
type="button"
|
||||
onClick={async () => {
|
||||
setPushActionLoading(true)
|
||||
const ok = await subscribe()
|
||||
setError("")
|
||||
const result = await subscribe()
|
||||
setPushActionLoading(false)
|
||||
if (!ok) setError("Could not enable notifications. Check that the site has permission.")
|
||||
if (!result.success) setError(result.error ?? "Could not enable notifications. Try again or check browser settings.")
|
||||
}}
|
||||
disabled={pushActionLoading}
|
||||
className="px-4 py-2.5 bg-indigo-500 text-white rounded-xl font-bold hover:bg-indigo-600 transition-colors flex items-center gap-2 disabled:opacity-50"
|
||||
|
||||
@ -57,10 +57,17 @@ export function usePushNotifications() {
|
||||
checkSubscription()
|
||||
}, [checkSubscription])
|
||||
|
||||
const subscribe = async () => {
|
||||
const subscribe = async (): Promise<{ success: boolean; error?: string }> => {
|
||||
if (!VAPID_PUBLIC_KEY) {
|
||||
console.error('VAPID public key not found')
|
||||
return false
|
||||
return { success: false, error: 'Server configuration error. Please try again later or contact support.' }
|
||||
}
|
||||
|
||||
if (Notification.permission === 'denied') {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Notifications are blocked. In your browser, click the lock icon in the address bar → Site settings → Notifications → set to Allow, then refresh this page.'
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@ -79,12 +86,28 @@ export function usePushNotifications() {
|
||||
if (response.ok) {
|
||||
setIsSubscribed(true)
|
||||
setPermission(Notification.permission)
|
||||
return true
|
||||
return { success: true }
|
||||
}
|
||||
return false
|
||||
|
||||
const data = await response.json().catch(() => ({}))
|
||||
const msg = data?.message || (response.status === 401 ? 'Please log in again.' : 'Server error. Please try again later.')
|
||||
return { success: false, error: msg }
|
||||
} catch (error) {
|
||||
console.error('Error subscribing to push notifications:', error)
|
||||
return false
|
||||
const isPermissionDenied = error instanceof Error && (
|
||||
error.name === 'NotAllowedError' ||
|
||||
(error as DOMException).name === 'NotAllowedError'
|
||||
)
|
||||
if (isPermissionDenied) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Notification permission was denied. To enable: click the lock icon in the address bar → Site settings → Notifications → Allow, then refresh.'
|
||||
}
|
||||
}
|
||||
return {
|
||||
success: false,
|
||||
error: 'Could not enable notifications. Make sure the site is allowed to show notifications in your browser settings, then try again.'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user