11import { useEffect , useRef , useState } from 'react'
22import React from 'react'
33import { Spinner } from '../Layout.js'
4- import ContentHeader from './ContentHeader.js'
4+ import ContentHeader , { parseFileSize } from './ContentHeader.js'
55
66enum LoadingState {
77 NotLoaded ,
@@ -15,12 +15,17 @@ interface ViewerProps {
1515 setProgress : ( progress : number ) => void
1616}
1717
18+ interface Content {
19+ text : string
20+ fileSize ?: number
21+ }
22+
1823/**
1924 * Text viewer component.
2025 */
2126export default function TextView ( { file, setError } : ViewerProps ) {
2227 const [ loading , setLoading ] = useState ( LoadingState . NotLoaded )
23- const [ text , setText ] = useState < string | undefined > ( )
28+ const [ content , setContent ] = useState < Content > ( )
2429 const textRef = useRef < HTMLPreElement > ( null )
2530
2631 const isUrl = file . startsWith ( 'http://' ) || file . startsWith ( 'https://' )
@@ -32,7 +37,8 @@ export default function TextView({ file, setError }: ViewerProps) {
3237 try {
3338 const res = await fetch ( url )
3439 const text = await res . text ( )
35- setText ( text )
40+ const fileSize = parseFileSize ( res . headers ) ?? text . length
41+ setContent ( { text, fileSize } )
3642 } catch ( error ) {
3743 setError ( error as Error )
3844 } finally {
@@ -49,13 +55,13 @@ export default function TextView({ file, setError }: ViewerProps) {
4955 } , [ file , loading , setError ] )
5056
5157 const headers = < >
52- < span > { text ? newlines ( text ) : 0 } lines</ span >
58+ < span > { newlines ( content ?. text ?? "" ) } lines</ span >
5359 </ >
5460
5561 // Simple text viewer
56- return < ContentHeader content = { { fileSize : text ?. length } } headers = { headers } >
62+ return < ContentHeader content = { content } headers = { headers } >
5763 < code className = 'text' ref = { textRef } >
58- { text }
64+ { content ?. text }
5965 </ code >
6066
6167 { loading && < Spinner className = 'center' /> }
0 commit comments