-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathConnectionWatcher.tsx
30 lines (29 loc) · 1011 Bytes
/
ConnectionWatcher.tsx
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
import { useSubscribe } from "@react-admin/ra-realtime";
import { useNotify, useDataProvider } from "react-admin";
export const ConnectionWatcher = () => {
const notify = useNotify();
const dataProvider = useDataProvider();
useSubscribe("connectedUsers", (event) => {
if (event.type === "connected") {
dataProvider
.getOne("agents", { id: event.payload.agentId })
.then(({ data }) => {
notify(`Agent ${data.firstName} ${data.lastName} just logged in`, {
type: "success",
anchorOrigin: { vertical: "bottom", horizontal: "right" },
});
});
}
if (event.type === "disconnected") {
dataProvider
.getOne("agents", { id: event.payload.agentId })
.then(({ data }) => {
notify(`Agent ${data.firstName} ${data.lastName} just logged out`, {
type: "success",
anchorOrigin: { vertical: "bottom", horizontal: "right" },
});
});
}
});
return null;
};