feat: notifikasi real-time bug baru via WebSocket

- presence.ts: tambah notifSubs (ADMIN+DEVELOPER) dan broadcastNotification
- app.ts: broadcast new_bug event setelah bug dibuat, update WS handler
- usePresence: terima callback onNewBug, expose NewBugPayload type
- DashboardLayout: pasang usePresence, tampilkan Mantine notification saat bug baru masuk
This commit is contained in:
2026-05-25 11:35:21 +08:00
parent 8c33003b17
commit e32addbc85
4 changed files with 61 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import type { ServerWebSocket } from 'bun'
const connections = new Map<string, Set<ServerWebSocket<{ userId: string }>>>()
const adminSubs = new Set<ServerWebSocket<{ userId: string }>>()
const notifSubs = new Set<ServerWebSocket<{ userId: string }>>()
export function getOnlineUserIds(): string[] {
return Array.from(connections.keys())
@@ -13,7 +14,12 @@ function broadcast() {
for (const ws of adminSubs) ws.send(msg)
}
export function addConnection(ws: ServerWebSocket<{ userId: string }>, userId: string, isAdmin: boolean) {
export function addConnection(
ws: ServerWebSocket<{ userId: string }>,
userId: string,
isAdmin: boolean,
canReceiveNotifs: boolean,
) {
let set = connections.get(userId)
if (!set) {
set = new Set()
@@ -24,6 +30,7 @@ export function addConnection(ws: ServerWebSocket<{ userId: string }>, userId: s
adminSubs.add(ws)
ws.send(JSON.stringify({ type: 'presence', online: getOnlineUserIds() }))
}
if (canReceiveNotifs) notifSubs.add(ws)
broadcast()
}
@@ -32,6 +39,11 @@ export function broadcastToAdmins(message: object) {
for (const ws of adminSubs) ws.send(msg)
}
export function broadcastNotification(message: object) {
const msg = JSON.stringify(message)
for (const ws of notifSubs) ws.send(msg)
}
export function removeConnection(ws: ServerWebSocket<{ userId: string }>) {
const userId = ws.data.userId
const set = connections.get(userId)
@@ -40,5 +52,6 @@ export function removeConnection(ws: ServerWebSocket<{ userId: string }>) {
if (set.size === 0) connections.delete(userId)
}
adminSubs.delete(ws)
notifSubs.delete(ws)
broadcast()
}