RightContent.js 5.68 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
2 3
import { FormattedMessage, formatMessage } from 'umi/locale';
import { Spin, Tag, Menu, Icon, Dropdown, Avatar, Tooltip } from 'antd';
jim's avatar
jim committed
4 5 6 7
import moment from 'moment';
import groupBy from 'lodash/groupBy';
import NoticeIcon from '../NoticeIcon';
import HeaderSearch from '../HeaderSearch';
8
import SelectLang from '../SelectLang';
jim's avatar
jim committed
9 10 11 12 13 14 15 16
import styles from './index.less';

export default class GlobalHeaderRight extends PureComponent {
  getNoticeData() {
    const { notices = [] } = this.props;
    if (notices.length === 0) {
      return {};
    }
jim's avatar
jim committed
17
    const newNotices = notices.map(notice => {
jim's avatar
jim committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
      const newNotice = { ...notice };
      if (newNotice.datetime) {
        newNotice.datetime = moment(notice.datetime).fromNow();
      }
      if (newNotice.id) {
        newNotice.key = newNotice.id;
      }
      if (newNotice.extra && newNotice.status) {
        const color = {
          todo: '',
          processing: 'blue',
          urgent: 'red',
          doing: 'gold',
        }[newNotice.status];
        newNotice.extra = (
          <Tag color={color} style={{ marginRight: 0 }}>
            {newNotice.extra}
          </Tag>
        );
      }
      return newNotice;
    });
    return groupBy(newNotices, 'type');
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
42

jim's avatar
jim committed
43 44 45 46 47 48 49
  render() {
    const {
      currentUser,
      fetchingNotices,
      onNoticeVisibleChange,
      onMenuClick,
      onNoticeClear,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50
      theme,
jim's avatar
jim committed
51 52 53 54
    } = this.props;
    const menu = (
      <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
        <Menu.Item key="userCenter">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
55 56
          <Icon type="user" />
          <FormattedMessage id="menu.account.center" defaultMessage="account center" />
jim's avatar
jim committed
57 58
        </Menu.Item>
        <Menu.Item key="userinfo">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59 60
          <Icon type="setting" />
          <FormattedMessage id="menu.account.settings" defaultMessage="account settings" />
jim's avatar
jim committed
61 62
        </Menu.Item>
        <Menu.Item key="triggerError">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63 64
          <Icon type="close-circle" />
          <FormattedMessage id="menu.account.trigger" defaultMessage="Trigger Error" />
jim's avatar
jim committed
65 66 67
        </Menu.Item>
        <Menu.Divider />
        <Menu.Item key="logout">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
68
          <Icon type="logout" />
69
          <FormattedMessage id="menu.account.logout" defaultMessage="logout" />
jim's avatar
jim committed
70 71 72 73 74
        </Menu.Item>
      </Menu>
    );
    const noticeData = this.getNoticeData();
    let className = styles.right;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75
    if (theme === 'dark') {
jim's avatar
jim committed
76
      className = `${styles.right}  ${styles.dark}`;
jim's avatar
jim committed
77 78
    }
    return (
jim's avatar
jim committed
79
      <div className={className}>
jim's avatar
jim committed
80 81
        <HeaderSearch
          className={`${styles.action} ${styles.search}`}
82
          placeholder={formatMessage({ id: 'component.globalHeader.search' })}
83 84 85 86 87
          dataSource={[
            formatMessage({ id: 'component.globalHeader.search.example1' }),
            formatMessage({ id: 'component.globalHeader.search.example2' }),
            formatMessage({ id: 'component.globalHeader.search.example3' }),
          ]}
jim's avatar
jim committed
88
          onSearch={value => {
jim's avatar
jim committed
89 90
            console.log('input', value); // eslint-disable-line
          }}
jim's avatar
jim committed
91
          onPressEnter={value => {
jim's avatar
jim committed
92 93 94
            console.log('enter', value); // eslint-disable-line
          }}
        />
95
        <Tooltip title={formatMessage({ id: 'component.globalHeader.help' })}>
jim's avatar
jim committed
96 97
          <a
            target="_blank"
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98
            href="https://pro.ant.design/docs/getting-started"
jim's avatar
jim committed
99 100 101 102 103 104 105 106 107 108 109 110
            rel="noopener noreferrer"
            className={styles.action}
          >
            <Icon type="question-circle-o" />
          </a>
        </Tooltip>
        <NoticeIcon
          className={styles.action}
          count={currentUser.notifyCount}
          onItemClick={(item, tabProps) => {
            console.log(item, tabProps); // eslint-disable-line
          }}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
111 112 113 114
          locale={{
            emptyText: formatMessage({ id: 'component.noticeIcon.empty' }),
            clear: formatMessage({ id: 'component.noticeIcon.clear' }),
          }}
jim's avatar
jim committed
115 116 117 118 119 120
          onClear={onNoticeClear}
          onPopupVisibleChange={onNoticeVisibleChange}
          loading={fetchingNotices}
          popupAlign={{ offset: [20, -16] }}
        >
          <NoticeIcon.Tab
121
            list={noticeData.notification}
122
            title={formatMessage({ id: 'component.globalHeader.notification' })}
123
            name="notification"
124
            emptyText={formatMessage({ id: 'component.globalHeader.notification.empty' })}
jim's avatar
jim committed
125 126 127
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg"
          />
          <NoticeIcon.Tab
128
            list={noticeData.message}
129
            title={formatMessage({ id: 'component.globalHeader.message' })}
130
            name="message"
131
            emptyText={formatMessage({ id: 'component.globalHeader.message.empty' })}
jim's avatar
jim committed
132 133 134
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg"
          />
          <NoticeIcon.Tab
135
            list={noticeData.event}
136
            title={formatMessage({ id: 'component.globalHeader.event' })}
137
            name="event"
138
            emptyText={formatMessage({ id: 'component.globalHeader.event.empty' })}
jim's avatar
jim committed
139 140 141 142 143 144
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/HsIsxMZiWKrNUavQUXqx.svg"
          />
        </NoticeIcon>
        {currentUser.name ? (
          <Dropdown overlay={menu}>
            <span className={`${styles.action} ${styles.account}`}>
jim's avatar
jim committed
145 146 147 148 149 150
              <Avatar
                size="small"
                className={styles.avatar}
                src={currentUser.avatar}
                alt="avatar"
              />
jim's avatar
jim committed
151 152 153 154
              <span className={styles.name}>{currentUser.name}</span>
            </span>
          </Dropdown>
        ) : (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
155
          <Spin size="small" style={{ marginLeft: 8, marginRight: 8 }} />
jim's avatar
jim committed
156
        )}
157
        <SelectLang className={styles.action} />
jim's avatar
jim committed
158 159 160 161
      </div>
    );
  }
}