Skip to content

Commit 7b9cba4

Browse files
committed
submission in progress notification
1 parent c59fb38 commit 7b9cba4

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/components/LeetCodeNotebookToolbar.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Notification } from '@jupyterlab/apputils';
33
import { NotebookPanel, NotebookActions } from '@jupyterlab/notebook';
44
import { ToolbarButtonComponent } from '@jupyterlab/ui-components';
55
import { ICellModel } from '@jupyterlab/cells';
6+
import { PromiseDelegate, ReadonlyJSONValue } from '@lumino/coreutils';
67
import { submitNotebook } from '../services/notebook';
78
import { makeWebSocket } from '../services/handler';
89
import { leetcodeIcon } from '../icons/leetcode';
@@ -16,7 +17,7 @@ const status2Emoji = (status: string) => {
1617
case 'Accepted':
1718
return '😃';
1819
case 'Wrong Answer':
19-
return '😕';
20+
return '🐛';
2021
case 'Time Limit Exceeded':
2122
return '⏳';
2223
case 'Memory Limit Exceeded':
@@ -41,6 +42,8 @@ const LeetCodeNotebookToolbar: React.FC<{ notebook: NotebookPanel }> = ({
4142
const [ws, setWs] = useState<WebSocket | null>(null);
4243
const [wsRetries, setWsRetries] = useState(0);
4344
const [result, setResult] = useState<LeetCodeSubmissionResult | null>(null);
45+
const [submitPromise, setSubmitPromise] =
46+
useState<PromiseDelegate<ReadonlyJSONValue> | null>(null);
4447

4548
const submit = () => {
4649
notebook.context.save().then(() => {
@@ -136,27 +139,55 @@ const LeetCodeNotebookToolbar: React.FC<{ notebook: NotebookPanel }> = ({
136139
setWs(makeWs(submissionId));
137140
setWsRetries(0);
138141
setResult(null);
142+
setSubmitPromise(new PromiseDelegate<ReadonlyJSONValue>());
139143
}, [submissionId]);
140144

141145
// reconnect websocket
142146
useEffect(() => {
143147
if (!ws) {
144148
return;
145149
}
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+
}
152162
}
153163
}, [ws, ws?.readyState]);
154164

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+
155185
// render result cell to notebook
156186
useEffect(() => {
157187
if (result?.state !== 'SUCCESS') {
158188
return;
159189
}
190+
submitPromise?.resolve({ status_msg: result.status_msg });
160191
const resultCellModel = getResultCell();
161192
if (resultCellModel) {
162193
populateResultCell(resultCellModel, result);

0 commit comments

Comments
 (0)