Skip to content

Commit c68c4c5

Browse files
committed
extract Center as a new component, and move the loading spinner to ContentWrapper
1 parent c501fa4 commit c68c4c5

File tree

12 files changed

+43
-46
lines changed

12 files changed

+43
-46
lines changed

src/components/Center.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ReactNode } from 'react'
2+
import { useConfig } from '../hooks/useConfig.js'
3+
import { cn } from '../lib/utils.js'
4+
import styles from '../styles/Center.module.css'
5+
6+
export default function Center({ children }: {children?: ReactNode}) {
7+
const { customClass } = useConfig()
8+
return <div className={cn(styles.center, customClass?.center)}>{children}</div>
9+
}

src/components/Folder.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useConfig } from '../hooks/useConfig.js'
33
import type { DirSource, FileMetadata } from '../lib/sources/types.js'
44
import { cn, formatFileSize, getFileDate, getFileDateShort } from '../lib/utils.js'
55
import Breadcrumb from './Breadcrumb.js'
6+
import Center from './Center.js'
67
import Layout from './Layout.js'
78
import Spinner from './Spinner.js'
89

@@ -109,8 +110,8 @@ export default function Folder({ source }: FolderProps) {
109110
</li>
110111
)}
111112
</ul>}
112-
{files?.length === 0 && <div className='center'>No files</div>}
113-
{files === undefined && <div className='center'><Spinner /></div>}
113+
{files?.length === 0 && <Center>No files</Center>}
114+
{files === undefined && <Center><Spinner /></Center>}
114115
</Layout>
115116
}
116117

src/components/viewers/AvroView.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { FileSource } from '../../lib/sources/types.js'
44
import { parseFileSize } from '../../lib/utils.js'
55
import styles from '../../styles/Json.module.css'
66
import Json from '../Json.js'
7-
import Spinner from '../Spinner.js'
87
import ContentWrapper, { ContentSize } from './ContentWrapper.js'
98

109
interface ViewerProps {
@@ -54,11 +53,9 @@ export default function AvroView({ source, setError }: ViewerProps) {
5453

5554
const headers = content === undefined && <span>Loading...</span>
5655

57-
return <ContentWrapper content={content} headers={headers}>
56+
return <ContentWrapper content={content} headers={headers} isLoading={isLoading}>
5857
<code className={styles.jsonView}>
5958
<Json json={json} />
6059
</code>
61-
62-
{isLoading && <div className='center'><Spinner /></div>}
6360
</ContentWrapper>
6461
}

src/components/viewers/ContentWrapper.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { ReactNode } from 'react'
22
import { useConfig } from '../../hooks/useConfig.js'
33
import { cn, formatFileSize } from '../../lib/utils.js'
44
import styles from '../../styles/viewers/ContentWrapper.module.css'
5+
import Center from '../Center.js'
6+
import Spinner from '../Spinner.js'
57

68
export interface ContentSize {
79
fileSize?: number
@@ -14,10 +16,11 @@ export interface TextContent extends ContentSize {
1416
interface ContentWrapperProps {
1517
content?: ContentSize
1618
headers?: ReactNode
19+
isLoading?: boolean
1720
children?: ReactNode
1821
}
1922

20-
export default function ContentWrapper({ content, headers, children }: ContentWrapperProps) {
23+
export default function ContentWrapper({ content, headers, isLoading, children }: ContentWrapperProps) {
2124
const { customClass } = useConfig()
2225
return <div className={cn(styles.contentWrapper, customClass?.contentWrapper)}>
2326
<header>
@@ -26,6 +29,6 @@ export default function ContentWrapper({ content, headers, children }: ContentWr
2629
</span>}
2730
{headers}
2831
</header>
29-
{children}
32+
{isLoading ? <Center><Spinner /></Center> : children }
3033
</div>
3134
}

src/components/viewers/ImageView.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useConfig } from '../../hooks/useConfig.js'
33
import { FileSource } from '../../lib/sources/types.js'
44
import { cn, contentTypes, parseFileSize } from '../../lib/utils.js'
55
import styles from '../../styles/viewers/ImageView.module.css'
6-
import Spinner from '../Spinner.js'
76
import ContentWrapper from './ContentWrapper.js'
87

98
interface ViewerProps {
@@ -54,13 +53,11 @@ export default function ImageView({ source, setError }: ViewerProps) {
5453
void loadContent()
5554
}, [fileName, resolveUrl, requestInit, setError])
5655

57-
return <ContentWrapper content={content}>
56+
return <ContentWrapper content={content} isLoading={isLoading}>
5857
{content?.dataUri && <img
5958
alt={source.sourceId}
6059
className={cn(styles.image, customClass?.imageView)}
6160
src={content.dataUri} />}
62-
63-
{isLoading && <div className='center'><Spinner /></div>}
6461
</ContentWrapper>
6562
}
6663

src/components/viewers/JsonView.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { useEffect, useState } from 'react'
22
import type { FileSource } from '../../lib/sources/types.js'
33
import { parseFileSize } from '../../lib/utils.js'
44
import styles from '../../styles/Json.module.css'
5+
import Center from '../Center.js'
56
import Json from '../Json.js'
6-
import Spinner from '../Spinner.js'
77
import ContentWrapper, { TextContent } from './ContentWrapper.js'
88

99
interface ViewerProps {
@@ -63,19 +63,18 @@ export default function JsonView({ source, setError }: ViewerProps) {
6363
// If json failed to parse, show the text instead
6464
const showFallbackText = content?.text !== undefined && json === undefined
6565

66-
return <ContentWrapper content={content} headers={headers}>
67-
{!isLarge && <>
68-
{!showFallbackText && <code className={styles.jsonView}>
69-
<Json json={json} />
70-
</code>}
71-
{showFallbackText && <code className={styles.text}>
72-
{content.text}
73-
</code>}
74-
</>}
75-
{isLarge && <div className='center'>
76-
File is too large to display
77-
</div>}
78-
79-
{isLoading && <div className='center'><Spinner /></div>}
66+
return <ContentWrapper content={content} headers={headers} isLoading={isLoading}>
67+
{isLarge ?
68+
<Center>File is too large to display</Center>
69+
:
70+
<>
71+
{!showFallbackText && <code className={styles.jsonView}>
72+
<Json json={json} />
73+
</code>}
74+
{showFallbackText && <code className={styles.text}>
75+
{content.text}
76+
</code>}
77+
</>
78+
}
8079
</ContentWrapper>
8180
}

src/components/viewers/MarkdownView.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { FileSource } from '../../lib/sources/types.js'
44
import { cn, parseFileSize } from '../../lib/utils.js'
55
import styles from '../../styles/viewers/MarkdownView.module.css'
66
import Markdown from '../Markdown.js'
7-
import Spinner from '../Spinner.js'
87
import ContentWrapper, { TextContent } from './ContentWrapper.js'
98

109
interface ViewerProps {
@@ -47,9 +46,7 @@ export default function MarkdownView({ source, setError }: ViewerProps) {
4746
void loadContent()
4847
}, [resolveUrl, requestInit, setError])
4948

50-
return <ContentWrapper content={content}>
49+
return <ContentWrapper content={content} isLoading={isLoading}>
5150
<Markdown className={cn(styles.markdownView, customClass?.markdownView)} text={content?.text ?? ''} />
52-
53-
{ isLoading && <div className='center'><Spinner /></div> }
5451
</ContentWrapper>
5552
}

src/components/viewers/ParquetView.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { FileSource } from '../../lib/sources/types.js'
88
import { parquetDataFrame } from '../../lib/tableProvider.js'
99
import { cn } from '../../lib/utils.js'
1010
import styles from '../../styles/ParquetView.module.css'
11-
import Spinner from '../Spinner.js'
1211
import CellPanel from './CellPanel.js'
1312
import ContentWrapper, { ContentSize } from './ContentWrapper.js'
1413
import SlidePanel from './SlidePanel.js'
@@ -96,7 +95,7 @@ export default function ParquetView({ source, setProgress, setError }: ViewerPro
9695

9796
const headers = <span>{content?.dataframe.numRows.toLocaleString() ?? '...'} rows</span>
9897

99-
const mainContent = <ContentWrapper content={content} headers={headers}>
98+
const mainContent = <ContentWrapper content={content} headers={headers} isLoading={isLoading}>
10099
{content?.dataframe && <HighTable
101100
cacheKey={source.resolveUrl}
102101
data={content.dataframe}
@@ -105,8 +104,6 @@ export default function ParquetView({ source, setProgress, setError }: ViewerPro
105104
onError={setError}
106105
className={cn(styles.hightable, customClass?.highTable)}
107106
/>}
108-
109-
{isLoading && <div className='center'><Spinner /></div>}
110107
</ContentWrapper>
111108

112109
let panelContent

src/components/viewers/TextView.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useConfig } from '../../hooks/useConfig.js'
33
import { FileSource } from '../../lib/sources/types.js'
44
import { cn, parseFileSize } from '../../lib/utils.js'
55
import styles from '../../styles/viewers/TextView.module.css'
6-
import Spinner from '../Spinner.js'
76
import ContentWrapper, { TextContent } from './ContentWrapper.js'
87

98
interface ViewerProps {
@@ -51,12 +50,10 @@ export default function TextView({ source, setError }: ViewerProps) {
5150
</>
5251

5352
// Simple text viewer
54-
return <ContentWrapper content={content} headers={headers}>
53+
return <ContentWrapper content={content} headers={headers} isLoading={isLoading}>
5554
{content && <code className={cn(styles.text, customClass?.textView)}>
5655
{content.text}
5756
</code>}
58-
59-
{isLoading && <div className='center'><Spinner /></div>}
6057
</ContentWrapper>
6158
}
6259

src/hooks/useConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Config {
1111
customClass?: {
1212
brand?: string
1313
breadcrumb?: string
14+
center?: string
1415
contentWrapper?: string
1516
errorBar?: string
1617
highTable?: string

0 commit comments

Comments
 (0)