Deduplicate pick list products by name

This commit is contained in:
beatz174-bit
2025-11-23 15:13:14 +10:00
parent 2f7d5024e8
commit 777c53f6ae
2 changed files with 144 additions and 27 deletions
+20 -3
View File
@@ -48,15 +48,32 @@ export const ActivePickListScreen = () => {
[areas, pickList?.area_id],
);
const sortedProducts = useMemo(() => {
const uniqueProducts = new Map<string, Product>();
products.forEach((product) => {
const normalizedName = product.name.trim().toLowerCase();
const existing = uniqueProducts.get(normalizedName);
if (!existing || product.updated_at > existing.updated_at) {
uniqueProducts.set(normalizedName, product);
}
});
return Array.from(uniqueProducts.values()).sort((a, b) =>
a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }),
);
}, [products]);
const filteredProducts = useMemo(() => {
const normalizedQuery = query.trim().toLowerCase();
if (!normalizedQuery) return products;
if (!normalizedQuery) return sortedProducts;
return products.filter((product) => {
return sortedProducts.filter((product) => {
const searchableText = `${product.name} ${product.category} ${product.barcode ?? ''}`.toLowerCase();
return searchableText.includes(normalizedQuery);
});
}, [products, query]);
}, [sortedProducts, query]);
useEffect(() => {
if (!selectedProduct) return;