@@ -14,12 +14,6 @@ import style from './style.module.scss';
1414function Mom ( ) {
1515 const { selectedMom } = useSelectedMomContext ( ) ;
1616 const { momSocket : socket } = useSocketContext ( ) ;
17-
18- const initMom = ( ) => {
19- if ( ! selectedMom ) return ;
20- socket . emit ( MOM_EVENT . INIT , selectedMom . _id ) ;
21- } ;
22-
2317 const {
2418 syncCRDT,
2519 spreadCRDT,
@@ -30,6 +24,17 @@ function Mom() {
3024 } = useCRDT ( ) ;
3125
3226 const titleRef = useRef < HTMLHeadingElement > ( null ) ;
27+ const blockRefs = useRef < React . RefObject < HTMLElement > [ ] > ( [ ] ) ;
28+ const focusIndex = useRef < number > ( ) ;
29+
30+ const [ blocks , setBlocks ] = useState < string [ ] > ( [ ] ) ;
31+ const [ isLoaded , setIsLoaded ] = useState < boolean > ( false ) ;
32+ const [ isMomsEmpty , setIsMomsEmpty ] = useState ( false ) ;
33+
34+ const initMom = ( ) => {
35+ if ( ! selectedMom ) return ;
36+ socket . emit ( MOM_EVENT . INIT , selectedMom . _id ) ;
37+ } ;
3338
3439 const onTitleUpdate : React . FormEventHandler < HTMLHeadingElement > = useDebounce (
3540 ( ) => {
@@ -43,11 +48,6 @@ function Mom() {
4348 500 ,
4449 ) ;
4550
46- const [ blocks , setBlocks ] = useState < string [ ] > ( [ ] ) ;
47-
48- const blockRefs = useRef < React . RefObject < HTMLElement > [ ] > ( [ ] ) ;
49- const focusIndex = useRef < number > ( ) ;
50-
5151 const updateBlockFocus = ( idx : number | undefined ) => {
5252 focusIndex . current = idx ;
5353 } ;
@@ -216,43 +216,63 @@ function Mom() {
216216 } ;
217217 } , [ selectedMom ] ) ;
218218
219+ useEffect ( ( ) => {
220+ ee . on ( MOM_EVENT . LOADED , ( momsLength : number ) => {
221+ setIsLoaded ( true ) ;
222+ setIsMomsEmpty ( momsLength === 0 ) ;
223+ } ) ;
224+
225+ ee . emit ( MOM_EVENT . REQUEST_LOADED ) ;
226+
227+ return ( ) => {
228+ ee . off ( MOM_EVENT . LOADED ) ;
229+ } ;
230+ } , [ socket ] ) ;
231+
219232 const registerRef =
220233 ( index : number ) => ( ref : React . RefObject < HTMLElement > ) => {
221234 blockRefs . current [ index ] = ref ;
222235 setBlockFocus ( index ) ;
223236 } ;
224237
225- return selectedMom ? (
226- < div className = { style [ 'mom-container' ] } >
227- < div className = { style [ 'mom' ] } >
228- < div className = { style [ 'mom-header' ] } >
229- < h1
230- ref = { titleRef }
231- contentEditable = { true }
232- suppressContentEditableWarning = { true }
233- onInput = { onTitleUpdate }
234- >
235- { selectedMom . title }
236- </ h1 >
237- < span > { new Date ( selectedMom . createdAt ) . toLocaleString ( ) } </ span >
238- </ div >
238+ if ( ! isLoaded ) {
239+ return < div className = { style [ 'mom-container' ] } > </ div > ;
240+ }
241+
242+ if ( isMomsEmpty ) {
243+ return < DefaultMom /> ;
244+ }
239245
240- < div className = { style [ 'mom-body' ] } >
241- { blocks . map ( ( id , index ) => (
242- < Block
243- key = { id }
244- id = { id }
245- index = { index }
246- createBlock = { createBlock }
247- onHandleBlocks = { onHandleBlocks }
248- registerRef = { registerRef ( index ) }
249- />
250- ) ) }
246+ return (
247+ < div className = { style [ 'mom-container' ] } >
248+ { selectedMom && (
249+ < div className = { style [ 'mom' ] } >
250+ < div className = { style [ 'mom-header' ] } >
251+ < h1
252+ ref = { titleRef }
253+ contentEditable = { true }
254+ suppressContentEditableWarning = { true }
255+ onInput = { onTitleUpdate }
256+ >
257+ { selectedMom . title }
258+ </ h1 >
259+ < span > { new Date ( selectedMom . createdAt ) . toLocaleString ( ) } </ span >
260+ </ div >
261+ < div className = { style [ 'mom-body' ] } >
262+ { blocks . map ( ( id , index ) => (
263+ < Block
264+ key = { id }
265+ id = { id }
266+ index = { index }
267+ createBlock = { createBlock }
268+ onHandleBlocks = { onHandleBlocks }
269+ registerRef = { registerRef ( index ) }
270+ />
271+ ) ) }
272+ </ div >
251273 </ div >
252- </ div >
274+ ) }
253275 </ div >
254- ) : (
255- < DefaultMom />
256276 ) ;
257277}
258278
0 commit comments