import React from "react"; import { Badge } from "../UI/Badge"; import { Button } from "../UI/Button"; import { Panel } from "../UI/Panel"; import { getInvitationReferenceLabel } from "./invitationReference"; import { supabase } from "../../supabaseClient"; import { matchesStopWord } from "../../hooks/useStopWords"; const flattenOrderProducts = (rawItems) => { if (!Array.isArray(rawItems) || rawItems.length === 0) return []; const products = []; for (const item of rawItems) { if (!item || typeof item !== "object") continue; const subItems = Array.isArray(item.items) ? item.items : []; if (subItems.length > 0) { const hasSubOrders = subItems.some( (s) => typeof s === "object" && ("nom" in s || ("items" in s && Array.isArray(s.items))), ); if (hasSubOrders) { for (const sub of subItems) { if (!sub || typeof sub !== "object") continue; const productsList = Array.isArray(sub.items) ? sub.items : []; for (const p of productsList) { if (!p || typeof p !== "object") continue; const pName = String(p.product_name || p.name || "").trim(); if (!pName) continue; products.push({ name: pName, quantity: String(p.product_quantity || p.quantity || p.count || p.amount || "").trim(), unit: String(p.product_ed || p.unit || "").trim(), _sourceOrder: sub, }); } } } else { for (const p of subItems) { if (!p || typeof p !== "object") continue; const pName = String(p.product_name || p.name || "").trim(); if (!pName) continue; products.push({ name: pName, quantity: String(p.product_quantity || p.quantity || p.count || p.amount || "").trim(), unit: String(p.product_ed || p.unit || "").trim(), _sourceOrder: item, }); } } continue; } const name = String(item.product_name || item.name || item.nom || "").trim(); if (!name) continue; products.push({ name, quantity: String(item.product_quantity || item.quantity || item.count || item.amount || "").trim(), unit: String(item.product_ed || item.unit || "").trim(), _sourceOrder: item, }); } return products; }; export const OrderCompositionPanel = ({ invitation = {} }) => { const [stopWords, setStopWords] = React.useState([]); const [stopWordsLoaded, setStopWordsLoaded] = React.useState(false); const [scopeActive, setScopeActive] = React.useState(true); React.useEffect(() => { if (!supabase) { setStopWordsLoaded(true); return; } Promise.all([ supabase.from("stop_words").select("word"), supabase.from("stop_words_scope").select("scope").eq("id", 1).single(), ]).then(([{ data: wordsData }, { data: scopeData }]) => { if (wordsData) setStopWords(wordsData.map((d) => d.word)); setScopeActive(scopeData?.scope === "everywhere" || scopeData?.scope === "client_only"); setStopWordsLoaded(true); }) .catch(() => setStopWordsLoaded(true)); }, []); const rawItems = invitation.orderItems || invitation.items || []; const allProducts = flattenOrderProducts(rawItems); const products = (stopWords.length && scopeActive) ? allProducts.filter((p) => !matchesStopWord(p.name, stopWords)) : allProducts; const filteredCount = allProducts.length - products.length; const reference = getInvitationReferenceLabel(invitation); const [isExpanded, setIsExpanded] = React.useState(false); // Hide the entire panel if there are no products to show and some were filtered if (products.length === 0 && filteredCount > 0) return null; if (products.length === 0 && filteredCount === 0) return null; return ( {isExpanded && (
{products.map((product, idx) => (
{product.name} {(product.quantity || product.unit) ? ( {[product.quantity, product.unit].filter(Boolean).join(" ")} ) : null}
))}
)}
); };