MobileMenu.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 border-r border-neutral-200 bg-white drawer-scrollbar-hidden"
  101. style={{
  102. top: "0px",
  103. bottom: "0px",
  104. width: "100%",
  105. height: "100%",
  106. }}
  107. >
  108. {/* <div className="h-full overflow-y-auto px-4 py-4 drawer-scrollbar-hidden">
  109. <MobileSearchBar onClose={onClose} />
  110. <h1 className="mt-4 px-2 text-2xl font-semibold text-black dark:text-white">
  111. Category
  112. </h1>
  113. <ul className="mt-2 flex w-full flex-col drawer-scrollbar-hidden">
  114. {menu.map((item: any) => (
  115. <li
  116. key={item.id + item.name}
  117. className="p-2 text-xl text-black dark:text-white"
  118. >
  119. <Link
  120. href={item.slug ? `/category/${item.slug}/${item._id}` : "/search"}
  121. aria-label={`${item?.name}`}
  122. onClick={onClose}
  123. >
  124. {item.name}{item._id} 4444
  125. </Link>
  126. </li>
  127. ))}
  128. </ul>
  129. </div> */}
  130. <div className="h-full flex flex-col overflow-hidden bg-white">
  131. <div className="flex justify-between items-center px-4 h-13.5 border-b border-[#E1E1E1FF]">
  132. <h2 className="text-base font-medium">VIP Rewards</h2>
  133. <div className="flex gap-3 items-center">
  134. {
  135. isGuest ? (
  136. <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">
  137. Register/Login
  138. </button>
  139. ) : null
  140. }
  141. <svg
  142. width="24"
  143. height="24"
  144. viewBox="0 0 24 24"
  145. fill="none"
  146. stroke="currentColor"
  147. strokeWidth="2"
  148. onClick={onClose}
  149. >
  150. <path d="M18 6L6 18M6 6l12 12" />
  151. </svg>
  152. </div>
  153. </div>
  154. <div className="flex flex-1 overflow-hidden">
  155. <aside className="w-23 border-r border-gray-200 overflow-y-auto">
  156. {menu.map((mainItem) => {
  157. // 判断条件:没有子菜单 并且 配置了url
  158. const isLinkItem = mainItem.children.edges.length === 0 && !!mainItem.url;
  159. if (isLinkItem) {
  160. return (
  161. <Link
  162. key={mainItem._id}
  163. href={mainItem.url}
  164. className={`relative block py-3 pl-4 pr-3 text-xs cursor-pointer border-r-2 border-b border-[#E1E1E1FF]
  165. ${
  166. activeMainId === mainItem._id
  167. ? "border-r-emerald-700 text-emerald-700 font-bold"
  168. : "text-gray-700 font-normal"
  169. }
  170. `}
  171. >
  172. {mainItem.name}
  173. {mainItem.showIcon && (
  174. <Image
  175. className="block"
  176. src="/image/theme/image_flame.webp"
  177. alt="userphoto"
  178. width={16}
  179. height={16}
  180. />
  181. )}
  182. </Link>
  183. );
  184. }
  185. // 不满足条件:走原来点击切换active的div逻辑
  186. return (
  187. <div
  188. key={mainItem._id}
  189. onClick={() => setActiveMainId(mainItem._id)}
  190. className={`py-3 pl-4 pr-3 text-xs cursor-pointer border-r-2 border-b border-[#E1E1E1FF]
  191. ${
  192. activeMainId === mainItem._id
  193. ? "border-r-emerald-700 text-emerald-700 font-bold"
  194. : "text-gray-700 font-normal"
  195. }
  196. `}
  197. >
  198. {mainItem.name}
  199. {mainItem.showIcon && (
  200. <Image
  201. className="block"
  202. src="/image/theme/image_flame.webp"
  203. alt="userphoto"
  204. width={16}
  205. height={16}
  206. />
  207. )}
  208. </div>
  209. );
  210. })}
  211. </aside>
  212. <main className="flex-1 overflow-y-auto p-3 pr-4">
  213. {/*顶部横幅占位 */}
  214. {activeMainMenu?.wapImage &&
  215. <div className="w-full bg-gray-100 mb-4 flex items-end">
  216. {/* <span className="text-3xl text-white pl-6 pb-4">Shop By Feature</span> */}
  217. <Image
  218. src={activeMainMenu?.wapBanner||''}
  219. alt={activeMainMenu?.name||''}
  220. className="object-cover"
  221. width={269}
  222. height={37}
  223. />
  224. </div>
  225. }
  226. {subMenuList.length === 0 ? (
  227. <div className="text-gray-500 text-xl">No sub categories</div>
  228. ) : (
  229. subMenuList.map((subItem) => {
  230. const isOpen = !!expandedSubIds[subItem._id];
  231. const cardEdges = subItem?.children?.edges ?? [];
  232. return (
  233. <div key={subItem._id} className="mb-6">
  234. <div
  235. onClick={() => toggleSubExpand(subItem._id)}
  236. className="flex justify-between items-center cursor-pointer py-2 px-3 h-10 bg-[#F9F9F9FF]"
  237. >
  238. <h3 className="text-2xl font-medium">{subItem.url ? (
  239. <Link href={subItem.url} className="inline-block">
  240. {subItem.name}
  241. </Link>
  242. ) : (
  243. subItem.name
  244. )}
  245. </h3>
  246. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
  247. className={`transition-transform duration-300 ${isOpen ? "rotate-0" : "rotate-180"}`}
  248. >
  249. <path stroke="rgba(0, 0, 0, 1)" strokeWidth="1.5" strokeLinejoin="round" strokeLinecap="square" d="M18 15L12 9L6 15"></path>
  250. </svg>
  251. </div>
  252. {/*二级展开之后渲染三级卡片网格 */}
  253. {isOpen && cardEdges.length > 0 && (
  254. <div className="grid grid-cols-3 gap-2 pt-3">
  255. {cardEdges.map((edge) => {
  256. const cardItem = edge.node;
  257. return (
  258. <Link
  259. href={cardItem.url}
  260. key={cardItem._id}
  261. className="flex flex-col items-center cursor-pointer"
  262. >
  263. <div className="w-full relative bg-gray-200 mb-1 overflow-hidden">
  264. <Image
  265. src={cardItem.wapImage||''}
  266. alt={cardItem.name||''}
  267. width={84}
  268. height={112}
  269. className="object-cover"
  270. />
  271. {
  272. cardItem.showIcon&&(
  273. <Image
  274. className=" block absolute top-0 left-0"
  275. src="/image/theme/image_hot.webp"
  276. alt={"userphoto"}
  277. width={30}
  278. height={14}
  279. />
  280. )
  281. }
  282. </div>
  283. <span className="text-xs font-normal text-center">{cardItem.name}</span>
  284. </Link>
  285. );
  286. })}
  287. </div>
  288. )}
  289. {isOpen && cardEdges.length === 0 && (
  290. <div className="py-3 text-gray-500">No feature items</div>
  291. )}
  292. </div>
  293. );
  294. })
  295. )}
  296. </main>
  297. </div>
  298. {/* =========底部固定货币选择栏 ========= */}
  299. <div className="h-12 border-t border-gray-200 flex items-center px-4 gap-4">
  300. <CurrencySwitch/>
  301. </div>
  302. </div>
  303. </motion.div>
  304. </>
  305. {/* )} */}
  306. </AnimatePresence>
  307. </>
  308. );
  309. }