import clsx from "clsx"; import { Fragment, useState, useMemo, useCallback, useRef } from "react"; import { Card, CardContent, Typography, Box, Avatar } from "@mui/material"; import { useTheme } from "@mui/material/styles"; import { Close } from "@mui/icons-material"; import RdsButton from "../../raaghu-elements/rds-button/rds-button"; import RdsIconButton from "../../raaghu-elements/rds-icon-button/rds-icon-button"; import { getRaaghuLogoUrlFromDark } from "../../raaghu-elements/shared/constants/raaghu-logo"; import "./rds-comp-notification.scss"; import { NotificationLayout, NotificationStyle, NotificationType, type RdsCompNotificationProps, } from "./rds-comp-notification.types"; const CustomBellIcon = () => ( ); const RdsCompNotification = ({ notifications, title, defaultImage, layout = NotificationLayout.Horizontal, style = NotificationStyle.Default, type = NotificationType.Info, actions = 'none', dismiss = 'hidden', description, onDismiss, onAccept, ...legacyProps }) => { const legacyShowButton = typeof legacyProps['showButton'] === 'boolean' ? (legacyProps['showButton'] as boolean) : undefined; const legacyShowPrimaryButton = typeof legacyProps['showPrimaryButton'] === 'boolean' ? (legacyProps['showPrimaryButton'] as boolean) : undefined; const legacyShowSecondaryButton = typeof legacyProps['showSecondaryButton'] === 'boolean' ? (legacyProps['showSecondaryButton'] as boolean) : undefined; const legacyShowDismiss = typeof legacyProps['showDismiss'] === 'boolean' ? (legacyProps['showDismiss'] as boolean) : undefined; const theme = useTheme(); const resolvedDefaultImage = defaultImage ?? getRaaghuLogoUrlFromDark(theme.palette.mode === 'dark'); const resolvedDismissVisible = dismiss === 'visible' || legacyShowDismiss === true; const resolvedShowPrimaryButton = actions === 'primary' || actions === 'both' || legacyShowPrimaryButton === true; const resolvedShowSecondaryButton = actions === 'secondary' || actions === 'both' || legacyShowSecondaryButton === true; const resolvedShowButton = actions !== 'none' || legacyShowButton === true || resolvedShowPrimaryButton || resolvedShowSecondaryButton; const [dismissedNotificationKeys, setDismissedNotificationKeys] = useState>([]); // Track the notifications reference from the *previous* render so we can // detect when the caller passes a new array (even with the same contents). // We intentionally do NOT update this ref during the current render body so // that the useMemo below can still see the old value while computing. const prevNotificationsRef = useRef(null); const dismissIconElement = useMemo(() => , []); const notificationsChanged = prevNotificationsRef.current !== null && prevNotificationsRef.current !== notifications; const visibleNotifications = useMemo(() => { // If the notifications prop reference changed, the caller has supplied a // fresh list – treat it as an authoritative reset and show everything. if (notificationsChanged) { return notifications; } return notifications.filter((notification, index) => { const notificationKey = notification.userNotificationId ?? `${notification.title}-${index}`; return !dismissedNotificationKeys.includes(notificationKey); }); }, [dismissedNotificationKeys, notifications, notificationsChanged]); // Update the ref *after* computing visibleNotifications so the next render // can correctly detect a further reference change. prevNotificationsRef.current = notifications; const removeNotificationByIndex = useCallback((notificationIndex: number) => { const notification = visibleNotifications[notificationIndex]; if (!notification) return; const notificationKey = notification.userNotificationId ?? `${notification.title}-${notificationIndex}`; setDismissedNotificationKeys((prev) => prev.includes(notificationKey) ? prev : [...prev, notificationKey]); }, [visibleNotifications]); const handleDismiss = useCallback((event: any, notification: any, notificationIndex: number) => { removeNotificationByIndex(notificationIndex); onDismiss?.(event, notification); }, [onDismiss, removeNotificationByIndex]); const handleSecondaryButtonClick = useCallback((event: any, notification: any, notificationIndex: number) => { removeNotificationByIndex(notificationIndex); onDismiss?.(event, notification); }, [onDismiss, removeNotificationByIndex]); return ( {visibleNotifications.map((notification, index) => ( {layout === NotificationLayout.Horizontal && style === NotificationStyle.Image && ( )} {resolvedDismissVisible && ( handleDismiss(e, notification, index)} className="rds-comp-notification__dismiss" />)} {style === NotificationStyle.Image && (layout === NotificationLayout.Vertical || layout === NotificationLayout.Horizontal) && ( {title ?? notification.title} {notification.time && ( {notification.time} )} )} {style === NotificationStyle.Image && layout === NotificationLayout.Vertical && ( )} {style === NotificationStyle.Avatar && ( )} {style === NotificationStyle.Icon && ( {typeof notification.icon === 'string' ? ( ) : ( notification.icon || )} )} {((layout !== NotificationLayout.Vertical && style !== NotificationStyle.Image) || (layout === NotificationLayout.Horizontal && style !== NotificationStyle.Image) || (layout === NotificationLayout.Vertical && style !== NotificationStyle.Image)) && ( {title ?? notification.title} {notification.time && ( {notification.time} )} )} {description ?? notification.description} {resolvedShowButton && ( <> {resolvedShowSecondaryButton && ( handleSecondaryButtonClick(e, notification, index)} className="rds-comp-notification__secondary-button" /> )} {resolvedShowPrimaryButton && ( onAccept?.(e, notification)} className="rds-comp-notification__primary-button" /> )} )} ))} ); }; RdsCompNotification.displayName = "RdsCompNotification"; export default RdsCompNotification;