import React, {useState} from 'react'; import {ChevronDown, ChevronUp, Plus, Trash2} from 'lucide-react'; import {Incident} from '@/lib/types/book'; import {ActChapter, ChapterListProps} from '@/lib/types/chapter'; import ActChapterItem from './ActChapter'; import IconButton from '@/components/ui/IconButton'; import Collapse from '@/components/ui/Collapse'; import SelectBox, {SelectBoxProps} from '@/components/form/SelectBox'; import InputField from '@/components/form/InputField'; import TextInput from '@/components/form/TextInput'; import {useTranslations} from '@/lib/i18n'; 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], })); } function mainChaptersData(): SelectBoxProps[] { return mainChapters.map(function (chapter: ChapterListProps): SelectBoxProps { return { value: chapter.chapterId, label: `${chapter.chapterOrder}. ${chapter.title}`, }; }); } return (
{incidents && incidents.length > 0 ? ( incidents.map(function (item: Incident): React.JSX.Element { const itemKey: string = `incident_${item.incidentId}`; const isItemExpanded: boolean = expandedItems[itemKey]; return (
{isItemExpanded && (
{item.chapters && item.chapters.length > 0 ? ( item.chapters.map(function (chapter: ActChapter): React.JSX.Element { return ( { return onUnlinkChapter(chapterInfoId, chapterId, item.incidentId); }} /> ); }) ) : (

{t('noLinkedChapter')}

)} ): void { setSelectedChapterId(e.target.value); }} data={mainChaptersData()} placeholder={t('selectChapterPlaceholder')} /> } addButtonCallBack={function (): Promise { return onLinkChapter(actId, selectedChapterId, item.incidentId); }} isAddButtonDisabled={selectedChapterId.length === 0} />
)}
); }) ) : (

{t('noIncidentAdded')}

)} ): void { setNewIncidentTitle(e.target.value); }} placeholder={t('newIncidentPlaceholder')} /> } actionIcon={Plus} addButtonCallBack={function (): Promise { return onAddIncident(actId); }} isAddButtonDisabled={newIncidentTitle.trim() === ''} />
); }