From c719884cbdc248b73dc07c994b88d86da770fc4b Mon Sep 17 00:00:00 2001 From: gbuomprisco Date: Wed, 17 Sep 2025 13:08:34 +0800 Subject: [PATCH] Refactor `useNotificationsStream` hook to improve parameter handling and conditional subscription - Updated the `useNotificationsStream` hook to destructure parameters directly in the function signature for clarity. - Added a conditional check to prevent subscription when the `enabled` flag is false, enhancing performance and preventing unnecessary operations. - Adjusted dependencies in the `useEffect` hook to ensure proper reactivity based on the new parameter structure. --- .../src/hooks/use-notifications-stream.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/features/notifications/src/hooks/use-notifications-stream.ts b/packages/features/notifications/src/hooks/use-notifications-stream.ts index 21519ebc1..05d50c63d 100644 --- a/packages/features/notifications/src/hooks/use-notifications-stream.ts +++ b/packages/features/notifications/src/hooks/use-notifications-stream.ts @@ -11,7 +11,11 @@ type Notification = { link: string | null; }; -export function useNotificationsStream(params: { +export function useNotificationsStream({ + onNotifications, + accountIds, + enabled, +}: { onNotifications: (notifications: Notification[]) => void; accountIds: string[]; enabled: boolean; @@ -19,6 +23,10 @@ export function useNotificationsStream(params: { const client = useSupabase(); useEffect(() => { + if (!enabled) { + return; + } + const channel = client.channel('notifications-channel'); const subscription = channel @@ -27,12 +35,11 @@ export function useNotificationsStream(params: { { event: 'INSERT', schema: 'public', - filter: `account_id=in.(${params.accountIds.join(', ')})`, + filter: `account_id=in.(${accountIds.join(', ')})`, table: 'notifications', }, (payload) => { - console.log('payload', payload); - params.onNotifications([payload.new as Notification]); + onNotifications([payload.new as Notification]); }, ) .subscribe(); @@ -40,5 +47,5 @@ export function useNotificationsStream(params: { return () => { void subscription?.unsubscribe(); }; - }, [client, params]); + }, [client, onNotifications, accountIds, enabled]); }