-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
채팅 페이지 api 통합
- Loading branch information
Showing
20 changed files
with
325 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,30 @@ | ||
import Chat, { ChatMessageProps } from '@_components/Chat/Chat'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import Chat from '@_components/Chat/Chat'; | ||
import { Chat as ChatType } from '@_types/index'; | ||
import { list } from './ChatList.style'; | ||
import { useTheme } from '@emotion/react'; | ||
|
||
interface ChatListProps { | ||
chats: ChatMessageProps[]; | ||
chats: ChatType[]; | ||
} | ||
|
||
export default function ChatList(props: ChatListProps) { | ||
const { chats } = props; | ||
const [lastChats, setLastChats] = useState<ChatMessageProps[]>([]); | ||
const messageEndRef = useRef<HTMLDivElement | null>(null); | ||
const observer = useRef<IntersectionObserver | null>(); | ||
const theme = useTheme(); | ||
|
||
useEffect(() => { | ||
observer.current = new IntersectionObserver(() => { | ||
if (lastChats.length === chats.length) return; | ||
if (messageEndRef) messageEndRef.current?.focus(); | ||
}); | ||
if (messageEndRef.current) observer.current.observe(messageEndRef.current); | ||
}, [messageEndRef, lastChats, chats]); | ||
const theme = useTheme(); | ||
|
||
useEffect(() => { | ||
if (lastChats.length === chats.length) return; | ||
const latestMyMessageIndex = chats.findLastIndex( | ||
(chat) => chat.isMyMessage, | ||
); | ||
if (latestMyMessageIndex >= lastChats.length) | ||
messageEndRef?.current?.focus(); | ||
|
||
setLastChats(chats); | ||
}, [lastChats, chats]); | ||
messageEndRef.current?.scrollIntoView(); | ||
}, [chats]); | ||
|
||
return ( | ||
<div css={list({ theme })}> | ||
{chats.map((chat) => ( | ||
<Chat key={chat.message + chat.time} {...chat} /> | ||
<Chat key={chat.chatId} chat={chat} /> | ||
))} | ||
<div ref={messageEndRef}></div> | ||
<div ref={messageEndRef} tabIndex={1}></div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.