generated from nim-od/softeer-mono-boilerplate
-
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 #81 from softeerbootcamp4th/TASK-171
[Feature][Task-171] 어드민 페이지 Socket연동 및 에러 핸들링 추기
- Loading branch information
Showing
29 changed files
with
368 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { BlockedChat, ChatProps, Message, Notice } from '@softeer/common/components'; | ||
import { FunctionComponent, useCallback } from 'react'; | ||
|
||
export default function Chat({ type, team, sender, content }: ChatProps) { | ||
const Render: FunctionComponent = useCallback(() => { | ||
switch (type) { | ||
case 'n': | ||
return <Notice>{content}</Notice>; | ||
case 'b': | ||
return <BlockedChat />; | ||
case 'm': | ||
default: | ||
return ( | ||
<Message sender={sender} team={team} isMyMessage={false}> | ||
{content} | ||
</Message> | ||
); | ||
} | ||
}, [type, sender, content]); | ||
|
||
return <Render />; | ||
} |
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,24 @@ | ||
import { ChatList } from '@softeer/common/components'; | ||
import { UseSocketReturnType } from 'src/hooks/socket/index.ts'; | ||
import Chat from './Chat.tsx'; | ||
|
||
/** 실시간 기대평 섹션 */ | ||
|
||
function RealTimeChatting({ | ||
chatSocket: { onSendMessage, messages }, | ||
}: Pick<UseSocketReturnType, 'chatSocket'>) { | ||
return ( | ||
<section className="container flex max-w-[1200px] snap-start flex-col items-center pb-[115px] pt-[50px]"> | ||
<h6 className="text-heading-10 mb-[25px] font-medium">기대평을 남겨보세요!</h6> | ||
<div className="h-[1000px] w-full overflow-y-auto rounded-[10px] bg-neutral-800 py-10"> | ||
<ChatList> | ||
{messages.map((message) => ( | ||
<Chat key={message.id} {...message} /> | ||
))} | ||
</ChatList> | ||
</div> | ||
</section> | ||
); | ||
} | ||
|
||
export default RealTimeChatting; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//* *********************************** */ | ||
//* * Environment Variables */ | ||
//* *********************************** */ | ||
|
||
export const { VITE_BASE_URL: API_BASE_URL, VITE_SOCKET_BASE_URL: SOCKET_BASE_URL } = import.meta | ||
.env; |
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,18 @@ | ||
import { ACCESS_TOKEN_KEY } from '@softeer/common/constants'; | ||
import { Cookie } from '@softeer/common/utils'; | ||
import { useEffect } from 'react'; | ||
import socketManager from 'src/services/socket.ts'; | ||
import useChatSocket from './useChatSocket.ts'; | ||
|
||
export type UseSocketReturnType = ReturnType<typeof useSocket>; | ||
|
||
export default function useSocket() { | ||
const accessToken = Cookie.getCookie<string>(ACCESS_TOKEN_KEY) ?? ''; | ||
const chatSocket = useChatSocket(); | ||
const { onReceiveMessage, ...chatSocketProps } = chatSocket; | ||
useEffect(() => { | ||
socketManager.connectSocketClient({ token: accessToken, onReceiveMessage }); | ||
}, [socketManager, accessToken]); | ||
|
||
return { chatSocket: chatSocketProps }; | ||
} |
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,47 @@ | ||
import { ChatProps } from '@softeer/common/components'; | ||
import { CHAT_SOCKET_ENDPOINTS } from '@softeer/common/constants'; | ||
import { SocketSubscribeCallbackType } from '@softeer/common/utils'; | ||
import { useCallback, useState } from 'react'; | ||
// import { useToast } from 'src/hooks/useToast.ts'; | ||
import socketManager from 'src/services/socket.ts'; | ||
|
||
export type UseChatSocketReturnType = ReturnType<typeof useChatSocket>; | ||
|
||
export default function useChatSocket() { | ||
// const { toast } = useToast(); | ||
|
||
const socketClient = socketManager.getSocketClient(); | ||
const [chatMessages, setChatMessages] = useState<ChatProps[]>([]); | ||
|
||
const handleIncomingMessage: SocketSubscribeCallbackType = useCallback( | ||
(data: unknown, messageId: string) => { | ||
const parsedData = data as Omit<ChatProps, 'id'>; | ||
const parsedMessage = { id: messageId, ...parsedData }; | ||
setChatMessages((prevMessages) => [...prevMessages, parsedMessage] as ChatProps[]); | ||
}, | ||
[], | ||
); | ||
|
||
const handleSendMessage = useCallback( | ||
(content: string) => { | ||
try { | ||
const chatMessage = { content }; | ||
|
||
socketClient.sendMessages({ | ||
destination: CHAT_SOCKET_ENDPOINTS.PUBLISH, | ||
body: chatMessage, | ||
}); | ||
} catch (error) { | ||
// const errorMessage = (error as Error).message; | ||
// toast({ description: errorMessage.length > 0 ? errorMessage : '문제가 발생했습니다.' }); | ||
} | ||
}, | ||
[socketClient], | ||
); | ||
|
||
return { | ||
onReceiveMessage: handleIncomingMessage, | ||
onSendMessage: handleSendMessage, | ||
messages: chatMessages, | ||
}; | ||
} |
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
Oops, something went wrong.