1+ import { BiPlus } from '@react-icons/all-files/bi/BiPlus' ;
12import { BLOCK_EVENT } from '@wabinar/constants/socket-message' ;
23import ee from 'components/Mom/EventEmitter' ;
34import { memo , useEffect , useRef , useState } from 'react' ;
45import useSocketContext from 'src/hooks/context/useSocketContext' ;
56
67import QuestionBlock from './QuestionBlock' ;
8+ import style from './style.module.scss' ;
79import TextBlock from './TextBlock' ;
810import VoteBlock from './VoteBlock' ;
911
@@ -19,11 +21,18 @@ export enum BlockType {
1921interface BlockProps {
2022 id : string ;
2123 index : number ;
22- onHandleBlock : React . KeyboardEventHandler ;
24+ createBlock : ( arg : number ) => void ;
25+ onHandleBlocks : React . KeyboardEventHandler ;
2326 registerRef : ( arg : React . RefObject < HTMLElement > ) => void ;
2427}
2528
26- function Block ( { id, index, onHandleBlock, registerRef } : BlockProps ) {
29+ function Block ( {
30+ id,
31+ index,
32+ createBlock,
33+ onHandleBlocks,
34+ registerRef,
35+ } : BlockProps ) {
2736 const { momSocket : socket } = useSocketContext ( ) ;
2837
2938 const [ type , setType ] = useState < BlockType > ( ) ;
@@ -47,31 +56,46 @@ function Block({ id, index, onHandleBlock, registerRef }: BlockProps) {
4756 const setBlockType = ( type : BlockType ) => {
4857 localUpdateFlagRef . current = true ;
4958 setType ( type ) ;
59+
60+ if ( [ BlockType . VOTE , BlockType . QUESTION ] . includes ( type ) ) {
61+ createBlock ( index ) ;
62+ }
63+ } ;
64+
65+ const onCreate = ( ) => createBlock ( index ) ;
66+
67+ const getCurrentBlock = ( ) => {
68+ switch ( type ) {
69+ case BlockType . H1 :
70+ case BlockType . H2 :
71+ case BlockType . H3 :
72+ case BlockType . P :
73+ return (
74+ < TextBlock
75+ id = { id }
76+ index = { index }
77+ onHandleBlocks = { onHandleBlocks }
78+ type = { type }
79+ setType = { setBlockType }
80+ isLocalTypeUpdate = { localUpdateFlagRef . current }
81+ registerRef = { registerRef }
82+ />
83+ ) ;
84+ case BlockType . VOTE :
85+ return < VoteBlock id = { id } registerable = { localUpdateFlagRef . current } /> ;
86+ case BlockType . QUESTION :
87+ return < QuestionBlock id = { id } /> ;
88+ default :
89+ return < > </ > ;
90+ }
5091 } ;
5192
52- switch ( type ) {
53- case BlockType . H1 :
54- case BlockType . H2 :
55- case BlockType . H3 :
56- case BlockType . P :
57- return (
58- < TextBlock
59- id = { id }
60- index = { index }
61- onHandleBlock = { onHandleBlock }
62- type = { type }
63- setType = { setBlockType }
64- isLocalTypeUpdate = { localUpdateFlagRef . current }
65- registerRef = { registerRef }
66- />
67- ) ;
68- case BlockType . VOTE :
69- return < VoteBlock id = { id } registerable = { localUpdateFlagRef . current } /> ;
70- case BlockType . QUESTION :
71- return < QuestionBlock id = { id } /> ;
72- default :
73- return < p /> ;
74- }
93+ return (
94+ < div className = { style [ 'block' ] } >
95+ < BiPlus size = { 20 } onClick = { onCreate } />
96+ { getCurrentBlock ( ) }
97+ </ div >
98+ ) ;
7599}
76100
77101export default memo ( Block ) ;
0 commit comments