import { useId, useRef, useState, type ReactNode } from 'react'; import type { AdminExtensionSettingField } from '@/api/admin/extensions/queries'; import Label from '@/components/elements/Label'; import Select from '@/components/ui/Select'; import { cn } from '@/lib/cn'; export const GENERAL_SECTION = 'General'; export const settingsBodyClass = 'flex h-[min(30rem,calc(100dvh-14rem))] flex-col'; export interface SettingsSection { name: string; fields: AdminExtensionSettingField[]; } export function groupSettingsBySection(schema: AdminExtensionSettingField[]): SettingsSection[] { if (!schema.some((field) => field.tab)) { return [{ name: GENERAL_SECTION, fields: schema }]; } const sections = new Map(); if (schema.some((field) => !field.tab)) { sections.set(GENERAL_SECTION, []); } for (const field of schema) { const name = field.tab || GENERAL_SECTION; sections.set(name, [...(sections.get(name) ?? []), field]); } return [...sections].map(([name, fields]) => ({ name, fields })); } export default function SettingsSections({ schema, renderField, }: { schema: AdminExtensionSettingField[]; renderField: (field: AdminExtensionSettingField) => ReactNode; }) { const id = useId(); const scrollRef = useRef(null); const sections = groupSettingsBySection(schema); const [selected, setSelected] = useState(sections[0].name); const current = sections.find((section) => section.name === selected) ?? sections[0]; return (
{sections.length > 1 && (