'use client';
import React, { useState, useEffect } from 'react';
import { Textarea } from '@/components/ui/textarea';
import { Label } from '@/components/ui/label';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Eye, Edit3, AlertCircle, HelpCircle } from 'lucide-react';
import { previewMarkdown, validateMarkdown } from '@/lib/markdown';
interface MarkdownEditorProps {
value: string;
onChange: (value: string) => void;
placeholder?: string;
label?: string;
maxLength?: number;
className?: string;
}
export function MarkdownEditor({
value,
onChange,
placeholder = "Écrivez votre description...",
label = "Description",
maxLength = 5000,
className = ""
}: MarkdownEditorProps) {
const [activeTab, setActiveTab] = useState<'edit' | 'preview'>('edit');
const [validation, setValidation] = useState<{ isValid: boolean; errors: string[] }>({ isValid: true, errors: [] });
const [showHelp, setShowHelp] = useState(false);
// Validation en temps réel
useEffect(() => {
const validationResult = validateMarkdown(value);
setValidation(validationResult);
}, [value]);
const handleChange = (newValue: string) => {
if (newValue.length <= maxLength) {
onChange(newValue);
}
};
const previewContent = previewMarkdown(value);
return (
{value.length}/{maxLength}
{activeTab === 'edit' && (
)}
{activeTab === 'preview' && (
{value ? (
) : (
Aucun contenu à prévisualiser
)}
)}
{/* Messages d'erreur */}
{!validation.isValid && (
{validation.errors.map((error, index) => (
- {error}
))}
)}
{/* Avertissement de longueur */}
{value.length > maxLength * 0.9 && (
maxLength ? "destructive" : "default"}>
{value.length > maxLength
? `Le contenu dépasse la limite de ${maxLength} caractères`
: `Le contenu approche de la limite de ${maxLength} caractères`
}
)}
);
}