RightContent.js 5.25 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
2
import { FormattedMessage, setLocale, getLocale } from 'umi/locale';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
3
import { Spin, Tag, Menu, Icon, Dropdown, Avatar, Tooltip, Button } from 'antd';
jim's avatar
jim committed
4 5 6 7 8 9 10 11 12 13 14 15
import moment from 'moment';
import groupBy from 'lodash/groupBy';
import NoticeIcon from '../NoticeIcon';
import HeaderSearch from '../HeaderSearch';
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
16
    const newNotices = notices.map(notice => {
jim's avatar
jim committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      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
41

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
42
  changLang = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
43
    const locale = getLocale();
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
44
    if (!locale || locale === 'zh-CN') {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
45
      setLocale('en-US');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
46
    } else {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
47
      setLocale('zh-CN');
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
48 49 50
    }
  };

jim's avatar
jim committed
51 52 53 54 55 56 57
  render() {
    const {
      currentUser,
      fetchingNotices,
      onNoticeVisibleChange,
      onMenuClick,
      onNoticeClear,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
58
      theme,
jim's avatar
jim committed
59 60 61 62
    } = this.props;
    const menu = (
      <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
        <Menu.Item key="userCenter">
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
63 64
          <Icon type="user" />
          <FormattedMessage id="menu.account.center" defaultMessage="account center" />
jim's avatar
jim committed
65 66
        </Menu.Item>
        <Menu.Item key="userinfo">
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
67 68
          <Icon type="setting" />
          <FormattedMessage id="menu.account.settings" defaultMessage="account settings" />
jim's avatar
jim committed
69 70
        </Menu.Item>
        <Menu.Item key="triggerError">
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
71 72
          <Icon type="close-circle" />
          <FormattedMessage id="menu.account.trigger" defaultMessage="Trigger Error" />
jim's avatar
jim committed
73 74 75
        </Menu.Item>
        <Menu.Divider />
        <Menu.Item key="logout">
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
76 77
          <Icon type="logout" />
          ้€€ๅ‡บ็™ปๅฝ•
jim's avatar
jim committed
78 79 80 81 82
        </Menu.Item>
      </Menu>
    );
    const noticeData = this.getNoticeData();
    let className = styles.right;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
83
    if (theme === 'dark') {
jim's avatar
jim committed
84
      className = `${styles.right}  ${styles.dark}`;
jim's avatar
jim committed
85 86
    }
    return (
jim's avatar
jim committed
87
      <div className={className}>
jim's avatar
jim committed
88 89 90 91
        <HeaderSearch
          className={`${styles.action} ${styles.search}`}
          placeholder="็ซ™ๅ†…ๆœ็ดข"
          dataSource={['ๆœ็ดขๆ็คบไธ€', 'ๆœ็ดขๆ็คบไบŒ', 'ๆœ็ดขๆ็คบไธ‰']}
jim's avatar
jim committed
92
          onSearch={value => {
jim's avatar
jim committed
93 94
            console.log('input', value); // eslint-disable-line
          }}
jim's avatar
jim committed
95
          onPressEnter={value => {
jim's avatar
jim committed
96 97 98 99 100 101
            console.log('enter', value); // eslint-disable-line
          }}
        />
        <Tooltip title="ไฝฟ็”จๆ–‡ๆกฃ">
          <a
            target="_blank"
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
102
            href="https://pro.ant.design/docs/getting-started"
jim's avatar
jim committed
103 104
            rel="noopener noreferrer"
            className={styles.action}
jim's avatar
jim committed
105
            title="ไฝฟ็”จๆ–‡ๆกฃ"
jim's avatar
jim committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
          >
            <Icon type="question-circle-o" />
          </a>
        </Tooltip>
        <NoticeIcon
          className={styles.action}
          count={currentUser.notifyCount}
          onItemClick={(item, tabProps) => {
            console.log(item, tabProps); // eslint-disable-line
          }}
          onClear={onNoticeClear}
          onPopupVisibleChange={onNoticeVisibleChange}
          loading={fetchingNotices}
          popupAlign={{ offset: [20, -16] }}
        >
          <NoticeIcon.Tab
            list={noticeData['้€š็Ÿฅ']}
            title="้€š็Ÿฅ"
            emptyText="ไฝ ๅทฒๆŸฅ็œ‹ๆ‰€ๆœ‰้€š็Ÿฅ"
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg"
          />
          <NoticeIcon.Tab
            list={noticeData['ๆถˆๆฏ']}
            title="ๆถˆๆฏ"
            emptyText="ๆ‚จๅทฒ่ฏปๅฎŒๆ‰€ๆœ‰ๆถˆๆฏ"
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg"
          />
          <NoticeIcon.Tab
            list={noticeData['ๅพ…ๅŠž']}
            title="ๅพ…ๅŠž"
            emptyText="ไฝ ๅทฒๅฎŒๆˆๆ‰€ๆœ‰ๅพ…ๅŠž"
            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
143 144 145 146 147 148
              <Avatar
                size="small"
                className={styles.avatar}
                src={currentUser.avatar}
                alt="avatar"
              />
jim's avatar
jim committed
149 150 151 152
              <span className={styles.name}>{currentUser.name}</span>
            </span>
          </Dropdown>
        ) : (
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
153
          <Spin size="small" style={{ marginLeft: 8, marginRight: 8 }} />
jim's avatar
jim committed
154
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
155 156
        <Button
          size="small"
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
157
          ghost={theme === 'dark'}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
158 159 160
          style={{
            margin: '0 8px',
          }}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
161 162
          onClick={() => {
            this.changLang();
163
          }}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
164 165 166
        >
          <FormattedMessage id="navbar.lang" />
        </Button>
jim's avatar
jim committed
167 168 169 170
      </div>
    );
  }
}