feat: add auction chat
This commit is contained in:
1 parent
c4f19d256d
commit
5ad0f8a1c2
7 files changed
+449
-20
No files matched your search
@@ -272,6 +272,14 @@ export interface ApiAuctionBid {
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export interface ApiAuctionChatMessage {
|
||||
id: string;
|
||||
userId?: string;
|
||||
displayName: string;
|
||||
message: string;
|
||||
sentAt: string;
|
||||
}
|
||||
|
||||
export interface ApiAuction {
|
||||
_id: string;
|
||||
matchId: string;
|
||||
@@ -290,6 +298,7 @@ export interface ApiAuction {
|
||||
initialPoints: number;
|
||||
topTierThreshold: AuctionTopTierThreshold;
|
||||
manualTopTierUserIds: string[];
|
||||
chatMessages?: ApiAuctionChatMessage[];
|
||||
startedAt?: string;
|
||||
completedAt?: string;
|
||||
createdAt: string;
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from 'react';
|
||||
import { io, type Socket } from 'socket.io-client';
|
||||
import { API_BASE_URL, useAuthUserId } from '../config';
|
||||
import { acquireAccessToken } from '../lib/auth/token-provider';
|
||||
import type { ApiAuction, Position } from './types';
|
||||
import type { ApiAuction, ApiAuctionChatMessage, Position } from './types';
|
||||
|
||||
type ConnectionStatus =
|
||||
| 'idle'
|
||||
@@ -17,18 +17,42 @@ interface BidPayload {
|
||||
bidderUserId?: string;
|
||||
}
|
||||
|
||||
interface ChatMessagePayload {
|
||||
displayName?: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface ChatHistoryPayload {
|
||||
messages?: ApiAuctionChatMessage[];
|
||||
}
|
||||
|
||||
const MAX_CHAT_MESSAGES = 200;
|
||||
|
||||
function mergeChatMessages(
|
||||
current: ApiAuctionChatMessage[],
|
||||
incoming: ApiAuctionChatMessage[],
|
||||
) {
|
||||
const byId = new Map(current.map((message) => [message.id, message]));
|
||||
for (const message of incoming) {
|
||||
byId.set(message.id, message);
|
||||
}
|
||||
|
||||
return [...byId.values()]
|
||||
.sort((left, right) => Date.parse(left.sentAt) - Date.parse(right.sentAt))
|
||||
.slice(-MAX_CHAT_MESSAGES);
|
||||
}
|
||||
|
||||
export function useAuctionSocket(matchId: string | undefined) {
|
||||
const authUserId = useAuthUserId();
|
||||
const socketRef = useRef<Socket | null>(null);
|
||||
const [auction, setAuction] = useState<ApiAuction | null>(null);
|
||||
const [chatMessages, setChatMessages] = useState<ApiAuctionChatMessage[]>([]);
|
||||
const [connectionStatus, setConnectionStatus] =
|
||||
useState<ConnectionStatus>('idle');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!matchId) {
|
||||
setAuction(null);
|
||||
setConnectionStatus('idle');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -65,9 +89,20 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
|
||||
socket.on('state', (nextAuction: ApiAuction) => {
|
||||
setAuction(nextAuction);
|
||||
if (nextAuction.chatMessages) {
|
||||
setChatMessages(mergeChatMessages([], nextAuction.chatMessages));
|
||||
}
|
||||
setError(null);
|
||||
});
|
||||
|
||||
socket.on('chat-history', (payload: ChatHistoryPayload) => {
|
||||
setChatMessages(mergeChatMessages([], payload.messages ?? []));
|
||||
});
|
||||
|
||||
socket.on('chat-message', (message: ApiAuctionChatMessage) => {
|
||||
setChatMessages((current) => mergeChatMessages(current, [message]));
|
||||
});
|
||||
|
||||
socket.on('error', (payload: { message?: string }) => {
|
||||
setConnectionStatus('error');
|
||||
setError(payload.message ?? '실시간 경매 연결 중 오류가 발생했습니다.');
|
||||
@@ -101,9 +136,15 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
socket.emit(eventName, payload);
|
||||
}
|
||||
|
||||
const visibleAuction = auction?.matchId === matchId ? auction : null;
|
||||
const visibleConnectionStatus: ConnectionStatus = matchId
|
||||
? connectionStatus
|
||||
: 'idle';
|
||||
|
||||
return {
|
||||
auction,
|
||||
connectionStatus,
|
||||
auction: visibleAuction,
|
||||
chatMessages: visibleAuction ? chatMessages : [],
|
||||
connectionStatus: visibleConnectionStatus,
|
||||
error,
|
||||
placeBid(payload: BidPayload) {
|
||||
emit('bid', payload);
|
||||
@@ -117,5 +158,8 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
next() {
|
||||
emit('next', {});
|
||||
},
|
||||
sendChatMessage(payload: ChatMessagePayload) {
|
||||
emit('chat-message', payload);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user