import { useState, useEffect } from 'react' import { Send, X } from 'lucide-react' import './ContributionSessionModal.css' import PzBusy from './PzBusy.jsx' import { readContributorName } from '../utils/contributorName' import { t } from '../i18n/index.js' function formatAction(action) { if (action === 'create') return t('Ajout') if (action === 'update') return t('Modification') if (action === 'delete') return t('Suppression') return t('Changement') } /** * Modal de finalisation de session contribution * Pour les contributeurs : commentaire optionnel + récap + liste des changements * * @param {Object} props * @param {boolean} props.visible - Afficher la modal * @param {Array} props.changes - Liste des changements { entityType, action, label } * @param {Object} props.recap - Récap des modifications { added, modified, deleted } * @param {boolean} props.loading - Soumission en cours * @param {string} props.error - Message d'erreur * @param {Function} props.onSubmit - Handler soumission ({ comment }) * @param {Function} props.onCancel - Handler annulation */ function ContributionSessionModal({ visible, changes = [], recap = {}, loading = false, error = '', askName = true, onSubmit, onCancel }) { const [comment, setComment] = useState('') const [name, setName] = useState('') // Reset à l'ouverture ; le prénom déjà donné sur cet appareil est repris useEffect(() => { if (visible) { setComment('') setName(readContributorName()) } }, [visible]) // ESC pour fermer useEffect(() => { if (!visible) return function handleKeyDown(e) { if (e.key === 'Escape') onCancel() } document.addEventListener('keydown', handleKeyDown) return () => document.removeEventListener('keydown', handleKeyDown) }, [visible, onCancel]) const handleSubmit = (e) => { e.preventDefault() if (askName && !name.trim()) return onSubmit({ comment: comment.trim(), name: name.trim() }) } const totalChanges = (recap.added || 0) + (recap.modified || 0) + (recap.deleted || 0) if (!visible) return null const [titleBefore, titleAfter] = t('Envoyer vos {em}').split('{em}') return (
e.stopPropagation()} >

{t('Votre contribution')}

{titleBefore}{t('modifications')}{titleAfter}

{t("La personne qui gère l'arbre les relit, puis elles apparaissent pour toute la famille.")}

{totalChanges > 0 && (
{recap.added > 0 && {recap.added > 1 ? t('{n} ajouts', { n: recap.added }) : t('{n} ajout', { n: recap.added })}} {recap.modified > 0 && {recap.modified > 1 ? t('{n} modifications', { n: recap.modified }) : t('{n} modification', { n: recap.modified })}} {recap.deleted > 0 && {recap.deleted > 1 ? t('{n} suppressions', { n: recap.deleted }) : t('{n} suppression', { n: recap.deleted })}}
)} {changes.length > 0 && ( )}
{askName && (
setName(e.target.value.slice(0, 120))} placeholder={t('Pour que la famille sache qui a ajouté quoi')} autoComplete="given-name" required />
)}