fix: smooth auction timer bar
This commit is contained in:
1 parent
55b80e62cd
commit
9c69e35cfa
1 file changed
+206
-38
@@ -3,6 +3,7 @@ import {
|
||||
memo,
|
||||
Suspense,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
@@ -161,6 +162,7 @@ const POSITION_TO_RIOT_LANE = {
|
||||
ADC: 'BOTTOM',
|
||||
SUP: 'UTILITY',
|
||||
} as const;
|
||||
const LIVE_TIMER_TICK_MS = 100;
|
||||
|
||||
function getAuctionTeamSize(team: AuctionStateView['teams'][number]) {
|
||||
return team.members.length + 1;
|
||||
@@ -240,15 +242,189 @@ function PosBadge({ pos }: { pos: Position }) {
|
||||
);
|
||||
}
|
||||
|
||||
function TimerBar({ timer, maxTimer }: { timer: number; maxTimer: number }) {
|
||||
const pct = maxTimer > 0 ? (timer / maxTimer) * 100 : 0;
|
||||
const urgent = timer <= 5;
|
||||
function getLiveRemainingSeconds(
|
||||
deadline: string | undefined,
|
||||
serverTimeOffsetMs: number,
|
||||
) {
|
||||
if (!deadline) return 0;
|
||||
|
||||
const deadlineTimestampMS = Date.parse(deadline);
|
||||
if (!Number.isFinite(deadlineTimestampMS)) return 0;
|
||||
|
||||
return Math.max(
|
||||
0,
|
||||
(deadlineTimestampMS - (Date.now() + serverTimeOffsetMs)) / 1000,
|
||||
);
|
||||
}
|
||||
|
||||
function useLiveRemainingSeconds(
|
||||
deadline: string | undefined,
|
||||
serverTimeOffsetMs: number,
|
||||
enabled: boolean,
|
||||
) {
|
||||
const [remainingSeconds, setRemainingSeconds] = useState(() =>
|
||||
getLiveRemainingSeconds(deadline, serverTimeOffsetMs),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
let timeoutId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
let timerId: ReturnType<typeof window.setInterval> | null = null;
|
||||
const updateRemainingSeconds = () => {
|
||||
const nextRemainingSeconds = getLiveRemainingSeconds(
|
||||
deadline,
|
||||
serverTimeOffsetMs,
|
||||
);
|
||||
setRemainingSeconds(nextRemainingSeconds);
|
||||
|
||||
if (nextRemainingSeconds <= 0 && timerId) {
|
||||
window.clearInterval(timerId);
|
||||
timerId = null;
|
||||
}
|
||||
};
|
||||
|
||||
timeoutId = window.setTimeout(updateRemainingSeconds, 0);
|
||||
if (!enabled) {
|
||||
return () => {
|
||||
if (timeoutId) window.clearTimeout(timeoutId);
|
||||
};
|
||||
}
|
||||
|
||||
timerId = window.setInterval(
|
||||
updateRemainingSeconds,
|
||||
LIVE_TIMER_TICK_MS,
|
||||
);
|
||||
|
||||
return () => {
|
||||
if (timeoutId) window.clearTimeout(timeoutId);
|
||||
if (timerId) window.clearInterval(timerId);
|
||||
};
|
||||
}, [deadline, enabled, serverTimeOffsetMs]);
|
||||
|
||||
return remainingSeconds;
|
||||
}
|
||||
|
||||
function useIsDeadlineExpired(
|
||||
deadline: string | undefined,
|
||||
serverTimeOffsetMs: number,
|
||||
enabled: boolean,
|
||||
) {
|
||||
const [isExpired, setIsExpired] = useState(
|
||||
() => getLiveRemainingSeconds(deadline, serverTimeOffsetMs) <= 0,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
let syncTimeoutId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
let expireTimeoutId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
|
||||
const remainingMs =
|
||||
getLiveRemainingSeconds(deadline, serverTimeOffsetMs) * 1000;
|
||||
|
||||
syncTimeoutId = window.setTimeout(() => {
|
||||
setIsExpired(remainingMs <= 0);
|
||||
}, 0);
|
||||
|
||||
if (enabled && remainingMs > 0) {
|
||||
expireTimeoutId = window.setTimeout(() => {
|
||||
setIsExpired(true);
|
||||
}, remainingMs);
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (syncTimeoutId) window.clearTimeout(syncTimeoutId);
|
||||
if (expireTimeoutId) window.clearTimeout(expireTimeoutId);
|
||||
};
|
||||
}, [deadline, enabled, serverTimeOffsetMs]);
|
||||
|
||||
return isExpired;
|
||||
}
|
||||
|
||||
function AuctionTimerText({
|
||||
deadline,
|
||||
serverTimeOffsetMs,
|
||||
}: {
|
||||
deadline: string | undefined;
|
||||
serverTimeOffsetMs: number;
|
||||
}) {
|
||||
const timer = useLiveRemainingSeconds(deadline, serverTimeOffsetMs, true);
|
||||
|
||||
return (
|
||||
<div className="w-full h-1.5 bg-base-content/5">
|
||||
<span
|
||||
className={`text-lg font-black tabular-nums ${timer <= 5 ? 'text-red-500' : 'text-base-content'}`}
|
||||
>
|
||||
{timer.toFixed(1)}s
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function TimerBar({
|
||||
deadline,
|
||||
maxTimer,
|
||||
serverTimeOffsetMs,
|
||||
}: {
|
||||
deadline: string | undefined;
|
||||
maxTimer: number;
|
||||
serverTimeOffsetMs: number;
|
||||
}) {
|
||||
const barRef = useRef<HTMLDivElement>(null);
|
||||
const serverTimeOffsetRef = useRef(serverTimeOffsetMs);
|
||||
|
||||
useEffect(() => {
|
||||
serverTimeOffsetRef.current = serverTimeOffsetMs;
|
||||
}, [serverTimeOffsetMs]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const bar = barRef.current;
|
||||
if (!bar) return;
|
||||
|
||||
let animationFrameId: number | null = null;
|
||||
let urgentTimeoutId: ReturnType<typeof window.setTimeout> | null = null;
|
||||
const remainingSeconds = getLiveRemainingSeconds(
|
||||
deadline,
|
||||
serverTimeOffsetRef.current,
|
||||
);
|
||||
const nextScale =
|
||||
maxTimer > 0
|
||||
? Math.max(0, Math.min(1, remainingSeconds / maxTimer))
|
||||
: 0;
|
||||
|
||||
bar.style.transition = 'none';
|
||||
bar.style.transform = `scaleX(${nextScale})`;
|
||||
bar.classList.toggle('bg-red-500', remainingSeconds <= 5);
|
||||
bar.classList.toggle('bg-primary', remainingSeconds > 5);
|
||||
void bar.offsetWidth;
|
||||
|
||||
if (remainingSeconds > 5) {
|
||||
urgentTimeoutId = window.setTimeout(() => {
|
||||
bar.classList.remove('bg-primary');
|
||||
bar.classList.add('bg-red-500');
|
||||
}, (remainingSeconds - 5) * 1000);
|
||||
}
|
||||
|
||||
animationFrameId = window.requestAnimationFrame(() => {
|
||||
const currentRemainingSeconds = getLiveRemainingSeconds(
|
||||
deadline,
|
||||
serverTimeOffsetRef.current,
|
||||
);
|
||||
bar.style.transition = `transform ${Math.round(
|
||||
currentRemainingSeconds * 1000,
|
||||
)}ms linear`;
|
||||
bar.style.transform = 'scaleX(0)';
|
||||
});
|
||||
|
||||
return () => {
|
||||
if (animationFrameId !== null) {
|
||||
window.cancelAnimationFrame(animationFrameId);
|
||||
}
|
||||
if (urgentTimeoutId) window.clearTimeout(urgentTimeoutId);
|
||||
};
|
||||
}, [deadline, maxTimer]);
|
||||
|
||||
return (
|
||||
<div className="h-1.5 w-full overflow-hidden bg-base-content/5">
|
||||
<div
|
||||
className={`h-full transition-all duration-300 linear ${urgent ? 'bg-red-500' : 'bg-primary'}`}
|
||||
style={{ width: `${pct}%` }}
|
||||
ref={barRef}
|
||||
className="h-full origin-left transform-gpu bg-primary will-change-transform"
|
||||
style={{ transform: 'scaleX(0)' }}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
@@ -712,11 +888,13 @@ function BidControls({
|
||||
state,
|
||||
captainRole,
|
||||
isBidPending,
|
||||
serverTimeOffsetMs,
|
||||
onBid,
|
||||
}: {
|
||||
state: AuctionStateView;
|
||||
captainRole: CaptainRole | null;
|
||||
isBidPending: boolean;
|
||||
serverTimeOffsetMs: number;
|
||||
onBid: (teamIndex: number, amount: number) => void;
|
||||
}) {
|
||||
const [bidAmount, setBidAmount] = useState(100);
|
||||
@@ -731,7 +909,11 @@ function BidControls({
|
||||
: 0;
|
||||
const maxBid = activeTeam ? getMaxBidForTeam(activeTeam) : 0;
|
||||
const canAffordNextBid = !isActiveTeamFull && maxBid >= minBid;
|
||||
const isBidExpired = state.remainingMs <= 0;
|
||||
const isBidExpired = useIsDeadlineExpired(
|
||||
state.deadline,
|
||||
serverTimeOffsetMs,
|
||||
state.phase === 'bidding',
|
||||
);
|
||||
const effectiveBidAmount = activeTeam
|
||||
? Math.min(maxBid, Math.max(minBid, bidAmount))
|
||||
: Math.max(minBid, bidAmount);
|
||||
@@ -902,7 +1084,6 @@ export default function AuctionPage() {
|
||||
const resetAuctionMutation = useResetAuction(DEFAULT_COMMUNITY_ID);
|
||||
const socket = useAuctionSocket(id);
|
||||
const captainTeamIndex = useDevCaptainTeamIndex();
|
||||
const [now, setNow] = useState(() => Date.now());
|
||||
const [assignedPosition, setAssignedPosition] = useState<Position>('MID');
|
||||
const [showCancelModal, setShowCancelModal] = useState(false);
|
||||
const [cancelConfirmText, setCancelConfirmText] = useState('');
|
||||
@@ -937,40 +1118,19 @@ export default function AuctionPage() {
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const timer = window.setInterval(() => setNow(Date.now()), 250);
|
||||
return () => window.clearInterval(timer);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!showCancelModal) return;
|
||||
cancelConfirmInputRef.current?.focus();
|
||||
}, [showCancelModal]);
|
||||
|
||||
const liveAuction = pickLatestAuction(socket.auction, auctionQuery.data);
|
||||
const syncedNow = now + socket.serverTimeOffsetMs;
|
||||
const baseAuctionState = useMemo(
|
||||
const auctionState = useMemo(
|
||||
() =>
|
||||
liveAuction && matchQuery.data
|
||||
? mapApiAuctionToAuctionState(liveAuction, matchQuery.data)
|
||||
: null,
|
||||
[liveAuction, matchQuery.data],
|
||||
);
|
||||
const auctionState = useMemo(() => {
|
||||
if (!baseAuctionState) return null;
|
||||
const deadlineTimestampMS = baseAuctionState.deadline
|
||||
? Date.parse(baseAuctionState.deadline)
|
||||
: undefined;
|
||||
const remainingMs =
|
||||
typeof deadlineTimestampMS === 'number' && Number.isFinite(deadlineTimestampMS)
|
||||
? Math.max(0, deadlineTimestampMS - syncedNow)
|
||||
: 0;
|
||||
return {
|
||||
...baseAuctionState,
|
||||
remainingMs,
|
||||
timer: Math.floor(remainingMs / 1000),
|
||||
};
|
||||
}, [baseAuctionState, syncedNow]);
|
||||
const availableTeamIndexes = useMemo(() => {
|
||||
const indexes = new Set((auctionState?.teams ?? []).map((team) => team.teamIndex));
|
||||
return [...indexes].sort((left, right) => left - right);
|
||||
@@ -1225,11 +1385,10 @@ export default function AuctionPage() {
|
||||
{phaseLabel[auctionState.phase]}
|
||||
</span>
|
||||
{auctionState.phase === 'bidding' && (
|
||||
<span
|
||||
className={`text-lg font-black tabular-nums ${auctionState.timer <= 5 ? 'text-red-500' : 'text-base-content'}`}
|
||||
>
|
||||
{auctionState.timer}s
|
||||
</span>
|
||||
<AuctionTimerText
|
||||
deadline={auctionState.deadline}
|
||||
serverTimeOffsetMs={socket.serverTimeOffsetMs}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-xs text-base-content/40">
|
||||
@@ -1257,9 +1416,17 @@ export default function AuctionPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{auctionState.phase === 'bidding' && (
|
||||
<TimerBar timer={auctionState.timer} maxTimer={auctionState.maxTimer} />
|
||||
)}
|
||||
<div className="h-1.5">
|
||||
{auctionState.phase === 'bidding' ? (
|
||||
<TimerBar
|
||||
deadline={auctionState.deadline}
|
||||
maxTimer={auctionState.maxTimer}
|
||||
serverTimeOffsetMs={socket.serverTimeOffsetMs}
|
||||
/>
|
||||
) : (
|
||||
<div aria-hidden className="h-full" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 xl:grid-cols-[260px_minmax(0,1fr)_minmax(340px,440px)_320px] gap-4 mt-4">
|
||||
<aside className="hidden xl:block">
|
||||
@@ -1324,6 +1491,7 @@ export default function AuctionPage() {
|
||||
state={auctionState}
|
||||
captainRole={captainRole}
|
||||
isBidPending={socket.isBidPending}
|
||||
serverTimeOffsetMs={socket.serverTimeOffsetMs}
|
||||
onBid={(teamIndex, amount) =>
|
||||
socket.placeBid({
|
||||
bidderTeamIndex: teamIndex,
|
||||
|
||||
Reference in new issue
Block a user