Add SSE push notifications — dream delivery triggers browser notification
This commit is contained in:
@@ -22,6 +22,37 @@ export default function Home() {
|
|||||||
setSidebarOpen,
|
setSidebarOpen,
|
||||||
} = useStore();
|
} = useStore();
|
||||||
|
|
||||||
|
// SSE — listen for dream notifications
|
||||||
|
const sseRef = useRef<EventSource | null>(null);
|
||||||
|
useEffect(() => {
|
||||||
|
function connect() {
|
||||||
|
const es = new EventSource('/api/events');
|
||||||
|
sseRef.current = es;
|
||||||
|
es.onmessage = (e) => {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(e.data);
|
||||||
|
if (data.type === 'dream') {
|
||||||
|
if ('Notification' in window && Notification.permission === 'granted') {
|
||||||
|
new Notification('Bird dreamed', {
|
||||||
|
body: `${data.mode?.toUpperCase()} dream delivered — ${data.filename}`,
|
||||||
|
icon: '/icon-192.png',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
};
|
||||||
|
es.onerror = () => {
|
||||||
|
es.close();
|
||||||
|
setTimeout(connect, 10000);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if ('Notification' in window) {
|
||||||
|
Notification.requestPermission();
|
||||||
|
}
|
||||||
|
connect();
|
||||||
|
return () => sseRef.current?.close();
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
api.getSettings().then(s => {
|
api.getSettings().then(s => {
|
||||||
setSettings(s);
|
setSettings(s);
|
||||||
|
|||||||
Reference in New Issue
Block a user