import React, {useState} from 'react'; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; import {faChevronDown, faChevronUp, faPlus, faTrash} from '@fortawesome/free-solid-svg-icons'; import {Incident} from '@/lib/models/Book'; import {ActChapter, ChapterListProps} from '@/lib/models/Chapter'; import ActChapterItem from './ActChapter'; import {useTranslations} from 'next-intl'; interface ActIncidentsProps { incidents: Incident[]; actId: number; mainChapters: ChapterListProps[]; newIncidentTitle: string; setNewIncidentTitle: (title: string) => void; onAddIncident: (actId: number) => Promise; onDeleteIncident: (actId: number, incidentId: string) => Promise; onLinkChapter: (actId: number, chapterId: string, incidentId: string) => Promise; onUpdateChapterSummary: (chapterId: string, summary: string, incidentId: string) => void; onUnlinkChapter: (chapterInfoId: string, chapterId: string, incidentId: string) => Promise; sectionKey: string; isExpanded: boolean; onToggleSection: (sectionKey: string) => void; } export default function ActIncidents({ incidents, actId, mainChapters, newIncidentTitle, setNewIncidentTitle, onAddIncident, onDeleteIncident, onLinkChapter, onUpdateChapterSummary, onUnlinkChapter, sectionKey, isExpanded, onToggleSection, }: ActIncidentsProps) { const t = useTranslations('actComponent'); const [expandedItems, setExpandedItems] = useState<{ [key: string]: boolean }>({}); const [selectedChapterId, setSelectedChapterId] = useState(''); function toggleItem(itemKey: string): void { setExpandedItems(prev => ({ ...prev, [itemKey]: !prev[itemKey], })); } return (
{isExpanded && (
{incidents && incidents.length > 0 ? ( <> {incidents.map((item: Incident) => { const itemKey = `incident_${item.incidentId}`; const isItemExpanded: boolean = expandedItems[itemKey]; return (
{isItemExpanded && (
{item.chapters && item.chapters.length > 0 ? ( <> {item.chapters.map((chapter: ActChapter) => ( onUpdateChapterSummary(chapterId, summary, item.incidentId) } onUnlink={(chapterInfoId, chapterId) => onUnlinkChapter(chapterInfoId, chapterId, item.incidentId) } /> ))} ) : (

{t('noLinkedChapter')}

)}
)}
); })} ) : (

{t('noIncidentAdded')}

)}
setNewIncidentTitle(e.target.value)} placeholder={t('newIncidentPlaceholder')} />
)} ); }