-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from prgrms-web-devcourse/BM-375
[BM-375] 채팅방 채팅 메세지 위에 해당 날짜들 표시하기
- Loading branch information
Showing
5 changed files
with
86 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
import { ChatMeesageResponseType, ChatMessageData } from 'types/chatMessages'; | ||
import chatDateFormat from 'utils/format/chatDateFormat'; | ||
|
||
export type ChatMessagesType = { | ||
chatDate: string; | ||
messages: ChatMeesageResponseType; | ||
}; | ||
|
||
const useChatMessages = () => { | ||
const map = new Map(); | ||
const [chatMessages, setChatMessages] = useState<ChatMessagesType[]>([]); | ||
|
||
const setPrevMessagesFromApi = useCallback( | ||
(messages: ChatMeesageResponseType) => { | ||
messages.forEach((message) => { | ||
const date = chatDateFormat(new Date(message.createdAt)); | ||
const prevMessages = map.get(date) || []; | ||
|
||
map.set(date, [...prevMessages, message]); | ||
}); | ||
|
||
setChatMessages( | ||
[...map].map(([chatDate, messages]) => ({ chatDate, messages })) | ||
); | ||
}, | ||
[] | ||
); | ||
|
||
const addMessage = useCallback((message: ChatMessageData) => { | ||
const today = chatDateFormat(new Date()); | ||
const prevMessages = map.get(today) || []; | ||
|
||
map.set(today, [...prevMessages, message]); | ||
|
||
setChatMessages( | ||
[...map].map(([chatDate, messages]) => ({ chatDate, messages })) | ||
); | ||
}, []); | ||
|
||
return { | ||
chatMessages, | ||
setPrevMessagesFromApi, | ||
addMessage, | ||
}; | ||
}; | ||
|
||
export default useChatMessages; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { format } from 'date-fns'; | ||
import { ko } from 'date-fns/locale'; | ||
|
||
const chatDateFormat = (createdAt: Date) => | ||
format(new Date(createdAt), 'yyyy년 M월 d일', { locale: ko }); | ||
|
||
export default chatDateFormat; |
4af966e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bidmarket – ./
bidmarket-git-main-saiko-team.vercel.app
bidmarket.vercel.app
bidmarket-saiko-team.vercel.app