index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use client";
  2. import { usePathname, useRouter } from "@/i18n/routing";
  3. import { useWalletStore } from "@/stores/useWalletStore";
  4. import { getToken } from "@/utils/Cookies";
  5. import clsx from "clsx";
  6. import { useTranslations } from "next-intl";
  7. import { FC, PropsWithChildren, ReactNode, useEffect, useState } from "react";
  8. import styles from "./style.module.scss";
  9. // import { getToken } from "@/utils/Cookies";
  10. import clearCache from "./actions";
  11. /**
  12. * @description 自定义头部返回组件
  13. * @param {string} title header标题
  14. * @param {ReactNode} children 插槽内容
  15. * @param {() => ReactNode} headerRender 自定义渲染
  16. */
  17. export interface HeaderBackProps {
  18. title?: string;
  19. children?: ReactNode;
  20. showBack?: Boolean;
  21. headerRender?: () => ReactNode;
  22. className?: string;
  23. useBg?: Boolean;
  24. Right?: () => ReactNode; // 右侧自定义渲染,优先级高于childre
  25. }
  26. const HeaderBack: FC<PropsWithChildren<HeaderBackProps>> = ({
  27. title = "",
  28. showBack = true,
  29. children,
  30. className,
  31. useBg = false,
  32. Right,
  33. }) => {
  34. const t = useTranslations("HeaderBack");
  35. const wallet = useWalletStore((state) => state.wallet);
  36. const iconClassName1 = clsx(
  37. {
  38. [styles.iconfontIcon1]: true,
  39. },
  40. "iconfont icon-xiangzuo1 text-[#87ccda]"
  41. );
  42. const iconClassName2 = clsx(
  43. {
  44. [styles.iconfontIcon2]: true,
  45. },
  46. "iconfont icon-company_nav_icon_home text-[#87ccda]"
  47. );
  48. let pathname = usePathname();
  49. let [selfTitle, setSelfTitle] = useState("");
  50. const setSelfTitleFun = () => {
  51. if (pathname == "/deposit") {
  52. setSelfTitle(t("Depósito"));
  53. return;
  54. }
  55. setSelfTitle("");
  56. };
  57. useEffect(() => {
  58. setSelfTitleFun();
  59. // eslint-disable-next-line react-hooks/exhaustive-deps
  60. }, [pathname]);
  61. const router = useRouter();
  62. const goPage = async (path = "") => {
  63. if (path) {
  64. await clearCache();
  65. router.replace("/");
  66. return;
  67. }
  68. router.back();
  69. };
  70. const cls = clsx(styles.headerBack, className, useBg && styles.useBg);
  71. return (
  72. <div className={cls}>
  73. <div className={styles.left}>
  74. {showBack && <span className={iconClassName1} onClick={() => goPage()}></span>}
  75. </div>
  76. <div className={clsx(styles.title, "text-[#87ccda]")}>
  77. {(title || selfTitle) && <span>{title || selfTitle}</span>}
  78. </div>
  79. <div className={styles.content}>{children}</div>
  80. {getToken() && (
  81. <div>
  82. {wallet.is_open_no_bonus === 1 && (
  83. <img src="/img/no_bouns.png" alt="" className="h-[20px]" />
  84. )}
  85. </div>
  86. )}
  87. {!Right && (
  88. <span className={styles.right} onClick={() => goPage("home")}>
  89. <span className={iconClassName2}></span>
  90. </span>
  91. )}
  92. {Right && Right()}
  93. </div>
  94. );
  95. };
  96. export default HeaderBack;