import * as React from "react" import { Search, Hash, Type } from "lucide-react" import { Dialog, DialogContent, DialogTitle, DialogDescription, } from "@/components/ui/dialog" import { SettingInput, SafeRender, ModernCardHeader } from "./settings-ui" interface PersonalizationDialogProps { open: boolean onOpenChange: (open: boolean) => void onInsert: (placeholder: string) => void placeholders: Record triggerKey?: string } export function PersonalizationDialog({ open, onOpenChange, onInsert, placeholders, triggerKey, }: PersonalizationDialogProps) { const [search, setSearch] = React.useState("") const filteredPlaceholders = React.useMemo(() => { const isWordPress = triggerKey?.startsWith('user_'); const isWooCommerce = triggerKey?.includes('wc_status_') || triggerKey?.includes('wc_order_') || triggerKey?.includes('wc_sub_'); const isSubscriptions = triggerKey?.includes('wc_sub_'); // 1. Categorize the flat placeholders const categorized: Record> = { "General Site Variables": {}, "WordPress User Accounts": {}, "WooCommerce Order Details": {}, "WooCommerce Items & Products": {}, "WooCommerce Billing & Shipping": {}, "WooCommerce Subscriptions": {}, }; Object.entries(placeholders).forEach(([key, label]) => { if (key.startsWith('{{wp-') || key.includes('user_')) { categorized["WordPress User Accounts"][key] = label; } else if (key.startsWith('{{wc-order') || key.startsWith('{{order_') || key.startsWith('{{status}}') || key.startsWith('{{payment_') || key.startsWith('{{shipping_method') || key.startsWith('{{transaction_id') || key.startsWith('{{cust_note')) { categorized["WooCommerce Order Details"][key] = label; } else if (key.startsWith('{{wc-product') || key.startsWith('{{product}}') || key.startsWith('{{wc-sku')) { categorized["WooCommerce Items & Products"][key] = label; } else if (key.startsWith('{{billing_') || key.startsWith('{{shipping_')) { categorized["WooCommerce Billing & Shipping"][key] = label; } else if (key.startsWith('{{subscription_') || key.startsWith('{{next_payment_') || key.startsWith('{{renewal_')) { categorized["WooCommerce Subscriptions"][key] = label; } else { categorized["General Site Variables"][key] = label; } }); const result: Record> = {} Object.entries(categorized).forEach(([group, items]) => { // 🚨 Filtering Logic based on trigger: const groupLower = group.toLowerCase(); let shouldShowGroup = false; if (groupLower.includes('general')) shouldShowGroup = true; else if (isSubscriptions) shouldShowGroup = true; else if (isWooCommerce) { if (!groupLower.includes('subscription')) shouldShowGroup = true; } else if (isWordPress) { if (groupLower.includes('wordpress')) shouldShowGroup = true; } else { shouldShowGroup = true; // Fallback } if (!shouldShowGroup) return; const filteredItems: Record = {} Object.entries(items).forEach(([key, label]) => { if ( (label || "").toLowerCase().includes(search.toLowerCase()) || (key || "").toLowerCase().includes(search.toLowerCase()) ) { filteredItems[key] = label } }) if (Object.keys(filteredItems).length > 0) { result[group] = filteredItems } }); return result }, [placeholders, search, triggerKey]) return ( Personalization Insert dynamic variables to customize your message for each recipient
{Object.entries(filteredPlaceholders).map(([group, items]) => (

{group}

{Object.entries(items).map(([key, label]) => ( ))}
))} {Object.keys(filteredPlaceholders).length === 0 && (

No variables found for your search.

)}
) }