acupedia-mobile-ui/src/features/content/renderers/GenericDetailRenderer.tsx

79 lines
2.2 KiB
TypeScript

import React, { useMemo } from "react";
import { ScrollView } from "react-native";
import type { DetailRendererProps } from "../types/renderer.types";
import { humanizeKey } from "./helpers/value";
import { ContentLinkAttributeSections } from "./shared/ContentLinkAttributeSections";
import { ContentValue } from "./shared/ContentValue";
import { Hero } from "./shared/Hero";
import { ImageGallery } from "./shared/ImageGallery";
import { SectionCard } from "./shared/SectionCard";
const RESERVED_KEYS = new Set(["code", "id", "slug", "sort_order", "link"]);
/**
* Generic detail renderer for modules and catalog items.
*
* Each attribute is rendered in its own separate SectionCard.
* For therapeutics modules, the redundant Hero name card is hidden.
*/
export function GenericDetailRenderer({
content,
data,
moduleCode,
templateCode,
definitions,
presentationAttributes,
}: DetailRendererProps) {
const isTherapeutic = Boolean(
moduleCode?.toLowerCase().startsWith("m004") ||
templateCode?.toLowerCase().startsWith("m004")
);
const definitionMap = useMemo(
() =>
new Map(
(definitions ?? []).map((d) => [d.code, d.label]),
),
[definitions],
);
return (
<ScrollView
contentContainerStyle={{
paddingHorizontal: 18,
paddingTop: 16,
paddingBottom: 40,
gap: 14,
}}
showsVerticalScrollIndicator={false}
>
<Hero
title={content.title || "Untitled content"}
subtitle={content.summary ?? undefined}
/>
<ImageGallery images={[]} />
{Object.entries(data)
.filter(
([key]) =>
!RESERVED_KEYS.has(key) &&
!(isTherapeutic && key.toLowerCase() === "name"),
)
.map(([key, value]) => {
const rawLabel = definitionMap.get(key) || key;
const label = humanizeKey(rawLabel);
return (
<SectionCard key={key} title={label}>
<ContentValue value={value} />
</SectionCard>
);
})}
<ContentLinkAttributeSections
data={data}
definitions={definitions}
presentationAttributes={presentationAttributes}
/>
</ScrollView>
);
}