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:
@@ -1,11 +1,22 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { useSession } from './useAuth'
|
||||
|
||||
export function usePresence() {
|
||||
export interface NewBugPayload {
|
||||
id: string
|
||||
description: string
|
||||
appId: string | null
|
||||
source: string
|
||||
affectedVersion: string
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export function usePresence(onNewBug?: (bug: NewBugPayload) => void) {
|
||||
const { data } = useSession()
|
||||
const [onlineUserIds, setOnlineUserIds] = useState<string[]>([])
|
||||
const wsRef = useRef<WebSocket | null>(null)
|
||||
const reconnectTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
|
||||
const onNewBugRef = useRef(onNewBug)
|
||||
onNewBugRef.current = onNewBug
|
||||
|
||||
useEffect(() => {
|
||||
if (!data?.user) return
|
||||
@@ -18,6 +29,7 @@ export function usePresence() {
|
||||
ws.onmessage = (e) => {
|
||||
const msg = JSON.parse(e.data)
|
||||
if (msg.type === 'presence') setOnlineUserIds(msg.online)
|
||||
if (msg.type === 'new_bug') onNewBugRef.current?.(msg.bug)
|
||||
}
|
||||
ws.onclose = () => {
|
||||
wsRef.current = null
|
||||
|
||||
Reference in New Issue
Block a user