Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[skeleton] Make cart banner optimistic #2502

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions templates/skeleton/app/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {Suspense} from 'react';
import {Await, NavLink} from '@remix-run/react';
import {type CartViewPayload, useAnalytics} from '@shopify/hydrogen';
import {Suspense, useDeferredValue} from 'react';
import {Await, NavLink, useAsyncValue} from '@remix-run/react';
frontsideair marked this conversation as resolved.
Show resolved Hide resolved
import {
type CartViewPayload,
useAnalytics,
useOptimisticCart,
} from '@shopify/hydrogen';
import type {HeaderQuery, CartApiQueryFragment} from 'storefrontapi.generated';
import {useAside} from '~/components/Aside';

Expand Down Expand Up @@ -159,15 +163,19 @@ function CartToggle({cart}: Pick<HeaderProps, 'cart'>) {
return (
<Suspense fallback={<CartBadge count={null} />}>
<Await resolve={cart}>
{(cart) => {
if (!cart) return <CartBadge count={0} />;
return <CartBadge count={cart.totalQuantity || 0} />;
}}
<CartBanner />
</Await>
</Suspense>
);
}

function CartBanner() {
const cart = useAsyncValue();
const optimisticCart = useOptimisticCart(cart);
const linesCount = optimisticCart?.lines?.nodes?.length || 0;
frontsideair marked this conversation as resolved.
Show resolved Hide resolved
return <CartBadge count={linesCount} />;
frontsideair marked this conversation as resolved.
Show resolved Hide resolved
}
frontsideair marked this conversation as resolved.
Show resolved Hide resolved

const FALLBACK_HEADER_MENU = {
id: 'gid://shopify/Menu/199655587896',
items: [
Expand Down
Loading