5
5
6
6
import { Map } from "immutable" ;
7
7
import { Alert , Button , Card , Input , Popconfirm , Space } from "antd" ;
8
- import {
9
- Component ,
10
- rclass ,
11
- redux ,
12
- rtypes ,
13
- } from "@cocalc/frontend/app-framework" ;
14
8
import { Icon , Loading , Title } from "@cocalc/frontend/components" ;
15
9
import { plural } from "@cocalc/util/misc" ;
10
+ import { useState } from "react" ;
11
+ import { useActions , useRedux } from "@cocalc/frontend/app-framework" ;
16
12
17
- interface Props {
18
- notifications ?: Map < string , any > ;
19
- }
20
-
21
- interface State {
22
- state : "view" | "edit" ;
23
- mesg ?: string ;
24
- }
13
+ export function SystemNotifications ( { } ) {
14
+ const [ state , setState ] = useState < "view" | "edit" > ( "view" ) ;
15
+ const [ mesg , setMesg ] = useState < string > ( "" ) ;
16
+ const notifications = useRedux ( "system_notifications" , "notifications" ) ;
17
+ const actions = useActions ( "system_notifications" ) ;
25
18
26
- class SystemNotifications extends Component < Props , State > {
27
- constructor ( props , state ) {
28
- super ( props , state ) ;
29
- this . state = { state : "view" } ;
30
- }
31
-
32
- static reduxProps ( ) : any {
33
- return {
34
- system_notifications : { notifications : rtypes . immutable } ,
35
- } ;
36
- }
37
-
38
- render_mark_done ( ) {
39
- if ( ! this . props . notifications ) return ;
19
+ function render_mark_done ( ) {
20
+ if ( ! notifications ) return ;
40
21
let open = 0 ;
41
- this . props . notifications . map ( ( mesg : Map < string , any > ) => {
22
+ notifications . map ( ( mesg : Map < string , any > ) => {
42
23
if ( mesg && ! mesg . get ( "done" ) ) {
43
24
open += 1 ;
44
25
}
45
26
} ) ;
46
27
if ( open > 0 ) {
47
28
return (
48
- < Button onClick = { ( ) => this . mark_all_done ( ) } >
29
+ < Button onClick = { ( ) => mark_all_done ( ) } >
49
30
Mark { open } { plural ( open , "Notification" ) } Done
50
31
</ Button >
51
32
) ;
@@ -54,34 +35,33 @@ class SystemNotifications extends Component<Props, State> {
54
35
}
55
36
}
56
37
57
- render_buttons ( ) {
38
+ function render_buttons ( ) {
58
39
return (
59
40
< Space >
60
- < Button onClick = { ( ) => this . setState ( { state : "edit" , mesg : "" } ) } >
41
+ < Button
42
+ onClick = { ( ) => {
43
+ setState ( "edit" ) ;
44
+ setMesg ( "" ) ;
45
+ } }
46
+ >
61
47
Compose...
62
48
</ Button >
63
- { this . render_mark_done ( ) }
49
+ { render_mark_done ( ) }
64
50
</ Space >
65
51
) ;
66
52
}
67
53
68
- render_editor ( ) {
54
+ function render_editor ( ) {
69
55
return (
70
56
< Card >
71
57
< Input . TextArea
72
58
autoFocus
73
- value = { this . state . mesg }
59
+ value = { mesg }
74
60
rows = { 3 }
75
- onChange = { ( e ) =>
76
- this . setState ( {
77
- mesg : e . target . value ,
78
- } )
79
- }
61
+ onChange = { ( e ) => setMesg ( e . target . value ) }
80
62
/>
81
63
< Space style = { { marginTop : "15px" } } >
82
- < Button onClick = { ( ) => this . setState ( { state : "view" } ) } >
83
- Cancel
84
- </ Button >
64
+ < Button onClick = { ( ) => setState ( "view" ) } > Cancel</ Button >
85
65
< Popconfirm
86
66
title = "Send notification?"
87
67
description = {
@@ -90,11 +70,11 @@ class SystemNotifications extends Component<Props, State> {
90
70
once in the upper right until you explicitly mark it done (they
91
71
can dismiss it).
92
72
< hr />
93
- < Alert message = { this . state . mesg } />
73
+ < Alert message = { mesg } />
94
74
</ div >
95
75
}
96
76
onConfirm = { ( ) => {
97
- this . send ( ) ;
77
+ send ( ) ;
98
78
} }
99
79
>
100
80
< Button danger >
@@ -106,40 +86,35 @@ class SystemNotifications extends Component<Props, State> {
106
86
) ;
107
87
}
108
88
109
- send ( ) : void {
110
- this . setState ( { state : "view" } ) ;
111
- if ( ! this . state . mesg ) return ;
112
- ( redux . getActions ( "system_notifications" ) as any ) . send_message ( {
113
- text : this . state . mesg . trim ( ) ,
89
+ function send ( ) : void {
90
+ setState ( "view" ) ;
91
+ if ( ! mesg ) return ;
92
+ actions . send_message ( {
93
+ text : mesg . trim ( ) ,
114
94
priority : "high" ,
115
95
} ) ;
116
96
}
117
97
118
- mark_all_done ( ) : void {
119
- ( redux . getActions ( "system_notifications" ) as any ) . mark_all_done ( ) ;
98
+ function mark_all_done ( ) : void {
99
+ actions . mark_all_done ( ) ;
120
100
}
121
101
122
- render_body ( ) {
123
- if ( this . props . notifications == null ) {
102
+ function render_body ( ) {
103
+ if ( notifications == null ) {
124
104
return < Loading /> ;
125
105
}
126
- switch ( this . state . state ) {
106
+ switch ( state ) {
127
107
case "view" :
128
- return this . render_buttons ( ) ;
108
+ return render_buttons ( ) ;
129
109
case "edit" :
130
- return this . render_editor ( ) ;
110
+ return render_editor ( ) ;
131
111
}
132
112
}
133
113
134
- render ( ) {
135
- return (
136
- < div >
137
- < Title level = { 4 } > System Notifications</ Title >
138
- { this . render_body ( ) }
139
- </ div >
140
- ) ;
141
- }
114
+ return (
115
+ < div >
116
+ < Title level = { 4 } > System Notifications</ Title >
117
+ { render_body ( ) }
118
+ </ div >
119
+ ) ;
142
120
}
143
-
144
- const SystemNotifications0 = rclass ( SystemNotifications ) ;
145
- export { SystemNotifications0 as SystemNotifications } ;
0 commit comments