File size: 2,330 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @flow
import * as React from "react";
import { Notification, Dropdown } from "../";
import type { Props as NotificationProps } from "./Notification.react";

export type Props = {|
  /**
   * Notification components
   */
  +children?: React.ChildrenArray<React.Element<typeof Notification>>,
  /**
   * An array containing objects of notification data
   */
  +notificationsObjects?: Array<NotificationProps>,
  /**
   * Display a small red circle to symbolize that there are unread notifications
   */
  +unread?: boolean,
  /**
   * Action to run when the 'Mark All As Read' button is activated
   */
  +markAllAsRead?: () => void,
|};

/**
 * An Icon triggered Dropdown containing Notifications
 */
function NotificationTray(props: Props): React.Node {
  const { children, unread, notificationsObjects, markAllAsRead } = props;
  const notifications = children && React.Children.toArray(children);
  return (
    <Dropdown
      triggerContent={unread && <span className="nav-unread" />}
      toggle={false}
      icon="bell"
      isNavLink={true}
      position="bottom-end"
      arrow={true}
      arrowPosition="right"
      flex
      items={
        <React.Fragment>
          {(notifications &&
            notifications.map((n: React.Node, i) => (
              <Dropdown.Item className="d-flex" key={i}>
                {n}
              </Dropdown.Item>
            ))) ||
            (notificationsObjects &&
              notificationsObjects.map((n, i) => (
                <Dropdown.Item
                  className={`d-flex ${n.unread ? "bg-light" : ""}`}
                  key={i}
                >
                  <Notification
                    unread={n.unread}
                    avatarURL={n.avatarURL}
                    message={n.message}
                    time={n.time}
                  />
                </Dropdown.Item>
              )))}
          {markAllAsRead && unread && (
            <React.Fragment>
              <Dropdown.ItemDivider />
              <Dropdown.Item
                className="text-center text-muted-dark"
                onClick={() => markAllAsRead()}
              >
                Mark all as read
              </Dropdown.Item>
            </React.Fragment>
          )}
        </React.Fragment>
      }
    />
  );
}

export default NotificationTray;