|
|
|
@ -2,12 +2,13 @@ import { BIG_RELAY_URLS, ExtendedKind } from '@/constants' |
|
|
|
import client from '@/services/client.service' |
|
|
|
import client from '@/services/client.service' |
|
|
|
import { kinds } from 'nostr-tools' |
|
|
|
import { kinds } from 'nostr-tools' |
|
|
|
import { SubCloser } from 'nostr-tools/abstract-pool' |
|
|
|
import { SubCloser } from 'nostr-tools/abstract-pool' |
|
|
|
import { createContext, useContext, useEffect, useState } from 'react' |
|
|
|
import { createContext, useContext, useEffect, useRef, useState } from 'react' |
|
|
|
import { useMuteList } from './MuteListProvider' |
|
|
|
import { useMuteList } from './MuteListProvider' |
|
|
|
import { useNostr } from './NostrProvider' |
|
|
|
import { useNostr } from './NostrProvider' |
|
|
|
|
|
|
|
|
|
|
|
type TNotificationContext = { |
|
|
|
type TNotificationContext = { |
|
|
|
hasNewNotification: boolean |
|
|
|
hasNewNotification: boolean |
|
|
|
|
|
|
|
getNotificationsSeenAt: () => number |
|
|
|
clearNewNotifications: () => Promise<void> |
|
|
|
clearNewNotifications: () => Promise<void> |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -24,16 +25,16 @@ export const useNotification = () => { |
|
|
|
export function NotificationProvider({ children }: { children: React.ReactNode }) { |
|
|
|
export function NotificationProvider({ children }: { children: React.ReactNode }) { |
|
|
|
const { pubkey, notificationsSeenAt, updateNotificationsSeenAt } = useNostr() |
|
|
|
const { pubkey, notificationsSeenAt, updateNotificationsSeenAt } = useNostr() |
|
|
|
const { mutePubkeys } = useMuteList() |
|
|
|
const { mutePubkeys } = useMuteList() |
|
|
|
const [hasNewNotification, setHasNewNotification] = useState(false) |
|
|
|
const [newNotificationIds, setNewNotificationIds] = useState(new Set<string>()) |
|
|
|
|
|
|
|
const subCloserRef = useRef<SubCloser | null>(null) |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
if (!pubkey || notificationsSeenAt < 0) return |
|
|
|
if (!pubkey || notificationsSeenAt < 0) return |
|
|
|
|
|
|
|
|
|
|
|
setHasNewNotification(false) |
|
|
|
setNewNotificationIds(new Set()) |
|
|
|
|
|
|
|
|
|
|
|
// Track if component is mounted
|
|
|
|
// Track if component is mounted
|
|
|
|
const isMountedRef = { current: true } |
|
|
|
const isMountedRef = { current: true } |
|
|
|
let currentSubCloser: SubCloser | null = null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const subscribe = async () => { |
|
|
|
const subscribe = async () => { |
|
|
|
if (!isMountedRef.current) return null |
|
|
|
if (!isMountedRef.current) return null |
|
|
|
@ -54,15 +55,14 @@ export function NotificationProvider({ children }: { children: React.ReactNode } |
|
|
|
], |
|
|
|
], |
|
|
|
'#p': [pubkey], |
|
|
|
'#p': [pubkey], |
|
|
|
since: notificationsSeenAt, |
|
|
|
since: notificationsSeenAt, |
|
|
|
limit: 10 |
|
|
|
limit: 20 |
|
|
|
} |
|
|
|
} |
|
|
|
], |
|
|
|
], |
|
|
|
{ |
|
|
|
{ |
|
|
|
onevent: (evt) => { |
|
|
|
onevent: (evt) => { |
|
|
|
// Only show notification if not from self and not muted
|
|
|
|
// Only show notification if not from self and not muted
|
|
|
|
if (evt.pubkey !== pubkey && !mutePubkeys.includes(evt.pubkey)) { |
|
|
|
if (evt.pubkey !== pubkey && !mutePubkeys.includes(evt.pubkey)) { |
|
|
|
setHasNewNotification(true) |
|
|
|
setNewNotificationIds((prev) => new Set([...prev, evt.id])) |
|
|
|
subCloser.close() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
onclose: (reasons) => { |
|
|
|
onclose: (reasons) => { |
|
|
|
@ -71,7 +71,7 @@ export function NotificationProvider({ children }: { children: React.ReactNode } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Only reconnect if still mounted and not a manual close
|
|
|
|
// Only reconnect if still mounted and not a manual close
|
|
|
|
if (isMountedRef.current && currentSubCloser) { |
|
|
|
if (isMountedRef.current && subCloserRef.current) { |
|
|
|
setTimeout(() => { |
|
|
|
setTimeout(() => { |
|
|
|
if (isMountedRef.current) { |
|
|
|
if (isMountedRef.current) { |
|
|
|
subscribe() |
|
|
|
subscribe() |
|
|
|
@ -82,7 +82,7 @@ export function NotificationProvider({ children }: { children: React.ReactNode } |
|
|
|
} |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
currentSubCloser = subCloser |
|
|
|
subCloserRef.current = subCloser |
|
|
|
return subCloser |
|
|
|
return subCloser |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
console.error('Subscription error:', error) |
|
|
|
console.error('Subscription error:', error) |
|
|
|
@ -105,30 +105,48 @@ export function NotificationProvider({ children }: { children: React.ReactNode } |
|
|
|
// Cleanup function
|
|
|
|
// Cleanup function
|
|
|
|
return () => { |
|
|
|
return () => { |
|
|
|
isMountedRef.current = false |
|
|
|
isMountedRef.current = false |
|
|
|
if (currentSubCloser) { |
|
|
|
if (subCloserRef.current) { |
|
|
|
currentSubCloser.close() |
|
|
|
subCloserRef.current.close() |
|
|
|
currentSubCloser = null |
|
|
|
subCloserRef.current = null |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, [notificationsSeenAt, pubkey]) |
|
|
|
}, [notificationsSeenAt, pubkey]) |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
if (hasNewNotification) { |
|
|
|
if (newNotificationIds.size >= 10 && subCloserRef.current) { |
|
|
|
document.title = '📥 Jumble' |
|
|
|
subCloserRef.current.close() |
|
|
|
|
|
|
|
subCloserRef.current = null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, [newNotificationIds]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
|
|
const newNotificationCount = newNotificationIds.size |
|
|
|
|
|
|
|
if (newNotificationCount > 0) { |
|
|
|
|
|
|
|
document.title = `(${newNotificationCount >= 10 ? '9+' : newNotificationCount}) Jumble` |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
document.title = 'Jumble' |
|
|
|
document.title = 'Jumble' |
|
|
|
} |
|
|
|
} |
|
|
|
}, [hasNewNotification]) |
|
|
|
}, [newNotificationIds]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getNotificationsSeenAt = () => { |
|
|
|
|
|
|
|
return notificationsSeenAt |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const clearNewNotifications = async () => { |
|
|
|
const clearNewNotifications = async () => { |
|
|
|
if (!pubkey) return |
|
|
|
if (!pubkey) return |
|
|
|
|
|
|
|
|
|
|
|
setHasNewNotification(false) |
|
|
|
setNewNotificationIds(new Set()) |
|
|
|
await updateNotificationsSeenAt() |
|
|
|
await updateNotificationsSeenAt() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<NotificationContext.Provider value={{ hasNewNotification, clearNewNotifications }}> |
|
|
|
<NotificationContext.Provider |
|
|
|
|
|
|
|
value={{ |
|
|
|
|
|
|
|
hasNewNotification: newNotificationIds.size > 0, |
|
|
|
|
|
|
|
clearNewNotifications, |
|
|
|
|
|
|
|
getNotificationsSeenAt |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
> |
|
|
|
{children} |
|
|
|
{children} |
|
|
|
</NotificationContext.Provider> |
|
|
|
</NotificationContext.Provider> |
|
|
|
) |
|
|
|
) |
|
|
|
|