index.jsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import styles from "@components/GameItem/styles.module.scss";
  2. import {NavLink, useNavigate} from "react-router-dom";
  3. import {LazyLoadImage} from "react-lazy-load-image-component";
  4. import symbol from "@assets/symbol-defs.svg";
  5. import React from "react";
  6. import {ROUTE_PATH} from "@constants/routes";
  7. import {localStorageSet} from "@utils/helpers";
  8. import {LOCAL_GAME_INFO, LOCAL_GAME_URL} from "@constants/localkey";
  9. import Imgix from "@components/Imgix";
  10. import LazyImgix from "@components/Imgix/LazyImgix";
  11. import {ReactComponent as UNLikeIcon} from "@assets/singlesvg/xiai_bai.svg";
  12. import {ReactComponent as LikeIcon} from "@assets/singlesvg/xiai_hong.svg";
  13. import {useDispatch, useSelector} from "react-redux";
  14. import {getGameLike, isLogined, updateGameLike} from "@features/mainSlice";
  15. import {updatePageGameLike} from "@features/pageSlice";
  16. import SvgIcon from "@components/SvgIcon";
  17. import {reqUpdateFavoInfo} from "@features/api";
  18. import styled from 'styled-components/macro';
  19. export const GameImgAspectRatio=384/516;
  20. const StyledGridItem = styled.div`
  21. ${(props) => {
  22. let whStyle=props.$whStyle;
  23. let width='';
  24. let aspectRatio='';
  25. if (whStyle!=null){
  26. width=`width:${whStyle.width} !important;`;
  27. aspectRatio=`aspect-ratio:${whStyle.aspectRatio} !important;`;
  28. }
  29. return `
  30. ${width}
  31. ${aspectRatio}
  32. `
  33. }}
  34. `
  35. const StyledImgWrap=styled.button`
  36. aspect-ratio: ${GameImgAspectRatio};
  37. ${(props) => {
  38. let whStyle=props.$whStyle;
  39. let aspectRatio='';
  40. if (whStyle!=null){
  41. aspectRatio=`aspect-ratio:${whStyle.aspectRatio} !important;`;
  42. }
  43. return `
  44. ${aspectRatio}
  45. `
  46. }}
  47. `
  48. const GameItem=({data,handleRoom,whStyle,...props})=>{
  49. const navigate=useNavigate();
  50. const like=useSelector(getGameLike(data.id));
  51. const isLogin=useSelector(isLogined);
  52. const dispatch=useDispatch();
  53. const renderOlicon=()=>{
  54. return (
  55. <svg xmlns="http://www.w3.org/2000/svg" width="9" height="10" viewBox="0 0 9 10" fill="none"><path d="M7.78243 7.00126C7.98931 8.18376 7.16525 9.0422 6.01681 9.23532C4.90775 9.42188 3.09993 9.42188 1.99087 9.23532C0.842435 9.04251 0.0183723 8.18438 0.225247 7.00157C0.400872 5.99813 1.25118 5.37282 2.28993 5.42657C3.04868 5.46594 3.51493 5.56469 4.05462 5.56469C4.60056 5.56469 4.96431 5.46594 5.71743 5.42657C6.75587 5.37219 7.60712 5.99782 7.78243 7.00126ZM3.86275 0.907195C4.95368 0.907195 5.83837 1.79157 5.83837 2.88313C5.83837 3.9747 4.95368 4.85907 3.86275 4.85907C2.7715 4.85907 1.88681 3.97438 1.88681 2.88313C1.88681 1.79188 2.7715 0.907195 3.86275 0.907195Z" fill="url(#paint0_linear_451_3515)"></path><path d="M7.42806 5.14407C7.95931 5.2425 8.59868 5.525 8.85493 6.28813C9.09181 6.99375 8.81275 7.54688 8.49493 7.91282C8.39368 8.02938 8.19087 7.92813 8.24118 7.78532C8.37181 7.41594 8.42431 6.94407 8.21993 6.42157C8.02118 5.91313 7.65337 5.59282 7.31962 5.39563C7.18025 5.31344 7.26681 5.11407 7.42806 5.14407ZM5.63118 0.938442C5.47243 0.825317 5.58681 0.582817 5.77868 0.631255C5.98087 0.682192 6.189 0.761567 6.38306 0.883442C7.02337 1.28657 7.33993 1.99969 7.22712 2.79188C7.17462 3.16157 7.01056 3.44907 6.82587 3.66625C6.70431 3.80969 6.46431 3.69032 6.51431 3.51157C6.63181 3.09219 6.6665 2.55657 6.44056 1.96813C6.25431 1.48344 5.93587 1.15625 5.63118 0.938442Z" fill="#26AB4B"></path><defs><linearGradient id="paint0_linear_451_3515" x1="5.21662" y1="0.333274" x2="5.21662" y2="9.37524" gradientUnits="userSpaceOnUse"><stop stop-color="#85F25E"></stop><stop offset="1" stop-color="#33EF00"></stop></linearGradient></defs></svg>
  56. );
  57. }
  58. const formatNum=(num)=>{
  59. num=Number(num);
  60. let snum=num+"";
  61. if (num>=1000){
  62. snum=(num/1000).toFixed(2)+"K";
  63. }
  64. return snum;
  65. }
  66. const getRoom=()=>{
  67. let room=[];
  68. let level_info=data.level_info;
  69. if (level_info){
  70. for (const key in level_info) {
  71. room.push({key:key,value:level_info[key]});
  72. }
  73. }
  74. return room;
  75. }
  76. const saveGameInfo=(item)=>{
  77. let info={
  78. data:item
  79. }
  80. localStorageSet(LOCAL_GAME_INFO+item.id,JSON.stringify(info));
  81. }
  82. const onEnterGame=()=>{
  83. let id=data.id;
  84. let url=data.link;
  85. localStorageSet(LOCAL_GAME_URL+id,url);
  86. saveGameInfo(data);
  87. let path=ROUTE_PATH.GAME+"/"+id;
  88. global.authLockPath=path;
  89. navigate(path);
  90. }
  91. const handleEnterRoom=(e)=>{
  92. if (handleRoom==null)onEnterGame();
  93. else
  94. {
  95. let room=getRoom();
  96. if (room.length>1)handleRoom(e,data);
  97. else onEnterGame();
  98. }
  99. }
  100. const handleLike=()=>{
  101. let info={id:data.id,data:data,like:(1-like)}
  102. dispatch(updateGameLike(info));
  103. dispatch(updatePageGameLike(info));
  104. if(isLogin)dispatch(reqUpdateFavoInfo(info));
  105. }
  106. // return (
  107. // <div className={styles.grid_item}>
  108. // <button onClick={()=>{
  109. // onEnterGame();
  110. // }} className={styles.game_img_wrap}>
  111. // <LazyLoadImage
  112. // src={data.img}
  113. // alt={"game-icon"}
  114. // effect={"blur"}
  115. // wrapperClassName={`lazy-image ${styles.game_img}`}
  116. // />
  117. // <div className={styles.online}>
  118. // {
  119. // renderOlicon()
  120. // }
  121. // <span>{formatNum(data.online)}</span>
  122. // </div>
  123. // </button>
  124. // <div className={styles.bot_info}>
  125. // <div className={styles.game_tit}>
  126. // <NavLink className={styles.game_desc}>
  127. // {data.brand}
  128. // </NavLink>
  129. // <button>
  130. // <svg className={`${styles.game_desc_icon}`}>
  131. // <use xlinkHref={`${symbol}#icon_Inform`} />
  132. // </svg>
  133. // </button>
  134. // </div>
  135. // </div>
  136. // </div>
  137. // )
  138. const getImgixW=()=>{
  139. let baseAspectRatio=GameImgAspectRatio;
  140. let whAspectRatio=baseAspectRatio;
  141. if (whStyle!=null&&whStyle.aspectRatio!=null)whAspectRatio=Number(whStyle.aspectRatio);
  142. let width=parseInt(384*(whAspectRatio/baseAspectRatio));
  143. return width;
  144. }
  145. return (
  146. <StyledGridItem $whStyle={whStyle} className={`${styles.grid_item}`} {...props}>
  147. <StyledImgWrap $whStyle={whStyle} className={styles.game_img_wrap} onClick={(e)=>{
  148. // onEnterGame();
  149. // if (handleRoom)handleRoom(e,data);
  150. handleEnterRoom(e)
  151. }}>
  152. {/*<LazyLoadImage*/}
  153. {/* src={data.img}*/}
  154. {/* alt={"game-icon"}*/}
  155. {/* effect={"blur"}*/}
  156. {/* wrapperClassName={`lazy-image ${styles.game_img}`}*/}
  157. {/*/>*/}
  158. {/*<Imgix className={`lazyload ${styles.game_img}`} src={data.img} alt={"game-icon"} width={192}></Imgix>*/}
  159. <LazyImgix className={`lazyload ${styles.game_img}`} src={data.img} alt={"game-icon"} width={getImgixW()}></LazyImgix>
  160. </StyledImgWrap>
  161. <button className={styles.favorite_con} onClick={handleLike}>
  162. {
  163. // like?(<LikeIcon></LikeIcon>):(<UNLikeIcon></UNLikeIcon>)
  164. <SvgIcon className={`${styles.icon} ${like?styles.icon_active:''}`} herf={like?"icon_Favorites1":"icon_Favorites"}></SvgIcon>
  165. }
  166. </button>
  167. </StyledGridItem>
  168. )
  169. }
  170. export default GameItem;