1- import { DataFrame , stringify } from 'hightable'
2- import { ReactNode , useEffect , useState } from 'react'
1+ import type { DataFrame , ResolvedValue } from 'hightable'
2+ import { stringify } from 'hightable'
3+ import { ReactNode , useCallback , useEffect , useState } from 'react'
34import { useConfig } from '../../hooks/useConfig.js'
45import { cn } from '../../lib/utils.js'
56import ContentWrapper from '../ContentWrapper/ContentWrapper.js'
@@ -13,7 +14,7 @@ interface ViewerProps {
1314 row : number
1415 col : number
1516 setProgress : ( progress : number ) => void
16- setError : ( error : Error ) => void
17+ setError : ( error : unknown ) => void
1718 onClose : ( ) => void
1819}
1920
@@ -26,6 +27,31 @@ export default function CellPanel({ df, row, col, setProgress, setError, onClose
2627 const [ content , setContent ] = useState < ReactNode > ( )
2728 const { customClass } = useConfig ( )
2829
30+ const fillContent = useCallback ( ( cell : ResolvedValue < unknown > | undefined ) => {
31+ let content : ReactNode
32+ if ( cell === undefined ) {
33+ content =
34+ < code className = { cn ( jsonStyles . textView , customClass ?. textView ) } >
35+ { UNLOADED_CELL_PLACEHOLDER }
36+ </ code >
37+ } else {
38+ const { value } = cell
39+ if ( value instanceof Object && ! ( value instanceof Date ) ) {
40+ content =
41+ < code className = { cn ( jsonStyles . jsonView , customClass ?. jsonView ) } >
42+ < Json json = { value } />
43+ </ code >
44+ } else {
45+ content =
46+ < code className = { cn ( styles . textView , customClass ?. textView ) } >
47+ { stringify ( value ) }
48+ </ code >
49+ }
50+ }
51+ setContent ( content )
52+ setError ( undefined )
53+ } , [ customClass ?. textView , customClass ?. jsonView , setError ] )
54+
2955 // Load cell data
3056 useEffect ( ( ) => {
3157 async function loadCellData ( ) {
@@ -36,31 +62,17 @@ export default function CellPanel({ df, row, col, setProgress, setError, onClose
3662 if ( columnName === undefined ) {
3763 throw new Error ( `Column name missing at index col=${ col } ` )
3864 }
39- await df . fetch ( { rowStart : row , rowEnd : row + 1 , columns : [ columnName ] } )
40- const cell = df . getCell ( { row, column : columnName } )
65+ let cell = df . getCell ( { row, column : columnName } )
4166 if ( cell === undefined ) {
42- setContent (
43- < code className = { cn ( jsonStyles . textView , customClass ?. textView ) } >
44- { /* TODO(SL) maybe change the style to highlight it's not real content */ }
45- { UNLOADED_CELL_PLACEHOLDER }
46- </ code >
47- )
67+ fillContent ( undefined )
4868 return
4969 }
50- const value : unknown = await cell . value
51- if ( value instanceof Object && ! ( value instanceof Date ) ) {
52- setContent (
53- < code className = { cn ( jsonStyles . jsonView , customClass ?. jsonView ) } >
54- < Json json = { value } />
55- </ code >
56- )
57- } else {
58- setContent (
59- < code className = { cn ( styles . textView , customClass ?. textView ) } >
60- { stringify ( value ) }
61- </ code >
62- )
70+ await df . fetch ( { rowStart : row , rowEnd : row + 1 , columns : [ columnName ] } )
71+ cell = df . getCell ( { row, column : columnName } )
72+ if ( cell === undefined ) {
73+ throw new Error ( `Cell at row=${ row } , column=${ columnName } is undefined` )
6374 }
75+ fillContent ( cell )
6476 } catch ( error ) {
6577 setError ( error as Error )
6678 } finally {
@@ -69,7 +81,7 @@ export default function CellPanel({ df, row, col, setProgress, setError, onClose
6981 }
7082
7183 void loadCellData ( )
72- } , [ df , col , row , setProgress , setError , customClass ] )
84+ } , [ df , col , row , setProgress , setError , fillContent ] )
7385
7486 const headers = < >
7587 < SlideCloseButton onClick = { onClose } />
0 commit comments