import React, { useState } from 'react'; import { Save } from 'lucide-react'; import { PanelShell, Label, Field, HelperText } from '../panels/PanelShell'; import { staticMicrositeApi, type MicrositeV2Template } from '../../../services/api'; const inputStyle: React.CSSProperties = { width: '100%', padding: '8px 10px', borderRadius: 'var(--radius-button)', border: '1px solid var(--border)', background: 'var(--input-background)', color: 'var(--foreground)', fontSize: 'var(--text-sm)', boxSizing: 'border-box', }; export function UploadMicrositeV2TemplatePanel({ template, onClose, onSaved, }: { template: MicrositeV2Template | null; onClose: () => void; onSaved: () => void; }) { const isEdit = Boolean(template); const [slug, setSlug] = useState(template?.slug || ''); const [name, setName] = useState(template?.name || ''); const [description, setDescription] = useState(template?.description || ''); const [templateHtml, setTemplateHtml] = useState(null); const [templateJson, setTemplateJson] = useState(null); const [thumbnail, setThumbnail] = useState(null); const [assetsZip, setAssetsZip] = useState(null); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); const handleSave = async () => { setSaving(true); setError(''); try { let templateId = template?._id; if (!isEdit) { if (!slug.trim() || !name.trim()) { throw new Error('Slug and name are required'); } const createRes = await staticMicrositeApi.createTemplate({ slug: slug.trim(), name: name.trim(), description: description.trim() || undefined, }); if (createRes.error || !createRes.data) throw new Error(createRes.error || 'Create failed'); templateId = createRes.data._id; } else if (name !== template?.name || description !== template?.description) { const updateRes = await staticMicrositeApi.updateTemplate(template!._id, { name: name.trim(), description: description.trim(), }); if (updateRes.error) throw new Error(updateRes.error); } if (!templateId) throw new Error('Missing template id'); const hasFiles = templateHtml || templateJson || thumbnail || assetsZip; if (!isEdit && (!templateHtml || !templateJson)) { throw new Error('template.html and template.json are required for new templates'); } if (hasFiles) { const uploadRes = await staticMicrositeApi.uploadTemplateFiles(templateId, { templateHtml: templateHtml || undefined, templateJson: templateJson || undefined, thumbnail: thumbnail || undefined, assetsZip: assetsZip || undefined, }); if (uploadRes.error) throw new Error(uploadRes.error); } onSaved(); } catch (e) { setError(e instanceof Error ? e.message : 'Save failed'); } finally { setSaving(false); } }; return ( void handleSave()} > {error ?

{error}

: null}