Created
November 11, 2023 16:41
-
-
Save pontusab/18f39afd0d23fca1da8163137950c24e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createClient } from "@midday/supabase/client"; | |
import { getUserQuery } from "@midday/supabase/queries"; | |
import { HeadlessService } from "@novu/headless"; | |
import { useCallback, useEffect, useRef, useState } from "react"; | |
export function useNotifications() { | |
const supabase = createClient(); | |
const [notifications, setNotifications] = useState([]); | |
const [subscriberId, setSubscriberId] = useState(); | |
const headlessServiceRef = useRef<HeadlessService>(); | |
const markAllMessagesAsRead = () => { | |
const headlessService = headlessServiceRef.current; | |
if (headlessService) { | |
setNotifications((prevNotifications) => | |
prevNotifications.map((notification) => { | |
return { | |
...notification, | |
read: true, | |
}; | |
}), | |
); | |
headlessService.markAllMessagesAsRead({ | |
listener: () => {}, | |
onError: () => {}, | |
}); | |
} | |
}; | |
const markMessageAsRead = (messageId: string) => { | |
const headlessService = headlessServiceRef.current; | |
if (headlessService) { | |
setNotifications((prevNotifications) => | |
prevNotifications.map((notification) => { | |
if (notification.id === messageId) { | |
return { | |
...notification, | |
read: true, | |
}; | |
} | |
return notification; | |
}), | |
); | |
headlessService.markNotificationsAsRead({ | |
messageId: [messageId], | |
listener: (result) => {}, | |
onError: (error) => {}, | |
}); | |
} | |
}; | |
const fetchNotifications = useCallback(() => { | |
const headlessService = headlessServiceRef.current; | |
if (headlessService) { | |
headlessService.fetchNotifications({ | |
listener: ({}) => {}, | |
onSuccess: (response) => { | |
setNotifications(response.data); | |
}, | |
}); | |
} | |
}, []); | |
const markAllMessagesAsSeen = () => { | |
const headlessService = headlessServiceRef.current; | |
if (headlessService) { | |
setNotifications((prevNotifications) => | |
prevNotifications.map((notification) => ({ | |
...notification, | |
seen: true, | |
})), | |
); | |
headlessService.markAllMessagesAsSeen({ | |
listener: () => {}, | |
onError: () => {}, | |
}); | |
} | |
}; | |
useEffect(() => { | |
async function fetchUser() { | |
const { | |
data: { session }, | |
} = await supabase.auth.getSession(); | |
const { data: userData } = await getUserQuery( | |
supabase, | |
session?.user?.id, | |
); | |
if (userData) { | |
setSubscriberId(`${userData.team_id}_${userData.id}`); | |
} | |
} | |
fetchUser(); | |
}, [supabase]); | |
useEffect(() => { | |
const headlessService = headlessServiceRef.current; | |
if (headlessService) { | |
headlessService.listenNotificationReceive({ | |
listener: () => { | |
fetchNotifications(); | |
}, | |
}); | |
} | |
}, [headlessServiceRef.current]); | |
useEffect(() => { | |
if (subscriberId && !headlessServiceRef.current) { | |
const headlessService = new HeadlessService({ | |
applicationIdentifier: process.env.NEXT_PUBLIC_APPLICATION_IDENTIFIER!, | |
subscriberId, | |
}); | |
headlessService.initializeSession({ | |
listener: () => {}, | |
onSuccess: () => { | |
headlessServiceRef.current = headlessService; | |
fetchNotifications(); | |
}, | |
onError: () => {}, | |
}); | |
} | |
}, [fetchNotifications, subscriberId]); | |
return { | |
markAllMessagesAsRead, | |
markMessageAsRead, | |
markAllMessagesAsSeen, | |
hasUnseenNotificaitons: notifications.some( | |
(notification) => !notification.seen, | |
), | |
notifications, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment