-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuseSse.ts
35 lines (30 loc) · 998 Bytes
/
useSse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import { useQueryClient } from '@tanstack/react-query'
import {
v1GetWorkspaceAlertsQueryKey,
v1GetWorkspaceMessagesQueryKey,
} from '@/api/generated/@tanstack/react-query.gen'
import { invalidateQueries } from '@/lib/react-query-utils'
import { getAppConfig } from '@/lib/utils'
const baseApiUrl = getAppConfig().DASHBOARD_API_BASE_URL
export function useSse() {
const location = useLocation()
const queryClient = useQueryClient()
useEffect(() => {
const eventSource = new EventSource(
`${baseApiUrl}/api/v1/alerts_notification`
)
eventSource.onmessage = function (event) {
if (event.data.toLowerCase().includes('new alert detected')) {
invalidateQueries(queryClient, [
v1GetWorkspaceAlertsQueryKey,
v1GetWorkspaceMessagesQueryKey,
])
}
}
return () => {
eventSource.close()
}
}, [location.pathname, queryClient])
}