137 lines
4.4 KiB
TypeScript
137 lines
4.4 KiB
TypeScript
import React, { useMemo } from "react";
|
|
import { ScrollView, StyleSheet, Text, View } from "react-native";
|
|
import { useRouter } from "expo-router";
|
|
import { moderateScale, verticalScale } from "react-native-size-matters";
|
|
|
|
import { Colors } from "@/theme";
|
|
import { useModuleTemplateContentDetailsQuery } from "../../hooks/use-content-queries";
|
|
import {
|
|
buildContentReferenceRoute,
|
|
getNavigableContentReferences,
|
|
} from "../../utils/content-reference";
|
|
import { asNumber, asString } from "../helpers/value";
|
|
import { Hero } from "../shared/Hero";
|
|
import { StandardIndexCard } from "../shared/StandardIndexCard";
|
|
import type { DetailRendererProps } from "../../types/renderer.types";
|
|
|
|
export function MeridianPointsDetailRenderer({
|
|
moduleCode,
|
|
content,
|
|
data,
|
|
}: DetailRendererProps) {
|
|
const router = useRouter();
|
|
const rawTitle = asString(data.name) || asString(data.title) || content.title;
|
|
const pointValue = data.point_codes ?? data.points ?? data.point_list;
|
|
const rawPoints = getNavigableContentReferences(pointValue);
|
|
const totalCount = asNumber(data.content_count) || rawPoints.length;
|
|
const countLabel = asString(data.content_label) || "points";
|
|
|
|
const targetTemplateCode = useMemo(() => {
|
|
for (const ref of rawPoints) {
|
|
if (ref.target?.template_code) {
|
|
return ref.target.template_code;
|
|
}
|
|
}
|
|
return "m0001_ct0003";
|
|
}, [rawPoints]);
|
|
|
|
const { data: pointsData } = useModuleTemplateContentDetailsQuery({
|
|
module_code: moduleCode || "m0001",
|
|
template_code: targetTemplateCode,
|
|
});
|
|
|
|
const detailsMap = useMemo(() => {
|
|
const map = new Map<string, { title?: string; subtitle?: string }>();
|
|
if (!pointsData?.items) return map;
|
|
for (const item of pointsData.items) {
|
|
const code =
|
|
asString(item.data.code) || asString(item.data.name) || item.content.title;
|
|
const alt1 = asString(item.data.alt_name_1);
|
|
const alt2 = asString(item.data.alt_name_2);
|
|
const subtitle = [alt1, alt2].filter(Boolean).join(" · ");
|
|
const info = {
|
|
title: code,
|
|
subtitle: subtitle || undefined,
|
|
};
|
|
if (code) map.set(code.toLowerCase(), info);
|
|
if (item.content.id) map.set(item.content.id, info);
|
|
if (item.content.title) map.set(item.content.title.toLowerCase(), info);
|
|
}
|
|
return map;
|
|
}, [pointsData?.items]);
|
|
|
|
return (
|
|
<ScrollView
|
|
contentContainerStyle={styles.content}
|
|
showsVerticalScrollIndicator={false}
|
|
>
|
|
<Hero
|
|
title={rawTitle}
|
|
subtitle={totalCount ? `${totalCount} ${countLabel}` : undefined}
|
|
tone="blue"
|
|
/>
|
|
|
|
<View style={styles.list}>
|
|
{rawPoints.map((ref, index) => {
|
|
const contentId = ref.type === "content_ref" ? ref.target.content_id : undefined;
|
|
const sourceKey = ref.type === "content_ref" ? ref.target.source_key ?? undefined : undefined;
|
|
const key = ref.label?.toLowerCase() || sourceKey?.toLowerCase() || contentId;
|
|
const info = key ? detailsMap.get(key) : undefined;
|
|
const title = ref.label || info?.title || sourceKey || `Point ${index + 1}`;
|
|
const subtitle = info?.subtitle || null;
|
|
|
|
return (
|
|
<View
|
|
key={contentId || `${title}-${index}`}
|
|
style={styles.card}
|
|
>
|
|
<StandardIndexCard
|
|
title={title}
|
|
subtitle={subtitle}
|
|
tone="blue"
|
|
onPress={() =>
|
|
router.push(buildContentReferenceRoute(ref) as never)
|
|
}
|
|
/>
|
|
</View>
|
|
);
|
|
})}
|
|
|
|
{rawPoints.length === 0 ? (
|
|
<View style={styles.emptyContainer}>
|
|
<Text style={styles.emptyText}>No points available for this channel.</Text>
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
content: {
|
|
paddingHorizontal: moderateScale(18),
|
|
paddingTop: verticalScale(16),
|
|
paddingBottom: verticalScale(32),
|
|
gap: verticalScale(14),
|
|
},
|
|
list: {
|
|
gap: verticalScale(10),
|
|
},
|
|
card: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(18),
|
|
borderWidth: 1,
|
|
borderColor: "#E2EAE5",
|
|
overflow: "hidden",
|
|
},
|
|
emptyContainer: {
|
|
padding: moderateScale(24),
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
emptyText: {
|
|
fontSize: moderateScale(14),
|
|
color: Colors.textSecondary,
|
|
},
|
|
});
|