-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: subscribe to notifications websocket
and update NotificationIsland
- Loading branch information
Showing
4 changed files
with
92 additions
and
83 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import useSWRSubscription from "swr/subscription"; | ||
|
||
import { BACKEND_URL } from "@/constants"; | ||
import { getAccessToken } from "@/cookies"; | ||
import Notification from "@/models/notification"; | ||
|
||
export default function useNotificationsWS(organizationId: string) { | ||
const { data, ...swr } = useSWRSubscription<Notification, Error, string>( | ||
`ws://${BACKEND_URL}/ws/notifications/${organizationId}?token=${getAccessToken()}`, | ||
(key, { next }) => { | ||
const socket = new WebSocket(key) | ||
|
||
socket.addEventListener('message', (event) => next(null, JSON.parse(event.data))) | ||
// @ts-ignore | ||
socket.addEventListener('error', (event) => next(event.error)) | ||
|
||
return () => socket.close() | ||
}) | ||
|
||
return { | ||
notification: data, | ||
...swr, | ||
}; | ||
} |
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