// * Packages 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 } from "@/back/types/global"; import { cn } from "@/lib/utils"; type DynamicPageDetailsProps = { value: string | object; config: ConfigField[]; }; export default function DynamicPageDetails({ value, config }: DynamicPageDetailsProps) { const transLabel = str(useTrans("validation.custom.label")).ucFirst().value(); const transValue = str(useTrans("validation.custom.value")).ucFirst().value(); const tableNotKnownTrans = str(useTrans("back.table_not_known")).ucFirst().value(); const RenderRows = (value: string | number | object, config: ConfigField[], hasBorders: boolean = false, path: string = ""): ReactNode => config.map((field, index) => { const hasChildren = !!field.children?.length; const accordionKey = path ? `${path}.${field.key}-${index}` : `${field.key}-${index}`; const rowContent = (hasChildren: boolean = false) => { const cellClasses = "break-words whitespace-normal"; const placeholderClasses = "text-sm text-muted-foreground break-words whitespace-normal"; const fieldValue = value[field.key as keyof typeof value]; const cellContent = hasChildren ? (() => { const count = Array.isArray(fieldValue) ? Object.values(fieldValue).length : 0; return (

{useTrans('back.element', { params: { count: String(count) }, choice: count > 1 ? 2 : 1, })}

); })() : fieldValue !== null && fieldValue !== undefined ?

{(typeof fieldValue === 'boolean') ? useTrans(fieldValue ? 'back.yes' : 'back.no') : fieldValue}

:

{tableNotKnownTrans}

; return (

{field.label}

{cellContent} {!hasChildren &&
}
); }; return ( {hasChildren ? ( {rowContent(hasChildren)} ) : (
{rowContent()}
)} {hasChildren && Array.isArray(value[field.key as keyof typeof value]) && Object.values(value[field.key as keyof typeof value]).length > 0 && (
{Object.values(value[field.key as keyof typeof value]).map((childItem: any, childIndex: number) => (
{RenderRows(childItem, field.children!, false, `${accordionKey}-${childIndex}`)}
))}
)}
); }); return ( {/* HEADER */}
{transLabel}
{transValue}
{/* ROWS */} {RenderRows(value, config, true)} ); }