diff --git a/src/features/content/renderers/details/ExtraOrdinaryVesselsDetailRenderer.tsx b/src/features/content/renderers/details/ExtraOrdinaryVesselsDetailRenderer.tsx new file mode 100644 index 0000000..9e10288 --- /dev/null +++ b/src/features/content/renderers/details/ExtraOrdinaryVesselsDetailRenderer.tsx @@ -0,0 +1,179 @@ +import React, { useMemo } from "react"; +import { ScrollView, StyleSheet } from "react-native"; + +import type { DetailRendererProps } from "../../types/renderer.types"; +import { + bareAttributeKey, + findAttributePresentationRule, + getEmptyDisplayText, + presentationDisplayOrder, + shouldRenderAttribute, +} from "../../utils/attribute-presentation"; +import { + asRecord, + asString, + asStringArray, + humanizeKey, +} from "../helpers/value"; +import { BulletList } from "../shared/BulletList"; +import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections"; +import { ContentValue } from "../shared/ContentValue"; +import { Hero } from "../shared/Hero"; +import { ImageGallery } from "../shared/ImageGallery"; +import { ObjectSections } from "../shared/ObjectSections"; +import { Paragraphs } from "../shared/Paragraphs"; +import { SectionCard } from "../shared/SectionCard"; + +const RESERVED_KEYS = new Set([ + "name", + "title", + "sub_title", + "subtitle", + "code", + "sort_order", + "link", + "images", + "media", + "point_images", +]); + +function toParagraphs(value: unknown): string[] { + if (!value) return []; + if (Array.isArray(value)) { + return value.map(asString).filter(Boolean); + } + const str = asString(value); + if (!str) return []; + return str.split(/\n+/).map((s) => s.trim()).filter(Boolean); +} + +export function ExtraOrdinaryVesselsDetailRenderer({ + content, + data, + definitions, + presentationAttributes, +}: DetailRendererProps) { + const title = + asString(data.name) || + asString(data.title) || + content.title; + + const subtitle = + asString(data.sub_title) || + asString(data.subtitle) || + undefined; + + const mediaValue = data.images ?? data.media ?? data.point_images; + const media = Array.isArray(mediaValue) + ? mediaValue.filter( + (item): item is Record => + item !== null && typeof item === "object" && !Array.isArray(item), + ) + : []; + + const orderedAttributes = useMemo(() => { + const rootDefinitions = definitions.filter((definition) => definition.is_root); + const definitionByBareKey = new Map( + rootDefinitions.map((definition) => [ + definition.code || bareAttributeKey(definition.attribute_key), + definition, + ]), + ); + const contractIndex = new Map( + presentationAttributes.map((rule, index) => [rule.attribute_key, index]), + ); + + const keys: string[] = []; + const seen = new Set(); + + for (const definition of rootDefinitions) { + const key = definition.code || bareAttributeKey(definition.attribute_key); + if (!key || seen.has(key)) continue; + seen.add(key); + keys.push(key); + } + + // Fallback for attributes in data that may not have root definitions + for (const [key, value] of Object.entries(data)) { + if (!seen.has(key) && value !== null && value !== undefined && value !== "") { + seen.add(key); + keys.push(key); + } + } + + return keys + .flatMap((key) => { + if (RESERVED_KEYS.has(key)) return []; + + const value = data[key]; + const definition = definitionByBareKey.get(key); + const rule = findAttributePresentationRule( + definition?.attribute_key ?? key, + presentationAttributes, + ); + if (rule && !shouldRenderAttribute(value, rule)) return []; + + const label = + definition?.label?.trim() || + humanizeKey(key); + const emptyText = getEmptyDisplayText(value, rule); + const index = rule ? contractIndex.get(rule.attribute_key) ?? 0 : 0; + const order = presentationDisplayOrder(rule, definition?.sort_order, index); + + return [{ key, label, value, emptyText, order }]; + }) + .sort((a, b) => a.order - b.order || a.label.localeCompare(b.label)); + }, [data, definitions, presentationAttributes]); + + const renderAttributeValue = (key: string, value: unknown, emptyText: string | null) => { + if (Array.isArray(value)) { + const strings = asStringArray(value); + if (strings.length > 0) { + return ; + } + } + + if (value !== null && typeof value === "object" && !Array.isArray(value)) { + return ; + } + + const paragraphs = toParagraphs(value); + if (paragraphs.length > 0) { + return ; + } + + return ; + }; + + return ( + + + + {media.length > 0 ? : null} + + {orderedAttributes.map((attribute) => ( + + {renderAttributeValue(attribute.key, attribute.value, attribute.emptyText)} + + ))} + + + + ); +} + +const styles = StyleSheet.create({ + content: { + paddingHorizontal: 18, + paddingTop: 16, + paddingBottom: 40, + gap: 14, + }, +}); diff --git a/src/features/content/renderers/registry.ts b/src/features/content/renderers/registry.ts index 3e2875b..cdce559 100644 --- a/src/features/content/renderers/registry.ts +++ b/src/features/content/renderers/registry.ts @@ -47,6 +47,7 @@ import { ThreeBurnersCollectionRenderer, ThreeBurnersCardRenderer } from "./coll import { ThreeBurnersDetailRenderer } from "./details/ThreeBurnersDetailRenderer"; import { SixStagesDetailRenderer } from "./details/SixStagesDetailRenderer"; import { TcmPatternDetailRenderer } from "./details/TcmPatternDetailRenderer"; +import { ExtraOrdinaryVesselsDetailRenderer } from "./details/ExtraOrdinaryVesselsDetailRenderer"; export const collectionRenderers: Readonly> = Object.freeze({ module_catalog_collection: ModuleCatalogCollectionRenderer, @@ -109,6 +110,7 @@ export const collectionItemRenderers: Readonly> = Object.freeze({ + extra_ordinary_vessel_detail: ExtraOrdinaryVesselsDetailRenderer, module_catalog_detail: IndexEntryDetailRenderer, acu_point_index_detail: IndexEntryDetailRenderer, meridian_index_detail: MeridianPointsDetailRenderer, @@ -355,6 +357,13 @@ export function resolveDetailRenderer( ) { return TcmPatternDetailRenderer; } + if ( + normalizedTemplate?.startsWith("m0020_") || + normalizedModule === "m0020" || + code === "extra_ordinary_vessel_detail" + ) { + return ExtraOrdinaryVesselsDetailRenderer; + } if (code && detailRenderers[code]) return detailRenderers[code]; reportMissingRenderer("detail", code);