'use client'; import React, {useCallback, useContext, useMemo, useState} from 'react'; import {useLocations, UseLocationsConfig, LocationProps, Element, SubElement} from '@/hooks/settings/useLocations'; import {useTranslations} from 'next-intl'; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import {faSpinner, faPlus, faToggleOn} from '@fortawesome/free-solid-svg-icons'; import {BookContext} from '@/context/BookContext'; import {SeriesLocationItem} from '@/lib/models/Series'; import ToolDetailHeader from '@/components/book/settings/ToolDetailHeader'; import AlertBox from '@/components/AlertBox'; import InputField from '@/components/form/InputField'; import TextInput from '@/components/form/TextInput'; import ToggleSwitch from '@/components/form/ToggleSwitch'; import SeriesImportSelector from '@/components/form/SeriesImportSelector'; import LocationEditorList from './LocationEditorList'; import LocationEditorDetail from './LocationEditorDetail'; import LocationEditorEdit from './LocationEditorEdit'; /** * LocationEditor - Orchestrateur pour ComposerRightBar * Mêmes fonctionnalités que LocationSettings, layout condensé * Inclut: toggle tool, import from series, export to series */ export default function LocationEditor(): React.JSX.Element { const t = useTranslations(); const {book} = useContext(BookContext); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [showAddForm, setShowAddForm] = useState(false); const config: UseLocationsConfig = useMemo(function (): UseLocationsConfig { return { entityType: 'book', entityId: book?.bookId || '', }; }, [book?.bookId]); const { sections, seriesLocations, toolEnabled, isLoading, bookSeriesId, newSectionName, newElementNames, newSubElementNames, viewMode, selectedSectionIndex, addSection, addElement, addSubElement, removeSection, removeElement, removeSubElement, updateElement, updateSubElement, saveLocations, toggleTool, importFromSeries, exportToSeries, setNewSectionName, setNewElementNames, setNewSubElementNames, enterDetailMode, enterEditMode, exitEditMode, backToList, } = useLocations(config); const availableSeriesLocations = useMemo(function (): SeriesLocationItem[] { return seriesLocations.filter(function (sl: SeriesLocationItem): boolean { return !sections.some(function (s: LocationProps): boolean { return s.seriesLocationId === sl.id; }); }); }, [seriesLocations, sections]); // Wrapper pour convertir LocationProps en index const handleSectionClick = useCallback(function (section: LocationProps, index: number): void { enterDetailMode(index); }, [enterDetailMode]); // Gestion de l'ajout async function handleAddSection(): Promise { if (newSectionName.trim()) { await addSection(); setShowAddForm(false); } else { setShowAddForm(true); } } async function handleSave(): Promise { await exitEditMode(true); } function handleCancel(): void { exitEditMode(false); } async function handleDelete(): Promise { if (selectedSectionIndex >= 0 && sections[selectedSectionIndex]) { await removeSection(sections[selectedSectionIndex].id); setShowDeleteConfirm(false); backToList(); } } if (isLoading) { return (
); } const selectedSection: LocationProps | undefined = sections[selectedSectionIndex]; const canExport: boolean = Boolean(bookSeriesId && selectedSection && !selectedSection.seriesLocationId); return (
{ return exportToSeries(selectedSection!); } : undefined} showExport={canExport} showDelete={Boolean(selectedSection)} />
{viewMode === 'list' && (
{/* Toggle tool */}
} />
{toolEnabled && ( <> {/* Import from series */} {bookSeriesId && availableSeriesLocations.length > 0 && ( )} {showAddForm && (
): void { setNewSectionName(e.target.value); }} placeholder={t('locationComponent.newSectionPlaceholder')} /> } actionIcon={faPlus} actionLabel={t('locationComponent.addSectionLabel')} addButtonCallBack={async function (): Promise { await addSection(); setShowAddForm(false); }} />
)} )}
)} {viewMode === 'detail' && selectedSection && (
)} {viewMode === 'edit' && selectedSection && (
)}
{showDeleteConfirm && selectedSection && ( )}
); }