1
1
import React , { useEffect , useState } from 'react' ;
2
- import { NotebookPanel } from '@jupyterlab/notebook' ;
2
+ import { NotebookPanel , NotebookActions } from '@jupyterlab/notebook' ;
3
+ import { ICellModel } from '@jupyterlab/cells' ;
3
4
import { submitNotebook } from '../services/notebook' ;
4
5
import { makeWebSocket } from '../services/handler' ;
5
6
import {
@@ -48,6 +49,61 @@ const LeetCodeNotebookHeader: React.FC<{ notebook: NotebookPanel }> = ({
48
49
return ws ;
49
50
} ;
50
51
52
+ const getResultCell = ( ) => {
53
+ let resultCellModel : ICellModel | null = null ;
54
+ const cells = notebook . content . model ?. cells ?? [ ] ;
55
+ for ( const cell of cells ) {
56
+ if ( cell . metadata [ 'id' ] === 'result' ) {
57
+ resultCellModel = cell ;
58
+ }
59
+ }
60
+ if ( ! resultCellModel ) {
61
+ const activeCellIdx = cells . length ? cells . length - 1 : 0 ;
62
+ notebook . content . activeCellIndex = activeCellIdx ;
63
+ NotebookActions . insertBelow ( notebook . content ) ;
64
+ const activeCell = notebook . content . activeCell ;
65
+ if ( activeCell ) {
66
+ resultCellModel = activeCell . model ;
67
+ resultCellModel . setMetadata ( 'id' , 'result' ) ;
68
+ }
69
+ }
70
+ return resultCellModel ;
71
+ } ;
72
+
73
+ const populateResultCell = (
74
+ cellModel : ICellModel ,
75
+ result : Extract < LeetCodeSubmissionResult , { state : 'SUCCESS' } >
76
+ ) => {
77
+ let source = '' ;
78
+ switch ( result . status_msg ) {
79
+ case 'Accepted' : {
80
+ source = `# Accepted\n\nRuntime: ${ result . display_runtime } \nMemory: ${ result . status_memory } \n\nOutput:\n\`\`\`\n${ result . code_output } \n\`\`\`` ;
81
+ break ;
82
+ }
83
+ case 'Wrong Answer' : {
84
+ source = `# Wrong Answer\n\nExpected: ${ result . expected_output } \nGot: ${ result . code_output } \n\nInput:\n\`\`\`\n${ result . input_formatted } \n\`\`\`` ;
85
+ break ;
86
+ }
87
+ case 'Time Limit Exceeded' : {
88
+ source = `# Time Limit Exceeded\n\nRuntime: ${ result . display_runtime } \nMemory: ${ result . status_memory } \n\nInput:\n\`\`\`\n${ result . input_formatted } \n\`\`\`` ;
89
+ break ;
90
+ }
91
+ case 'Memory Limit Exceeded' : {
92
+ source = `# Memory Limit Exceeded\n\nRuntime: ${ result . display_runtime } \nMemory: ${ result . status_memory } \n\nInput:\n\`\`\`\n${ result . input_formatted } \n\`\`\`` ;
93
+ break ;
94
+ }
95
+ case 'Runtime Error' : {
96
+ source = `# Runtime Error\n\nRuntime: ${ result . display_runtime } \nMemory: ${ result . status_memory } \n\nOutput:\n\`\`\`\n${ result . code_output } \n\`\`\`\n\nInput:\n\`\`\`\n${ result . input_formatted } \n\`\`\`` ;
97
+ break ;
98
+ }
99
+ case 'Internal Error' : {
100
+ source = `# Internal Error\n\nRuntime: ${ result . display_runtime } \nMemory: ${ result . status_memory } \n\nOutput:\n\`\`\`\n${ result . code_output } \n\`\`\`\n\nInput:\n\`\`\`\n${ result . input_formatted } \n\`\`\`` ;
101
+ break ;
102
+ }
103
+ }
104
+ cellModel . sharedModel . setSource ( source ) ;
105
+ } ;
106
+
51
107
useEffect ( ( ) => {
52
108
if ( ! submissionId ) {
53
109
return ;
@@ -70,6 +126,18 @@ const LeetCodeNotebookHeader: React.FC<{ notebook: NotebookPanel }> = ({
70
126
}
71
127
} , [ ws , ws ?. readyState ] ) ;
72
128
129
+ useEffect ( ( ) => {
130
+ if ( result ?. state !== 'SUCCESS' ) {
131
+ return ;
132
+ }
133
+ const resultCellModel = getResultCell ( ) ;
134
+ if ( resultCellModel ) {
135
+ populateResultCell ( resultCellModel , result ) ;
136
+ NotebookActions . changeCellType ( notebook . content , 'markdown' ) ;
137
+ NotebookActions . run ( notebook . content ) ;
138
+ }
139
+ } , [ result ?. state ] ) ;
140
+
73
141
return (
74
142
< div >
75
143
< button onClick = { submit } > Submit</ button >
0 commit comments