|
| 1 | +import { type ComponentType, useEffect, useState } from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import type { Notification, NotificationSeverity } from 'stream-chat'; |
| 4 | + |
| 5 | +import { |
| 6 | + IconCheckmark, |
| 7 | + IconExclamationCircleFill, |
| 8 | + IconExclamationTriangleFill, |
| 9 | + IconInfoCircle, |
| 10 | + IconLoading, |
| 11 | + useSystemNotifications, |
| 12 | +} from 'stream-chat-react'; |
| 13 | + |
| 14 | +const IconsBySeverity: Record<NotificationSeverity, ComponentType | null> = { |
| 15 | + error: IconExclamationCircleFill, |
| 16 | + info: IconInfoCircle, |
| 17 | + loading: IconLoading, |
| 18 | + success: IconCheckmark, |
| 19 | + warning: IconExclamationTriangleFill, |
| 20 | +}; |
| 21 | + |
| 22 | +type SystemNotificationFilter = (notification: Notification) => boolean; |
| 23 | + |
| 24 | +export type SystemNotificationProps = { |
| 25 | + /** Optional class name for the container */ |
| 26 | + className?: string; |
| 27 | + /** Optional additional filter applied after the default system-tag filter. */ |
| 28 | + filter?: SystemNotificationFilter; |
| 29 | +}; |
| 30 | + |
| 31 | +const SLIDE_OUT_ANIMATION_NAME = 'str-chat__system-notification-slide-out'; |
| 32 | + |
| 33 | +export const SystemNotification = ({ className, filter }: SystemNotificationProps) => { |
| 34 | + const notifications = useSystemNotifications(filter ? { filter } : undefined); |
| 35 | + const notification = notifications[0]; |
| 36 | + |
| 37 | + const [retainedNotification, setRetainedNotification] = useState< |
| 38 | + Notification | undefined |
| 39 | + >(notification); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (notification) { |
| 43 | + setRetainedNotification(notification); |
| 44 | + } |
| 45 | + }, [notification]); |
| 46 | + |
| 47 | + const isExiting = !notification && !!retainedNotification; |
| 48 | + const rendered = notification ?? retainedNotification; |
| 49 | + |
| 50 | + if (!rendered) return null; |
| 51 | + |
| 52 | + const Icon = rendered.severity |
| 53 | + ? (IconsBySeverity[rendered.severity] ?? null) |
| 54 | + : IconExclamationCircleFill; |
| 55 | + const action = rendered.actions?.[0]; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className='str-chat__system-notification-anchor'> |
| 59 | + <div |
| 60 | + aria-live='polite' |
| 61 | + className={clsx( |
| 62 | + 'str-chat__system-notification', |
| 63 | + { |
| 64 | + 'str-chat__system-notification--exiting': isExiting, |
| 65 | + 'str-chat__system-notification--interactive': action, |
| 66 | + [`str-chat__system-notification--${rendered.severity}`]: rendered.severity, |
| 67 | + }, |
| 68 | + className, |
| 69 | + )} |
| 70 | + data-testid='system-notification' |
| 71 | + onAnimationEnd={(e) => { |
| 72 | + if (e.animationName === SLIDE_OUT_ANIMATION_NAME) { |
| 73 | + setRetainedNotification(undefined); |
| 74 | + } |
| 75 | + }} |
| 76 | + onClick={action?.handler} |
| 77 | + onKeyDown={ |
| 78 | + action |
| 79 | + ? (event) => { |
| 80 | + if (event.key === 'Enter' || event.key === ' ') { |
| 81 | + event.preventDefault(); |
| 82 | + action.handler(); |
| 83 | + } |
| 84 | + } |
| 85 | + : undefined |
| 86 | + } |
| 87 | + role={action ? 'button' : 'status'} |
| 88 | + tabIndex={action ? 0 : undefined} |
| 89 | + > |
| 90 | + {Icon && ( |
| 91 | + <span aria-hidden className='str-chat__system-notification-icon'> |
| 92 | + <Icon /> |
| 93 | + </span> |
| 94 | + )} |
| 95 | + <span className='str-chat__system-notification-message'>{rendered.message}</span> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
0 commit comments