Desktop.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use client";
  2. import { useSystemStore } from "@/stores/useSystemStore";
  3. import { Button, Popup } from "antd-mobile";
  4. import { useTranslations } from "next-intl";
  5. import Image from "next/image";
  6. import { forwardRef, 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. const Desktop = forwardRef<DesktopRefProps, Props>(function Desktop(props, ref) {
  18. const prompt = useRef<Event | null>(null);
  19. const t = useTranslations("HomePage");
  20. const { isHasDesktop, setHasDesktop } = useSystemStore((state) => {
  21. return {
  22. isHasDesktop: state.isHasDesktop,
  23. setHasDesktop: state.setHasDesktop,
  24. };
  25. });
  26. const downloadHandler = () => {
  27. // @ts-ignore
  28. prompt.current?.prompt();
  29. // 不判断是否真的包含
  30. setHasDesktop(false);
  31. };
  32. const initDesktop = (e: Event) => {
  33. if (window.matchMedia("(display-mode: standalone)").matches) {
  34. setHasDesktop(false);
  35. return;
  36. }
  37. // 有pwa 则不会触发
  38. prompt.current = e;
  39. // 只触发一次
  40. if (useSystemStore.getState().isHasDesktop === undefined) {
  41. setHasDesktop(true);
  42. }
  43. };
  44. useEffect(() => {
  45. window.addEventListener("beforeinstallprompt", initDesktop);
  46. // @ts-ignore
  47. window.onappinstalled = function (ev) {
  48. // 安装完成
  49. console.log("The application was installed.");
  50. };
  51. return () => window.removeEventListener("beforeinstallprompt", initDesktop);
  52. }, []);
  53. useImperativeHandle(ref, () => {
  54. return {
  55. onClose: () => setHasDesktop(false),
  56. onOpen: () => {
  57. setHasDesktop(true);
  58. },
  59. };
  60. });
  61. return (
  62. <Popup
  63. visible={!!isHasDesktop}
  64. getContainer={null}
  65. bodyStyle={{ padding: "0.2rem", background: "#fff" }}
  66. >
  67. <div className={"flex text-[0.12rem] text-[#8d8d8d]"}>
  68. <Image
  69. src={"/icon-192x192.png"}
  70. alt={"logo"}
  71. className={"mr-[10px] flex-shrink-0"}
  72. width={80}
  73. height={80}
  74. />
  75. {t("saveTips")}
  76. </div>
  77. <div className={"mb-[0.16rem] mt-[0.1rem] text-right"}>
  78. <Button
  79. color="default"
  80. className={"mr-[10px]"}
  81. style={{ "--text-color": "#8d8d8d" }}
  82. fill="none"
  83. onClick={() => setHasDesktop(false)}
  84. >
  85. {t("cancel")}
  86. </Button>
  87. <Button
  88. color="primary"
  89. style={{
  90. "--text-color": "#ff6a01",
  91. "--border-color": "#ff6a01",
  92. "--border-radius": "10px",
  93. }}
  94. fill="outline"
  95. onClick={downloadHandler}
  96. >
  97. {t("save")}
  98. </Button>
  99. </div>
  100. </Popup>
  101. );
  102. });
  103. export default Desktop;