Skip to content

Commit

Permalink
[๐Ÿ› ๏ธ REFACTOR] fetchRoomPlayers ํ•จ์ˆ˜ ๋ชจ๋“ˆํ™”
Browse files Browse the repository at this point in the history
  • Loading branch information
bishoe01 committed Aug 2, 2024
1 parent 3eba8a0 commit 3298770
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
26 changes: 6 additions & 20 deletions handtris/src/components/TetrisPlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import { getRoomCode } from "@/util/getRoomCode";
import { AnimatePresence, motion } from "framer-motion";
import { useToast } from "@/components/ui/use-toast";
import { HandLandmarkResults, TetrisBoard } from "@/types";
import WaitingModal, { Player } from "@/components/WaitingModal";
import WaitingModal from "@/components/WaitingModal";
import LeftJoystickModel from "@/components/LeftJoystickModel";
import RightJoystickModel from "@/components/RightJoystickModel";
import GameResultModal from "@/components/GameResultModal";
import { searchRoomPlayer, updateStatus } from "@/services/gameService";
import { useMusic } from "./MusicProvider";
import ConfettiExplosion from "react-confetti-explosion";
import { ArrowUpNarrowWide, Donut, FlipVertical2 } from "lucide-react";
import { LandmarkList } from "@mediapipe/hands";
import { drawNextBlock } from "./drawNextBlock";
import { showCountdown } from "./showCountdown";
import { useHandleGesture } from "@/hook/useHandleGesture";
import { updateStatus } from "@/services/gameService";
import useFetchRoomPlayers from "@/hook/fetchRoomPlayers";

const TETRIS_CANVAS = `flex items-center justify-between w-full border-2 border-t-0`;

Expand Down Expand Up @@ -59,8 +60,6 @@ const Home: React.FC = () => {
const lastGestureRef = useRef<string | null>(null);
const previousLinesClearedRef = useRef(0);
const [showResultModal, setShowResultModal] = useState(false);
const [roomPlayers, setRoomPlayers] = useState<Player[]>([]);
const [isLoading, setIsLoading] = useState(true);
const isSub = useRef(false);
const isSubTemp = useRef(false);
const [isDangerous, setIsDangerous] = useState(false);
Expand All @@ -71,22 +70,9 @@ const Home: React.FC = () => {
const [isFlipping, setIsFlipping] = useState(false);
const [isNextBlockDonut, setIsNextBlockDonut] = useState(false);
const [showDonutWarning, setShowDonutWarning] = useState(false);
const fetchRoomPlayers = useCallback(async () => {
setIsLoading(true);
try {
const roomCode = getRoomCode();
if (roomCode) {
const response = await searchRoomPlayer(roomCode);
if (response.data) {
setRoomPlayers(response.data);
}
}
} catch (error) {
console.error("Failed to fetch room players:", error);
} finally {
setIsLoading(false);
}
}, []);

const { roomPlayers, isLoading, fetchRoomPlayers } = useFetchRoomPlayers();

useEffect(() => {
const checkDangerousState = () => {
if (tetrisGameRef.current) {
Expand Down
29 changes: 29 additions & 0 deletions handtris/src/hook/fetchRoomPlayers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useState, useCallback } from "react";
import { getRoomCode } from "@/util/getRoomCode";
import { searchRoomPlayer } from "@/services/gameService";

const useFetchRoomPlayers = () => {
const [roomPlayers, setRoomPlayers] = useState<Player[]>([]);
const [isLoading, setIsLoading] = useState(true);

const fetchRoomPlayers = useCallback(async () => {
setIsLoading(true);
try {
const roomCode = getRoomCode();
if (roomCode) {
const response = await searchRoomPlayer(roomCode);
if (response.data) {
setRoomPlayers(response.data);
}
}
} catch (error) {
console.error("Failed to fetch room players:", error);
} finally {
setIsLoading(false);
}
}, []);

return { roomPlayers, isLoading, fetchRoomPlayers };
};

export default useFetchRoomPlayers;

0 comments on commit 3298770

Please sign in to comment.