1
1
import React , { useEffect , useState , useRef , useCallback } from 'react'
2
2
import styled from 'styled-components'
3
3
import { useParams } from 'react-router-dom'
4
- import { useRecoilValue } from 'recoil'
4
+ import { useRecoilState , useRecoilValue } from 'recoil'
5
5
import ChatMessage from '../ChatMessage'
6
6
import { COLOR } from '../../constant/style'
7
7
import { getChatMessage } from '../../api/chat'
8
8
import MessageEditor from '../MessageEditor/MessageEditor'
9
- import { workspaceRecoil , socketRecoil } from '../../store'
9
+ import {
10
+ workspaceRecoil ,
11
+ socketRecoil ,
12
+ currentChannelInfoRecoil ,
13
+ } from '../../store'
10
14
import ChannelHeader from '../ChannelHeader'
11
15
import { isEmpty } from '../../util'
12
16
import { hasMyReaction , chageReactionState } from '../../util/reactionUpdate'
13
- import useChannelInfo from '../../hooks/useChannelInfo'
14
17
import Icon from '../../presenter/Icon'
15
18
import { ArrowDown } from '../../constant/icon'
19
+ import { getChannelHeaderInfo } from '../../api/channel'
20
+ import { SOCKET_EVENT } from '../../constant'
16
21
17
22
const ChatRoom = ( { width } ) => {
18
23
const viewport = useRef ( null )
@@ -23,7 +28,7 @@ const ChatRoom = ({ width }) => {
23
28
const isAllMessageFetched = useRef ( false )
24
29
const isReading = useRef ( false )
25
30
const workspaceUserInfo = useRecoilValue ( workspaceRecoil )
26
- const [ channelInfo ] = useChannelInfo ( )
31
+ const [ channelInfo , setChannelInfo ] = useRecoilState ( currentChannelInfoRecoil )
27
32
const { workspaceId, channelId } = useParams ( )
28
33
const params = useParams ( )
29
34
const socket = useRecoilValue ( socketRecoil )
@@ -52,6 +57,20 @@ const ChatRoom = ({ width }) => {
52
57
[ messages ] ,
53
58
)
54
59
60
+ const updateChannelInfo = useCallback ( async ( ) => {
61
+ if ( workspaceUserInfo && channelId )
62
+ setChannelInfo (
63
+ await getChannelHeaderInfo ( {
64
+ workspaceUserInfoId : workspaceUserInfo . _id ,
65
+ channelId,
66
+ } ) ,
67
+ )
68
+ } , [ channelId , workspaceUserInfo , setChannelInfo ] )
69
+
70
+ useEffect ( ( ) => {
71
+ updateChannelInfo ( )
72
+ } , [ channelId , workspaceUserInfo , updateChannelInfo ] )
73
+
55
74
useEffect ( ( ) => {
56
75
setMessages ( [ ] )
57
76
isLoading . current = false
@@ -74,7 +93,7 @@ const ChatRoom = ({ width }) => {
74
93
profileUrl : workspaceUserInfo . profileUrl ,
75
94
} ,
76
95
}
77
- socket . emit ( 'new message' , chat )
96
+ socket . emit ( SOCKET_EVENT . NEW_MESSAGE , chat )
78
97
}
79
98
80
99
useEffect ( ( ) => {
@@ -83,17 +102,18 @@ const ChatRoom = ({ width }) => {
83
102
84
103
useEffect ( ( ) => {
85
104
if ( socket ) {
86
- socket . on ( 'new message' , ( { message } ) => {
105
+ socket . on ( SOCKET_EVENT . NEW_MESSAGE , ( { message } ) => {
87
106
if ( message . channelId === channelId ) {
88
107
setMessages ( messages => [
89
108
...messages ,
90
109
...hasMyReaction ( [ message ] , workspaceUserInfo ) ,
91
110
] )
92
- if ( isReading . current && document . hasFocus ( ) ) {
111
+ if ( message . userInfo . _id === workspaceUserInfo . _id ) {
93
112
setHasUnreadMessage ( false )
94
113
scrollTo ( )
95
- } else if ( message . userInfo . _id !== workspaceUserInfo . _id )
114
+ } else if ( ! isReading . current && ! document . hasFocus ( ) ) {
96
115
setHasUnreadMessage ( true )
116
+ }
97
117
}
98
118
99
119
if ( document . hidden ) {
@@ -104,17 +124,30 @@ const ChatRoom = ({ width }) => {
104
124
105
125
if ( message . userInfo . _id === workspaceUserInfo . _id ) scrollTo ( )
106
126
} )
107
- socket . on ( 'update reaction' , ( { reaction } ) => {
108
- setMessages ( messages => chageReactionState ( messages , reaction ) )
127
+ socket . on ( SOCKET_EVENT . NEW_REPLY , ( { message } ) => {
128
+ setMessages ( messages =>
129
+ messages . map ( target =>
130
+ target . _id === message . parentId
131
+ ? { ...target , reply : [ ...target . reply , message ] }
132
+ : target ,
133
+ ) ,
134
+ )
135
+ } )
136
+ socket . on ( SOCKET_EVENT . UPDAETE_REACTION , ( { reaction } ) => {
137
+ setMessages ( messages =>
138
+ chageReactionState ( messages , reaction , workspaceUserInfo ) ,
139
+ )
109
140
} )
110
141
}
111
142
return ( ) => {
112
143
if ( socket ) {
113
- socket . off ( 'new message' )
114
- socket . off ( 'update reaction' )
144
+ socket . off ( SOCKET_EVENT . NEW_REPLY )
145
+ socket . off ( SOCKET_EVENT . NEW_MESSAGE )
146
+ socket . off ( SOCKET_EVENT . UPDAETE_REACTION )
115
147
}
116
148
}
117
149
} , [ socket , channelId , document . hidden , params ] )
150
+
118
151
useEffect ( ( ) => {
119
152
const handleIntersection = ( entries , observer ) => {
120
153
entries . forEach ( entry => {
@@ -225,7 +258,6 @@ const UnreadMessage = styled.div`
225
258
background-color: ${ COLOR . STARBLUE } ;
226
259
color: ${ COLOR . WHITE } ;
227
260
width: 170px;
228
- height: 50px;
229
261
margin-left: auto;
230
262
margin-right: auto;
231
263
position: sticky;
0 commit comments