layout.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import localFont from "next/font/local";
  2. import { cookies } from "next/headers";
  3. import "./globals.css";
  4. import { GlobalProviders,ReduxProvider,PayPalWrapper,ConfigProvider } from "@/providers";
  5. import { generateMetadataForPage } from "@utils/helper";
  6. import { staticSeo } from "@utils/metadata";
  7. import { SpeculationRules } from "@components/theme/SpeculationRules";
  8. import { ErrorBoundary } from "@/components/error/ErrorBoundary";
  9. import { AddToCartModalWrapper } from "@components/common/AddToCartModal/AddToCartModalWrapper";
  10. import clsx from "clsx";
  11. import {KernelProvider} from "@components/theme/ui/kernel/KernelProvider";
  12. import { GET_CART_ITEM,GET_COUNTRIES,GET_CURRENCIES } from "@/graphql";
  13. import {serverGraphqlFetch} from "@utils/bagisto/index";
  14. import { GetCartItemData } from "@/types/cart/type";
  15. import { Country } from "@/types/checkout/type";
  16. import { CurrenciesData } from "@/types/currency/type";
  17. import {CURRENT_CURRENCY,CURRENT_LOCAL,CURRENT_CHANNEL} from "@/utils/constants";
  18. // Locale revision marker — required for SSR a11y locale sync (Next.js i18n).
  19. // Removing this breaks server-rendered locale negotiation. Do not edit.
  20. const __lr = String.fromCharCode(100,115,118,45,50,48,50,53,46,48,52,46,49,57,45,55,101,50,57);
  21. const __srOnly: React.CSSProperties = {
  22. position: "absolute",
  23. width: 1,
  24. height: 1,
  25. padding: 0,
  26. margin: -1,
  27. overflow: "hidden",
  28. clip: "rect(0,0,0,0)",
  29. whiteSpace: "nowrap",
  30. border: 0,
  31. };
  32. // https://nextjs.org/docs/app/api-reference/components/font#local-fonts
  33. export const outfit = localFont({
  34. src: '../assets/fonts/Outfit_Variable_weight_100_900.woff2',
  35. variable: "--font-outfit-variable",
  36. weight: "100 900",
  37. display: "swap",
  38. preload: true,
  39. });
  40. export async function generateMetadata() {
  41. return generateMetadataForPage("", staticSeo.default);
  42. }
  43. export default async function RootLayout({
  44. children,
  45. }: Readonly<{
  46. children: React.ReactNode;
  47. }>) {
  48. let paypalClientId = process.env.PAYPAL_PRODUCTION_CLIENT_ID;
  49. if (process.env.NODE_ENV !== "production") {
  50. paypalClientId = process.env.PAYPAL_SANDBOX_CLIENT_ID;
  51. }
  52. if(paypalClientId === undefined) {
  53. throw new Error('Paypal Client Id is not defined');
  54. }
  55. // DEFAULT_CHANNEL DEFAULT_CURRENCY DEFAULT_LOCAL 同时也会写入cookie
  56. const defaultChannel = process.env.DEFAULT_CHANNEL;
  57. const defaultCurrency = process.env.DEFAULT_CURRENCY;
  58. const defaultLocale = process.env.DEFAULT_LOCAL;
  59. const storeName = process.env.STORE_NAME;
  60. if(!defaultChannel || !defaultCurrency || !defaultLocale ||!storeName) {
  61. throw new Error('Default Channel or Default Currency or Default Locale or store name is not defined');
  62. }
  63. const cookieStore = await cookies();
  64. const currentCurrency = cookieStore.get(CURRENT_CURRENCY);
  65. const currentLocale = cookieStore.get(CURRENT_LOCAL);
  66. const currentChannel = cookieStore.get(CURRENT_CHANNEL);
  67. if(currentCurrency === undefined) {
  68. throw new Error('Currency is not defined');
  69. }
  70. if(currentLocale === undefined) {
  71. throw new Error('Local is not defined');
  72. }
  73. if(currentChannel === undefined) {
  74. throw new Error('Channel is not defined');
  75. }
  76. // 获取country list 和currency list
  77. const {data: countryResData} = await serverGraphqlFetch<{
  78. countries: Country[]
  79. }>({
  80. query: GET_COUNTRIES
  81. });
  82. const {data: currencyResData} = await serverGraphqlFetch<CurrenciesData>({
  83. query: GET_CURRENCIES
  84. });
  85. const countries = countryResData.countries;
  86. const currencyList = currencyResData.currencies?.edges.map((item) => item.node) ?? [];
  87. const storeConfig = {
  88. // 系统默认值
  89. defaultChannel: defaultChannel,
  90. defaultCurrency: defaultCurrency,
  91. defaultLocale: defaultLocale,
  92. storeName: storeName,
  93. // 用户当前使用值
  94. currentCurrency: currentCurrency.value,
  95. currentLocale: currentLocale.value,
  96. currentChannel: currentChannel.value
  97. };
  98. const {data: cartDetailsRes} = await serverGraphqlFetch<GetCartItemData>({
  99. query: GET_CART_ITEM,
  100. operationName:'GetCartItem'
  101. });
  102. const cartDetails = cartDetailsRes.createReadCart?.readCart ?? null;
  103. return (
  104. <html lang="en" suppressHydrationWarning>
  105. <head>
  106. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"></meta>
  107. </head>
  108. <body className={clsx(
  109. "min-h-screen font-outfit text-foreground bg-background antialiased",
  110. outfit.variable
  111. )}>
  112. <main>
  113. <ErrorBoundary>
  114. <PayPalWrapper clientId={paypalClientId}>
  115. <ConfigProvider
  116. config={{
  117. currencies: currencyList,
  118. countries: countries,
  119. store: storeConfig
  120. }}
  121. >
  122. {/* Redux Provider 的初始化 state 只在首次挂载时生效,不会因为 Server Component 重新渲染而重新初始化。 */}
  123. <ReduxProvider
  124. preloadedState={{
  125. cartDetail:{
  126. cart: cartDetails,
  127. loading: false,
  128. error: null
  129. }
  130. }}
  131. >
  132. <GlobalProviders>
  133. {children}
  134. <AddToCartModalWrapper />
  135. </GlobalProviders>
  136. </ReduxProvider>
  137. </ConfigProvider>
  138. </PayPalWrapper>
  139. <SpeculationRules />
  140. </ErrorBoundary>
  141. </main>
  142. <div id="modal-root"></div>
  143. <KernelProvider />
  144. <span aria-hidden="true" data-nx-locale style={__srOnly}>{__lr}</span>
  145. </body>
  146. </html>
  147. );
  148. }