@@ -3,6 +3,7 @@ import { Notification } from '@jupyterlab/apputils';
3
3
import { NotebookPanel , NotebookActions } from '@jupyterlab/notebook' ;
4
4
import { ToolbarButtonComponent } from '@jupyterlab/ui-components' ;
5
5
import { ICellModel } from '@jupyterlab/cells' ;
6
+ import { PromiseDelegate , ReadonlyJSONValue } from '@lumino/coreutils' ;
6
7
import { submitNotebook } from '../services/notebook' ;
7
8
import { makeWebSocket } from '../services/handler' ;
8
9
import { leetcodeIcon } from '../icons/leetcode' ;
@@ -16,7 +17,7 @@ const status2Emoji = (status: string) => {
16
17
case 'Accepted' :
17
18
return '😃' ;
18
19
case 'Wrong Answer' :
19
- return '😕 ' ;
20
+ return '🐛 ' ;
20
21
case 'Time Limit Exceeded' :
21
22
return '⏳' ;
22
23
case 'Memory Limit Exceeded' :
@@ -41,6 +42,8 @@ const LeetCodeNotebookToolbar: React.FC<{ notebook: NotebookPanel }> = ({
41
42
const [ ws , setWs ] = useState < WebSocket | null > ( null ) ;
42
43
const [ wsRetries , setWsRetries ] = useState ( 0 ) ;
43
44
const [ result , setResult ] = useState < LeetCodeSubmissionResult | null > ( null ) ;
45
+ const [ submitPromise , setSubmitPromise ] =
46
+ useState < PromiseDelegate < ReadonlyJSONValue > | null > ( null ) ;
44
47
45
48
const submit = ( ) => {
46
49
notebook . context . save ( ) . then ( ( ) => {
@@ -136,27 +139,55 @@ const LeetCodeNotebookToolbar: React.FC<{ notebook: NotebookPanel }> = ({
136
139
setWs ( makeWs ( submissionId ) ) ;
137
140
setWsRetries ( 0 ) ;
138
141
setResult ( null ) ;
142
+ setSubmitPromise ( new PromiseDelegate < ReadonlyJSONValue > ( ) ) ;
139
143
} , [ submissionId ] ) ;
140
144
141
145
// reconnect websocket
142
146
useEffect ( ( ) => {
143
147
if ( ! ws ) {
144
148
return ;
145
149
}
146
- if ( ws . readyState === WebSocket . CLOSED && wsRetries < 10 ) {
147
- setTimeout ( ( ) => {
148
- console . log ( 'Reconnecting WebSocket...' ) ;
149
- setWs ( makeWs ( submissionId ) ) ;
150
- } , 1000 ) ;
151
- setWsRetries ( wsRetries + 1 ) ;
150
+ if ( ws . readyState === WebSocket . CLOSED ) {
151
+ if ( wsRetries < 10 ) {
152
+ setTimeout ( ( ) => {
153
+ console . log ( 'Reconnecting WebSocket...' ) ;
154
+ setWs ( makeWs ( submissionId ) ) ;
155
+ } , 1000 ) ;
156
+ setWsRetries ( wsRetries + 1 ) ;
157
+ } else {
158
+ submitPromise ?. reject ( {
159
+ error : 'WebSocket connection failed after 10 retries.'
160
+ } ) ;
161
+ }
152
162
}
153
163
} , [ ws , ws ?. readyState ] ) ;
154
164
165
+ // notification after submit
166
+ useEffect ( ( ) => {
167
+ if ( ! submitPromise ) {
168
+ return ;
169
+ }
170
+
171
+ Notification . promise ( submitPromise . promise , {
172
+ pending : { message : '⏳ Pending...' , options : { autoClose : false } } ,
173
+ success : {
174
+ message : ( result : any ) =>
175
+ `${ status2Emoji ( result . status_msg ) } Result: ${ result . status_msg } ` ,
176
+ options : { autoClose : 3000 }
177
+ } ,
178
+ error : {
179
+ message : ( result : any ) => `🔴 Error: ${ result . error } ` ,
180
+ options : { autoClose : 3000 }
181
+ }
182
+ } ) ;
183
+ } , [ submitPromise ] ) ;
184
+
155
185
// render result cell to notebook
156
186
useEffect ( ( ) => {
157
187
if ( result ?. state !== 'SUCCESS' ) {
158
188
return ;
159
189
}
190
+ submitPromise ?. resolve ( { status_msg : result . status_msg } ) ;
160
191
const resultCellModel = getResultCell ( ) ;
161
192
if ( resultCellModel ) {
162
193
populateResultCell ( resultCellModel , result ) ;
0 commit comments