Implement 5 UX enhancements for ingredient management

This commit implements the requested enhancements for the ingredient
quick-add system and batch management:

**1. Duplicate Detection**
- Real-time Levenshtein distance-based similarity checking
- Shows warning with top 3 similar ingredients (70%+ similarity)
- Prevents accidental duplicate creation
- Location: QuickAddIngredientModal.tsx

**2. Smart Category Suggestions**
- Auto-populates category based on ingredient name patterns
- Supports Spanish and English ingredient names
- Shows visual indicator when category is AI-suggested
- Pattern matching for: Baking, Dairy, Fruits, Vegetables, Meat, Seafood, Spices
- Location: ingredientHelpers.ts

**3. Quick Templates**
- 10 pre-configured common bakery ingredients
- One-click template application
- Templates include: Flour, Butter, Sugar, Eggs, Yeast, Milk, Chocolate, Vanilla, Salt, Cream
- Each template has sensible defaults (shelf life, refrigeration requirements)
- Location: QuickAddIngredientModal.tsx

**4. Batch Creation Mode**
- BatchAddIngredientsModal component for adding multiple ingredients at once
- Table-based interface for efficient data entry
- "Load from Templates" quick action
- Duplicate detection within batch
- Partial success handling (some ingredients succeed, some fail)
- Location: BatchAddIngredientsModal.tsx
- Integration: UploadSalesDataStep.tsx (2 buttons: "Add One" / "Add Multiple")

**5. Dashboard Alert for Incomplete Ingredients**
- IncompleteIngredientsAlert component on dashboard
- Queries ingredients with needs_review metadata flag
- Shows count badge and first 5 incomplete ingredients
- "Complete Information" button links to inventory page
- Only shows when incomplete ingredients exist
- Location: IncompleteIngredientsAlert.tsx
- Integration: DashboardPage.tsx

**New Files Created:**
- ingredientHelpers.ts - Utilities for duplicate detection, smart suggestions, templates
- BatchAddIngredientsModal.tsx - Batch ingredient creation component
- IncompleteIngredientsAlert.tsx - Dashboard alert component

**Files Modified:**
- QuickAddIngredientModal.tsx - Added duplicate detection, smart suggestions, templates
- UploadSalesDataStep.tsx - Integrated batch creation modal
- DashboardPage.tsx - Added incomplete ingredients alert

**Technical Highlights:**
- Levenshtein distance algorithm for fuzzy name matching
- Pattern-based category suggestions (supports 100+ ingredient patterns)
- Metadata tracking (needs_review, created_context)
- Real-time validation and error handling
- Responsive UI with animations
- Consistent with existing design system

All features built and tested successfully.
Build time: 21.29s
This commit is contained in:
Claude
2025-11-06 15:39:30 +00:00
parent 9162fc32a5
commit 000e352ef9
6 changed files with 987 additions and 16 deletions

View File

@@ -0,0 +1,107 @@
import React from 'react';
import { useNavigate } from 'react-router-dom';
import { useIngredients } from '../../../api/hooks/inventory';
import { useCurrentTenant } from '../../../stores/tenant.store';
import { AlertTriangle, ExternalLink } from 'lucide-react';
export const IncompleteIngredientsAlert: React.FC = () => {
const navigate = useNavigate();
const currentTenant = useCurrentTenant();
// Fetch all ingredients
const { data: ingredients = [], isLoading } = useIngredients(currentTenant?.id || '', {}, {
enabled: !!currentTenant?.id
});
// Filter ingredients that need review (created via quick add or batch with incomplete data)
const incompleteIngredients = React.useMemo(() => {
return ingredients.filter(ing => {
// Check metadata for needs_review flag
const metadata = ing.metadata as any;
return metadata?.needs_review === true;
});
}, [ingredients]);
// Don't show if no incomplete ingredients or still loading
if (isLoading || incompleteIngredients.length === 0) {
return null;
}
const handleViewIncomplete = () => {
// Navigate to inventory page
// TODO: In the future, this could pass a filter parameter to show only incomplete items
navigate('/app/operations/inventory');
};
return (
<div className="bg-gradient-to-r from-[var(--color-warning)]/10 to-[var(--color-warning)]/5 border border-[var(--color-warning)]/30 rounded-lg p-4 shadow-sm hover:shadow-md transition-shadow">
<div className="flex items-start gap-4">
{/* Icon */}
<div className="flex-shrink-0 w-10 h-10 rounded-full bg-[var(--color-warning)]/20 flex items-center justify-center">
<AlertTriangle className="w-5 h-5 text-[var(--color-warning)]" />
</div>
{/* Content */}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<h3 className="text-base font-semibold text-[var(--text-primary)]">
Ingredientes con información incompleta
</h3>
<span className="px-2 py-0.5 bg-[var(--color-warning)] text-white text-xs font-bold rounded-full">
{incompleteIngredients.length}
</span>
</div>
<p className="text-sm text-[var(--text-secondary)] mb-3">
{incompleteIngredients.length === 1
? 'Hay 1 ingrediente que fue agregado rápidamente y necesita información completa.'
: `Hay ${incompleteIngredients.length} ingredientes que fueron agregados rápidamente y necesitan información completa.`}
</p>
{/* Incomplete ingredients list */}
<div className="mb-3 flex flex-wrap gap-2">
{incompleteIngredients.slice(0, 5).map((ing) => (
<span
key={ing.id}
className="inline-flex items-center gap-1.5 px-2.5 py-1 bg-[var(--bg-secondary)] border border-[var(--border-secondary)] rounded-md text-xs text-[var(--text-primary)]"
>
<span className="font-medium">{ing.name}</span>
<span className="text-[var(--text-tertiary)]">({ing.category})</span>
</span>
))}
{incompleteIngredients.length > 5 && (
<span className="inline-flex items-center px-2.5 py-1 text-xs text-[var(--text-secondary)]">
+{incompleteIngredients.length - 5} más
</span>
)}
</div>
{/* What's missing info box */}
<div className="mb-3 p-2.5 bg-[var(--bg-secondary)] rounded-md border border-[var(--border-secondary)]">
<p className="text-xs text-[var(--text-secondary)]">
<span className="font-medium text-[var(--text-primary)]">Información faltante típica:</span>
{' '}Stock inicial, costo por unidad, vida útil, punto de reorden, requisitos de almacenamiento
</p>
</div>
{/* Action button */}
<button
onClick={handleViewIncomplete}
className="inline-flex items-center gap-2 px-4 py-2 bg-[var(--color-warning)] hover:bg-[var(--color-warning-dark)] text-white rounded-lg transition-colors font-medium text-sm"
>
<span>Completar Información</span>
<ExternalLink className="w-4 h-4" />
</button>
</div>
{/* Dismiss button (optional - could be added later) */}
{/* <button
className="flex-shrink-0 p-1 text-[var(--text-tertiary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-secondary)] rounded transition-colors"
title="Dismiss"
>
<X className="w-4 h-4" />
</button> */}
</div>
</div>
);
};

View File

@@ -0,0 +1,444 @@
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useCreateIngredient } from '../../../api/hooks/inventory';
import type { Ingredient } from '../../../api/types/inventory';
import { commonIngredientTemplates } from './ingredientHelpers';
interface BatchIngredientRow {
id: string;
name: string;
category: string;
unit_of_measure: string;
stock_quantity?: number;
cost_per_unit?: number;
error?: string;
}
interface BatchAddIngredientsModalProps {
isOpen: boolean;
onClose: () => void;
onCreated: (ingredients: Ingredient[]) => void;
tenantId: string;
}
export const BatchAddIngredientsModal: React.FC<BatchAddIngredientsModalProps> = ({
isOpen,
onClose,
onCreated,
tenantId
}) => {
const { t } = useTranslation();
const createIngredient = useCreateIngredient();
const [rows, setRows] = useState<BatchIngredientRow[]>([
{ id: '1', name: '', category: 'Baking Ingredients', unit_of_measure: 'kg' },
{ id: '2', name: '', category: 'Baking Ingredients', unit_of_measure: 'kg' },
{ id: '3', name: '', category: 'Baking Ingredients', unit_of_measure: 'kg' }
]);
const [isSubmitting, setIsSubmitting] = useState(false);
const [globalError, setGlobalError] = useState<string | null>(null);
const categoryOptions = [
'Baking Ingredients',
'Dairy',
'Fruits',
'Vegetables',
'Meat',
'Seafood',
'Spices',
'Other'
];
const unitOptions = ['kg', 'g', 'L', 'ml', 'units', 'dozen'];
const updateRow = (id: string, field: keyof BatchIngredientRow, value: any) => {
setRows(rows.map(row =>
row.id === id ? { ...row, [field]: value, error: undefined } : row
));
};
const addRow = () => {
const newId = String(Date.now());
setRows([...rows, {
id: newId,
name: '',
category: 'Baking Ingredients',
unit_of_measure: 'kg'
}]);
};
const removeRow = (id: string) => {
if (rows.length > 1) {
setRows(rows.filter(row => row.id !== id));
}
};
const loadFromTemplates = () => {
const templateRows: BatchIngredientRow[] = commonIngredientTemplates.slice(0, 10).map((template, index) => ({
id: String(Date.now() + index),
name: template.name,
category: template.category,
unit_of_measure: template.unit_of_measure,
stock_quantity: 0,
cost_per_unit: 0
}));
setRows(templateRows);
};
const validateRows = (): boolean => {
let hasError = false;
const updatedRows = rows.map(row => {
if (!row.name.trim()) {
hasError = true;
return { ...row, error: 'El nombre es requerido' };
}
if (!row.category) {
hasError = true;
return { ...row, error: 'La categoría es requerida' };
}
return { ...row, error: undefined };
});
if (hasError) {
setRows(updatedRows);
return false;
}
// Check for duplicates within batch
const names = rows.map(r => r.name.toLowerCase().trim());
const duplicates = names.filter((name, index) => names.indexOf(name) !== index);
if (duplicates.length > 0) {
setGlobalError(`Hay nombres duplicados en el lote: ${duplicates.join(', ')}`);
return false;
}
return true;
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setGlobalError(null);
if (!validateRows()) return;
setIsSubmitting(true);
try {
const createdIngredients: Ingredient[] = [];
const errors: string[] = [];
// Create all ingredients
for (const row of rows) {
try {
const ingredientData = {
name: row.name.trim(),
product_type: 'ingredient',
category: row.category,
unit_of_measure: row.unit_of_measure,
low_stock_threshold: 1,
max_stock_level: 100,
reorder_point: 2,
shelf_life_days: 30,
requires_refrigeration: false,
requires_freezing: false,
is_seasonal: false,
average_cost: row.cost_per_unit || 0,
notes: 'Creado mediante adición por lote',
metadata: {
created_context: 'batch',
is_complete: !!(row.stock_quantity && row.cost_per_unit),
needs_review: !(row.stock_quantity && row.cost_per_unit),
}
};
const created = await createIngredient.mutateAsync({
tenantId,
ingredientData
});
createdIngredients.push(created);
} catch (error: any) {
errors.push(`${row.name}: ${error.message || 'Error al crear'}`);
}
}
if (createdIngredients.length > 0) {
onCreated(createdIngredients);
handleClose();
}
if (errors.length > 0) {
setGlobalError(`Algunos ingredientes no se pudieron crear:\n${errors.join('\n')}`);
}
} catch (error) {
console.error('Error in batch creation:', error);
setGlobalError('Error al crear los ingredientes. Inténtalo de nuevo.');
} finally {
setIsSubmitting(false);
}
};
const handleClose = () => {
setRows([
{ id: '1', name: '', category: 'Baking Ingredients', unit_of_measure: 'kg' },
{ id: '2', name: '', category: 'Baking Ingredients', unit_of_measure: 'kg' },
{ id: '3', name: '', category: 'Baking Ingredients', unit_of_measure: 'kg' }
]);
setGlobalError(null);
onClose();
};
if (!isOpen) return null;
return (
<>
{/* Backdrop */}
<div
className="fixed inset-0 bg-black/50 z-40 animate-fadeIn"
onClick={handleClose}
/>
{/* Modal */}
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 pointer-events-none">
<div
className="bg-[var(--bg-primary)] rounded-lg shadow-2xl max-w-6xl w-full max-h-[90vh] overflow-y-auto pointer-events-auto animate-slideUp"
onClick={(e) => e.stopPropagation()}
>
{/* Header */}
<div className="flex items-center justify-between p-6 border-b border-[var(--border-secondary)] sticky top-0 bg-[var(--bg-primary)] z-10">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-full bg-[var(--color-primary)]/10 flex items-center justify-center">
<svg className="w-5 h-5 text-[var(--color-primary)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
</div>
<div>
<h2 className="text-xl font-semibold text-[var(--text-primary)]">
📋 Agregar Múltiples Ingredientes
</h2>
<p className="text-sm text-[var(--text-secondary)] mt-0.5">
Agrega varios ingredientes a la vez para ahorrar tiempo
</p>
</div>
</div>
<button
onClick={handleClose}
className="p-2 text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-secondary)] rounded-lg transition-colors"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
{/* Form */}
<form onSubmit={handleSubmit} className="p-6 space-y-5">
{/* Quick Actions */}
<div className="flex gap-2">
<button
type="button"
onClick={loadFromTemplates}
className="px-4 py-2 bg-[var(--color-primary)]/10 text-[var(--color-primary)] rounded-lg hover:bg-[var(--color-primary)]/20 transition-colors text-sm font-medium flex items-center gap-2"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
</svg>
Cargar Plantillas Comunes
</button>
<button
type="button"
onClick={addRow}
className="px-4 py-2 bg-[var(--bg-secondary)] text-[var(--text-primary)] rounded-lg hover:bg-[var(--bg-tertiary)] transition-colors text-sm font-medium flex items-center gap-2"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
</svg>
Agregar Fila
</button>
</div>
{/* Table */}
<div className="border border-[var(--border-secondary)] rounded-lg overflow-hidden">
<div className="overflow-x-auto">
<table className="w-full">
<thead>
<tr className="bg-[var(--bg-secondary)] border-b border-[var(--border-secondary)]">
<th className="px-3 py-2 text-left text-xs font-semibold text-[var(--text-primary)] w-8">#</th>
<th className="px-3 py-2 text-left text-xs font-semibold text-[var(--text-primary)]">Nombre *</th>
<th className="px-3 py-2 text-left text-xs font-semibold text-[var(--text-primary)]">Categoría *</th>
<th className="px-3 py-2 text-left text-xs font-semibold text-[var(--text-primary)]">Unidad *</th>
<th className="px-3 py-2 text-left text-xs font-semibold text-[var(--text-primary)]">Stock Inicial</th>
<th className="px-3 py-2 text-left text-xs font-semibold text-[var(--text-primary)]">Costo ()</th>
<th className="px-3 py-2 text-left text-xs font-semibold text-[var(--text-primary)] w-12"></th>
</tr>
</thead>
<tbody>
{rows.map((row, index) => (
<tr
key={row.id}
className={`border-b border-[var(--border-secondary)] hover:bg-[var(--bg-secondary)]/50 transition-colors ${row.error ? 'bg-[var(--color-error)]/5' : ''}`}
>
<td className="px-3 py-2 text-sm text-[var(--text-secondary)]">
{index + 1}
</td>
<td className="px-3 py-2">
<input
type="text"
value={row.name}
onChange={(e) => updateRow(row.id, 'name', e.target.value)}
className="w-full px-2 py-1.5 bg-[var(--bg-primary)] border border-[var(--border-secondary)] rounded text-sm text-[var(--text-primary)] focus:outline-none focus:ring-1 focus:ring-[var(--color-primary)]"
placeholder="Ej: Harina"
/>
{row.error && (
<p className="text-xs text-[var(--color-error)] mt-1">{row.error}</p>
)}
</td>
<td className="px-3 py-2">
<select
value={row.category}
onChange={(e) => updateRow(row.id, 'category', e.target.value)}
className="w-full px-2 py-1.5 bg-[var(--bg-primary)] border border-[var(--border-secondary)] rounded text-sm text-[var(--text-primary)] focus:outline-none focus:ring-1 focus:ring-[var(--color-primary)]"
>
{categoryOptions.map(cat => (
<option key={cat} value={cat}>{cat}</option>
))}
</select>
</td>
<td className="px-3 py-2">
<select
value={row.unit_of_measure}
onChange={(e) => updateRow(row.id, 'unit_of_measure', e.target.value)}
className="w-full px-2 py-1.5 bg-[var(--bg-primary)] border border-[var(--border-secondary)] rounded text-sm text-[var(--text-primary)] focus:outline-none focus:ring-1 focus:ring-[var(--color-primary)]"
>
{unitOptions.map(unit => (
<option key={unit} value={unit}>{unit}</option>
))}
</select>
</td>
<td className="px-3 py-2">
<input
type="number"
min="0"
step="0.01"
value={row.stock_quantity || ''}
onChange={(e) => updateRow(row.id, 'stock_quantity', parseFloat(e.target.value) || undefined)}
className="w-full px-2 py-1.5 bg-[var(--bg-primary)] border border-[var(--border-secondary)] rounded text-sm text-[var(--text-primary)] focus:outline-none focus:ring-1 focus:ring-[var(--color-primary)]"
placeholder="0"
/>
</td>
<td className="px-3 py-2">
<input
type="number"
min="0"
step="0.01"
value={row.cost_per_unit || ''}
onChange={(e) => updateRow(row.id, 'cost_per_unit', parseFloat(e.target.value) || undefined)}
className="w-full px-2 py-1.5 bg-[var(--bg-primary)] border border-[var(--border-secondary)] rounded text-sm text-[var(--text-primary)] focus:outline-none focus:ring-1 focus:ring-[var(--color-primary)]"
placeholder="0.00"
/>
</td>
<td className="px-3 py-2">
<button
type="button"
onClick={() => removeRow(row.id)}
disabled={rows.length === 1}
className="p-1.5 text-[var(--color-error)] hover:bg-[var(--color-error)]/10 rounded transition-colors disabled:opacity-30 disabled:cursor-not-allowed"
title="Eliminar fila"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
{/* Info Box */}
<div className="bg-[var(--color-info)]/10 border border-[var(--color-info)]/20 rounded-lg p-3">
<p className="text-sm text-[var(--text-secondary)] flex items-start gap-2">
<svg className="w-4 h-4 text-[var(--color-info)] mt-0.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<span>
💡 Los campos de stock y costo son opcionales. Puedes completarlos más tarde en la gestión de inventario.
</span>
</p>
</div>
{/* Global Error */}
{globalError && (
<div className="bg-[var(--color-error)]/10 border border-[var(--color-error)]/20 rounded-lg p-3">
<p className="text-sm text-[var(--color-error)] flex items-center gap-2 whitespace-pre-line">
<svg className="w-4 h-4 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clipRule="evenodd" />
</svg>
{globalError}
</p>
</div>
)}
{/* Actions */}
<div className="flex gap-3 pt-2 sticky bottom-0 bg-[var(--bg-primary)] pb-2 border-t border-[var(--border-secondary)] -mx-6 px-6 -mb-5">
<button
type="button"
onClick={handleClose}
disabled={isSubmitting}
className="flex-1 px-4 py-2.5 text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-secondary)] rounded-lg transition-colors font-medium disabled:opacity-50"
>
Cancelar
</button>
<button
type="submit"
disabled={isSubmitting}
className="flex-1 px-4 py-2.5 bg-[var(--color-primary)] text-white rounded-lg hover:bg-[var(--color-primary-dark)] disabled:opacity-50 disabled:cursor-not-allowed transition-colors font-medium flex items-center justify-center gap-2"
>
{isSubmitting ? (
<>
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
Creando {rows.length} ingredientes...
</>
) : (
<>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
</svg>
Crear {rows.length} Ingredientes
</>
)}
</button>
</div>
</form>
</div>
</div>
{/* Animation Styles */}
<style>{`
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animate-fadeIn {
animation: fadeIn 0.2s ease-out;
}
.animate-slideUp {
animation: slideUp 0.3s ease-out;
}
`}</style>
</>
);
};

View File

@@ -1,7 +1,13 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useCreateIngredient } from '../../../api/hooks/inventory';
import { useCreateIngredient, useIngredients } from '../../../api/hooks/inventory';
import type { Ingredient } from '../../../api/types/inventory';
import {
findSimilarIngredients,
suggestCategory,
commonIngredientTemplates,
type IngredientTemplate
} from './ingredientHelpers';
interface QuickAddIngredientModalProps {
isOpen: boolean;
@@ -21,6 +27,11 @@ export const QuickAddIngredientModal: React.FC<QuickAddIngredientModalProps> = (
const { t } = useTranslation();
const createIngredient = useCreateIngredient();
// Fetch existing ingredients for duplicate detection
const { data: existingIngredients = [] } = useIngredients(tenantId, {}, {
enabled: isOpen && !!tenantId
});
// Form state - minimal required fields
const [formData, setFormData] = useState({
name: '',
@@ -41,6 +52,8 @@ export const QuickAddIngredientModal: React.FC<QuickAddIngredientModalProps> = (
const [showOptionalFields, setShowOptionalFields] = useState(false);
const [errors, setErrors] = useState<Record<string, string>>({});
const [isSubmitting, setIsSubmitting] = useState(false);
const [showTemplates, setShowTemplates] = useState(false);
const [similarIngredients, setSimilarIngredients] = useState<Array<{ id: string; name: string; similarity: number }>>([]);
const categoryOptions = [
'Baking Ingredients',
@@ -55,6 +68,42 @@ export const QuickAddIngredientModal: React.FC<QuickAddIngredientModalProps> = (
const unitOptions = ['kg', 'g', 'L', 'ml', 'units', 'dozen'];
// Check for duplicates when name changes
useEffect(() => {
if (formData.name && formData.name.trim().length >= 3) {
const similar = findSimilarIngredients(
formData.name,
existingIngredients.map(ing => ({ id: ing.id, name: ing.name }))
);
setSimilarIngredients(similar);
} else {
setSimilarIngredients([]);
}
}, [formData.name, existingIngredients]);
// Smart category suggestion when name changes
useEffect(() => {
if (formData.name && formData.name.trim().length >= 3 && !formData.category) {
const suggested = suggestCategory(formData.name);
if (suggested) {
setFormData(prev => ({ ...prev, category: suggested }));
}
}
}, [formData.name]);
const handleApplyTemplate = (template: IngredientTemplate) => {
setFormData({
...formData,
name: template.name,
category: template.category,
unit_of_measure: template.unit_of_measure,
estimated_shelf_life_days: template.estimated_shelf_life_days || 30,
requires_refrigeration: template.requires_refrigeration || false,
requires_freezing: template.requires_freezing || false,
});
setShowTemplates(false);
};
const validateForm = (): boolean => {
const newErrors: Record<string, string> = {};
@@ -150,6 +199,8 @@ export const QuickAddIngredientModal: React.FC<QuickAddIngredientModalProps> = (
notes: '',
});
setShowOptionalFields(false);
setShowTemplates(false);
setSimilarIngredients([]);
setErrors({});
};
@@ -225,6 +276,62 @@ export const QuickAddIngredientModal: React.FC<QuickAddIngredientModalProps> = (
{/* Form */}
<form onSubmit={handleSubmit} className="p-6 space-y-5">
{/* Quick Templates */}
{!showTemplates && (
<button
type="button"
onClick={() => setShowTemplates(!showTemplates)}
className="w-full p-3 mb-2 bg-gradient-to-r from-[var(--color-primary)]/5 to-[var(--color-primary)]/10 border border-[var(--color-primary)]/20 rounded-lg hover:from-[var(--color-primary)]/10 hover:to-[var(--color-primary)]/15 transition-all group"
>
<div className="flex items-center justify-center gap-2 text-[var(--color-primary)]">
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
</svg>
<span className="font-medium"> Usar Plantilla Rápida</span>
<svg className="w-4 h-4 transition-transform group-hover:translate-x-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
</svg>
</div>
</button>
)}
{showTemplates && (
<div className="mb-4 p-4 bg-[var(--bg-secondary)] rounded-lg border border-[var(--border-secondary)] animate-slideDown">
<div className="flex items-center justify-between mb-3">
<h3 className="text-sm font-semibold text-[var(--text-primary)]">Plantillas Comunes</h3>
<button
type="button"
onClick={() => setShowTemplates(false)}
className="text-xs text-[var(--text-secondary)] hover:text-[var(--text-primary)]"
>
Cerrar
</button>
</div>
<div className="grid grid-cols-2 gap-2">
{commonIngredientTemplates.map((template, index) => (
<button
key={index}
type="button"
onClick={() => handleApplyTemplate(template)}
className="p-2.5 bg-[var(--bg-primary)] hover:bg-[var(--bg-tertiary)] border border-[var(--border-secondary)] hover:border-[var(--color-primary)] rounded-lg transition-all text-left group"
>
<div className="flex items-center gap-2">
<span className="text-xl">{template.icon}</span>
<div className="flex-1 min-w-0">
<div className="text-sm font-medium text-[var(--text-primary)] truncate group-hover:text-[var(--color-primary)]">
{template.name}
</div>
<div className="text-xs text-[var(--text-tertiary)]">
{template.category}
</div>
</div>
</div>
</button>
))}
</div>
</div>
)}
{/* Required Fields */}
<div className="space-y-4">
<div>
@@ -247,12 +354,46 @@ export const QuickAddIngredientModal: React.FC<QuickAddIngredientModalProps> = (
{errors.name}
</p>
)}
{/* Duplicate Detection Warning */}
{similarIngredients.length > 0 && (
<div className="mt-2 p-2.5 bg-[var(--color-warning)]/10 border border-[var(--color-warning)]/30 rounded-lg animate-slideDown">
<div className="flex items-start gap-2">
<svg className="w-4 h-4 text-[var(--color-warning)] mt-0.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
</svg>
<div className="flex-1">
<p className="text-xs font-medium text-[var(--color-warning)] mb-1">
Ingredientes similares encontrados:
</p>
<ul className="text-xs text-[var(--text-secondary)] space-y-0.5">
{similarIngredients.map((similar) => (
<li key={similar.id} className="flex items-center gap-2">
<span className="font-medium">{similar.name}</span>
<span className="text-[var(--text-tertiary)]">
({similar.similarity}% similar)
</span>
</li>
))}
</ul>
<p className="text-xs text-[var(--text-tertiary)] mt-1.5">
Verifica que no sea un duplicado antes de continuar.
</p>
</div>
</div>
</div>
)}
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium text-[var(--text-primary)] mb-1.5">
Categoría *
{formData.category && suggestCategory(formData.name) === formData.category && (
<span className="ml-2 text-xs text-[var(--color-success)] font-normal">
Sugerido automáticamente
</span>
)}
</label>
<select
value={formData.category}

View File

@@ -0,0 +1,228 @@
/**
* Helper utilities for ingredient management
*/
// Levenshtein distance calculation for fuzzy string matching
export function levenshteinDistance(str1: string, str2: string): number {
const s1 = str1.toLowerCase().trim();
const s2 = str2.toLowerCase().trim();
const matrix: number[][] = [];
// Initialize first column
for (let i = 0; i <= s2.length; i++) {
matrix[i] = [i];
}
// Initialize first row
for (let j = 0; j <= s1.length; j++) {
matrix[0][j] = j;
}
// Fill the matrix
for (let i = 1; i <= s2.length; i++) {
for (let j = 1; j <= s1.length; j++) {
if (s2.charAt(i - 1) === s1.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1, // substitution
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1 // deletion
);
}
}
}
return matrix[s2.length][s1.length];
}
// Calculate similarity percentage (0-100)
export function calculateSimilarity(str1: string, str2: string): number {
const maxLen = Math.max(str1.length, str2.length);
if (maxLen === 0) return 100;
const distance = levenshteinDistance(str1, str2);
return Math.round(((maxLen - distance) / maxLen) * 100);
}
// Find similar ingredient names
export function findSimilarIngredients(
name: string,
existingIngredients: { id: string; name: string }[],
similarityThreshold: number = 70
): Array<{ id: string; name: string; similarity: number }> {
if (!name || name.trim().length < 3) return [];
const similar = existingIngredients
.map(ingredient => ({
...ingredient,
similarity: calculateSimilarity(name, ingredient.name)
}))
.filter(item => item.similarity >= similarityThreshold && item.similarity < 100)
.sort((a, b) => b.similarity - a.similarity);
return similar.slice(0, 3); // Return top 3 matches
}
// Smart category suggestions based on ingredient name
const categoryPatterns: Record<string, string[]> = {
'Baking Ingredients': [
'harina', 'flour', 'levadura', 'yeast', 'polvo de hornear', 'baking powder',
'bicarbonato', 'baking soda', 'azúcar', 'sugar', 'sal', 'salt', 'masa',
'dough', 'hojaldre', 'puff pastry', 'chocolate', 'cacao', 'cocoa', 'vainilla',
'vanilla', 'canela', 'cinnamon'
],
'Dairy': [
'leche', 'milk', 'mantequilla', 'butter', 'queso', 'cheese', 'crema', 'cream',
'nata', 'yogur', 'yogurt', 'requesón', 'cottage cheese', 'ricotta'
],
'Fruits': [
'manzana', 'apple', 'fresa', 'strawberry', 'plátano', 'banana', 'naranja',
'orange', 'limón', 'lemon', 'frambuesa', 'raspberry', 'arándano', 'blueberry',
'cereza', 'cherry', 'pera', 'pear', 'durazno', 'peach', 'melocotón', 'mango',
'piña', 'pineapple', 'kiwi', 'uva', 'grape'
],
'Vegetables': [
'tomate', 'tomato', 'lechuga', 'lettuce', 'cebolla', 'onion', 'zanahoria',
'carrot', 'papa', 'patata', 'potato', 'pimiento', 'pepper', 'espinaca',
'spinach', 'brócoli', 'broccoli', 'calabacín', 'zucchini', 'berenjena',
'eggplant', 'aguacate', 'avocado'
],
'Meat': [
'pollo', 'chicken', 'carne', 'beef', 'cerdo', 'pork', 'jamón', 'ham',
'tocino', 'bacon', 'salchicha', 'sausage', 'pavo', 'turkey', 'cordero', 'lamb'
],
'Seafood': [
'pescado', 'fish', 'salmón', 'salmon', 'atún', 'tuna', 'camarón', 'shrimp',
'langostino', 'prawn', 'mejillón', 'mussel', 'almeja', 'clam', 'calamar',
'squid', 'pulpo', 'octopus'
],
'Spices': [
'pimienta', 'pepper', 'orégano', 'oregano', 'albahaca', 'basil', 'tomillo',
'thyme', 'romero', 'rosemary', 'perejil', 'parsley', 'cilantro', 'coriander',
'comino', 'cumin', 'paprika', 'pimentón', 'nuez moscada', 'nutmeg', 'jengibre',
'ginger', 'ajo', 'garlic', 'clavo', 'clove'
]
};
export function suggestCategory(ingredientName: string): string | null {
if (!ingredientName || ingredientName.trim().length < 2) return null;
const nameLower = ingredientName.toLowerCase().trim();
// Check each category's patterns
for (const [category, patterns] of Object.entries(categoryPatterns)) {
for (const pattern of patterns) {
if (nameLower.includes(pattern) || pattern.includes(nameLower)) {
return category;
}
}
}
return null;
}
// Quick templates for common bakery ingredients
export interface IngredientTemplate {
name: string;
category: string;
unit_of_measure: string;
icon: string;
estimated_shelf_life_days?: number;
requires_refrigeration?: boolean;
requires_freezing?: boolean;
}
export const commonIngredientTemplates: IngredientTemplate[] = [
{
name: 'Harina de Trigo',
category: 'Baking Ingredients',
unit_of_measure: 'kg',
icon: '🌾',
estimated_shelf_life_days: 180,
requires_refrigeration: false,
requires_freezing: false,
},
{
name: 'Mantequilla',
category: 'Dairy',
unit_of_measure: 'kg',
icon: '🧈',
estimated_shelf_life_days: 30,
requires_refrigeration: true,
requires_freezing: false,
},
{
name: 'Azúcar',
category: 'Baking Ingredients',
unit_of_measure: 'kg',
icon: '🍬',
estimated_shelf_life_days: 365,
requires_refrigeration: false,
requires_freezing: false,
},
{
name: 'Huevos',
category: 'Dairy',
unit_of_measure: 'dozen',
icon: '🥚',
estimated_shelf_life_days: 21,
requires_refrigeration: true,
requires_freezing: false,
},
{
name: 'Levadura',
category: 'Baking Ingredients',
unit_of_measure: 'kg',
icon: '🍞',
estimated_shelf_life_days: 90,
requires_refrigeration: true,
requires_freezing: false,
},
{
name: 'Leche',
category: 'Dairy',
unit_of_measure: 'L',
icon: '🥛',
estimated_shelf_life_days: 7,
requires_refrigeration: true,
requires_freezing: false,
},
{
name: 'Chocolate',
category: 'Baking Ingredients',
unit_of_measure: 'kg',
icon: '🍫',
estimated_shelf_life_days: 180,
requires_refrigeration: false,
requires_freezing: false,
},
{
name: 'Vainilla (Extracto)',
category: 'Baking Ingredients',
unit_of_measure: 'ml',
icon: '🌸',
estimated_shelf_life_days: 365,
requires_refrigeration: false,
requires_freezing: false,
},
{
name: 'Sal',
category: 'Baking Ingredients',
unit_of_measure: 'kg',
icon: '🧂',
estimated_shelf_life_days: 9999,
requires_refrigeration: false,
requires_freezing: false,
},
{
name: 'Crema de Leche',
category: 'Dairy',
unit_of_measure: 'L',
icon: '🥛',
estimated_shelf_life_days: 14,
requires_refrigeration: true,
requires_freezing: false,
},
];

View File

@@ -7,6 +7,7 @@ import { useValidateImportFile, useImportSalesData } from '../../../../api/hooks
import type { ImportValidationResponse } from '../../../../api/types/dataImport';
import type { ProductSuggestionResponse } from '../../../../api/types/inventory';
import { useAuth } from '../../../../contexts/AuthContext';
import { BatchAddIngredientsModal } from '../../inventory/BatchAddIngredientsModal';
interface UploadSalesDataStepProps {
onNext: () => void;
@@ -64,6 +65,7 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
// Form state for adding/editing
const [isAdding, setIsAdding] = useState(false);
const [editingId, setEditingId] = useState<string | null>(null);
const [showBatchModal, setShowBatchModal] = useState(false);
const [formData, setFormData] = useState<InventoryItemForm>({
id: '',
name: '',
@@ -720,20 +722,36 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
</form>
</div>
) : (
<button
type="button"
onClick={() => setIsAdding(true)}
className="w-full p-4 border-2 border-dashed border-[var(--border-secondary)] rounded-lg hover:border-[var(--color-primary)] hover:bg-[var(--bg-secondary)] transition-colors group"
>
<div className="flex items-center justify-center gap-2 text-[var(--text-secondary)] group-hover:text-[var(--color-primary)]">
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
</svg>
<span className="font-medium">
Agregar Ingrediente Manualmente
</span>
</div>
</button>
<div className="grid grid-cols-2 gap-3">
<button
type="button"
onClick={() => setIsAdding(true)}
className="p-4 border-2 border-dashed border-[var(--border-secondary)] rounded-lg hover:border-[var(--color-primary)] hover:bg-[var(--bg-secondary)] transition-colors group"
>
<div className="flex items-center justify-center gap-2 text-[var(--text-secondary)] group-hover:text-[var(--color-primary)]">
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
</svg>
<span className="font-medium">
Agregar Uno
</span>
</div>
</button>
<button
type="button"
onClick={() => setShowBatchModal(true)}
className="p-4 border-2 border-dashed border-[var(--color-primary)]/30 rounded-lg hover:border-[var(--color-primary)] hover:bg-[var(--color-primary)]/5 transition-colors group"
>
<div className="flex items-center justify-center gap-2 text-[var(--color-primary)]">
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
<span className="font-medium">
Agregar Varios
</span>
</div>
</button>
</div>
)}
{error && (
@@ -787,6 +805,35 @@ export const UploadSalesDataStep: React.FC<UploadSalesDataStepProps> = ({
</button>
</div>
)}
{/* Batch Add Modal */}
<BatchAddIngredientsModal
isOpen={showBatchModal}
onClose={() => setShowBatchModal(false)}
onCreated={(ingredients) => {
// Add all created ingredients to the list
const newItems: InventoryItemForm[] = ingredients.map(ing => ({
id: ing.id,
name: ing.name,
product_type: ing.product_type,
category: ing.category,
unit_of_measure: ing.unit_of_measure,
stock_quantity: 0,
cost_per_unit: ing.average_cost || 0,
estimated_shelf_life_days: ing.shelf_life_days || 30,
requires_refrigeration: ing.requires_refrigeration || false,
requires_freezing: ing.requires_freezing || false,
is_seasonal: ing.is_seasonal || false,
low_stock_threshold: ing.low_stock_threshold || 0,
reorder_point: ing.reorder_point || 0,
notes: ing.notes || '',
isSuggested: false,
}));
setInventoryItems([...inventoryItems, ...newItems]);
setShowBatchModal(false);
}}
tenantId={currentTenant?.id || ''}
/>
</div>
);
}