1- import React from "react" ;
2- import { useRenderData } from "../../hooks/index.js" ;
3- import {
4- Cell as LGCell ,
5- HeaderCell as LGHeaderCell ,
6- HeaderRow ,
7- Row as LGRow ,
8- Table ,
9- TableBody ,
10- TableHead ,
11- } from "@leafygreen-ui/table" ;
12- import { tableStyles } from "./ListDatabases.styles.js" ;
1+ import { type ReactElement } from "react" ;
2+ import { useDarkMode , useRenderData } from "../../hooks/index.js" ;
3+ import { Cell , HeaderCell , HeaderRow , Row , Table , TableBody , TableHead } from "@leafygreen-ui/table" ;
4+ import { Body } from "@leafygreen-ui/typography" ;
135import type { ListDatabasesOutput } from "../../../tools/mongodb/metadata/listDatabases.js" ;
6+ import { AmountTextStyles , getContainerStyles } from "./ListDatabases.styles.js" ;
147
15- const HeaderCell = LGHeaderCell as React . FC < React . ComponentPropsWithoutRef < "th" > > ;
16- const Cell = LGCell as React . FC < React . ComponentPropsWithoutRef < "td" > > ;
17- const Row = LGRow as React . FC < React . ComponentPropsWithoutRef < "tr" > > ;
8+ export type Database = ListDatabasesOutput [ "databases" ] [ number ] ;
9+
10+ interface ListDatabasesProps {
11+ databases ?: Database [ ] ;
12+ darkMode ?: boolean ;
13+ }
1814
1915function formatBytes ( bytes : number ) : string {
2016 if ( bytes === 0 ) return "0 Bytes" ;
@@ -26,37 +22,49 @@ function formatBytes(bytes: number): string {
2622 return Math . round ( ( bytes / Math . pow ( k , i ) ) * 100 ) / 100 + " " + sizes [ i ] ;
2723}
2824
29- export const ListDatabases = ( ) : React . ReactElement | null => {
30- const { data, isLoading, error } = useRenderData < ListDatabasesOutput > ( ) ;
25+ export const ListDatabases = ( {
26+ databases : propDatabases ,
27+ darkMode : darkModeProp ,
28+ } : ListDatabasesProps ) : ReactElement | null => {
29+ const darkMode = useDarkMode ( darkModeProp ) ;
30+ const { data : hookData , isLoading, error } = useRenderData < ListDatabasesOutput > ( ) ;
31+ const databases = propDatabases ?? hookData ?. databases ;
3132
32- if ( isLoading ) {
33- return < div > Loading...</ div > ;
34- }
33+ if ( ! propDatabases ) {
34+ if ( isLoading ) {
35+ return < div > Loading...</ div > ;
36+ }
3537
36- if ( error ) {
37- return < div > Error: { error } </ div > ;
38+ if ( error ) {
39+ return < div > Error: { error } </ div > ;
40+ }
3841 }
3942
40- if ( ! data ) {
43+ if ( ! databases ) {
4144 return null ;
4245 }
4346
4447 return (
45- < Table className = { tableStyles } >
46- < TableHead >
47- < HeaderRow >
48- < HeaderCell > DB Name</ HeaderCell >
49- < HeaderCell > DB Size</ HeaderCell >
50- </ HeaderRow >
51- </ TableHead >
52- < TableBody >
53- { data . databases . map ( ( db ) => (
54- < Row key = { db . name } >
55- < Cell > { db . name } </ Cell >
56- < Cell > { formatBytes ( db . size ) } </ Cell >
57- </ Row >
58- ) ) }
59- </ TableBody >
60- </ Table >
48+ < div className = { getContainerStyles ( darkMode ) } >
49+ < Body className = { AmountTextStyles } darkMode = { darkMode } >
50+ Your cluster has < strong > { databases . length } databases</ strong > :
51+ </ Body >
52+ < Table darkMode = { darkMode } >
53+ < TableHead >
54+ < HeaderRow >
55+ < HeaderCell > Database</ HeaderCell >
56+ < HeaderCell > Size</ HeaderCell >
57+ </ HeaderRow >
58+ </ TableHead >
59+ < TableBody >
60+ { databases . map ( ( db ) => (
61+ < Row key = { db . name } >
62+ < Cell > { db . name } </ Cell >
63+ < Cell > { formatBytes ( db . size ) } </ Cell >
64+ </ Row >
65+ ) ) }
66+ </ TableBody >
67+ </ Table >
68+ </ div >
6169 ) ;
6270} ;
0 commit comments