Skip to content

Commit e27389a

Browse files
committed
fix(n1): stabilize cart-dependent hydration
1 parent bd4b459 commit e27389a

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

apps/n1/src/app/pokladna/page.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { Button } from "@ui/atoms/button"
33
import { useRouter } from "next/navigation"
44
import { useEffect, useRef } from "react"
5+
import { useClientReady } from "@/hooks/use-client-ready"
56
import { useAnalytics } from "@/providers/analytics-provider"
67
import type { PplAccessPointData } from "@/utils/address-helpers"
78
import { accessPointToShippingData } from "@/utils/address-helpers"
@@ -16,13 +17,30 @@ import {
1617
} from "./_context/checkout-context"
1718

1819
export default function CheckoutPage() {
20+
const isClientReady = useClientReady()
21+
22+
if (!isClientReady) {
23+
return <CheckoutLoadingState />
24+
}
25+
1926
return (
2027
<CheckoutProvider>
2128
<CheckoutContent />
2229
</CheckoutProvider>
2330
)
2431
}
2532

33+
function CheckoutLoadingState() {
34+
return (
35+
<div className="container mx-auto p-500">
36+
<h1 className="font-bold text-2xl text-fg-primary">Načítání pokladny</h1>
37+
<p className="mt-200 text-fg-secondary">
38+
Načítám obsah košíku a checkout data.
39+
</p>
40+
</div>
41+
)
42+
}
43+
2644
function CheckoutContent() {
2745
const router = useRouter()
2846
const {

apps/n1/src/components/header/cart-popover.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Icon } from "@techsio/ui-kit/atoms/icon"
44
import { Popover } from "@techsio/ui-kit/molecules/popover"
55
import { Suspense } from "react"
66
import { ErrorBoundary } from "@/components/error-boundary"
7+
import { useClientReady } from "@/hooks/use-client-ready"
78
import { cartFlow } from "@/hooks/storefront-preset"
89
import { CartContent } from "./cart-content"
910
import { CartEmptyState } from "./cart-empty-state"
@@ -12,6 +13,7 @@ import { useHeaderContext } from "./store/header-context"
1213

1314
export const CartPopover = () => {
1415
const { toggleCart, setIsCartOpen } = useHeaderContext()
16+
const isClientReady = useClientReady()
1517

1618
const handleHover = () => {
1719
toggleCart()
@@ -25,9 +27,13 @@ export const CartPopover = () => {
2527
<ErrorBoundary
2628
fallback={<CartPopoverErrorFallback onClose={toggleCart} />}
2729
>
28-
<Suspense fallback={<CartPopoverLoadingFallback />}>
29-
<CartPopoverContent onClose={toggleCart} />
30-
</Suspense>
30+
{isClientReady ? (
31+
<Suspense fallback={<CartPopoverLoadingFallback />}>
32+
<CartPopoverContent onClose={toggleCart} />
33+
</Suspense>
34+
) : (
35+
<CartPopoverLoadingFallback />
36+
)}
3137
</ErrorBoundary>
3238
</div>
3339
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use client"
2+
3+
import { useEffect, useState } from "react"
4+
5+
export function useClientReady(): boolean {
6+
const [isClientReady, setIsClientReady] = useState(false)
7+
8+
useEffect(() => {
9+
setIsClientReady(true)
10+
}, [])
11+
12+
return isClientReady
13+
}

0 commit comments

Comments
 (0)