1+ import * as React from 'react' ;
2+ import { actionsListener } from './ActionsListener' ;
3+ import { ActionListener } from './ActionListener' ;
4+ import Timer = NodeJS . Timer ;
5+
6+ import './VisualizedActions.css' ;
7+
8+ export interface State {
9+ actionLabelToShow ?: string ;
10+ timer ?: Timer ;
11+ }
12+
13+ export class VisualizedActions extends React . Component < { } , State > {
14+ state : State = {
15+ } ;
16+
17+ private actionListener : ActionListener ;
18+
19+ constructor ( props : { } ) {
20+ super ( props ) ;
21+ this . actionListener = {
22+ onAction : this . onNewAction
23+ } ;
24+ }
25+
26+ render ( ) {
27+ const { actionLabelToShow} = this . state ;
28+
29+ return (
30+ < div className = "rcv-visualized-actions" >
31+ { actionLabelToShow &&
32+ < div className = "rcv-visualized-action" > { actionLabelToShow } </ div >
33+ }
34+ </ div >
35+ ) ;
36+ }
37+
38+ componentDidMount ( ) {
39+ actionsListener . add ( this . actionListener ) ;
40+ }
41+
42+ componentWillUnmount ( ) {
43+ actionsListener . remove ( this . actionListener ) ;
44+ }
45+
46+ private removeActions = ( ) => {
47+ this . setState ( { actionLabelToShow : undefined } ) ;
48+ }
49+
50+ private onNewAction = ( label : string ) => {
51+ this . setState ( prev => {
52+ if ( prev . timer ) {
53+ clearTimeout ( prev . timer ) ;
54+ }
55+
56+ return {
57+ actionLabelToShow : label ,
58+ timer : setTimeout ( this . removeActions , 3000 )
59+ } ;
60+ } ) ;
61+ }
62+ }
0 commit comments