1- import React from 'react' ;
1+ import React , { useMemo } from 'react' ;
22import {
33 Icon ,
44 Body ,
@@ -7,14 +7,14 @@ import {
77 spacing ,
88 palette ,
99 InlineDefinition ,
10- Subtitle ,
1110 useDarkMode ,
11+ Button ,
12+ ButtonVariant ,
1213} from '@mongodb-js/compass-components' ;
13- import { SAMPLE_SIZE } from '../modules/sample-documents' ;
14- import {
15- InvalidDocumentPreview ,
16- ValidDocumentPreview ,
17- } from './document-preview' ;
14+ import { fetchSampleDocuments , SAMPLE_SIZE } from '../modules/sample-documents' ;
15+ import { DocumentPreview } from './document-preview' ;
16+ import { connect } from 'react-redux' ;
17+ import type { RootState } from '../modules' ;
1818
1919const SAMPLE_DEFINITION = `A sample is fetched from a sample-space of ${ SAMPLE_SIZE } randomly selected documents` ;
2020
@@ -66,15 +66,128 @@ const documentHeadingTextStyles = css({
6666 color : 'inherit' ,
6767} ) ;
6868
69- export function SampleDocuments ( ) {
69+ const initialStateContainerStyles = css ( {
70+ display : 'flex' ,
71+ flexDirection : 'column' ,
72+ alignItems : 'center' ,
73+ gap : spacing [ 200 ] ,
74+ padding : spacing [ 400 ] ,
75+ marginTop : spacing [ 400 ] ,
76+ border : `1px solid ${ palette . gray . light2 } ` ,
77+ borderRadius : spacing [ 400 ] ,
78+ } ) ;
79+
80+ const initialStateContainerDarkModeStyles = css ( {
81+ border : `1px solid ${ palette . gray . dark2 } ` ,
82+ } ) ;
83+
84+ const previewHeaderStyles = css ( {
85+ fontSize : spacing [ 400 ] ,
86+ fontWeight : 'bold' ,
87+ color : palette . green . dark2 ,
88+ } ) ;
89+
90+ const previewHeaderDarkModeStyles = css ( {
91+ color : palette . green . light2 ,
92+ } ) ;
93+
94+ const DocumentGraphic : React . FunctionComponent = ( ) => {
7095 const darkMode = useDarkMode ( ) ;
96+ const fillColor = useMemo (
97+ ( ) => ( darkMode ? palette . green . light1 : palette . green . base ) ,
98+ [ darkMode ]
99+ ) ;
100+ const strokeColor = useMemo (
101+ ( ) => ( darkMode ? palette . white : palette . black ) ,
102+ [ darkMode ]
103+ ) ;
104+
71105 return (
72- < div className = { sampleDocumentsSectionStyles } >
73- < Subtitle >
106+ < svg
107+ width = "64"
108+ height = "58"
109+ viewBox = "0 0 64 58"
110+ fill = "none"
111+ xmlns = "http://www.w3.org/2000/svg"
112+ >
113+ < rect x = "0.5" y = "1" width = "63" height = "56" rx = "2" stroke = { strokeColor } />
114+ < line y1 = "28.5" x2 = "64" y2 = "28.5" stroke = { strokeColor } />
115+ < line
116+ x1 = "8.61035"
117+ y1 = "15.5898"
118+ x2 = "32.9054"
119+ y2 = "15.5898"
120+ stroke = { strokeColor }
121+ />
122+ < circle
123+ cx = "44.9268"
124+ cy = "16.0898"
125+ r = "2"
126+ fill = { fillColor }
127+ stroke = { strokeColor }
128+ />
129+ < circle
130+ cx = "52.8896"
131+ cy = "16.0898"
132+ r = "2"
133+ fill = { fillColor }
134+ stroke = { strokeColor }
135+ />
136+ </ svg >
137+ ) ;
138+ } ;
139+
140+ const InitialState : React . FC < {
141+ onPreviewClick : ( ) => void ;
142+ } > = ( { onPreviewClick } ) => {
143+ const darkMode = useDarkMode ( ) ;
144+ return (
145+ < div
146+ className = { cx (
147+ initialStateContainerStyles ,
148+ darkMode && initialStateContainerDarkModeStyles
149+ ) }
150+ >
151+ < DocumentGraphic />
152+ < div
153+ className = { cx (
154+ previewHeaderStyles ,
155+ darkMode && previewHeaderDarkModeStyles
156+ ) }
157+ >
74158 < InlineDefinition definition = { SAMPLE_DEFINITION } >
75- Sample documents
159+ Preview your sample here
76160 </ InlineDefinition >
77- </ Subtitle >
161+ </ div >
162+ < div >
163+ This section displays one document that passed validation and one that
164+ failed validation.
165+ </ div >
166+ < Button onClick = { onPreviewClick } variant = { ButtonVariant . PrimaryOutline } >
167+ Preview documents
168+ </ Button >
169+ </ div >
170+ ) ;
171+ } ;
172+
173+ function SampleDocuments ( {
174+ validDocument,
175+ invalidDocument,
176+ isInitialState,
177+ fetchSampleDocuments,
178+ } : {
179+ validDocument ?: Record < string , unknown > ;
180+ invalidDocument ?: Record < string , unknown > ;
181+ isInitialState : boolean ;
182+ fetchSampleDocuments : ( ) => void ;
183+ } ) {
184+ const darkMode = useDarkMode ( ) ;
185+
186+ if ( isInitialState )
187+ return < InitialState onPreviewClick = { fetchSampleDocuments } /> ;
188+
189+ return (
190+ < div className = { sampleDocumentsSectionStyles } >
78191 < div className = { sampleDocumentsStyles } >
79192 { /* Documents that match the validation */ }
80193 < div className = { sampleDocumentStyles } data-testid = "matching-documents" >
@@ -87,7 +200,7 @@ export function SampleDocuments() {
87200 < Icon glyph = "CheckmarkWithCircle" size = "small" />
88201 < Body className = { documentHeadingTextStyles } > Passed validation</ Body >
89202 </ div >
90- < ValidDocumentPreview />
203+ < DocumentPreview document = { validDocument } />
91204 </ div >
92205
93206 { /* Documents that do not match the validation */ }
@@ -104,9 +217,22 @@ export function SampleDocuments() {
104217 < Icon glyph = "XWithCircle" size = "small" />
105218 < Body className = { documentHeadingTextStyles } > Failed validation</ Body >
106219 </ div >
107- < InvalidDocumentPreview />
220+ < DocumentPreview document = { invalidDocument } />
108221 </ div >
109222 </ div >
110223 </ div >
111224 ) ;
112225}
226+
227+ const ConnectedSampleDocuments = connect (
228+ ( state : RootState ) => ( {
229+ validDocument : state . sampleDocuments . validDocument ,
230+ invalidDocument : state . sampleDocuments . invalidDocument ,
231+ isInitialState : state . sampleDocuments . validDocumentState === 'initial' ,
232+ } ) ,
233+ {
234+ fetchSampleDocuments,
235+ }
236+ ) ( SampleDocuments ) ;
237+
238+ export { ConnectedSampleDocuments as SampleDocuments } ;
0 commit comments