|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Alert, Anchor, Group, Stack, StackProps } from '@mantine/core'; |
| 4 | +import { |
| 5 | + IconAlertTriangle, |
| 6 | + IconCircleCheck, |
| 7 | + IconInfoCircle, |
| 8 | + IconX, |
| 9 | +} from '@tabler/icons-react'; |
| 10 | + |
| 11 | +import { useSystemNotifications } from '@/contexts/system-notifications'; |
| 12 | +import { |
| 13 | + NotificationType, |
| 14 | + SystemNotification, |
| 15 | +} from '@/contexts/system-notifications/types'; |
| 16 | + |
| 17 | +interface SystemNotificationBannerProps |
| 18 | + extends Pick<StackProps, 'm' | 'mb' | 'mt' | 'my' | 'mx' | 'p'> { |
| 19 | + layout?: 'main' | 'guest' | 'auth'; |
| 20 | +} |
| 21 | + |
| 22 | +const notificationConfig: Record< |
| 23 | + NotificationType, |
| 24 | + { color: string; icon: React.ReactNode } |
| 25 | +> = { |
| 26 | + info: { |
| 27 | + color: 'blue', |
| 28 | + icon: <IconInfoCircle size={16} />, |
| 29 | + }, |
| 30 | + warning: { |
| 31 | + color: 'yellow', |
| 32 | + icon: <IconAlertTriangle size={16} />, |
| 33 | + }, |
| 34 | + error: { |
| 35 | + color: 'red', |
| 36 | + icon: <IconX size={16} />, |
| 37 | + }, |
| 38 | + success: { |
| 39 | + color: 'green', |
| 40 | + icon: <IconCircleCheck size={16} />, |
| 41 | + }, |
| 42 | +}; |
| 43 | + |
| 44 | +const SystemNotificationBanner = ({ |
| 45 | + layout, |
| 46 | + ...others |
| 47 | +}: SystemNotificationBannerProps) => { |
| 48 | + const { getActiveNotifications, dismissNotification } = |
| 49 | + useSystemNotifications(); |
| 50 | + const activeNotifications = getActiveNotifications(layout); |
| 51 | + |
| 52 | + if (activeNotifications.length === 0) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <Stack gap={0} {...others}> |
| 58 | + {activeNotifications.map((notification: SystemNotification) => { |
| 59 | + const config = notificationConfig[notification.type]; |
| 60 | + |
| 61 | + return ( |
| 62 | + <Alert |
| 63 | + key={notification.id} |
| 64 | + bg={`var(--mantine-color-${config.color}-filled)`} |
| 65 | + c="white" |
| 66 | + py="xs" |
| 67 | + px="md" |
| 68 | + icon={config.icon} |
| 69 | + withCloseButton={notification.dismissible !== false} |
| 70 | + onClose={() => dismissNotification(notification.id)} |
| 71 | + > |
| 72 | + <Group gap="xs" wrap="nowrap"> |
| 73 | + {notification.message} |
| 74 | + {notification.action?.href && ( |
| 75 | + <Anchor |
| 76 | + href={notification.action.href} |
| 77 | + target="_blank" |
| 78 | + rel="noopener noreferrer" |
| 79 | + c="white" |
| 80 | + underline="always" |
| 81 | + style={{ flexShrink: 0 }} |
| 82 | + > |
| 83 | + {notification.action.label} |
| 84 | + </Anchor> |
| 85 | + )} |
| 86 | + </Group> |
| 87 | + </Alert> |
| 88 | + ); |
| 89 | + })} |
| 90 | + </Stack> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +export default SystemNotificationBanner; |
0 commit comments