turkmentv_front/components/lottery/LotteryWinnersSection.tsx

213 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-12-29 18:02:23 +00:00
"use client";
import { useState, useEffect, useRef } from "react";
import { useLotteryAuth } from "@/store/useLotteryAuth";
import { LotteryWinnerDataSimplified } from "@/typings/lottery/lottery.types";
import LotteryWinnersList from "./winners/LotteryWinnersList";
import LotterySlotCounter from "./slotCounter/LotterySlotCounter";
import ReactConfetti from "react-confetti";
import { useWindowSize } from "react-use";
import LotteryCountDownAllert from "./countDown/countDownAllert/LotteryCountDownAllert";
const WEBSOCKET_URL = "wss://sms.turkmentv.gov.tm/ws/lottery?dst=0506";
2024-12-27 12:52:55 +00:00
const PING_INTERVAL = 25000;
2024-12-27 15:23:39 +00:00
const SLOT_COUNTER_DURATION = 20000;
2024-12-27 12:52:55 +00:00
2024-12-29 18:02:23 +00:00
const LotteryWinnersSection = ({
lotteryStatus,
}: {
lotteryStatus: string;
}) => {
2024-12-27 12:52:55 +00:00
// UI States
const [winners, setWinners] = useState<LotteryWinnerDataSimplified[]>([]);
2024-12-29 18:02:23 +00:00
const [currentNumber, setCurrentNumber] = useState<string>("00-00-00-00-00");
2024-12-25 13:46:20 +00:00
const [isConfettiActive, setIsConfettiActive] = useState(false);
2024-12-29 18:02:23 +00:00
const [wsStatus, setWsStatus] = useState<
"connecting" | "connected" | "error"
>("connecting");
2024-12-25 13:46:20 +00:00
const { width, height } = useWindowSize();
2024-12-23 19:32:28 +00:00
const { lotteryData } = useLotteryAuth();
2024-12-27 15:23:39 +00:00
const [isSlotCounterAnimating, setIsSlotCounterAnimating] = useState(false);
2024-12-29 18:02:23 +00:00
const [pendingWinner, setPendingWinner] =
useState<LotteryWinnerDataSimplified | null>(null);
2024-12-23 19:32:28 +00:00
2024-12-27 12:52:55 +00:00
// Refs
const wsRef = useRef<WebSocket | null>(null);
const pingIntervalRef = useRef<NodeJS.Timeout>();
const mountedRef = useRef(false);
// Initialize winners from lottery data
2024-12-23 19:32:28 +00:00
useEffect(() => {
if (lotteryData?.data.winners) {
2024-12-27 12:52:55 +00:00
const simplifiedWinners = lotteryData.data.winners.map((winner) => ({
client: winner.client,
winner_no: winner.winner_no,
ticket: winner.ticket,
}));
setWinners(simplifiedWinners);
2024-12-29 18:02:23 +00:00
setCurrentNumber(
lotteryData.data.winners.at(-1)?.ticket || "00-00-00-00-00"
);
2024-12-23 19:32:28 +00:00
}
2024-12-27 12:52:55 +00:00
}, [lotteryData]);
2024-12-23 19:32:28 +00:00
2024-12-27 12:52:55 +00:00
// Handle WebSocket connection
useEffect(() => {
mountedRef.current = true;
const setupWebSocket = () => {
try {
const socket = new WebSocket(WEBSOCKET_URL);
wsRef.current = socket;
2024-12-29 18:02:23 +00:00
socket.addEventListener("open", () => {
2024-12-27 12:52:55 +00:00
if (!mountedRef.current) return;
2024-12-29 18:02:23 +00:00
console.log("WebSocket Connected");
setWsStatus("connected");
2024-12-27 12:52:55 +00:00
pingIntervalRef.current = setInterval(() => {
if (socket.readyState === WebSocket.OPEN) {
2024-12-29 18:02:23 +00:00
socket.send(JSON.stringify({ type: "ping" }));
2024-12-27 12:52:55 +00:00
}
}, PING_INTERVAL);
});
2024-12-29 18:02:23 +00:00
socket.addEventListener("message", async (event) => {
2024-12-27 12:52:55 +00:00
if (!mountedRef.current) return;
2024-12-29 18:02:23 +00:00
console.log("Message received:", event.data);
2024-12-27 12:52:55 +00:00
try {
const newWinner = JSON.parse(event.data);
const winnerData = {
2024-12-27 15:23:39 +00:00
client: newWinner.phone,
2024-12-27 12:52:55 +00:00
winner_no: newWinner.winner_no,
ticket: newWinner.ticket,
};
2024-12-27 15:23:39 +00:00
// Start the sequence
setIsSlotCounterAnimating(true);
setPendingWinner(winnerData);
setCurrentNumber(winnerData.ticket);
// Wait for slot counter animation
2024-12-29 18:02:23 +00:00
await new Promise((resolve) =>
setTimeout(resolve, SLOT_COUNTER_DURATION)
);
2024-12-27 15:23:39 +00:00
2024-12-27 12:52:55 +00:00
setIsConfettiActive(true);
2024-12-27 15:23:39 +00:00
setWinners((prev) => [...prev, winnerData]);
// Hide confetti after 5 seconds
// setTimeout(() => {
// if (mountedRef.current) {
// setIsConfettiActive(false);
// setIsSlotCounterAnimating(false);
// setPendingWinner(null);
// }
// }, 5000);
// Show confetti and add winner simultaneously
if (mountedRef.current) {
setIsConfettiActive(true);
setWinners((prev) => [...prev, winnerData]);
// Hide confetti after 5 seconds
setTimeout(() => {
if (mountedRef.current) {
setIsConfettiActive(false);
setIsSlotCounterAnimating(false);
setPendingWinner(null);
}
}, 5000);
}
2024-12-27 12:52:55 +00:00
} catch (error) {
2024-12-29 18:02:23 +00:00
console.error("Error processing message:", error);
2024-12-27 15:23:39 +00:00
setIsSlotCounterAnimating(false);
setPendingWinner(null);
2024-12-27 12:52:55 +00:00
}
});
2024-12-29 18:02:23 +00:00
socket.addEventListener("error", (error) => {
2024-12-27 12:52:55 +00:00
if (!mountedRef.current) return;
2024-12-29 18:02:23 +00:00
console.error("WebSocket Error:", error);
setWsStatus("error");
2024-12-27 12:52:55 +00:00
});
2024-12-29 18:02:23 +00:00
socket.addEventListener("close", () => {
2024-12-27 12:52:55 +00:00
if (!mountedRef.current) return;
2024-12-29 18:02:23 +00:00
console.log("WebSocket Closed");
setWsStatus("error");
2024-12-27 12:52:55 +00:00
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current);
}
});
} catch (error) {
2024-12-29 18:02:23 +00:00
console.error("Error creating WebSocket:", error);
setWsStatus("error");
2024-12-27 12:52:55 +00:00
}
2024-12-25 13:46:20 +00:00
};
2024-12-27 15:23:39 +00:00
// Initial connection
setupWebSocket();
2024-12-25 13:46:20 +00:00
2024-12-27 12:52:55 +00:00
return () => {
mountedRef.current = false;
if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current);
}
if (wsRef.current) {
wsRef.current.close();
}
2024-12-23 19:32:28 +00:00
};
2024-12-27 15:23:39 +00:00
}, []);
2024-11-25 14:33:01 +00:00
return (
<section>
2024-12-29 18:02:23 +00:00
{wsStatus === "error" && (
2024-12-25 13:46:20 +00:00
<div className="text-red-500 text-center mb-2">
Connection error. Please refresh the page.
</div>
)}
{isConfettiActive && (
<div className="fixed top-0 left-0 z-50">
<ReactConfetti
width={width}
height={height}
recycle={false}
numberOfPieces={1000}
tweenDuration={10000}
run={true}
colors={[
2024-12-29 18:02:23 +00:00
"linear-gradient(45deg, #5D5D72, #8589DE)",
"linear-gradient(45deg, #E1E0FF, #575992)",
"#8589DE",
"#575992",
"#E1E0FF",
"#BA1A1A",
2024-12-25 13:46:20 +00:00
]}
/>
</div>
)}
2024-11-25 14:33:01 +00:00
<div className="container">
2024-12-27 12:52:55 +00:00
<div className="flex flex-col items-center">
2024-12-30 09:02:49 +00:00
<div className="translate-y-1/2 z-10">
2024-12-29 18:02:23 +00:00
<LotterySlotCounter
numberString={currentNumber}
isAnimating={isSlotCounterAnimating}
/>
2024-12-25 13:46:20 +00:00
</div>
2024-12-30 09:02:49 +00:00
<div className="flex gap-6 bg-lightPrimaryContainer rounded-[12px] flex-1 w-full items-center justify-center md:pt-[122px] sm:pt-[90px] pt-[40px] sm:pb-[62px] pb-[32px] px-4">
2024-12-23 19:32:28 +00:00
<LotteryWinnersList winners={winners} />
2024-11-25 14:33:01 +00:00
</div>
</div>
</div>
</section>
);
};
export default LotteryWinnersSection;