import React, { useState } from 'react'; import { ExcelRow, COLUMNS } from '../types'; import { X, Sparkles, Save, Loader2, Languages, Package } from 'lucide-react'; import { generateGemini } from '../services/gemini'; import { cn } from '../lib/utils'; interface EditPanelProps { row: ExcelRow; rowIndex: number; onSave: (rowIndex: number, updatedRow: ExcelRow) => void; onClose: () => void; } export function EditPanel({ row, rowIndex, onSave, onClose }: EditPanelProps) { const [formData, setFormData] = useState({ longDe: row[COLUMNS.LONG_DE] || '', longEn: row[COLUMNS.LONG_EN] || '', shortDe: row[COLUMNS.SHORT_DE] || '', shortEn: row[COLUMNS.SHORT_EN] || '', innerW: row[COLUMNS.INNER_W] || '', innerL: row[COLUMNS.INNER_L] || '', innerH: row[COLUMNS.INNER_H] || '', outerW: row[COLUMNS.OUTER_W] || '', outerL: row[COLUMNS.OUTER_L] || '', outerH: row[COLUMNS.OUTER_H] || '', unitsOuter: row[COLUMNS.UNITS_OUTER] || '', moq: row[COLUMNS.MOQ] || '', }); const [loadingField, setLoadingField] = useState(null); const [error, setError] = useState(null); const isModified = (field: keyof typeof formData) => { const colMap: Record = { longDe: COLUMNS.LONG_DE, longEn: COLUMNS.LONG_EN, shortDe: COLUMNS.SHORT_DE, shortEn: COLUMNS.SHORT_EN, innerW: COLUMNS.INNER_W, innerL: COLUMNS.INNER_L, innerH: COLUMNS.INNER_H, outerW: COLUMNS.OUTER_W, outerL: COLUMNS.OUTER_L, outerH: COLUMNS.OUTER_H, unitsOuter: COLUMNS.UNITS_OUTER, moq: COLUMNS.MOQ, }; return formData[field] !== (row[colMap[field]] || ''); }; const handleGenerate = async (field: keyof typeof formData) => { setLoadingField(field); setError(null); try { let prompt = ''; const baseContext = `Article Name: ${row[COLUMNS.ARTICLE_NAME]}\nArticle Details (EN): ${row[COLUMNS.DETAILS_EN] || 'N/A'}\nArticle Details (DE): ${row[COLUMNS.DETAILS_DE] || 'N/A'}`; const systemPrompt = "You are a professional copywriter and expert translator for CRAZE GmbH, a German toy company. You excel at translating product descriptions from German to English, maintaining the commercial and professional tone while ensuring all technical toy details are accurate. Use clear, engaging language."; if (field === 'longDe') { prompt = `Based on the following product details, generate a long commercial description in German. It should be fluent, oriented towards B2B toy buyers, 3-5 paragraphs long. Include features, benefits, and material if available.\n\n${baseContext}`; } else if (field === 'longEn') { if (formData.longDe) { prompt = `Translate the EVERYTHING below from German into professional English. CRITICAL INSTRUCTIONS: 1. Do NOT summarize. 2. Maintain the EXACT same length and detailed information. 3. Keep all technical specs intact. 4. Translate every single paragraph. German Text to Translate: ${formData.longDe}`; } else { prompt = `Based on the following product details, generate a long commercial description in English. It should be fluent, oriented towards B2B toy buyers, 3-5 paragraphs long.\n\n${baseContext}`; } } else if (field === 'shortDe') { if (formData.longDe) { prompt = `Create a short version (2-4 sentences max) of the following German product description:\n\n${formData.longDe}`; } else { prompt = `Based on the following product details, generate a short commercial description in German (2-4 sentences max).\n\n${baseContext}`; } } else if (field === 'shortEn') { if (formData.shortDe) { prompt = `Translate exactly this short German product description into professional English for the toy market:\n\n${formData.shortDe}`; } else if (formData.longEn) { prompt = `Create a short version (2-4 sentences max) of the following English product description:\n\n${formData.longEn}`; } else { prompt = `Based on the following product details, generate a short commercial description in English (2-4 sentences max).\n\n${baseContext}`; } } const generatedText = await generateGemini(prompt, systemPrompt); setFormData(prev => ({ ...prev, [field]: generatedText.trim() })); } catch (err: any) { setError(err.message || 'An error occurred during generation.'); } finally { setLoadingField(null); } }; const handleSave = () => { const newRow = [...row]; newRow[COLUMNS.LONG_DE] = formData.longDe; newRow[COLUMNS.LONG_EN] = formData.longEn; newRow[COLUMNS.SHORT_DE] = formData.shortDe; newRow[COLUMNS.SHORT_EN] = formData.shortEn; newRow[COLUMNS.INNER_W] = formData.innerW; newRow[COLUMNS.INNER_L] = formData.innerL; newRow[COLUMNS.INNER_H] = formData.innerH; newRow[COLUMNS.OUTER_W] = formData.outerW; newRow[COLUMNS.OUTER_L] = formData.outerL; newRow[COLUMNS.OUTER_H] = formData.outerH; newRow[COLUMNS.UNITS_OUTER] = formData.unitsOuter; newRow[COLUMNS.MOQ] = formData.moq; onSave(rowIndex, newRow); }; const DimensionInput = ({ label, field, placeholder }: { label: string, field: keyof typeof formData, placeholder?: string }) => (
setFormData(prev => ({ ...prev, [field]: e.target.value }))} placeholder={placeholder} className={cn( "w-full bg-slate-900 border rounded p-2 text-sm text-white focus:outline-none focus:ring-1 transition-colors", isModified(field) ? "border-blue-500 focus:ring-blue-500" : "border-slate-700 focus:border-slate-500" )} />
); const FieldEditor = ({ title, field }: { title: string, field: keyof typeof formData }) => (
{((field === 'longEn' && formData.longDe) || (field === 'shortDe' && formData.longDe) || (field === 'shortEn' && (formData.shortDe || formData.longEn))) && (
Can translate from existing
)}