Files
myeasycms-v2/packages/features/notifications/src/hooks/use-fetch-notifications.ts
giancarlo 9baee90eec Update configuration settings and improve notifications
This commit updates the configuration settings and improves the notification functionality by filtering out duplicate notifications. It also includes changes to feature flag data types and removed dependencies in `package.json`, alongside updates to `pnpm-lock.yaml`.
2024-05-01 12:13:19 +07:00

74 lines
1.6 KiB
TypeScript

import { useEffect } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useSupabase } from '@kit/supabase/hooks/use-supabase';
import { useNotificationsStream } from './use-notifications-stream';
type Notification = {
id: number;
body: string;
dismissed: boolean;
type: 'info' | 'warning' | 'error';
created_at: string;
link: string | null;
};
export function useFetchNotifications({
onNotifications,
accountIds,
realtime,
}: {
onNotifications: (notifications: Notification[]) => unknown;
accountIds: string[];
realtime: boolean;
}) {
const { data: initialNotifications } = useFetchInitialNotifications({
accountIds,
});
useNotificationsStream({
onNotifications,
accountIds,
enabled: realtime,
});
useEffect(() => {
if (initialNotifications) {
onNotifications(initialNotifications);
}
}, [initialNotifications, onNotifications]);
}
function useFetchInitialNotifications(props: { accountIds: string[] }) {
const client = useSupabase();
const now = new Date().toISOString();
return useQuery({
queryKey: ['notifications', ...props.accountIds],
queryFn: async () => {
const { data } = await client
.from('notifications')
.select(
`id,
body,
dismissed,
type,
created_at,
link
`,
)
.in('account_id', props.accountIds)
.eq('dismissed', false)
.gt('expires_at', now)
.order('created_at', { ascending: false })
.limit(10);
return data;
},
refetchOnMount: false,
refetchOnWindowFocus: false,
});
}