index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import Link from "next/link";
  2. import { Suspense } from "react";
  3. // import Search from "./Search";
  4. // import { SearchSkeleton } from "@/components/common/skeleton/SearchSkeleton";
  5. import LogoIcon from "@components/common/icons/LogoIcon";
  6. import MobileMenuTrigger from "./MobileMenuTrigger";
  7. import { CartAndUserActions } from "./CartAndUserActions";
  8. import { NavigationSkeleton } from "./NavigationSkeleton";
  9. import { ActionsSkeleton } from "./ActionsSkeleton";
  10. import { NavbarErrorBoundary } from "@/components/error/ErrorBoundary";
  11. import {
  12. // GET_TREE_CATEGORIES,
  13. GET_TREEFRONT_MENUS } from "@/graphql";
  14. // import { cachedGraphQLRequest } from "@utils/hooks/useCache";
  15. import { serverGraphqlFetch } from "@utils/bagisto/index";
  16. // import { TreeCategoriesResponse } from "@/types/theme/category-tree";
  17. import NavbarSearchIcon from "./NavbarSearchIcon"
  18. import type { GetTreefrontMenusResult } from "@/types/theme/menus";
  19. import { authOptions } from "@utils/auth";
  20. import { getServerSession } from "next-auth";
  21. export default async function Navbar() {
  22. const session = await getServerSession(authOptions);// 游客是null
  23. const isGuest = session === null;
  24. // const { data } = await cachedGraphQLRequest<TreeCategoriesResponse>(
  25. // "category",
  26. // GET_TREE_CATEGORIES,
  27. // { parentId: 1 },
  28. // );
  29. // console.log('TreeCategoriesResponse --- ',data)
  30. // const categories = data?.treeCategories || [];
  31. const { data: menudata } = await serverGraphqlFetch<
  32. GetTreefrontMenusResult
  33. >({
  34. query: GET_TREEFRONT_MENUS,
  35. });
  36. console.log('GetTreefrontMenusResult --- ',menudata)
  37. const menudatas =menudata? menudata.treefrontMenus:[];
  38. // const filteredCategories = categories
  39. // .filter((cat: any) => cat.id !== "1")
  40. // .map((cat: any) => {
  41. // const translation = cat.translation;
  42. // return {
  43. // id: cat.id,
  44. // _id: cat._id,
  45. // name: translation?.name || "",
  46. // slug: translation?.slug || "",
  47. // };
  48. // })
  49. // .filter((item: any) => item.name && item.slug);
  50. // const menuData = [
  51. // { id: "all", name: "All", slug: "" },
  52. // ...filteredCategories.slice(0, 3),
  53. // ];
  54. return (
  55. <NavbarErrorBoundary>
  56. <header className="sticky top-0 z-10">
  57. <nav className="relative flex flex-col items-center justify-between gap-4 bg-neutral-50 p-4 pt-3 pb-3 dark:bg-neutral-900 md:flex-row lg:px-6 lg:py-4">
  58. <div className="flex w-full items-center justify-between gap-0 sm:gap-4">
  59. {/* 1. THE STATIC SHELL (Visible Instantly) */}
  60. <div className="flex items-center max-w-fit gap-5 xl:gap-6">
  61. {/* 2. STATIC HOLE: Categories (Suspended) */}
  62. <Suspense fallback={<NavigationSkeleton />}>
  63. <MobileMenuTrigger isGuest={isGuest} menuData={menudatas} />
  64. </Suspense>
  65. <div className="flex-1 justify-center flex">
  66. {/* <Suspense fallback={<SearchSkeleton />}>
  67. <Search search={false} />
  68. </Suspense> */}
  69. <NavbarSearchIcon/>
  70. </div>
  71. </div>
  72. <Link
  73. className="flex-initial h-9 w-36.5"
  74. href="/"
  75. aria-label="Go to homepage"
  76. >
  77. <LogoIcon />
  78. </Link>
  79. {/* 3. DYNAMIC HOLE: Auth & Cart (Blocked by Session/Cookies) */}
  80. <Suspense fallback={<ActionsSkeleton />}>
  81. <CartAndUserActions />
  82. </Suspense>
  83. </div>
  84. </nav>
  85. </header>
  86. </NavbarErrorBoundary>
  87. );
  88. }