notification implemented

This commit is contained in:
root
2026-05-25 13:12:06 -04:00
parent 33b6bb55f0
commit c8bde121fc
31 changed files with 1491 additions and 1291 deletions
@@ -3,6 +3,7 @@
import { Bell } from 'lucide-react'
import { usePathname, useRouter } from 'next/navigation'
import { useState, useEffect } from 'react'
import { io } from 'socket.io-client'
import { EMPLOYEE_PROFILE_KEY, EMPLOYEE_TOKEN_KEY, apiFetch } from '@/lib/api'
import { useDashboardI18n } from '@/components/I18nProvider'
import { toDashboardAppPath } from '@/lib/dashboardPaths'
@@ -34,6 +35,11 @@ export default function TopBar() {
const [mounted, setMounted] = useState(false)
useEffect(() => { setMounted(true) }, [])
function getEmployeeToken() {
if (typeof window === 'undefined') return null
return window.localStorage.getItem(EMPLOYEE_TOKEN_KEY)
}
const title = (() => {
if (!mounted) return dict.titles['/']
if (dict.titles[appPath]) return dict.titles[appPath]
@@ -42,6 +48,11 @@ export default function TopBar() {
return dict.titles['/']
})()
async function refreshUnreadCount() {
if (!getEmployeeToken()) {
setUnreadCount(0)
return
}
try {
const data = await apiFetch<{ unread: number }>('/notifications/unread-count')
setUnreadCount(data.unread)
@@ -60,8 +71,29 @@ export default function TopBar() {
return () => window.removeEventListener('notifications:updated', onNotificationsUpdated)
}, [])
useEffect(() => {
const token = window.localStorage.getItem(EMPLOYEE_TOKEN_KEY)
if (!token) return
const socketUrl = (process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:4000/api/v1').replace('/api/v1', '')
const socket = io(socketUrl, { auth: { token }, transports: ['websocket'] })
socket.on('notification', () => {
setUnreadCount((prev) => prev + 1)
window.dispatchEvent(new Event('notifications:updated'))
})
return () => { socket.disconnect() }
}, [])
useEffect(() => {
if (!showNotifs) return
if (!getEmployeeToken()) {
setNotifications([])
setLoadingNotifs(false)
return
}
let cancelled = false
setLoadingNotifs(true)
apiFetch<Array<{