import React, { useState, useEffect } from "react";
import { View, ScrollView, Dimensions, Alert } from "react-native";
import { Image } from 'expo-image';
import { useRouter } from "expo-router";
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
import {
StorageServiceCasual,
tw,
MyText,
MyTouchableOpacity,
MiniQuantifier,
BottomDropdown,
BottomDialog,
theme,
updateStatusBarColor,
} from "common-ui";
import {
useGetCart,
useUpdateCartItem,
useRemoveFromCart,
useAddToCart,
type CartType,
} from "@/hooks/cart-query-hooks";
import { useGetEssentialConsts, useSlots } from "@/src/hooks/prominent-api-hooks"
import { useProductSlotIdentifier } from "@/hooks/useProductSlotIdentifier";
import { useCentralProductStore } from "@/src/store/centralProductStore";
import dayjs from "dayjs";
import { LinearGradient } from "expo-linear-gradient";
const { height: screenHeight } = Dimensions.get("window");
interface FloatingCartBarProps {
isFlashDelivery?: boolean;
isExpanded?: boolean;
setIsExpanded?: (value: boolean) => void;
}
// Smart time window formatting function
const formatTimeRange = (deliveryTime: string | Date) => {
const time = dayjs(deliveryTime);
const endTime = time.add(1, 'hour');
const startPeriod = time.format('A');
const endPeriod = endTime.format('A');
let timeRange;
if (startPeriod === endPeriod) {
timeRange = `${time.format('h')}-${endTime.format('h')} ${startPeriod}`;
} else {
timeRange = `${time.format('h:mm')} ${startPeriod} - ${endTime.format('h:mm')} ${endPeriod}`;
}
return `${time.format('ddd, DD MMM ')}${timeRange}`;
};
// Product name component with quantity
const ProductNameWithQuantity = ({ name, productQuantity, unitNotation }: { name: string; productQuantity: number; unitNotation: string }) => {
const truncatedName = name.length > 25 ? name.substring(0, 25) + '...' : name;
const unit = unitNotation ? ` ${unitNotation}` : '';
return (
{truncatedName} ({productQuantity}{unit})
);
};
const FloatingCartBar: React.FC = ({
isFlashDelivery = false,
isExpanded: controlledIsExpanded,
setIsExpanded: controlledSetIsExpanded,
}) => {
const cartBarColor = isFlashDelivery ? '#f81260' : theme.colors.brand600;
const cartBarBorderColor = isFlashDelivery ? '#e11d48' : theme.colors.brand500;
const router = useRouter();
const [localIsExpanded, setLocalIsExpanded] = useState(false);
const [quantities, setQuantities] = useState>({});
const cartType: CartType = isFlashDelivery ? "flash" : "regular";
const isExpanded = controlledIsExpanded ?? localIsExpanded;
const setIsExpanded = controlledSetIsExpanded ?? setLocalIsExpanded;
const { data: cartData, refetch: refetchCart } = useGetCart({}, cartType);
const { data: constsData } = useGetEssentialConsts();
const { data: slotsData } = useSlots();
const productsById = useCentralProductStore((state) => state.productsById);
const { productSlotsMap } = useProductSlotIdentifier();
const cartItems = cartData?.items || [];
const itemCount = cartItems.length;
const updateCartItem = useUpdateCartItem({
showSuccessAlert: false,
showErrorAlert: false,
refetchCart: true,
}, cartType);
const removeFromCart = useRemoveFromCart({
showSuccessAlert: false,
showErrorAlert: false,
refetchCart: true,
}, cartType);
const addToCartHook = useAddToCart({
showSuccessAlert: false,
showErrorAlert: false,
refetchCart: true,
}, cartType);
useEffect(() => {
const initial: Record = {};
cartItems.forEach((item) => {
initial[item.id] = item.quantity;
});
setQuantities(initial);
}, [cartData]);
useEffect(() => {
if (!cartItems.length || !slotsData?.slots || !productSlotsMap) return;
const itemsToUpdate = cartItems.filter(item => {
if (isFlashDelivery || !item.slotId) return false;
const availableSlots = productSlotsMap[item.productId]?.slots || [];
const isSlotAvailable = availableSlots.some((slot) => slot.id === item.slotId);
return !isSlotAvailable;
});
itemsToUpdate.forEach((item) => {
const availableSlots = productSlotsMap[item.productId]?.slots || [];
if (availableSlots.length > 0 && !isFlashDelivery) {
const nearestSlotId = availableSlots[0].id;
removeFromCart.mutate({ itemId: item.id });
addToCartHook.addToCart(item.productId, item.quantity, nearestSlotId);
}
});
}, []);
const firstItem = cartItems[0];
const expandedHeight = screenHeight * 0.7;
// Calculate total cart value and free delivery info
const totalCartValue = cartItems.reduce(
(sum, item) => {
const product = productsById[item.productId];
const basePrice = product?.price ?? 0;
const price = isFlashDelivery ? (product?.flashPrice ?? basePrice) : basePrice;
return sum + price * item.quantity;
},
0
);
const freeDeliveryThreshold = isFlashDelivery
? constsData?.flashFreeDeliveryThreshold
: constsData?.freeDeliveryThreshold;
const remainingForFreeDelivery = Math.max(
0,
freeDeliveryThreshold - totalCartValue
);
return (
<>
{!isExpanded && (
// --- Collapsed View ---
itemCount > 0 && setIsExpanded(true)}
activeOpacity={0.9}
>
{itemCount === 0 ? (isFlashDelivery ? "No Flash Items" : "No Items In Cart") : (
<>
₹{totalCartValue}
{` • ${itemCount} ${itemCount === 1 ? "Item" : "Items"}`}
>
)}
{itemCount > 0 && }
{remainingForFreeDelivery > 0 ? (
₹{remainingForFreeDelivery} more for FREE Delivery
) : itemCount > 0 ? (
Free Delivery Unlocked
) : (
Shop for ₹{freeDeliveryThreshold}+ for free shipping
)}
router.push(
isFlashDelivery
? "/(drawer)/(tabs)/flash-delivery/(cart)/cart"
: "/(drawer)/(tabs)/home/cart"
)}
>
Go to Cart
)}
setIsExpanded(false)} enableDismiss={true}>
{/* Header */}
Your Cart
{itemCount} Items
setIsExpanded(false)}
>
{/* Progress Bar Header */}
{remainingForFreeDelivery > 0 && (
Free Delivery Progress
{Math.round((totalCartValue / freeDeliveryThreshold) * 100)}%
Needed
+₹{remainingForFreeDelivery}
)}
{/* Items List */}
{cartItems.map((item, index) => (
{
if (value === 0) {
removeFromCart.mutate({ itemId: item.id });
} else {
setQuantities((prev) => ({ ...prev, [item.id]: value }));
updateCartItem.mutate({ itemId: item.id, quantity: value });
}
}}
step={productsById[item.productId]?.incrementStep || 1}
showUnits={true}
unit={productsById[item.productId]?.unitNotation}
/>
{item.slotId && slotsData && productSlotsMap[item.productId] && (
{
return {
label: slot ? formatTimeRange(slot.deliveryTime) : "N/A",
value: slot.id,
};
})}
onValueChange={async (val) => {
const newSlot = slotsData.slots.find(s => s.id === val);
if (!newSlot) return;
const productId = item.productId;
const quantity = item.quantity;
const itemId = item.id;
const slotId = typeof val === 'number' ? val : Number(val);
// await removeFromCart.mutateAsync(itemId);
addToCartHook.addToCart(productId, 0, slotId, () => {
refetchCart();
Alert.alert("Delivery Updated", `Scheduled for ${formatTimeRange(newSlot?.deliveryTime || '')}`);
});
}}
triggerComponent={({ onPress, displayText }) => (
{displayText}
)}
/>
)}
₹{(() => {
const product = productsById[item.productId];
const basePrice = product?.price ?? 0;
const price = isFlashDelivery ? (product?.flashPrice ?? basePrice) : basePrice;
return price * item.quantity;
})()}
{index < cartItems.length - 1 && (
)}
))}
{/* Fancy Footer */}
Subtotal
₹{totalCartValue}
{remainingForFreeDelivery === 0 && (
Free Delivery
)}
router.push(
isFlashDelivery
? "/(drawer)/(tabs)/flash-delivery/(cart)/cart"
: "/(drawer)/(tabs)/home/cart"
)}
activeOpacity={0.9}
>
Go to cart
>
);
};
export default FloatingCartBar;