import React, { ReactNode, useCallback, useEffect, useMemo, useState, } from 'react'; import PanelHeader from '../ui/PanelHeader'; import { CloseIcon } from '../ui/icons'; import { DEFAULT_SETTINGS_SECTIONS } from './sections'; export interface SettingsSectionConfig { id: string; label: string; component?: React.ComponentType; render?: (props: Record) => ReactNode; } interface SettingsModalProps { open?: boolean; onClose?: () => void; sections?: SettingsSectionConfig[]; defaultSectionId?: string; [key: string]: unknown; } const SettingsModal: React.FC = ({ open = false, onClose, sections, defaultSectionId, ...sectionProps }) => { const sectionList: SettingsSectionConfig[] = useMemo(() => { if (Array.isArray(sections) && sections.length) { return sections; } return DEFAULT_SETTINGS_SECTIONS as SettingsSectionConfig[]; }, [sections]); const firstSectionId = sectionList[0]?.id ?? null; const resolvedDefaultSection = defaultSectionId || firstSectionId; const [activeSection, setActiveSection] = useState(resolvedDefaultSection); useEffect(() => { if (!open) { setActiveSection(resolvedDefaultSection); return; } const hasActiveSection = sectionList.some((section) => section.id === activeSection); if (!hasActiveSection) { setActiveSection(resolvedDefaultSection); } }, [open, sectionList, resolvedDefaultSection, activeSection]); const handleBackdropClick = useCallback(() => { onClose?.(); }, [onClose]); const handleInnerClick = useCallback((event) => { event.stopPropagation(); }, []); if (!open) { return null; } const activeSectionConfig = sectionList.find((section) => section.id === activeSection); let sectionContent = null; if (activeSectionConfig) { if (activeSectionConfig.component) { const SectionComponent = activeSectionConfig.component; sectionContent = ; } else if (activeSectionConfig.render) { sectionContent = activeSectionConfig.render(sectionProps); } } return (
)} />
{sectionContent || (

Select a settings section.

)}
); }; export default SettingsModal;