-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathNotificationDropdown.tsx
113 lines (105 loc) · 3.35 KB
/
NotificationDropdown.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
import { UR } from 'getstream';
import { Feed, useFeedContext, DefaultAT, DefaultUT } from '../context';
import { smartRender, ElementOrComponentOrLiteralType, PropsWithElementAttributes } from '../utils';
import { useOnClickOutside } from '../hooks/useOnClickOutside';
import { NotificationFeed, NotificationFeedProps } from './NotificationFeed';
import { DropdownPanel, DropdownPanelProps } from './DropdownPanel';
import { IconBadge } from './IconBadge';
export type NotificationDropdownProps<
UT extends DefaultUT = DefaultUT,
AT extends DefaultAT = DefaultAT,
CT extends UR = UR,
RT extends UR = UR,
CRT extends UR = UR,
PT extends UR = UR
> = PropsWithElementAttributes<
{
Icon?: ElementOrComponentOrLiteralType;
width?: number;
} & Pick<DropdownPanelProps, 'Footer' | 'Header' | 'right'> &
NotificationFeedProps<UT, AT, CT, RT, CRT, PT>
>;
const NotificationDropdownInner = <
UT extends DefaultUT = DefaultUT,
AT extends DefaultAT = DefaultAT,
CT extends UR = UR,
RT extends UR = UR,
CRT extends UR = UR,
PT extends UR = UR
>({
width,
Footer,
Header,
Icon,
right,
className,
style,
...feedProps
}: NotificationDropdownProps<UT, AT, CT, RT, CRT, PT>) => {
const feed = useFeedContext<UT, AT, CT, RT, CRT, PT>();
const [open, setOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
useOnClickOutside(dropdownRef, () => setOpen(false), open);
useEffect(() => {
feed.refreshUnreadUnseen();
}, []);
return (
<div className={classNames('raf-notification-dropdown', className)} style={style}>
<IconBadge showNumber unseen={feed.unseen} hidden={!feedProps.notify} onClick={() => setOpen(true)}>
{Icon && smartRender(Icon)}
</IconBadge>
<div
ref={dropdownRef}
style={{ maxWidth: width }}
className={`raf-notification-dropdown__panel${open ? ' raf-notification-dropdown__panel--open' : ''}${
right ? ' raf-notification-dropdown__panel--right' : ''
}`}
>
{open && (
<DropdownPanel arrow right={right} Header={Header} Footer={Footer}>
<NotificationFeed {...feedProps} />
</DropdownPanel>
)}
</div>
</div>
);
};
/**
* IMPORTANT: Changing most of the props below doesn't result in the desired effect.
* These settings related to feed management should be changed in the `sharedFeeds` prop of the [`StreamApp`](#streamapp) component.
*/
export const NotificationDropdown = <
UT extends DefaultUT = DefaultUT,
AT extends DefaultAT = DefaultAT,
CT extends UR = UR,
RT extends UR = UR,
CRT extends UR = UR,
PT extends UR = UR
>({
width = 475,
Footer,
Header,
Icon,
right,
feedGroup = 'notification',
options,
...feedProps
}: NotificationDropdownProps<UT, AT, CT, RT, CRT, PT>) => {
const optionsWithDefaults = { ...options, mark_seen: options?.mark_seen ?? true };
return (
<Feed<UT, AT, CT, RT, CRT, PT> {...feedProps} feedGroup={feedGroup} options={optionsWithDefaults}>
<NotificationDropdownInner<UT, AT, CT, RT, CRT, PT>
width={width}
Footer={Footer}
Header={Header}
Icon={Icon}
right={right}
{...feedProps}
feedGroup={feedGroup}
options={optionsWithDefaults}
/>
</Feed>
);
};