Skip to content

Commit

Permalink
feat:#226 sse 커스텀훅 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
cho-subin committed Nov 1, 2023
1 parent ea2ee4d commit c63f3e4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/components/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ const Chat = ({
<S.Chat>
{isCustomRoomId ? (
<ChatList
customRoomId={customRoomId}
handleOpen={handleOpen}
clickListBox={clickListBox}
seller={seller}
isSeller={isSeller}
role={role}
product={product}
/>
) : (
Expand All @@ -124,10 +126,12 @@ const Chat = ({
<S.Chat>
{!isCustomRoomId ? (
<ChatList
customRoomId={customRoomId}
handleOpen={handleOpen}
clickListBox={clickListBox}
seller={seller}
isSeller={isSeller}
role={role}
product={product}
/>
) : (
Expand Down
17 changes: 16 additions & 1 deletion src/components/chat/ChatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
import { sellerChatList, userChatList } from '@/apis/chat';
import ChatBody from '@/components/chat/chatList/ChatBody';
import ChatHeader from '@/components/chat/chatList/ChatHeader';
import { useSSE } from '@/hooks/useSSE';
import { Message } from '@/models/chat';

export type Chat = {
Expand Down Expand Up @@ -35,13 +36,27 @@ type chatProps = {
productName: string;
};
isSeller: boolean;
role: string;
customRoomId: string;
};

const ChatList = ({ handleOpen, clickListBox, seller, isSeller, product }: chatProps) => {
const ChatList = ({
handleOpen,
clickListBox,
seller,
isSeller,
product,
customRoomId,
role,
}: chatProps) => {
const [list, setList] = useState<List[]>([]);

const [shopImg, setShopImg] = useState<string>('');

const message = useSSE(customRoomId, role);

console.log(message);

const loadUserChatList: () => Promise<void> = async () => {
const sellerId = seller.sellerId;
userChatList(sellerId)
Expand Down
49 changes: 49 additions & 0 deletions src/hooks/useSSE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useEffect, useState } from 'react';

type chatAlarm = {
content: string;
createAt: string;
customRoomId: string;
role: string;
sellerId: number;
sender: string;
};

export const useSSE = (customRoomId: string, role: string) => {
console.log('useSSE', customRoomId);
console.log('role', role);
const [message, setMessage] = useState<chatAlarm[]>([]);

console.log(message);

useEffect(() => {
const eventSource = new EventSource(
`${import.meta.env.VITE_API_SSE_URL}/chat-alarm/${role}/${customRoomId}`,
);

eventSource.addEventListener('sse', function (event) {
const message = JSON.parse(event.data);
console.log('새로운 채팅 알람: ', message);
setMessage((prevMessages) => [...prevMessages, message]);
});

eventSource.onerror = function (error) {
console.error('EventSource failed:', error);
eventSource.close();
};

window.addEventListener('unload', function () {
if (eventSource) {
eventSource.close();
console.log('EventSource closed');
}
});

return () => {
eventSource.close();
console.log('EventSource closed');
};
}, [customRoomId, role]);

return message;
};

0 comments on commit c63f3e4

Please sign in to comment.