this repo has no description
at adapters-api 35 lines 904 B view raw
1'use client'; 2 3import React, { useState } from 'react'; 4 5const CartCountContext = React.createContext< 6 | [null | number, React.Dispatch<React.SetStateAction<null | number>>] 7 | undefined 8>(undefined); 9 10export function CartCountProvider({ children }: { children: React.ReactNode }) { 11 const [optimisticCartCount, setOptimisticCartCount] = useState<null | number>( 12 null, 13 ); 14 15 return ( 16 <CartCountContext.Provider 17 value={[optimisticCartCount, setOptimisticCartCount]} 18 > 19 {children} 20 </CartCountContext.Provider> 21 ); 22} 23 24export function useCartCount( 25 initialCount: number, 26): [null | number, React.Dispatch<React.SetStateAction<null | number>>] { 27 const context = React.useContext(CartCountContext); 28 if (context === undefined) { 29 throw new Error('useCartCount must be used within a CartCountProvider'); 30 } 31 if (context[0] === null) { 32 return [initialCount, context[1]]; 33 } 34 return context; 35}