|
1
|
"use client" |
|
2
|
|
|
3
|
import { |
|
4
|
NotificationContextType, |
|
5
|
NotificationItemType, |
|
6
|
useNotifications, |
|
7
|
} from "@/providers/Notification" |
|
8
|
import React from "react" |
|
9
|
import { NotificationItem } from "./Item" |
|
10
|
// @ts-expect-error can't install the types package because it doesn't support React v19 |
|
11
|
import { TransitionGroup } from "react-transition-group" |
|
12
|
import clsx from "clsx" |
|
13
|
|
|
14
|
export const NotificationContainer = () => { |
|
15
|
const { notifications, removeNotification } = |
|
16
|
useNotifications() as NotificationContextType |
|
17
|
|
|
18
|
const handleClose = (notification: NotificationItemType) => { |
|
19
|
notification.onClose?.() |
|
20
|
if (notification.id) { |
|
21
|
removeNotification(notification.id) |
|
22
|
} |
|
23
|
} |
|
24
|
|
|
25
|
const renderFilteredNotifications = ( |
|
26
|
condition: (notificaiton: NotificationItemType) => boolean, |
|
27
|
className?: string |
|
28
|
) => { |
|
29
|
return ( |
|
30
|
<TransitionGroup |
|
31
|
className={clsx( |
|
32
|
"flex fixed z-40 flex-col gap-docs_0.5 right-0", |
|
33
|
"md:w-auto w-full overflow-y-auto", |
|
34
|
"max-h-[50%] md:max-h-[calc(100vh-57px)]", |
|
35
|
"max-[768px]:max-h-[50%]", |
|
36
|
className |
|
37
|
)} |
|
38
|
> |
|
39
|
{notifications.filter(condition).map((notification) => ( |
|
40
|
<NotificationItem |
|
41
|
{...notification} |
|
42
|
onClose={() => handleClose(notification)} |
|
43
|
className={clsx( |
|
44
|
notification.className, |
|
45
|
"!relative !top-0 !bottom-0 !right-0" |
|
46
|
)} |
|
47
|
key={notification.id} |
|
48
|
/> |
|
49
|
))} |
|
50
|
</TransitionGroup> |
|
51
|
) |
|
52
|
} |
|
53
|
|
|
54
|
return ( |
|
55
|
<> |
|
56
|
{renderFilteredNotifications( |
|
57
|
(notification) => notification.placement === "top", |
|
58
|
"top-0" |
|
59
|
)} |
|
60
|
{renderFilteredNotifications( |
|
61
|
(notification) => notification.placement !== "top", |
|
62
|
"bottom-0" |
|
63
|
)} |
|
64
|
</> |
|
65
|
) |
|
66
|
} |