MobileMenu.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. "use client";
  2. import { AnimatePresence, motion } from "framer-motion";
  3. // import Link from "next/link";
  4. // import { MobileSearchBar } from "./MobileSearch";
  5. import { useBodyScrollLock } from "@utils/hooks/useBodyScrollLock";
  6. import{useState} from "react";
  7. import {useLoginModal} from "@/providers/LoginModalProvider";
  8. import { useCustomToast } from "@/utils/hooks/useToast";
  9. import { confirmDialog } from "@/components/theme/ui/kernel/confirm/api";
  10. import { useRouter } from 'next/navigation';
  11. import Image from "next/image";
  12. import type { TreefrontMenu } from "@/types/theme/menus";
  13. import {CurrencySwitch} from "@/components/common/CurrencySwitch/CurrencySwitch";
  14. interface MobileMenuProps {
  15. menu: TreefrontMenu[];
  16. isOpen: boolean;
  17. onClose: () => void; // 接收关闭函数
  18. isGuest:boolean;
  19. }
  20. export default function MobileMenu({ menu, isOpen, onClose,isGuest }: MobileMenuProps) {
  21. const {openLoginModal} = useLoginModal();
  22. const { showToast } = useCustomToast();
  23. const router = useRouter();
  24. // 当前选中一级菜单ID
  25. const [activeMainId, setActiveMainId] = useState(menu[0]._id);
  26. // 存储右侧二级菜单展开状态集合 key:二级id value:是否展开boolean
  27. const [expandedSubIds, setExpandedSubIds] = useState<Record<string, boolean>>({});
  28. // 获取选中的一级菜单
  29. const activeMainMenu = menu.find((item) => item._id === activeMainId);
  30. // 当前一级下面全部二级菜单列表
  31. const subMenuList = activeMainMenu?.children?.edges?.map((e) => e.node) ?? [];
  32. // 切换右侧二级菜单展开/收起
  33. const toggleSubExpand = (subId: number) => {
  34. setExpandedSubIds((prev) => ({
  35. ...prev,
  36. [subId]: !prev[subId],
  37. }));
  38. };
  39. useBodyScrollLock(isOpen);
  40. const guestUserClick = () => {
  41. openLoginModal({
  42. onFinalResult: async (res) => {
  43. if(res.mergeCartSuccess) { // 购物车合并成功
  44. // if(!res.cartAfterMerge?.isVip && res.cartAfterMerge?.vipPlusAmount === 0) {
  45. // await switchVipPlus(true);
  46. // }
  47. return
  48. } else { // 购物车合并失败
  49. // 三种 情况: 登录成功; 注册成功;
  50. if(res.loginSuccess) {
  51. // 登录成功 需要刷新页面
  52. showToast('You have logged in successfully. But something wrong','danger');
  53. await confirmDialog({
  54. title: "Warning",
  55. content: "You have logged in successfully. But something wrong. You must refresh the page.",
  56. noCancel: true,
  57. });
  58. window.location.reload();
  59. }
  60. if(res.registerSuccess && !res.loginSuccess) {
  61. // 注册成功,去登录
  62. await confirmDialog({
  63. title: "Warning",
  64. content: "You have registered successfully. But something wrong. You have to login.",
  65. noCancel: true,
  66. });
  67. router.replace('/customer/login');
  68. }
  69. }
  70. }
  71. });
  72. };
  73. return (
  74. <>
  75. <AnimatePresence>
  76. {/* {isOpen && ( */}
  77. <>
  78. <motion.div
  79. // initial={{ opacity: 0 }}
  80. // animate={{ opacity: 1 }}
  81. initial={false}
  82. animate={{
  83. opacity: isOpen ? 1 : 0,
  84. pointerEvents: isOpen ? "auto" : "none"
  85. }}
  86. // exit={{ opacity: 0 }}
  87. onClick={onClose}
  88. className="fixed inset-0 z-40 bg-transparent lg:hidden"
  89. style={{ top: "68px", bottom: "64px" }}
  90. />
  91. <motion.div
  92. // initial={{ x: "-100%" }}
  93. // animate={{ x: 0 }}
  94. // exit={{ x: "-100%" }}
  95. initial={false}
  96. animate={{
  97. x: isOpen ? 0 : "-100%"
  98. }}
  99. transition={{ type: "spring", damping: 25, stiffness: 200 }}
  100. className="fixed left-0 z-50 w-full max-w-[448px] border-r border-neutral-200 bg-white dark:border-neutral-800 dark:bg-black lg:hidden drawer-scrollbar-hidden"
  101. style={{
  102. top: "0px",
  103. bottom: "0px",
  104. width: "100%",
  105. maxWidth: "448px",
  106. height: "calc(var(--visual-viewport-height) - 132px)",
  107. }}
  108. >
  109. {/* <div className="h-full overflow-y-auto px-4 py-4 drawer-scrollbar-hidden">
  110. <MobileSearchBar onClose={onClose} />
  111. <h1 className="mt-4 px-2 text-2xl font-semibold text-black dark:text-white">
  112. Category
  113. </h1>
  114. <ul className="mt-2 flex w-full flex-col drawer-scrollbar-hidden">
  115. {menu.map((item: any) => (
  116. <li
  117. key={item.id + item.name}
  118. className="p-2 text-xl text-black dark:text-white"
  119. >
  120. <Link
  121. href={item.slug ? `/category/${item.slug}/${item._id}` : "/search"}
  122. aria-label={`${item?.name}`}
  123. onClick={onClose}
  124. >
  125. {item.name}{item._id} 4444
  126. </Link>
  127. </li>
  128. ))}
  129. </ul>
  130. </div> */}
  131. <div className="h-screen flex flex-col overflow-hidden bg-white">
  132. <div className="flex justify-between items-center px-4 h-13.5 border-b border-[#E1E1E1FF]">
  133. <h2 className="text-base font-medium">VIP Rewards</h2>
  134. <div className="flex gap-3 items-center">
  135. {
  136. isGuest ? (
  137. <button onClick={guestUserClick} className="bg-emerald-700 text-white w-25.75 h-9 flex items-center justify-center rounded-full text-xs font-normal">
  138. Register/Login
  139. </button>
  140. ) : null
  141. }
  142. <svg
  143. width="24"
  144. height="24"
  145. viewBox="0 0 24 24"
  146. fill="none"
  147. stroke="currentColor"
  148. strokeWidth="2"
  149. onClick={onClose}
  150. >
  151. <path d="M18 6L6 18M6 6l12 12" />
  152. </svg>
  153. </div>
  154. </div>
  155. <div className="flex flex-1 overflow-hidden">
  156. <aside className="w-23 border-r border-gray-200 overflow-y-auto">
  157. {menu.map((mainItem) => (
  158. <div
  159. key={mainItem._id}
  160. onClick={() => setActiveMainId(mainItem._id)}
  161. className={`py-3 pl-4 pr-3 text-xs cursor-pointer border-r-2 border-b border-[#E1E1E1FF]
  162. ${
  163. activeMainId === mainItem._id
  164. ? "border-r-emerald-700 text-emerald-700 font-bold"
  165. : " text-gray-700 font-normal"
  166. }
  167. `}
  168. >
  169. {mainItem.name} {mainItem.showIcon&&(<Image
  170. className=" block"
  171. src="/image/theme/image_flame.webp"
  172. alt={"userphoto"}
  173. width={16}
  174. height={16}
  175. />)}
  176. </div>
  177. ))}
  178. </aside>
  179. <main className="flex-1 overflow-y-auto p-3 pr-4">
  180. {/*顶部横幅占位 */}
  181. <div className="w-full bg-gray-100 mb-4 flex items-end">
  182. {/* <span className="text-3xl text-white pl-6 pb-4">Shop By Feature</span> */}
  183. <Image
  184. src={activeMainMenu?.wapImage||''}
  185. alt={activeMainMenu?.name||''}
  186. className="object-cover"
  187. width={269}
  188. height={37}
  189. />
  190. </div>
  191. {subMenuList.length === 0 ? (
  192. <div className="text-gray-500 text-xl">No sub categories</div>
  193. ) : (
  194. subMenuList.map((subItem) => {
  195. const isOpen = !!expandedSubIds[subItem._id];
  196. const cardEdges = subItem?.children?.edges ?? [];
  197. return (
  198. <div key={subItem._id} className="mb-6">
  199. <div
  200. onClick={() => toggleSubExpand(subItem._id)}
  201. className="flex justify-between items-center cursor-pointer py-2 px-3 h-10 bg-[#F9F9F9FF]"
  202. >
  203. <h3 className="text-2xl font-medium">{subItem.name}</h3>
  204. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  205. className={`transition-transform duration-300 ${isOpen ? "rotate-0" : "rotate-180"}`}
  206. >
  207. <path stroke="rgba(0, 0, 0, 1)" stroke-width="1.5" stroke-linejoin="round" stroke-linecap="square" d="M18 15L12 9L6 15"></path>
  208. </svg>
  209. </div>
  210. {/*二级展开之后渲染三级卡片网格 */}
  211. {isOpen && cardEdges.length > 0 && (
  212. <div className="grid grid-cols-3 gap-2 pt-3">
  213. {cardEdges.map((edge) => {
  214. const cardItem = edge.node;
  215. return (
  216. <div
  217. key={cardItem._id}
  218. className="flex flex-col items-center cursor-pointer"
  219. >
  220. <div className="w-full relative bg-gray-200 mb-1 overflow-hidden">
  221. <Image
  222. src={cardItem.image||''}
  223. alt={cardItem.name||''}
  224. width={84}
  225. height={112}
  226. className="object-cover"
  227. />
  228. {
  229. cardItem.showIcon&&(
  230. <Image
  231. className=" block absolute top-0 left-0"
  232. src="/image/theme/image_hot.webp"
  233. alt={"userphoto"}
  234. width={30}
  235. height={14}
  236. />
  237. )
  238. }
  239. </div>
  240. <span className="text-xs font-normal text-center">{cardItem.name}</span>
  241. </div>
  242. );
  243. })}
  244. </div>
  245. )}
  246. {isOpen && cardEdges.length === 0 && (
  247. <div className="py-3 text-gray-500">No feature items</div>
  248. )}
  249. </div>
  250. );
  251. })
  252. )}
  253. </main>
  254. </div>
  255. {/* =========底部固定货币选择栏 ========= */}
  256. <div className="h-12 border-t border-gray-200 flex items-center px-4 gap-4">
  257. <CurrencySwitch/>
  258. </div>
  259. </div>
  260. </motion.div>
  261. </>
  262. {/* )} */}
  263. </AnimatePresence>
  264. </>
  265. );
  266. }