feat(mobile): add ExtraOrdinaryVesselsDetailRenderer with accordion section cards matching point leaf node UI
This commit is contained in:
parent
f47f9cc710
commit
9ddc958a6a
|
|
@ -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<string, unknown> =>
|
||||
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<string>();
|
||||
|
||||
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 <BulletList items={strings} />;
|
||||
}
|
||||
}
|
||||
|
||||
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
||||
return <ObjectSections value={asRecord(value)} />;
|
||||
}
|
||||
|
||||
const paragraphs = toParagraphs(value);
|
||||
if (paragraphs.length > 0) {
|
||||
return <Paragraphs items={paragraphs} />;
|
||||
}
|
||||
|
||||
return <ContentValue value={value} emptyText={emptyText ?? undefined} />;
|
||||
};
|
||||
|
||||
return (
|
||||
<ScrollView
|
||||
contentContainerStyle={styles.content}
|
||||
showsVerticalScrollIndicator={false}
|
||||
>
|
||||
<Hero title={title} subtitle={subtitle} />
|
||||
|
||||
{media.length > 0 ? <ImageGallery images={media} /> : null}
|
||||
|
||||
{orderedAttributes.map((attribute) => (
|
||||
<SectionCard key={attribute.key} title={attribute.label}>
|
||||
{renderAttributeValue(attribute.key, attribute.value, attribute.emptyText)}
|
||||
</SectionCard>
|
||||
))}
|
||||
|
||||
<ContentLinkAttributeSections
|
||||
data={data}
|
||||
definitions={definitions}
|
||||
presentationAttributes={presentationAttributes}
|
||||
/>
|
||||
</ScrollView>
|
||||
);
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
content: {
|
||||
paddingHorizontal: 18,
|
||||
paddingTop: 16,
|
||||
paddingBottom: 40,
|
||||
gap: 14,
|
||||
},
|
||||
});
|
||||
|
|
@ -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<Record<string, CollectionRendererComponent>> = Object.freeze({
|
||||
module_catalog_collection: ModuleCatalogCollectionRenderer,
|
||||
|
|
@ -109,6 +110,7 @@ export const collectionItemRenderers: Readonly<Record<string, CollectionItemRend
|
|||
m0017_ct0002_card: ThreeBurnersCardRenderer,
|
||||
});
|
||||
export const detailRenderers: Readonly<Record<string, DetailRendererComponent>> = 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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue