@@ -11,6 +11,7 @@ import {
1111 useEffect ,
1212 setup_mathjax ,
1313 useState ,
14+ useErrorBoundary ,
1415} from "@plutojl/rainbow/ui" ;
1516import { type RendererContext } from "vscode-notebook-renderer" ;
1617
@@ -40,21 +41,23 @@ const cutMime = (s: { msg: string }, l = 88) => {
4041
4142export function PlutoOutput ( { state, context } : PlutoOutputProps ) {
4243 useMathjaxEffect ( ) ;
44+ const [ error , resetError ] = useErrorBoundary ( ) ;
45+
4346 const [ localState , setLocalState ] = useState ( state ) ;
4447 const [ progress , setProgress ] = useState < any > ( null ) ;
4548 const [ terminal , setTerminal ] = useState < any > ( null ) ;
4649 const [ logs , setLogs ] = useState < any > ( null ) ;
4750 useEffect ( ( ) => {
4851 // Listen for messages from the controller
4952 const d = context . onDidReceiveMessage ?.( ( message ) => {
50- if ( message . cell_id !== state . cell_id ) {
53+ if ( message . cell_id !== localState . cell_id ) {
5154 return ;
5255 }
5356 // Placeholder: Handle different message types from controller
5457 switch ( message . type ) {
5558 case "setState" : {
5659 const state = message . state as CellResultData ;
57- setLocalState ( state ) ;
60+ setLocalState ( { ... state } ) ;
5861
5962 const logs = state . logs . filter ( ( log ) => {
6063 return (
@@ -82,45 +85,51 @@ export function PlutoOutput({ state, context }: PlutoOutputProps) {
8285 } ) ;
8386 return ( ) => d ?. dispose ( ) ;
8487 } , [ state . cell_id , context ] ) ;
85-
86- return html `
87- ${
88- state . running && progress
89- ? html `< div >
90- < label for =${ `progress_${ state . cell_id } ` } > ${ progress } % </ label
91- > < progress
92- style ="width: 240px; "
93- id =${ `progress_${ state . cell_id } ` }
94- max ="100"
95- value=${ progress }
96- > </ progress >
97- </ div > `
98- : null
88+ if ( error ) {
89+ return html `< div onclick =${ resetError } >
90+ An error occured. Click < button onClick =${ resetError } > here</ button > to
91+ reset the view
92+ < details >
93+ < summary > View error</ summary >
94+ (Thank you for using a pre-release. This is on us. Please copy-paste
95+ this and send it our way! Sorry again!)
96+ < pre > ${ JSON . stringify ( error ) } </ pre >
97+ </ details >
98+ </ div > ` ;
9999 }
100- < ${ OutputBody }
101- persist _js_state ="${ true } "
100+ return html ` ${ localState . running && progress
101+ ? html `< div >
102+ < label for =${ `progress_${ localState . cell_id } ` } > ${ progress } % </ label
103+ > < progress
104+ style ="width: 240px; "
105+ id =${ `progress_${ localState . cell_id } ` }
106+ max ="100"
107+ value=${ progress }
108+ > </ progress >
109+ </ div > `
110+ : null }
111+ ${ localState . output ?. mime
112+ ? html `< ${ OutputBody }
113+ persist _js_state ="${ localState . output . persist_js_state } "
102114 body ="${ localState . output ?. body } "
103115 mime ="${ localState . output ?. mime } "
104116 sanitize_html ="${ false /* Maybe reconsider */ } "
105- > </ ${ OutputBody } >
106- ${
107- terminal ?. length
108- ? html `< details >
117+ > </ ${ OutputBody } > `
118+ : "Loading..." }
119+ ${ terminal ?. length
120+ ? html `< details >
109121 < summary > stdout</ summary >
110122 < ${ ANSITextOutput }
111123 body ="${ terminal . map ( cutMime ) . join ( "\n" ) } "
112124 > </ ${ ANSITextOutput } >
113125 </ details > `
114- : null
115- }
116- ${
117- logs ?. length
118- ? html `< details open >
126+ : null }
127+ ${ logs ?. length
128+ ? html `< details open >
119129 < summary > Logs</ summary >
120130 < ${ ANSITextOutput }
121131 body ="${ logs . map ( cutMime ) . join ( "\n" ) } "
122132 > </ ${ ANSITextOutput } >
123133 </ details > `
124- : null
125- } `;
134+ : null } `;
126135}
0 commit comments