2024-11-01 17:32:20 +08:00
|
|
|
import React, {useRef, useEffect, useState} from 'react'
|
2023-12-01 11:52:37 +08:00
|
|
|
import {Droppable} from "@hello-pangea/dnd";
|
2023-12-15 13:49:54 +08:00
|
|
|
import {cartStyle, compareArraysWithIds} from "./utils";
|
2023-12-12 18:21:09 +08:00
|
|
|
import {ProductCartItem} from "./ProductCartItem";
|
|
|
|
import {FakePlaceholder} from "./FakePlaceholder";
|
2023-12-13 15:17:14 +08:00
|
|
|
import {hp_to_slots} from "./count_resources";
|
2023-12-11 17:05:35 +08:00
|
|
|
import {useShopStore} from "./shop_store";
|
2023-12-15 13:49:54 +08:00
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
import {useRenderCount} from "@uidotdev/usehooks";
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
export function Cart({crate_index}) {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
2024-11-01 17:32:20 +08:00
|
|
|
const containerRef = useRef(null);
|
|
|
|
const [visiblePlaceholders, setVisiblePlaceholders] = useState(0);
|
2023-12-14 16:09:33 +08:00
|
|
|
|
2023-12-14 17:45:54 +08:00
|
|
|
const crate = useShopStore((state) => state.crates[crate_index], (a, b) => {
|
2023-12-15 13:49:54 +08:00
|
|
|
return compareArraysWithIds(a.items, b.items) && a.occupiedHP === b.occupiedHP && a.crate_mode === b.crate_mode
|
2023-12-14 16:09:33 +08:00
|
|
|
});
|
|
|
|
const crateParams = useShopStore((state) => state.crateParams);
|
2024-11-01 17:32:20 +08:00
|
|
|
const isCrate = useShopStore((state) => state.modes_order.includes(state.crates[crate_index].crate_mode));
|
2023-12-14 16:09:33 +08:00
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("Cart renders: ", renderCount)
|
2023-12-11 17:05:35 +08:00
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
const nbrOccupied = hp_to_slots(crate.occupiedHP);
|
|
|
|
const nbrSlots = hp_to_slots(crateParams(crate.crate_mode).hp);
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2024-11-01 17:32:20 +08:00
|
|
|
useEffect(() => {
|
|
|
|
const updateVisiblePlaceholders = () => {
|
|
|
|
if (!containerRef.current) return;
|
|
|
|
setVisiblePlaceholders(isCrate ?
|
|
|
|
(nbrSlots - nbrOccupied) :
|
|
|
|
// 10 is padding and 77 is the actual size with all the margins
|
|
|
|
Math.max(1, Math.floor((containerRef.current.offsetWidth - 10) / 77) - nbrOccupied));
|
|
|
|
};
|
|
|
|
updateVisiblePlaceholders();
|
|
|
|
const resizeObserver = new ResizeObserver(updateVisiblePlaceholders);
|
|
|
|
if (containerRef.current) {
|
|
|
|
resizeObserver.observe(containerRef.current);
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if (containerRef.current) {
|
|
|
|
resizeObserver.unobserve(containerRef.current);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [nbrOccupied, nbrSlots]);
|
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
const products = crate.items.map((item, index) => {
|
2023-11-30 17:26:17 +08:00
|
|
|
return (
|
2023-12-07 17:08:22 +08:00
|
|
|
<ProductCartItem
|
2023-12-11 17:05:35 +08:00
|
|
|
card_index={index}
|
|
|
|
crate_index={crate_index}
|
|
|
|
key={item.id}/>
|
2023-12-07 17:08:22 +08:00
|
|
|
);
|
|
|
|
});
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
return (
|
2023-12-11 17:05:35 +08:00
|
|
|
<Droppable droppableId={crate.id} direction="horizontal">
|
2023-12-07 17:08:22 +08:00
|
|
|
{(provided, snapshot) => (
|
|
|
|
<div
|
2024-11-01 17:32:20 +08:00
|
|
|
ref={(element) => {
|
|
|
|
containerRef.current = element;
|
|
|
|
provided.innerRef(element);
|
|
|
|
}}
|
2023-12-07 17:08:22 +08:00
|
|
|
{...provided.droppableProps}
|
|
|
|
style={cartStyle(
|
|
|
|
provided.droppableProps.style,
|
|
|
|
snapshot,
|
|
|
|
)}
|
|
|
|
className="items-cart-list">
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
{products}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
{provided.placeholder && (
|
|
|
|
<div style={{display: 'none'}}>
|
|
|
|
{provided.placeholder}
|
|
|
|
</div>
|
|
|
|
)}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-07 17:08:22 +08:00
|
|
|
<FakePlaceholder
|
2024-11-01 17:32:20 +08:00
|
|
|
nToDraw={visiblePlaceholders}
|
2023-12-07 17:08:22 +08:00
|
|
|
isDraggingOver={snapshot.isDraggingOver}/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Droppable>
|
|
|
|
);
|
2023-11-30 17:26:17 +08:00
|
|
|
}
|