import { useState } from 'react';
import { Upload, Plus, Trash2 } from 'lucide-react';
import { adminApi } from '../api';
export function ImageUpload({ value, onChange, label = 'Image' }) {
const [uploading, setUploading] = useState(false);
const handleUpload = async (e) => {
const file = e.target.files?.[0];
if (!file) return;
setUploading(true);
try {
const { url } = await adminApi.upload(file);
onChange(url);
} catch (err) {
alert(err.message);
} finally {
setUploading(false);
}
};
return (
{value && (
{value.match(/\.(mp4|webm)$/i) ? (
) : (

)}
)}
onChange(e.target.value)}
placeholder="Paste URL or upload a file below"
/>
);
}
export function Field({ label, value, onChange, multiline = false, type = 'text' }) {
return (
{multiline ? (
);
}
export function ArrayEditor({ label, items, onChange, fields }) {
const updateItem = (index, key, val) => {
const next = items.map((item, i) => (i === index ? { ...item, [key]: val } : item));
onChange(next);
};
const addItem = () => {
const blank = fields.reduce((acc, f) => ({ ...acc, [f.key]: f.default ?? '' }), { id: Date.now() });
onChange([...items, blank]);
};
const removeItem = (index) => onChange(items.filter((_, i) => i !== index));
return (
{items.length === 0 && (
No items yet. Click "Add item" to create one.
)}
{items.map((item, index) => (
Item {index + 1}
{fields.map((f) =>
f.type === 'image' ? (
updateItem(index, f.key, v)} />
) : (
updateItem(index, f.key, v)}
multiline={f.multiline}
/>
)
)}
))}
);
}