-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
notification-component.tsx
111 lines (101 loc) · 4.8 KB
/
notification-component.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
/********************************************************************************
* Copyright (C) 2019 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import * as React from 'react';
import { NotificationManager, Notification } from './notifications-manager';
export interface NotificationComponentProps {
readonly manager: NotificationManager;
readonly notification: Notification;
}
export class NotificationComponent extends React.Component<NotificationComponentProps> {
constructor(props: NotificationComponentProps) {
super(props);
this.state = {};
}
protected onClear = (event: React.MouseEvent) => {
if (event.target instanceof HTMLElement) {
const messageId = event.target.dataset.messageId;
if (messageId) {
this.props.manager.clear(messageId);
}
}
};
protected onToggleExpansion = (event: React.MouseEvent) => {
if (event.target instanceof HTMLElement) {
const messageId = event.target.dataset.messageId;
if (messageId) {
this.props.manager.toggleExpansion(messageId);
}
}
};
protected onAction = (event: React.MouseEvent) => {
if (event.target instanceof HTMLElement) {
const messageId = event.target.dataset.messageId;
const action = event.target.dataset.action;
if (messageId && action) {
this.props.manager.accept(messageId, action);
}
}
};
protected onMessageClick = (event: React.MouseEvent) => {
if (event.target instanceof HTMLAnchorElement) {
event.stopPropagation();
event.preventDefault();
const link = event.target.href;
this.props.manager.openLink(link);
}
};
render(): React.ReactNode {
const { messageId, message, type, progress, collapsed, expandable, source, actions } = this.props.notification;
return (<div key={messageId} className='theia-notification-list-item'>
<div className={`theia-notification-list-item-content ${collapsed ? 'collapsed' : ''}`}>
<div className='theia-notification-list-item-content-main'>
<div className={`theia-notification-icon theia-notification-icon-${type}`} />
<div className='theia-notification-message'>
<span dangerouslySetInnerHTML={{ __html: message }} onClick={this.onMessageClick} />
</div>
<ul className='theia-notification-actions'>
{expandable && (
<li className={collapsed ? 'expand' : 'collapse'} title={collapsed ? 'Expand' : 'Collapse'}
data-message-id={messageId} onClick={this.onToggleExpansion} />
)}
<li className='clear' title='Clear' data-message-id={messageId} onClick={this.onClear} />
</ul>
</div>
{(source || !!actions.length) && (
<div className='theia-notification-list-item-content-bottom'>
<div className='theia-notification-source'>
{source && (<span>{source}</span>)}
</div>
<div className='theia-notification-buttons'>
{actions && actions.map((action, index) => (
<button key={messageId + `-action-${index}`} className='theia-button'
data-message-id={messageId} data-action={action}
onClick={this.onAction}>
{action}
</button>
))}
</div>
</div>
)}
</div>
{typeof progress === 'number' && (
<div className='theia-notification-item-progress'>
<div className='theia-notification-item-progressbar' style={{ width: `${progress}%` }} />
</div>
)}
</div>);
}
}