@@ -50,8 +50,8 @@ export default function Chat({ user }: { user: User }) {
5050 const [ allUsers , setAllUsers ] = useState < ChatUser [ ] > ( [ ] ) ;
5151 const channelRef = useRef < RealtimeChannel > ( null ) ;
5252 const textareaRef = useRef < HTMLTextAreaElement > ( null ) ;
53- const [ creatingConversation , setCreatingConversation ] = useState ( false ) ;
5453 const bottomRef = useRef < HTMLDivElement | null > ( null ) ;
54+ const creatingRef = useRef ( false ) ;
5555
5656 useEffect ( ( ) => {
5757 if ( textareaRef . current ) {
@@ -124,6 +124,16 @@ export default function Chat({ user }: { user: User }) {
124124 . then ( ( { data } ) => {
125125 if ( ! data || data . length === 0 ) return ;
126126 const convo = data [ 0 ] ;
127+ // Double check the user is part of the conversation (should always be true)
128+ if (
129+ ! convo . users . some (
130+ ( u : { user_id : string } ) => u . user_id === user . id ,
131+ )
132+ )
133+ return ;
134+ // Check if we already have this conversation in state
135+ if ( conversations . some ( ( c ) => c . id === convo . id ) ) return ;
136+
127137 setConversations ( ( prev ) => [
128138 ...prev ,
129139 {
@@ -141,7 +151,7 @@ export default function Chat({ user }: { user: User }) {
141151 return ( ) => {
142152 channel . unsubscribe ( ) ;
143153 } ;
144- } , [ user . id ] ) ;
154+ } , [ user . id , conversations ] ) ;
145155
146156 useEffect ( ( ) => {
147157 if ( ! conversationId ) return ;
@@ -223,9 +233,9 @@ export default function Chat({ user }: { user: User }) {
223233 } , [ showModal , user . id ] ) ;
224234
225235 const createConversation = async ( otherUser : ChatUser ) => {
226- if ( creatingConversation ) return ;
236+ if ( creatingRef . current ) return ;
237+ creatingRef . current = true ;
227238
228- setCreatingConversation ( true ) ;
229239 const existing = conversations . find ( ( conv ) =>
230240 conv . users . some ( ( u ) => u . id === otherUser . user_id ) ,
231241 ) ;
@@ -274,8 +284,9 @@ export default function Chat({ user }: { user: User }) {
274284 ] ,
275285 } ,
276286 ] ) ;
277- setCreatingConversation ( false ) ;
287+
278288 setShowModal ( false ) ;
289+ creatingRef . current = false ;
279290 } ;
280291
281292 const sendMessage = async ( ) => {
@@ -296,22 +307,25 @@ export default function Chat({ user }: { user: User }) {
296307
297308 return (
298309 < div className = "flex flex-col h-screen" >
299- < div className = "p-4 flex gap-4 overflow-x-auto border-b border-neutral-700" >
310+ < div className = "px-3 pt-3 flex border-neutral-700" >
300311 < button
301312 onClick = { ( ) => setShowModal ( true ) }
302313 className = "flex flex-col items-center min-w-15"
303314 >
304- < div className = "w-12 h-12 rounded-full bg-indigo-500 flex items-center justify-center" >
315+ < div className = "w-10 h-10 rounded-full bg-indigo-500 flex items-center justify-center" >
305316 < FontAwesomeIcon icon = { faPlus } className = "text-white" />
306317 </ div >
307318 < span className = "text-xs mt-1" > New</ span >
308319 </ button >
309320
310- < Conversations
311- conversations = { conversations }
312- user = { user }
313- setConversationId = { setConversationId }
314- />
321+ < div className = "flex-1 flex gap-4 overflow-x-auto" >
322+ < Conversations
323+ conversations = { conversations }
324+ user = { user }
325+ conversationId = { conversationId }
326+ setConversationId = { setConversationId }
327+ />
328+ </ div >
315329 </ div >
316330
317331 { conversationId ? (
0 commit comments