Card.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use client";
  2. import { GameListRep, getGameDetailApi } from "@/api/home";
  3. import { useRouter } from "@/i18n";
  4. import { useGlobalStore } from "@/stores";
  5. import { Button, Modal, ModalBody, ModalContent, useDisclosure } from "@nextui-org/react";
  6. import { useTranslations } from "next-intl";
  7. import { FC, PropsWithChildren, ReactNode, useRef } from "react";
  8. import styles from "./style.module.scss";
  9. export interface CardProps {
  10. item?: GameListRep;
  11. render?: (value: GameListRep) => ReactNode;
  12. }
  13. const Card: FC<PropsWithChildren<CardProps>> = (props) => {
  14. const { render, item } = props;
  15. const { isOpen, onOpen, onOpenChange } = useDisclosure();
  16. const app: HTMLElement = document.querySelector("#app")!;
  17. const t = useTranslations("Game");
  18. const urlRef = useRef<string>("");
  19. const state = useGlobalStore();
  20. const router = useRouter();
  21. const handler = (game: GameListRep) => {
  22. onOpen();
  23. const params = {
  24. id: game.id,
  25. };
  26. getGameDetailApi(params).then((res) => {
  27. urlRef.current = res.data.game_url;
  28. });
  29. };
  30. const playGameHandler = () => {
  31. const { token } = state;
  32. if (token) {
  33. window.open(urlRef.current);
  34. } else {
  35. router.push("/login");
  36. }
  37. };
  38. return (
  39. <>
  40. {render ? (
  41. render(item!)
  42. ) : (
  43. <div className={styles.cardWrap} onClick={() => handler(item!)}>
  44. <img src={item?.game_icon} alt={item?.game_name_cn} className={"h-1/1"} />
  45. </div>
  46. )}
  47. <Modal
  48. isOpen={isOpen}
  49. portalContainer={app}
  50. placement={"bottom"}
  51. onOpenChange={onOpenChange}
  52. classNames={{
  53. header: "px-0 pb-0 ",
  54. wrapper: "w-[100vw] m-auto",
  55. closeButton: "text-[20px] top-[0rem] right-0 text-[#fff] ",
  56. backdrop: "absolute top-0 left-0 sm:my-0 ",
  57. base: "my-0 my-0 sm:mx-[0] mx-0 sm:my-0 max-w-[4.02rem] ",
  58. body: "py-[12px]",
  59. }}
  60. >
  61. <ModalContent>
  62. {(onClose) => (
  63. <>
  64. <ModalBody className={"w-[4rem] text-[0.1111rem]"}>
  65. <div className={"w-1/1 flex flex-1"}>
  66. <div className={styles.cardWrap}>
  67. <img src={item?.game_icon} alt={item?.game_name_cn} />
  68. </div>
  69. <div className={styles.cardWrapGmeInfo}>
  70. <p className={"h-[0.6rem]"}>{item?.game_name_cn}</p>
  71. <div className={"flex w-[2.2rem] justify-around"}>
  72. <Button
  73. onClick={playGameHandler}
  74. className={
  75. "h-[0.39rem] w-[0.89rem] rounded-[0.05rem] text-[0.15rem]" +
  76. " bg-[#3a3a3a]" +
  77. " font-bold"
  78. }
  79. >
  80. {t("demo")}
  81. </Button>
  82. <Button
  83. onClick={playGameHandler}
  84. className={`h-[0.39rem] w-[0.89rem] rounded-[0.05rem] bg-[#009d80] text-[0.15rem] font-bold`}
  85. >
  86. {t("join")}
  87. </Button>
  88. </div>
  89. </div>
  90. </div>
  91. </ModalBody>
  92. </>
  93. )}
  94. </ModalContent>
  95. </Modal>
  96. </>
  97. );
  98. };
  99. export default Card;