Increment version to 2.14.1 and refactor notifications stream hook

- Bumped package version in package.json from 2.14.0 to 2.14.1.
- Refactored the `useNotificationsStream` hook to remove unnecessary `useQuery` and streamline the subscription logic for real-time notifications, enhancing performance and clarity.
This commit is contained in:
gbuomprisco
2025-09-17 12:48:42 +08:00
parent 533dfba5b9
commit 530e127000
2 changed files with 20 additions and 27 deletions

View File

@@ -1,7 +1,5 @@
import { useEffect } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useSupabase } from '@kit/supabase/hooks/use-supabase';
type Notification = {
@@ -20,32 +18,27 @@ export function useNotificationsStream(params: {
}) {
const client = useSupabase();
const { data: subscription } = useQuery({
enabled: params.enabled,
queryKey: ['realtime-notifications', ...params.accountIds],
queryFn: () => {
const channel = client.channel('notifications-channel');
return channel
.on(
'postgres_changes',
{
event: 'INSERT',
schema: 'public',
filter: `account_id=in.(${params.accountIds.join(', ')})`,
table: 'notifications',
},
(payload) => {
params.onNotifications([payload.new as Notification]);
},
)
.subscribe();
},
});
useEffect(() => {
const channel = client.channel('notifications-channel');
const subscription = channel
.on(
'postgres_changes',
{
event: 'INSERT',
schema: 'public',
filter: `account_id=in.(${params.accountIds.join(', ')})`,
table: 'notifications',
},
(payload) => {
console.log('payload', payload);
params.onNotifications([payload.new as Notification]);
},
)
.subscribe();
return () => {
void subscription?.unsubscribe();
};
}, [subscription]);
}, [client, params]);
}