import { useCallback, useEffect, useState } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import { ContactInfoForm } from '#components/Profile/Subcomponents/ContactInfoForm' import { CrowdfundingForm } from '#components/Profile/Subcomponents/CrowdfundingForm' import { GalleryForm } from '#components/Profile/Subcomponents/GalleryForm' import { GroupSubheaderForm } from '#components/Profile/Subcomponents/GroupSubheaderForm' import { ProfileStartEndForm } from '#components/Profile/Subcomponents/ProfileStartEndForm' import { ProfileTagsForm } from '#components/Profile/Subcomponents/ProfileTagsForm' import { ProfileTextForm } from '#components/Profile/Subcomponents/ProfileTextForm' import type { FormState } from '#types/FormState' import type { Item } from '#types/Item' import type { Key } from 'react' // eslint-disable-next-line @typescript-eslint/no-explicit-any type ComponentMap = Record> const componentMap: ComponentMap = { groupSubheaders: GroupSubheaderForm, texts: ProfileTextForm, contactInfos: ContactInfoForm, startEnd: ProfileStartEndForm, crowdfundings: CrowdfundingForm, gallery: GalleryForm, inviteLinks: () => null, relations: () => null, // Relations are not editable in form tags_component: ProfileTagsForm, attestations_component: () => null, // Attestations are view-only } interface TabItem { id: string title: string icon?: string // eslint-disable-next-line @typescript-eslint/no-explicit-any items: { collection: string; id: Key | null | undefined; item: any }[] } interface Props { item: Item state: FormState setState: React.Dispatch> tabs: TabItem[] iconAsLabels?: boolean } export const TabsContainerForm = ({ item, state, setState, tabs, iconAsLabels = false }: Props) => { const location = useLocation() const navigate = useNavigate() const [activeTab, setActiveTab] = useState(0) const tabsLength = tabs.length useEffect(() => { if (tabs.length === 0) return const params = new URLSearchParams(location.search) const urlTab = params.get('tab') if (urlTab !== null && !isNaN(Number(urlTab))) { const index = Number(urlTab) if (index >= 0 && index < tabs.length) { setActiveTab(index) } } }, [tabs, tabsLength, location.search]) const updateActiveTab = useCallback( (index: number) => { setActiveTab(index) const params = new URLSearchParams(location.search) params.set('tab', `${index}`) const newUrl = location.pathname + '?' + params.toString() navigate(newUrl) }, [location.pathname, location.search, navigate], ) if (tabs.length === 0) { return null } return (
{/* Tabs */}
{tabs.map((tab, index) => ( ))}
{/* Tab Content */}
{/* eslint-disable-next-line security/detect-object-injection */} {tabs[activeTab].items.map((templateItem) => { const TemplateComponent = componentMap[templateItem.collection] // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition return TemplateComponent ? ( ) : (
{templateItem.collection} form not found
) })}
) }