Add SSE push notifications — dream delivery triggers browser notification

This commit is contained in:
2026-04-26 22:17:33 -04:00
parent 5a68362b76
commit 467269d09e
+31
View File
@@ -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);