'use client'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import type { Cart, CartItem, CartRecommendationsResponse, CartUpgradesResponse, CartBundlesResponse, } from 'brainerce'; import { useRouter } from '@/core/lib/navigation'; import { useCart } from '@/core/providers/store-provider'; export interface UseCartPageResult { cart: Cart | null; /** True before the first answer. Prefer `status`. */ cartLoading: boolean; /** * `loading`: no answer yet (draw a skeleton, `hintCount` tall) · `empty`: the * server says the cart has no lines (the ONLY state that may say "your cart is * empty") · `ready` · `error`: the cart could not be loaded, show a message and * `retry`. An `error` must never look like an empty cart or an endless spinner. */ status: 'loading' | 'empty' | 'ready' | 'error'; /** Item count remembered from the last visit, for sizing a loading skeleton. Cap the row count. */ hintCount: number; /** Leave `status === 'error'`: loads the cart again. */ retry: () => Promise; /** Last failed cart change (already rolled back). Tell the shopper, e.g. "could not update the quantity". */ mutationError: Error | null; /** Clear `mutationError` once the shopper has seen it. */ dismissMutationError: () => void; /** A change is queued or in flight, or the cart is being re-read. */ isSyncing: boolean; /** * The store's own whole-cart gate: ready, nothing pending, no unavailable line * ANYWHERE in the cart, reservation alive. Informational. Disable the checkout * button with `canProceedToCheckout`, which is scoped to the ticked lines. */ canCheckout: boolean; /** * Force a re-read from the server. A store action (`setQuantity`, `remove`, ...) * already updates the cart, so never call this after one. */ refreshCart: () => Promise; itemCount: number; /** * Recommendations for the current cart contents. `cartRecs.recommendations` is the merchant's * MANUAL list; `cartRecs.automaticRecommendations` is ONE separate group from purchase history * (`kind` picks its heading). Render them as two rails and never merge them. */ cartRecs: CartRecommendationsResponse | null; /** Per-item upgrade suggestions keyed by productId. */ upgrades: CartUpgradesResponse | null; /** Bundle offers matching the current cart. */ bundles: CartBundlesResponse | null; /** * True once the stock reservation on this cart has run out and the refreshed * cart still carries the same expired window. Goes back to false on its own * if the server hands back a fresh reservation. */ reservationExpired: boolean; /** Lines the server says cannot be bought right now (`isAvailable === false`). */ unavailableItems: CartItem[]; /** * Cart line ids the shopper has ticked for checkout. Starts as every line * and follows the cart: a newly added line arrives selected, a removed one * drops out. This is a PARTIAL CHECKOUT selection, not a UI nicety — * `createCheckout({ selectedItemIds })` is what the platform honours, and * after payment the unselected lines stay in the still-ACTIVE cart. */ selectedItemIds: string[]; /** Tick / untick one line. */ toggleItemSelected: (itemId: string) => void; /** True when every line is ticked (the default, and the full-cart case). */ allSelected: boolean; /** Tick or untick every line at once. */ setAllSelected: (selected: boolean) => void; /** * Where the proceed-to-checkout action should point. Carries the selection * as `?items=` ONLY when it is a strict subset — a full cart links to a * bare `/checkout`, so the default path is byte-for-byte what it was. */ checkoutHref: string; /** * Lines the shopper actually ticked. Scope the summary and the checkout * gate to these, not to the whole cart. */ selectedItems: CartItem[]; /** * False until the cart is `ready` with no change pending, while the * reservation is expired, or while any SELECTED line is unavailable. The * proceed-to-checkout action must be genuinely disabled on false, not just * styled as disabled. Scoped to the selection on purpose: an unavailable line * the shopper has unticked is not going to the server, so it must not block a * checkout of the rest. */ canProceedToCheckout: boolean; /** * Hand this to ``. It refreshes the * cart once per expiry window: the server, not the client timer, decides * what is still purchasable. */ onReservationExpired: () => void; /** * Go to checkout the safe way: wait for queued cart changes to land (so the * checkout is created from the cart the shopper SEES), then navigate to * `checkoutHref` with the locale-aware router. Resolves `true` when it * navigated, `false` when a change failed and the shopper stays on the cart * (the failure is in `mutationError`). Call it from the button's `onClick` * instead of rendering a bare link to checkout. */ proceedToCheckout: () => Promise; /** True while `proceedToCheckout` is waiting. Disable the button while it is. */ proceedingToCheckout: boolean; } /** * Cart-page behavior: the shared cart state (from StoreProvider) plus the * enrichment fetch (recommendations / upgrades / bundles) for the lines in the * cart. Pure data/behavior — rendering lives in `ui/cart/cart-view.tsx`. */ export function useCartPage(): UseCartPageResult { const { cart, cartLoading, status, hintCount, retry, mutationError, dismissMutationError, isSyncing, totalsStale, canCheckout, refreshCart, flush, loadEnrichment, itemCount, } = useCart(); const router = useRouter(); const [cartRecs, setCartRecs] = useState(null); const [upgrades, setUpgrades] = useState(null); const [bundles, setBundles] = useState(null); const [proceedingToCheckout, setProceedingToCheckout] = useState(false); // Load recommendations, upgrades, and bundles in a single request. // // Keyed on WHICH lines the cart has (the cart id plus the line ids), not on // quantities and not on the cart object: a burst of quantity taps changes // neither, so it costs no request. It also waits while changes are pending // (`totalsStale`), so adding three items quickly asks once, after the last one // landed, and the store dedupes concurrent identical calls. const cartId = cart?.id ?? null; const lineSignature = (cart?.items ?? []).map((item) => item.id).join(','); useEffect(() => { if (status !== 'ready' || !cartId || lineSignature === '') { setCartRecs(null); setUpgrades(null); setBundles(null); return; } if (totalsStale) return; let cancelled = false; void loadEnrichment(['recommendations', 'upgrades', 'bundles']).then((enriched) => { // null: the request failed or the cart changed identity meanwhile. Keep what is shown. if (cancelled || !enriched) return; setCartRecs(enriched.recommendations ?? null); setUpgrades(enriched.upgrades ?? null); setBundles(enriched.bundles ?? null); }); return () => { cancelled = true; }; }, [status, cartId, lineSignature, totalsStale, loadEnrichment]); // ---- Reservation expiry ---- // // The countdown component only ticks. Expiry is handled here, once, so all // three design variants share one behaviour and a user redesigning the // countdown cannot delete the gate along with the markup. // // Both pieces of state key on the reservation's `expiresAt`, which is the // only stable identity a reservation window has: // - `handledExpiryRef` stops the refresh loop (refresh remounts the // countdown, which would otherwise report the same expiry again). // - comparing `expiredWindow` to the CURRENT `expiresAt` means a server // that renews the reservation clears the expired state by itself. const reservationExpiresAt = cart?.reservation?.expiresAt ?? null; const handledExpiryRef = useRef(null); const [expiredWindow, setExpiredWindow] = useState(null); const onReservationExpired = useCallback(() => { if (!reservationExpiresAt) return; setExpiredWindow(reservationExpiresAt); if (handledExpiryRef.current === reservationExpiresAt) return; handledExpiryRef.current = reservationExpiresAt; // Re-read from the server: it is the source of truth for what survived // the released reservation. `isAvailable` on each line comes back updated. // A real refresh, not a mutate-then-refetch: the change happened on the // server's clock, not through a store action. void refreshCart(); }, [reservationExpiresAt, refreshCart]); const reservationExpired = expiredWindow !== null && expiredWindow === reservationExpiresAt && cart?.reservation?.hasReservation === true; // ---- Partial-checkout selection ---- // // Held as a Set of cart-line ids rather than a per-item boolean so it can be // reconciled against the cart in one pass. `null` means "nothing has been // untick'd yet", which is the everything-selected default — distinct from an // empty Set, which is a shopper who deliberately cleared the list. const [deselectedIds, setDeselectedIds] = useState>(() => new Set()); const items = useMemo(() => cart?.items ?? [], [cart?.items]); // Drop ids for lines that no longer exist, so a removed-and-readded product // does not come back invisibly unticked. const liveItemIds = items.map((item) => item.id).join(','); useEffect(() => { setDeselectedIds((prev) => { if (prev.size === 0) return prev; const live = new Set(liveItemIds ? liveItemIds.split(',') : []); const next = new Set([...prev].filter((id) => live.has(id))); return next.size === prev.size ? prev : next; }); }, [liveItemIds]); const selectedItems = items.filter((item) => !deselectedIds.has(item.id)); const selectedItemIds = selectedItems.map((item) => item.id); const allSelected = items.length > 0 && selectedItems.length === items.length; const toggleItemSelected = useCallback((itemId: string) => { setDeselectedIds((prev) => { const next = new Set(prev); if (next.has(itemId)) { next.delete(itemId); } else { next.add(itemId); } return next; }); }, []); const setAllSelected = useCallback( (selected: boolean) => { setDeselectedIds(selected ? new Set() : new Set(items.map((item) => item.id))); }, [items] ); // A full cart keeps the plain `/checkout` URL so the common path is // unchanged; only a strict subset carries the ids. const checkoutHref = allSelected || selectedItemIds.length === 0 ? '/checkout' : `/checkout?items=${encodeURIComponent(selectedItemIds.join(','))}`; // Reported over the whole cart (the banner names every blocked line) but // gated over the selection below. const unavailableItems = items.filter((item) => item.isAvailable === false); const unavailableSelected = selectedItems.filter((item) => item.isAvailable === false); // ⛔ Deliberately NOT the store's `canCheckout`. That flag is false while ANY // line in the cart is unavailable, which would block a partial checkout of the // lines the shopper ticked because of one they did not. What it guards that // matters here is kept: the cart must have loaded (`ready`) and no change may // be pending (`totalsStale` is true exactly while writes are queued). const canProceedToCheckout = status === 'ready' && !totalsStale && !!cart && selectedItems.length > 0 && !reservationExpired && unavailableSelected.length === 0; const proceedToCheckout = useCallback(async (): Promise => { if (proceedingToCheckout) return false; setProceedingToCheckout(true); try { // Let every queued change land first: a checkout created from a cart that // is still catching up would charge for lines the shopper no longer sees. const { ok } = await flush(); if (!ok) return false; // stay on the cart; `mutationError` says why router.push(checkoutHref); return true; } finally { setProceedingToCheckout(false); } }, [proceedingToCheckout, flush, router, checkoutHref]); return { cart, cartLoading, status, hintCount, retry, mutationError, dismissMutationError, isSyncing, canCheckout, refreshCart, itemCount, cartRecs, upgrades, bundles, reservationExpired, unavailableItems, selectedItemIds, selectedItems, toggleItemSelected, allSelected, setAllSelected, checkoutHref, canProceedToCheckout, onReservationExpired, proceedToCheckout, proceedingToCheckout, }; }