Skip to content

Commit 0ffce0c

Browse files
authored
fix: msw 대신 구현된 api를 활용하도록 수정 (#1123)
1 parent 2639127 commit 0ffce0c

13 files changed

Lines changed: 74 additions & 339 deletions

File tree

frontend/src/domains/chat/hooks/useChat.ts

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,73 @@
11
import { useCallback, useState } from 'react';
22

3+
import type { UserRole } from '@/domains/auth/types/api.types';
34
import { useWebSocket } from '@/libs/websocket/hooks/useWebSocket';
45

5-
import type { ChatIncomingMessageType } from '../types/api.types';
6+
import type { ChatMessageResponse } from '../types/api.types';
67
import type { ChatMessageType } from '../types/chat.types';
78

8-
const WS_CHAT_URL = `${process.env.REACT_APP_API_URL?.replace(/^http/, 'ws') ?? 'ws://localhost:8080'}/ws/chat/v1`;
9+
const WS_CHAT_URL = `${process.env.REACT_APP_API_URL?.replace(/^http/, 'ws') ?? 'ws://localhost:8080'}/ws/v1`;
910

1011
interface UseChatParams {
1112
routieSpaceUuid: string;
1213
accessToken: string;
1314
myNickname: string;
15+
myRole: UserRole;
1416
}
1517

16-
const useChat = ({ routieSpaceUuid, accessToken, myNickname }: UseChatParams) => {
18+
const useChat = ({
19+
routieSpaceUuid,
20+
accessToken,
21+
myNickname,
22+
myRole,
23+
}: UseChatParams) => {
1724
const [messages, setMessages] = useState<ChatMessageType[]>([]);
1825
const [isConnected, setIsConnected] = useState(false);
1926

20-
const handleMessage = useCallback(
21-
(data: ChatIncomingMessageType) => {
22-
if (data.type === 'CHAT_ACK') {
23-
setMessages((prev) =>
24-
prev.map((msg) =>
27+
const handleMessage = useCallback((data: ChatMessageResponse) => {
28+
setMessages((prev) => {
29+
if (data.tempId) {
30+
const pendingIndex = prev.findIndex(
31+
(msg) => msg.tempId === data.tempId,
32+
);
33+
if (pendingIndex !== -1) {
34+
return prev.map((msg) =>
2535
msg.tempId === data.tempId
26-
? { ...msg, messageId: data.messageId, timestamp: data.timestamp, status: 'sent', tempId: undefined }
36+
? {
37+
...msg,
38+
messageId: data.messageId,
39+
timestamp: data.timestamp,
40+
status: 'sent',
41+
tempId: undefined,
42+
}
2743
: msg,
28-
),
29-
);
30-
return;
44+
);
45+
}
3146
}
3247

33-
if (data.type === 'CHAT') {
34-
setMessages((prev) => {
35-
const alreadyExists = prev.some(
36-
(msg) => msg.messageId === data.messageId || msg.tempId === data.messageId,
37-
);
38-
if (alreadyExists) return prev;
48+
if (prev.some((msg) => msg.messageId === data.messageId)) return prev;
3949

40-
return [
41-
...prev,
42-
{
43-
messageId: data.messageId,
44-
senderId: data.senderId,
45-
senderName: data.senderName,
46-
content: data.content,
47-
timestamp: data.timestamp,
48-
status: 'sent',
49-
isMine: data.senderId === myNickname,
50-
},
51-
];
52-
});
53-
}
54-
},
55-
[myNickname],
56-
);
50+
return [
51+
...prev,
52+
{
53+
messageId: data.messageId,
54+
senderId: data.senderId,
55+
senderRole: data.senderRole,
56+
senderName: data.senderName,
57+
content: data.content,
58+
timestamp: data.timestamp,
59+
status: 'sent',
60+
isMine: false,
61+
},
62+
];
63+
});
64+
}, []);
5765

58-
const { send } = useWebSocket<ChatIncomingMessageType>({
66+
const { send } = useWebSocket<ChatMessageResponse>({
5967
url: WS_CHAT_URL,
6068
token: accessToken,
61-
subscribeDestination: `/topic/chat/${routieSpaceUuid}`,
62-
publishDestination: `/app/chat/${routieSpaceUuid}`,
69+
subscribeDestination: `/topic/chat/room/${routieSpaceUuid}`,
70+
publishDestination: `/app/chat/room/${routieSpaceUuid}`,
6371
onMessage: handleMessage,
6472
onConnect: useCallback(() => setIsConnected(true), []),
6573
onDisconnect: useCallback(() => setIsConnected(false), []),
@@ -75,6 +83,7 @@ const useChat = ({ routieSpaceUuid, accessToken, myNickname }: UseChatParams) =>
7583
messageId: tempId,
7684
tempId,
7785
senderId: myNickname,
86+
senderRole: myRole,
7887
senderName: myNickname,
7988
content,
8089
timestamp: new Date().toISOString(),
@@ -88,12 +97,12 @@ const useChat = ({ routieSpaceUuid, accessToken, myNickname }: UseChatParams) =>
8897
return;
8998
}
9099

91-
const isSent = send({ type: 'CHAT', routieSpaceId: routieSpaceUuid, tempId, content });
100+
const isSent = send({ type: 'CHAT', tempId, content });
92101
if (!isSent) {
93102
setMessages((prev) => prev.filter((msg) => msg.tempId !== tempId));
94103
}
95104
},
96-
[send, myNickname, isConnected, routieSpaceUuid],
105+
[send, myNickname, myRole, isConnected],
97106
);
98107

99108
return { messages, sendMessage };
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
1+
import type { UserRole } from '@/domains/auth/types/api.types';
2+
13
interface ChatSendRequest {
24
type: 'CHAT';
3-
routieSpaceId: string;
45
tempId: string;
56
content: string;
67
}
78

8-
interface ChatAckResponse {
9-
type: 'CHAT_ACK';
10-
tempId: string;
11-
messageId: string;
12-
timestamp: string;
13-
}
14-
159
interface ChatMessageResponse {
1610
type: 'CHAT';
11+
tempId: string;
1712
messageId: string;
1813
senderId: string;
14+
senderRole: UserRole;
1915
senderName: string;
2016
content: string;
2117
timestamp: string;
2218
}
2319

24-
type ChatIncomingMessageType = ChatAckResponse | ChatMessageResponse;
25-
26-
export type {
27-
ChatSendRequest,
28-
ChatAckResponse,
29-
ChatMessageResponse,
30-
ChatIncomingMessageType,
31-
};
20+
export type { ChatSendRequest, ChatMessageResponse };

frontend/src/domains/chat/types/chat.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import type { UserRole } from '@/domains/auth/types/api.types';
2+
13
type ChatMessageStatus = 'pending' | 'sent';
24

35
interface ChatMessageType {
46
messageId: string;
57
tempId?: string;
68
senderId: string;
9+
senderRole: UserRole;
710
senderName: string;
811
content: string;
912
timestamp: string;

frontend/src/index.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,5 @@ if (process.env.NODE_ENV === 'production') {
2222
});
2323
}
2424

25-
async function enableMocking() {
26-
if (window.location.hostname !== 'localhost') {
27-
return;
28-
}
29-
30-
const { worker } = await import('./mocks/browser');
31-
return worker.start();
32-
}
33-
34-
enableMocking().then(() => {
35-
const root = createRoot(document.getElementById('root')!);
36-
root.render(<Route />);
37-
});
25+
const root = createRoot(document.getElementById('root')!);
26+
root.render(<Route />);

frontend/src/libs/websocket/hooks/__tests__/useWebSocket.test.ts

Lines changed: 0 additions & 132 deletions
This file was deleted.

frontend/src/libs/websocket/hooks/useWebSocket.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ const useWebSocket = <T>({
6464
console.error('[WS] STOMP 오류:', frame.headers['message']);
6565
onErrorRef.current?.(frame);
6666
},
67+
onWebSocketError: (event) => {
68+
console.error('[WS] WebSocket 오류:', event);
69+
},
70+
onWebSocketClose: (event) => {
71+
console.warn('[WS] WebSocket 닫힘:', event.code, event.reason);
72+
},
6773
});
6874

6975
client.activate();

frontend/src/mocks/browser.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

frontend/src/mocks/handlers.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)