// * 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/dynamic-pages"; import { cn, GetTranslation } from "@/lib/utils"; type ChildItem = Record; const RenderRows = ( currentValue: string | number | object, currentConfig: ConfigField[], hasBorders: boolean = false, path: string = "", ): ReactNode => { const tableNotKnownTrans = str(useTrans("back.table_not_known")) .ucFirst() .value(); return currentConfig.map((field, index) => { const hasChildren = !!field.children?.length; const accordionKey = path ? `${path}.${field.key}-${index}` : `${field.key}-${index}`; const rowContent = (currentHasChildren: boolean = false) => { const cellClasses = "break-words whitespace-normal"; const placeholderClasses = "text-sm text-muted-foreground break-words whitespace-normal"; const fieldValue = currentValue[field.key as keyof typeof currentValue]; let cellContent: ReactNode; if (currentHasChildren) { const count = Array.isArray(fieldValue) ? Object.values(fieldValue).length : 0; cellContent = (

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

); } else if (fieldValue !== null && fieldValue !== undefined) { cellContent = (

{typeof fieldValue === "boolean" ? GetTranslation( fieldValue ? "back.yes" : "back.no", ) : fieldValue}

); } else { cellContent = (

{tableNotKnownTrans}

); } return (

{field.label}

{cellContent} {!currentHasChildren &&
}
); }; return ( {hasChildren ? ( {rowContent(hasChildren)} ) : (
{rowContent()}
)} {hasChildren && Array.isArray( currentValue[field.key as keyof typeof currentValue], ) && Object.values( currentValue[field.key as keyof typeof currentValue], ).length > 0 && (
{Object.values( currentValue[ field.key as keyof typeof currentValue ] as ChildItem[], ).map((childItem: ChildItem) => (
{RenderRows( childItem, field.children!, false, `${accordionKey}-${childItem.id}`, )}
))}
)}
); }); }; 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(); return ( {/* HEADER */}
{transLabel}
{transValue}
{/* ROWS */} {RenderRows(value, config, true)} ); }