Skip to main content

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

ParameterTypeRequiredDescription
userIdstringYesThe current user's ID

Return Value

useShareNotifications

PropertyTypeDescription
unreadCountnumberNumber of unread notifications
notificationsNotification[]Array of notification objects
loadingbooleanWhether notifications are being fetched
processingSharesbooleanWhether 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 INSERT events on the notifications table filtered by user_id.
  • The lightweight useProcessShareNotifications hook is suitable for use in App.jsx where you want background processing without full notification state.
  • Share processing fails silently as it is a background operation.

Last updated: 2026-02-07