Skip to content

Commit c59fb38

Browse files
committed
use jupyter lab notification component to show error
1 parent dda7f32 commit c59fb38

File tree

7 files changed

+24
-32
lines changed

7 files changed

+24
-32
lines changed

src/components/BrowserCookie.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2+
import { Notification } from '@jupyterlab/apputils';
23
import { getCookie } from '../services/cookie';
34
import Bowser from 'bowser';
45

@@ -47,14 +48,14 @@ const BrowserCookie: React.FC<{
4748
}, [checked, setCookieLoggedIn]);
4849

4950
const checkCookie = () => {
50-
// TODO: change alert
5151
if (!browser) {
52-
alert('Please select a browser.');
52+
Notification.error('Please select a browser.', { autoClose: 3000 });
5353
return;
5454
}
5555
if (browser === 'safari') {
56-
alert(
57-
'Safari does not support getting cookies from the browser. Please use another browser.'
56+
Notification.error(
57+
'Safari does not support getting cookies from the browser. Please use another browser.',
58+
{ autoClose: 3000 }
5859
);
5960
return;
6061
}
@@ -63,7 +64,7 @@ const BrowserCookie: React.FC<{
6364
.then(resp => {
6465
setChecked(resp['checked']);
6566
})
66-
.catch(console.error);
67+
.catch(e => Notification.error(e.message, { autoClose: 3000 }));
6768
};
6869

6970
return (

src/components/LeetCode.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import React, { useEffect, useState } from 'react';
22
import { IDocumentManager } from '@jupyterlab/docmanager';
3-
import { IDocumentWidget } from '@jupyterlab/docregistry';
4-
import { JupyterFrontEnd, LabShell } from '@jupyterlab/application';
3+
import { Notification } from '@jupyterlab/apputils';
4+
import { JupyterFrontEnd } from '@jupyterlab/application';
55
import { getProfile } from '../services/leetcode';
66
import { LeetCodeProfile } from '../types/leetcode';
77
import Profile from './Profile';
88
import Statistics from './Statistics';
99
import QuestionList from './QuestionList';
1010

11-
export function getCurrentOpenFilePath(
12-
shell: LabShell,
13-
docManager: IDocumentManager,
14-
widget?: IDocumentWidget
15-
): string | null {
16-
const currentWidget = widget ?? shell.currentWidget;
17-
if (!currentWidget || !docManager) {
18-
return null;
19-
}
20-
const context = docManager.contextForWidget(currentWidget);
21-
if (!context) {
22-
return null;
23-
}
24-
return context.path;
25-
}
26-
2711
const LeetCode: React.FC<{
2812
app: JupyterFrontEnd;
2913
docManager: IDocumentManager;
@@ -34,12 +18,14 @@ const LeetCode: React.FC<{
3418
getProfile()
3519
.then(profile => {
3620
if (!profile.isSignedIn) {
37-
alert('Please sign in to LeetCode.');
21+
Notification.error('Please sign in to LeetCode.', {
22+
autoClose: 3000
23+
});
3824
return;
3925
}
4026
setProfile(profile);
4127
})
42-
.catch(console.error);
28+
.catch(e => Notification.error(e.message, { autoClose: 3000 }));
4329
}, []);
4430

4531
const openNoteBook = (path: string) => {

src/components/LeetCodeMainArea.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { JupyterFrontEnd } from '@jupyterlab/application';
44
import LandingPage from './LandingPage';
55
import LeetCode from './LeetCode';
66
import { getCookie } from '../services/cookie';
7+
import { Notification } from '@jupyterlab/apputils';
78

89
const LeetCodeMainArea: React.FC<{
910
app: JupyterFrontEnd;
@@ -23,7 +24,7 @@ const LeetCodeMainArea: React.FC<{
2324
setCookieLoggedIn(leetcode_browser);
2425
}
2526
})
26-
.catch(console.error);
27+
.catch(e => Notification.error(e.message, { autoClose: 3000 }));
2728
}
2829
});
2930

src/components/LeetCodeNotebookToolbar.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2+
import { Notification } from '@jupyterlab/apputils';
23
import { NotebookPanel, NotebookActions } from '@jupyterlab/notebook';
34
import { ToolbarButtonComponent } from '@jupyterlab/ui-components';
45
import { ICellModel } from '@jupyterlab/cells';
@@ -48,14 +49,14 @@ const LeetCodeNotebookToolbar: React.FC<{ notebook: NotebookPanel }> = ({
4849
.then(({ submission_id }) => {
4950
setSubmissionId(submission_id);
5051
})
51-
.catch(console.error);
52+
.catch(e => Notification.error(e.message, { autoClose: 3000 }));
5253
});
5354
};
5455

5556
const makeWs = (submissionId: number) => {
5657
const ws = makeWebSocket(`submit?submission_id=${submissionId}`);
5758
ws.onmessage = event => {
58-
console.log('WebSocket message received:', event.data);
59+
console.debug('WebSocket message received:', event.data);
5960
const data = JSON.parse(event.data) as LeetCodeWebSocketMessage;
6061
if (data.submissionId !== submissionId) {
6162
return;
@@ -66,7 +67,7 @@ const LeetCodeNotebookToolbar: React.FC<{ notebook: NotebookPanel }> = ({
6667
break;
6768
}
6869
case 'error': {
69-
console.error('Error from WebSocket:', data.error);
70+
Notification.error(data.error, { autoClose: 3000 });
7071
break;
7172
}
7273
}

src/components/QuestionItem.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { Notification } from '@jupyterlab/apputils';
23
import { LeetCodeQuestion } from '../types/leetcode';
34
import { generateNotebook } from '../services/notebook';
45

@@ -20,7 +21,7 @@ const QuestionItem: React.FC<{
2021
.then(({ filePath }) => {
2122
onGenerateSuccess(filePath);
2223
})
23-
.catch(console.error);
24+
.catch(e => Notification.error(e.message, { autoClose: 3000 }));
2425
}}
2526
>
2627
C

src/components/QuestionList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2+
import { Notification } from '@jupyterlab/apputils';
23
import { listQuestions } from '../services/leetcode';
34
import { LeetCodeQuestion } from '../types/leetcode';
45
import QuestionItem from './QuestionItem';
@@ -30,7 +31,7 @@ const QuestionList: React.FC<{ openNotebook: (p: string) => void }> = ({
3031
setFinishedLength(fetchedFinishedLength);
3132
setTotalLength(fetchedTotalLength);
3233
})
33-
.catch(console.error);
34+
.catch(e => Notification.error(e.message, { autoClose: 3000 }));
3435
}, [keyword, skip]);
3536

3637
return (

src/components/Statistics.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from 'react';
2+
import { Notification } from '@jupyterlab/apputils';
23
import { getStatistics } from '../services/leetcode';
34
import { LeetCodeStatistics } from '../types/leetcode';
45

@@ -10,7 +11,7 @@ const Statistics: React.FC<{ username: string }> = ({ username }) => {
1011
.then(d => {
1112
setStatistics(d);
1213
})
13-
.catch(console.error);
14+
.catch(e => Notification.error(e.message, { autoClose: 3000 }));
1415
}, []);
1516

1617
return statistics ? (

0 commit comments

Comments
 (0)