// * Packages import { usePage } from "@inertiajs/react"; import { ReactNode } from "react"; // * Components shadcn import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from "@/components/ui/accordion"; // * Other import str from "@/hooks/use-string"; import useTrans from "@/hooks/use-translations"; import { ConfigField, DynamicFieldTypes } from "@/back/types/dynamic-pages"; import { cn } from "@/lib/utils"; type ConfigDetailsProps = { config: ConfigField[]; }; export default function ConfigDetails({ config }: ConfigDetailsProps) { const enumDynamicFieldTypes = usePage().props .enumDynamicFieldTypes as DynamicFieldTypes; const getEnumDynamicFieldType = (value: number) => { return Object.values(enumDynamicFieldTypes).find( (def) => def.value === value, ); }; const transType = str(useTrans("validation.custom.type")).ucFirst().value(); const transLabel = str(useTrans("validation.custom.label")) .ucFirst() .value(); const transValidation = str(useTrans("validation.custom.validation")) .ucFirst() .value(); const RenderRows = ( fields: ConfigField[], hasBorders: boolean = false, ): ReactNode => fields.map((field, index) => { const hasChildren = !!field.children?.length; const accordionKey = `${field.key}-${index}`; const rowContent = (targetHasChildren: boolean = false) => (

{getEnumDynamicFieldType(field.type)?.label}

{field.label} {`(${field.key})`}

{field.validation || "—"}
{!targetHasChildren &&
}
); return ( {hasChildren ? ( {rowContent(hasChildren)} ) : (
{rowContent()}
)} {hasChildren && (
{RenderRows(field.children!, false)}
)}
); }); return ( {/* HEADER */}
{transType}
{transLabel}
{transValidation}
{/* ROWS */} {RenderRows(config, true)} ); }