import styles from "@components/GameList/styles.module.scss"; import React, { useCallback, useEffect, useRef, useState } from "react"; import GameItem, { GameImgAspectRatio } from "@components/GameItem"; import { REQ_STATUS } from "@api/config"; import SmallLoading from "@components/SmallLoading"; import { useThemeProvider } from "@contexts/themeContext"; import GameRoom from "@components/GameRoom"; import { useWindowSize } from "react-use"; const i18n = global.i18n; const objInfo = { timer: null } const GameList = ({ data, status, reqMore, user }) => { // data={ // type:0,//游戏分类 // page:1, // pageSize:40, // total:42, // totalPage:1, // data:[ // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/f76d3f4a-9712-4bd1-9426-f3ab7069b6a2.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:1006,//游戏人数 // desc:"BC Originals",//描述 // }, // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/ff0b0511-1f48-43d1-8dd3-294bb6437b4b.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:100,//游戏人数 // desc:"BC Originals",//描述 // }, // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/d33b3fad-b6df-450b-bd85-528a17383043.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:100,//游戏人数 // desc:"BC Originals",//描述 // }, // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/5b3a4099-9462-4d3c-a854-ade78aad0d60.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:100,//游戏人数 // desc:"BC Originals",//描述 // }, // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/f76d3f4a-9712-4bd1-9426-f3ab7069b6a2.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:100,//游戏人数 // desc:"BC Originals",//描述 // }, // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/ff0b0511-1f48-43d1-8dd3-294bb6437b4b.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:100,//游戏人数 // desc:"BC Originals",//描述 // }, // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/d33b3fad-b6df-450b-bd85-528a17383043.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:100,//游戏人数 // desc:"BC Originals",//描述 // }, // { // gid:0,//gameid // img:"https://bc.imgix.net/game/image/5b3a4099-9462-4d3c-a854-ade78aad0d60.png?auto=format&dpr=3&w=200",//图片 // link:"",//跳转的链接 // online:100,//游戏人数 // desc:"BC Originals",//描述 // }, // ] // } const { isMobile, fontScale } = useThemeProvider(); const M_LIST_COL = 3; const [selected, setSelected] = useState(-1); const [roomRow, setRoomRow] = useState({}); const [showPopRoom, setShowPopRoom] = useState(false); const roomHeight = 5.2; const rowTop = roomHeight + 0.75; const gameListRef = useRef(null); const [gridInfo, setGridInfo] = useState({}); const { width } = useWindowSize(); const mobile = isMobile(); const handleReset = (e) => { setSelected(-1); setRoomRow({}); } useEffect(() => { document.addEventListener("click", handleReset); return () => { document.removeEventListener("click", handleReset); resetTimer(); } }, []) const getDesktopCol = useCallback(() => { if (width >= 1264 && width <= 1564) return 6; else if (width >= 963 && width <= 1263) return 5; else if (width >= 622 && width <= 962) return 4; else if (width <= 621) return 3; return 7; }, [width]); const initGridsInfo = useCallback(() => { let parent = gameListRef.current; let totalCol = 3; if (!mobile) totalCol = getDesktopCol(); let gridBaseRatio = GameImgAspectRatio; let totalWidth = mobile ? 100 * totalCol : 160 * totalCol; if (parent != null) totalWidth = parent.clientWidth; let rowGap = parent != null ? window.getComputedStyle(parent).getPropertyValue('grid-row-gap') : "0px"; let gap = Number(rowGap.replace("px", "")); if (gap === 0 || isNaN(gap)) gap = 0.75 * 16 * fontScale; let gridsData = {}; for (let i = 1; i <= 6; i++) { let gridNum = i; let gridWidth = (totalWidth - (totalCol - 1) * gap) / totalCol; let itemWidth = gridWidth * gridNum + (gridNum - 1) * gap; let width = itemWidth + "px"; let aspectRatio = gridBaseRatio * (itemWidth / gridWidth); gridsData[i] = { width, aspectRatio }; } setGridInfo(gridsData); }, [fontScale, getDesktopCol, mobile]); useEffect(() => { initGridsInfo(); }, [initGridsInfo]); const resetTimer = () => { if (objInfo.timer != null) { clearTimeout(objInfo.timer); objInfo.timer = null; } } const checkSetRoom = (e, item, row, total) => { let target = e.target; if (target) { let rect = target.getBoundingClientRect(); let cH = document.documentElement.clientHeight; let cB = rect.bottom; if (cH - cB > 90) row = row + 1; } else row = row + 1; if (row >= total) row = total - 1; let roomRowItem = { row: row, data: item } if (roomRow.row === row) { setRoomRow({ hide: true, ...roomRowItem }); resetTimer(); objInfo.timer = setTimeout(() => { setRoomRow(roomRowItem); }, 10) } else setRoomRow(roomRowItem); } const handleSelect = (e, data, row, total) => { e.stopPropagation(); if (data.id === selected) { handleReset(); } else { setSelected(data.id); checkSetRoom(e, data, row, total); } } const handlePopSelected = (e, data, row, totall) => { e.stopPropagation(); setRoomRow({ row: -1, data: data }); setShowPopRoom(true); } const handlePopClose = () => { setRoomRow({}); setShowPopRoom(false); } const renderGameListDesktop = () => { let list = data.data; if (list == null) list = []; return ( <>