RightContent.tsx 7.42 KB
Newer Older
何乐's avatar
何乐 committed
1 2 3
import { ConnectProps } from '@/models/connect';
import { NoticeItem } from '@/models/global';
import { CurrentUser } from '@/models/user';
4 5
import React, { Component } from 'react';
import { FormattedMessage, formatMessage } from 'umi-plugin-locale';
Yu's avatar
Yu committed
6
import { Spin, Tag, Menu, Icon, Avatar, Tooltip, message } from 'antd';
7
import { ClickParam } from 'antd/es/menu';
jim's avatar
jim committed
8 9
import moment from 'moment';
import groupBy from 'lodash/groupBy';
ζ„šι“'s avatar
ζ„šι“ committed
10
import { NoticeIcon } from 'ant-design-pro';
jim's avatar
jim committed
11
import HeaderSearch from '../HeaderSearch';
12
import HeaderDropdown from '../HeaderDropdown';
13
import SelectLang from '../SelectLang';
jim's avatar
jim committed
14 15
import styles from './index.less';

何乐's avatar
何乐 committed
16
export type SiderTheme = 'light' | 'dark';
17

何乐's avatar
何乐 committed
18 19 20
export interface GlobalHeaderRightProps extends ConnectProps {
  notices?: NoticeItem[];
  currentUser?: CurrentUser;
21 22 23 24 25 26
  fetchingNotices?: boolean;
  onNoticeVisibleChange?: (visible: boolean) => void;
  onMenuClick?: (param: ClickParam) => void;
  onNoticeClear?: (tabName: string) => void;
  theme?: SiderTheme;
}
何乐's avatar
何乐 committed
27

28
export default class GlobalHeaderRight extends Component<GlobalHeaderRightProps> {
何乐's avatar
何乐 committed
29
  getNoticeData = (): { [key: string]: NoticeItem[] } => {
jim's avatar
jim committed
30 31 32 33
    const { notices = [] } = this.props;
    if (notices.length === 0) {
      return {};
    }
jim's avatar
jim committed
34
    const newNotices = notices.map(notice => {
jim's avatar
jim committed
35 36
      const newNotice = { ...notice };
      if (newNotice.datetime) {
何乐's avatar
何乐 committed
37
        newNotice.datetime = moment(notice.datetime as string).fromNow();
jim's avatar
jim committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      }
      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
58
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59

何乐's avatar
何乐 committed
60 61
  getUnreadData = (noticeData: { [key: string]: NoticeItem[] }) => {
    const unreadMsg: { [key: string]: number } = {};
62 63 64 65 66 67 68 69 70 71 72
    Object.entries(noticeData).forEach(([key, value]) => {
      if (!unreadMsg[key]) {
        unreadMsg[key] = 0;
      }
      if (Array.isArray(value)) {
        unreadMsg[key] = value.filter(item => !item.read).length;
      }
    });
    return unreadMsg;
  };

何乐's avatar
何乐 committed
73
  changeReadState = (clickedItem: NoticeItem) => {
74 75
    const { id } = clickedItem;
    const { dispatch } = this.props;
何乐's avatar
何乐 committed
76
    dispatch!({
77 78 79 80 81
      type: 'global/changeNoticeReadState',
      payload: id,
    });
  };

jim's avatar
jim committed
82 83 84 85 86 87 88
  render() {
    const {
      currentUser,
      fetchingNotices,
      onNoticeVisibleChange,
      onMenuClick,
      onNoticeClear,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
89
      theme,
jim's avatar
jim committed
90 91 92 93
    } = this.props;
    const menu = (
      <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
        <Menu.Item key="userCenter">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
94 95
          <Icon type="user" />
          <FormattedMessage id="menu.account.center" defaultMessage="account center" />
jim's avatar
jim committed
96 97
        </Menu.Item>
        <Menu.Item key="userinfo">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98 99
          <Icon type="setting" />
          <FormattedMessage id="menu.account.settings" defaultMessage="account settings" />
jim's avatar
jim committed
100 101
        </Menu.Item>
        <Menu.Item key="triggerError">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
102 103
          <Icon type="close-circle" />
          <FormattedMessage id="menu.account.trigger" defaultMessage="Trigger Error" />
jim's avatar
jim committed
104 105 106
        </Menu.Item>
        <Menu.Divider />
        <Menu.Item key="logout">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
107
          <Icon type="logout" />
108
          <FormattedMessage id="menu.account.logout" defaultMessage="logout" />
jim's avatar
jim committed
109 110 111 112
        </Menu.Item>
      </Menu>
    );
    const noticeData = this.getNoticeData();
113
    const unreadMsg = this.getUnreadData(noticeData);
jim's avatar
jim committed
114
    let className = styles.right;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
115
    if (theme === 'dark') {
jim's avatar
jim committed
116
      className = `${styles.right}  ${styles.dark}`;
jim's avatar
jim committed
117 118
    }
    return (
jim's avatar
jim committed
119
      <div className={className}>
jim's avatar
jim committed
120 121
        <HeaderSearch
          className={`${styles.action} ${styles.search}`}
122
          placeholder={formatMessage({ id: 'component.globalHeader.search' })}
123 124 125 126 127
          dataSource={[
            formatMessage({ id: 'component.globalHeader.search.example1' }),
            formatMessage({ id: 'component.globalHeader.search.example2' }),
            formatMessage({ id: 'component.globalHeader.search.example3' }),
          ]}
jim's avatar
jim committed
128
          onSearch={value => {
何乐's avatar
何乐 committed
129
            console.log('input', value); // tslint:disable-line no-console
jim's avatar
jim committed
130
          }}
jim's avatar
jim committed
131
          onPressEnter={value => {
何乐's avatar
何乐 committed
132
            console.log('enter', value); // tslint:disable-line no-console
jim's avatar
jim committed
133 134
          }}
        />
135
        <Tooltip title={formatMessage({ id: 'component.globalHeader.help' })}>
jim's avatar
jim committed
136 137
          <a
            target="_blank"
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
138
            href="https://pro.ant.design/docs/getting-started"
jim's avatar
jim committed
139 140 141 142 143 144
            rel="noopener noreferrer"
            className={styles.action}
          >
            <Icon type="question-circle-o" />
          </a>
        </Tooltip>
145

jim's avatar
jim committed
146 147
        <NoticeIcon
          className={styles.action}
何乐's avatar
何乐 committed
148
          count={currentUser && currentUser.unreadCount}
jim's avatar
jim committed
149
          onItemClick={(item, tabProps) => {
何乐's avatar
何乐 committed
150 151
            console.log(item, tabProps); // tslint:disable-line no-console
            this.changeReadState(item as NoticeItem);
jim's avatar
jim committed
152
          }}
Yu's avatar
Yu committed
153
          loading={fetchingNotices}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
154 155 156
          locale={{
            emptyText: formatMessage({ id: 'component.noticeIcon.empty' }),
            clear: formatMessage({ id: 'component.noticeIcon.clear' }),
何乐's avatar
何乐 committed
157
            viewMore: formatMessage({ id: 'component.noticeIcon.view-more' }),
Yu's avatar
Yu committed
158 159 160
            notification: formatMessage({ id: 'component.globalHeader.notification' }),
            message: formatMessage({ id: 'component.globalHeader.message' }),
            event: formatMessage({ id: 'component.globalHeader.event' }),
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
161
          }}
jim's avatar
jim committed
162 163
          onClear={onNoticeClear}
          onPopupVisibleChange={onNoticeVisibleChange}
何乐's avatar
何乐 committed
164
          onViewMore={() => message.info('Click on view more')}
wingsico's avatar
wingsico committed
165
          clearClose
jim's avatar
jim committed
166 167
        >
          <NoticeIcon.Tab
168
            count={unreadMsg.notification}
169
            list={noticeData.notification}
Yu's avatar
Yu committed
170
            title="notification"
171
            emptyText={formatMessage({ id: 'component.globalHeader.notification.empty' })}
jim's avatar
jim committed
172
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg"
173
            showViewMore
jim's avatar
jim committed
174 175
          />
          <NoticeIcon.Tab
176
            count={unreadMsg.message}
177
            list={noticeData.message}
Yu's avatar
Yu committed
178
            title="message"
179
            emptyText={formatMessage({ id: 'component.globalHeader.message.empty' })}
jim's avatar
jim committed
180
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg"
Yu's avatar
Yu committed
181
            showViewMore
jim's avatar
jim committed
182 183
          />
          <NoticeIcon.Tab
184
            count={unreadMsg.event}
185
            list={noticeData.event}
Yu's avatar
Yu committed
186
            title="event"
187
            emptyText={formatMessage({ id: 'component.globalHeader.event.empty' })}
jim's avatar
jim committed
188
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/HsIsxMZiWKrNUavQUXqx.svg"
Yu's avatar
Yu committed
189
            showViewMore
jim's avatar
jim committed
190 191
          />
        </NoticeIcon>
何乐's avatar
何乐 committed
192
        {currentUser && currentUser.name ? (
193
          <HeaderDropdown overlay={menu}>
jim's avatar
jim committed
194
            <span className={`${styles.action} ${styles.account}`}>
jim's avatar
jim committed
195 196 197 198 199 200
              <Avatar
                size="small"
                className={styles.avatar}
                src={currentUser.avatar}
                alt="avatar"
              />
jim's avatar
jim committed
201 202
              <span className={styles.name}>{currentUser.name}</span>
            </span>
203
          </HeaderDropdown>
jim's avatar
jim committed
204
        ) : (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
205
          <Spin size="small" style={{ marginLeft: 8, marginRight: 8 }} />
jim's avatar
jim committed
206
        )}
207
        <SelectLang className={styles.action} />
jim's avatar
jim committed
208 209 210 211
      </div>
    );
  }
}