Skip to content

Commit

Permalink
initial system events
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoOliveira committed Mar 8, 2024
1 parent a521f41 commit 1603d70
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DashboardProvider } from './dashboard/provider/DashboardProvider.tsx';
import { PrinterWidgetProvider } from './printers/providers/PrinterWidgetProvider.tsx';
import { SSEProvider } from './core/sse/SSEProvider.tsx';
import { SettingsProvider } from './core/settings/settingsProvider.tsx';
import { EventNotifications } from './system/components/event-notifications/EventNotifications.tsx';

export default function App() {
const [opened, { toggle }] = useDisclosure();
Expand Down Expand Up @@ -40,6 +41,7 @@ export default function App() {
</AppShell>
<ScrollToTop />
<AxiosErrorHandler />
<EventNotifications />
<PrinterWidgetProvider />
</DashboardProvider>
</SSEProvider>
Expand Down
36 changes: 36 additions & 0 deletions src/system/components/event-notifications/EventNotifications.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import SSEContext from "@/core/sse/SSEContext";
import { useId } from "@mantine/hooks";
import { notifications } from "@mantine/notifications";
import { useContext, useEffect, useState } from "react";

export function EventNotifications() {
const subscriberId = useId();
const { connected, subscribe, unsubscribe } = useContext(SSEContext)
const [message, setMessage] = useState("")
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
if (!connected) return;
setMessage("")
const subscription = {
subscriberId,
provider: `system/events`,
}
subscribe({
...subscription,
event: `system.state`,
callback: setMessage
}).catch(setError);
return () => {
unsubscribe(subscriberId)
}
}, [connected])

useEffect(() => {
console.log(message)
notifications.show({
title: message,
message: message,
})
}, [message])
return (<></>)
}

0 comments on commit 1603d70

Please sign in to comment.