import { type ReactNode } from "react"; import type { ConfigField } from "@/back/types/dynamic-pages"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from "@/components/ui/accordion"; import str from "@/hooks/use-string"; import trans from "@/hooks/use-translations"; import { cn } from "@/lib/utils"; type ChildItem = Record & { id: string | number }; type FieldValue = string | number | boolean | null | undefined | ChildItem[]; const RenderRows = ( currentValue: Record, currentConfig: ConfigField[], hasBorders = false, path = "", ): ReactNode => { const tableNotKnownTrans = str(trans("uwil-backend::back.table_not_known")) .ucFirst() .value(); return currentConfig.map((field, index) => { const hasChildren = !!field.children?.length; const accordionKey = path ? `${path}.${field.key}-${String(index)}` : `${field.key}-${String(index)}`; const rowContent = (currentHasChildren = false) => { const cellClasses = "break-words whitespace-normal"; const placeholderClasses = "text-sm text-muted-foreground break-words whitespace-normal"; const fieldValue = currentValue[field.key] as FieldValue; let cellContent: ReactNode; if (currentHasChildren) { const count = Array.isArray(fieldValue) ? fieldValue.length : 0; cellContent = (

{trans("uwil-dynamic-pages::trans.element", { params: { count: String(count) }, choice: count > 1 ? 2 : 1, })}

); } else if (fieldValue !== null && fieldValue !== undefined) { const displayValue = Array.isArray(fieldValue) ? null : typeof fieldValue === "boolean" ? trans( fieldValue ? "uwil-backend::back.yes" : "uwil-backend::back.no", ) : String(fieldValue); cellContent = displayValue ? (

{displayValue}

) : (

{tableNotKnownTrans}

); } else { cellContent = (

{tableNotKnownTrans}

); } return (

{field.label}

{cellContent} {!currentHasChildren &&
}
); }; const childItems = currentValue[field.key]; const hasChildItems = hasChildren && Array.isArray(childItems) && childItems.length > 0; return ( {hasChildren ? ( {rowContent(hasChildren)} ) : (
{rowContent()}
)} {hasChildItems && (
{(childItems as ChildItem[]).map((childItem) => (
{RenderRows( childItem, field.children ?? [], false, `${accordionKey}-${String(childItem.id)}`, )}
))}
)}
); }); }; interface DynamicPageDetailsProps { value: Record; config: ConfigField[]; } export default function DynamicPageDetails({ value, config, }: Readonly) { const transLabel = str(trans("uwil-dynamic-pages::trans.attributes.label")) .ucFirst() .value(); const transValue = str(trans("uwil-dynamic-pages::trans.attributes.value")) .ucFirst() .value(); return (
{transLabel}
{transValue}
{RenderRows(value, config, true)} ); }