Add SSE push notifications — dream delivery triggers browser notification
This commit is contained in:
@@ -22,6 +22,37 @@ export default function Home() {
|
||||
setSidebarOpen,
|
||||
} = 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(() => {
|
||||
api.getSettings().then(s => {
|
||||
setSettings(s);
|
||||
|
||||
Reference in New Issue
Block a user