useShareNotifications
Location: src/hooks/useShareNotifications.js
Overview
Manages share-related notifications. On mount, triggers batch processing of pending share events via the process_share_notifications RPC. Provides unread count, notification fetching, mark-as-read actions, and real-time updates via Supabase channel subscriptions.
The file also exports a lightweight useProcessShareNotifications hook that only triggers share processing without managing notification state.
Privacy note: Notifications do not reveal who shared or which circles. Authors only see "Your post was shared to N circles."
Signature
function useShareNotifications(userId: string): {
unreadCount: number,
notifications: Notification[],
loading: boolean,
processingShares: boolean,
fetchNotifications: (limit?: number, offset?: number, unreadOnly?: boolean) => Promise<Notification[]>,
fetchUnreadCount: () => Promise<void>,
markAsRead: (notificationIds: string[]) => Promise<number>,
markAllAsRead: () => Promise<number>
}
function useProcessShareNotifications(userId: string): void
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
userId | string | Yes | The current user's ID |
Return Value
useShareNotifications
| Property | Type | Description |
|---|---|---|
unreadCount | number | Number of unread notifications |
notifications | Notification[] | Array of notification objects |
loading | boolean | Whether notifications are being fetched |
processingShares | boolean | Whether share events are being processed |
fetchNotifications | (limit?, offset?, unreadOnly?) => Promise<Notification[]> | Fetch notifications with pagination. Default limit is 20. |
fetchUnreadCount | () => Promise<void> | Refresh the unread count via get_unread_notification_count RPC |
markAsRead | (notificationIds: string[]) => Promise<number> | Mark specific notifications as read. Returns count of marked. |
markAllAsRead | () => Promise<number> | Mark all notifications as read. Returns count of marked. |
useProcessShareNotifications
Returns nothing. Triggers process_share_notifications RPC on mount and every 5 minutes.
Usage
import { useShareNotifications } from '../hooks/useShareNotifications';
function NotificationBell({ userId }) {
const { unreadCount, notifications, fetchNotifications, markAllAsRead } = useShareNotifications(userId);
return (
<div>
<button onClick={() => fetchNotifications()}>
Notifications {unreadCount > 0 && `(${unreadCount})`}
</button>
{notifications.map(n => (
<NotificationItem key={n.id} notification={n} />
))}
<button onClick={markAllAsRead}>Mark all read</button>
</div>
);
}
Notes
- Real-time subscription listens for
INSERTevents on thenotificationstable filtered byuser_id. - The lightweight
useProcessShareNotificationshook is suitable for use inApp.jsxwhere you want background processing without full notification state. - Share processing fails silently as it is a background operation.
Last updated: 2026-02-07