38 lines
792 B
TypeScript
38 lines
792 B
TypeScript
import { useState } from 'react'
|
|
import type { Notification } from '@/types/notifications'
|
|
|
|
export function useNotificationCenter(
|
|
markAsRead: (id: string) => void,
|
|
onClose?: () => void
|
|
): {
|
|
isOpen: boolean
|
|
handleToggle: () => void
|
|
handleClose: () => void
|
|
handleNotificationClick: (notification: Notification) => void
|
|
} {
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
const handleToggle = (): void => {
|
|
setIsOpen((prev) => !prev)
|
|
}
|
|
|
|
const handleClose = (): void => {
|
|
setIsOpen(false)
|
|
onClose?.()
|
|
}
|
|
|
|
const handleNotificationClick = (notification: Notification): void => {
|
|
if (!notification.read) {
|
|
markAsRead(notification.id)
|
|
}
|
|
handleClose()
|
|
}
|
|
|
|
return {
|
|
isOpen,
|
|
handleToggle,
|
|
handleClose,
|
|
handleNotificationClick,
|
|
}
|
|
}
|