From 98c2323ff258f3885b031e00131f92df47dafff7 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Mon, 22 Jun 2026 12:16:18 -0700 Subject: [PATCH] fix: sync auction timer with server time --- apps/web/src/api/useAuctionSocket.ts | 18 ++++++++++++++++++ apps/web/src/pages/AuctionPage.tsx | 7 ++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/apps/web/src/api/useAuctionSocket.ts b/apps/web/src/api/useAuctionSocket.ts index 6bc32f9f..0d138887 100644 --- a/apps/web/src/api/useAuctionSocket.ts +++ b/apps/web/src/api/useAuctionSocket.ts @@ -30,6 +30,11 @@ interface ResetPayload { matchId?: string; } +interface TickPayload { + deadline?: string; + serverTime?: string; +} + const MAX_CHAT_MESSAGES = 200; function mergeChatMessages( @@ -55,6 +60,7 @@ export function useAuctionSocket(matchId: string | undefined) { const [connectionStatus, setConnectionStatus] = useState('idle'); const [error, setError] = useState(null); + const [serverTimeOffsetMs, setServerTimeOffsetMs] = useState(0); useEffect(() => { if (!matchId) { @@ -100,6 +106,16 @@ export function useAuctionSocket(matchId: string | undefined) { setError(null); }); + socket.on('tick', (payload: TickPayload) => { + const serverTime = + typeof payload?.serverTime === 'string' + ? Date.parse(payload.serverTime) + : NaN; + if (Number.isFinite(serverTime)) { + setServerTimeOffsetMs(serverTime - Date.now()); + } + }); + socket.on('chat-history', (payload: ChatHistoryPayload) => { setChatMessages(mergeChatMessages([], payload.messages ?? [])); }); @@ -129,6 +145,7 @@ export function useAuctionSocket(matchId: string | undefined) { socket?.disconnect(); socketRef.current = null; setConnectionStatus('idle'); + setServerTimeOffsetMs(0); }; }, [authUserId, matchId]); @@ -160,6 +177,7 @@ export function useAuctionSocket(matchId: string | undefined) { resetAt, connectionStatus: visibleConnectionStatus, error, + serverTimeOffsetMs: matchId ? serverTimeOffsetMs : 0, placeBid(payload: BidPayload) { emit('bid', payload); }, diff --git a/apps/web/src/pages/AuctionPage.tsx b/apps/web/src/pages/AuctionPage.tsx index 178a977c..bd69d05b 100644 --- a/apps/web/src/pages/AuctionPage.tsx +++ b/apps/web/src/pages/AuctionPage.tsx @@ -781,9 +781,14 @@ export default function AuctionPage() { }, [showCancelModal]); const liveAuction = pickLatestAuction(socket.auction, auctionQuery.data); + const syncedNow = now + socket.serverTimeOffsetMs; const auctionState = liveAuction && matchQuery.data - ? mapApiAuctionToAuctionState(liveAuction, matchQuery.data, new Date(now)) + ? mapApiAuctionToAuctionState( + liveAuction, + matchQuery.data, + new Date(syncedNow), + ) : null; const availableTeamIndexes = useMemo(() => { const indexes = new Set((auctionState?.teams ?? []).map((team) => team.teamIndex));