index.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use client";
  2. import useDesktop from "@/hooks/useDesktop";
  3. import { Button, Popup } from "antd-mobile";
  4. import { useTranslations } from "next-intl";
  5. import Image from "next/image";
  6. import { forwardRef, memo, useEffect, useImperativeHandle, useRef } from "react";
  7. /**
  8. * @description 检测pwa是否下载
  9. * if 下载 不弹窗 , else 弹窗
  10. *
  11. */
  12. export interface DesktopRefProps {
  13. onOpen: () => void;
  14. onClose: () => void;
  15. }
  16. interface Props {
  17. source?: "page" | "components";
  18. }
  19. const Desktop = forwardRef<DesktopRefProps, Props>(function Desktop(props, ref) {
  20. const { source = "page" } = props;
  21. const { cancel, isHasDesktop, downloadHandler, setHasDesktop } = useDesktop(source);
  22. const elementRef = useRef<HTMLElement | null>(null);
  23. const t = useTranslations("HomePage");
  24. useEffect(() => {
  25. elementRef.current = document.getElementById("app");
  26. }, []);
  27. useImperativeHandle(ref, () => {
  28. return {
  29. onClose: () => setHasDesktop(false),
  30. onOpen: () => {
  31. setHasDesktop(true);
  32. },
  33. };
  34. });
  35. return (
  36. <Popup
  37. visible={!!isHasDesktop}
  38. getContainer={elementRef.current}
  39. bodyStyle={{ padding: "0.2rem", background: "#fff" }}
  40. >
  41. <div className={"flex text-[0.12rem] text-[#8d8d8d]"}>
  42. <Image
  43. src={"/icon-192x192.png"}
  44. alt={"logo"}
  45. className={"mr-[10px] flex-shrink-0"}
  46. width={80}
  47. height={80}
  48. />
  49. {t("saveTips")}
  50. </div>
  51. <div className={"mb-[0.16rem] mt-[0.1rem] text-right"}>
  52. <Button
  53. color="default"
  54. className={"mr-[10px]"}
  55. style={{ "--text-color": "#8d8d8d" }}
  56. fill="none"
  57. onClick={cancel}
  58. >
  59. {t("cancel")}
  60. </Button>
  61. <Button
  62. color="primary"
  63. style={{
  64. "--text-color": "#ff6a01",
  65. "--border-color": "#ff6a01",
  66. "--border-radius": "10px",
  67. }}
  68. fill="outline"
  69. onClick={downloadHandler}
  70. >
  71. {t("save")}
  72. </Button>
  73. </div>
  74. </Popup>
  75. );
  76. });
  77. export default memo(Desktop);