280 lines
8.1 KiB
TypeScript
280 lines
8.1 KiB
TypeScript
import React, { useMemo } from "react";
|
|
import {
|
|
ActivityIndicator,
|
|
Pressable,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
} from "react-native";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
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 type { NavigableContentReference } from "../../types/content-reference.types";
|
|
import { asNumber, asString, asStringArray, humanizeKey } from "../helpers/value";
|
|
import type { DetailRendererProps } from "../../types/renderer.types";
|
|
|
|
interface CategoryFoodItem {
|
|
id: string;
|
|
title: string;
|
|
subtitle: string | null;
|
|
ref: NavigableContentReference;
|
|
}
|
|
|
|
export function NutritionCategoryDetailRenderer({
|
|
moduleCode,
|
|
content,
|
|
data,
|
|
}: DetailRendererProps) {
|
|
const router = useRouter();
|
|
const rawTitle = asString(data.name) || content.title;
|
|
const normalizedCategory = rawTitle.toLowerCase().trim();
|
|
|
|
// Query all food items from m0031_ct0002
|
|
const { data: foodData, isLoading } = useModuleTemplateContentDetailsQuery({
|
|
module_code: moduleCode || "m0031",
|
|
template_code: "m0031_ct0002",
|
|
});
|
|
|
|
const items = useMemo(() => {
|
|
// Map of food item details from template m0031_ct0002
|
|
const foodMap = new Map<
|
|
string,
|
|
{
|
|
id: string;
|
|
title: string;
|
|
subtitle: string | null;
|
|
ref: NavigableContentReference;
|
|
}
|
|
>();
|
|
|
|
if (foodData?.items) {
|
|
for (const item of foodData.items) {
|
|
const foodName =
|
|
asString(item.data.food_name) ||
|
|
asString(item.data.name) ||
|
|
item.content.title;
|
|
const code = asString(item.data.code) || item.content.slug;
|
|
const temp = asString(item.data.temperature);
|
|
const taste = asStringArray(item.data.taste).join(", ");
|
|
const subtitle = [temp, taste].filter(Boolean).join(" · ") || null;
|
|
|
|
const ref: NavigableContentReference = {
|
|
type: "content_ref",
|
|
label: foodName,
|
|
target: {
|
|
module_code: moduleCode || "m0031",
|
|
template_code: "m0031_ct0002",
|
|
content_id: item.content.id,
|
|
},
|
|
};
|
|
|
|
const entry = {
|
|
id: item.content.id,
|
|
title: humanizeKey(foodName),
|
|
subtitle,
|
|
ref,
|
|
};
|
|
|
|
if (code) foodMap.set(code.toLowerCase().trim(), entry);
|
|
if (foodName) foodMap.set(foodName.toLowerCase().trim(), entry);
|
|
if (item.content.id) foodMap.set(item.content.id, entry);
|
|
}
|
|
}
|
|
|
|
// 1. Try references from data.food_items
|
|
const foodItemRefs = getNavigableContentReferences(data.food_items);
|
|
if (foodItemRefs.length > 0) {
|
|
return foodItemRefs.map((ref, idx): CategoryFoodItem => {
|
|
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().trim() ||
|
|
sourceKey?.toLowerCase().trim() ||
|
|
contentId;
|
|
const info = key ? foodMap.get(key) : undefined;
|
|
return {
|
|
id: contentId || info?.id || `${ref.label}-${idx}`,
|
|
title: info?.title || humanizeKey(ref.label || `Item ${idx + 1}`),
|
|
subtitle: info?.subtitle || null,
|
|
ref: info?.ref || ref,
|
|
};
|
|
});
|
|
}
|
|
|
|
// 2. Fallback: filter foodData items by matching category
|
|
if (foodData?.items) {
|
|
const filtered = foodData.items.filter((item) => {
|
|
const itemCat = asString(item.data.category).toLowerCase().trim();
|
|
return itemCat === normalizedCategory;
|
|
});
|
|
|
|
return filtered.map((item): CategoryFoodItem => {
|
|
const foodName =
|
|
asString(item.data.food_name) ||
|
|
asString(item.data.name) ||
|
|
item.content.title;
|
|
const temp = asString(item.data.temperature);
|
|
const taste = asStringArray(item.data.taste).join(", ");
|
|
const subtitle = [temp, taste].filter(Boolean).join(" · ") || null;
|
|
|
|
return {
|
|
id: item.content.id,
|
|
title: humanizeKey(foodName),
|
|
subtitle,
|
|
ref: {
|
|
type: "content_ref",
|
|
label: foodName,
|
|
target: {
|
|
module_code: moduleCode || "m0031",
|
|
template_code: "m0031_ct0002",
|
|
content_id: item.content.id,
|
|
},
|
|
},
|
|
};
|
|
});
|
|
}
|
|
|
|
return [];
|
|
}, [foodData, data.food_items, normalizedCategory, moduleCode]);
|
|
|
|
const totalCount = asNumber(data.content_count) || items.length;
|
|
|
|
if (isLoading && items.length === 0) {
|
|
return (
|
|
<View style={styles.center}>
|
|
<ActivityIndicator size="large" color={Colors.primary} />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<ScrollView
|
|
contentContainerStyle={styles.content}
|
|
showsVerticalScrollIndicator={false}
|
|
>
|
|
<Text style={styles.resultCount} accessibilityLiveRegion="polite">
|
|
{totalCount} {totalCount === 1 ? "ITEM" : "ITEMS"}
|
|
</Text>
|
|
|
|
<View style={styles.list}>
|
|
{items.map((item) => (
|
|
<View key={item.id} style={styles.card}>
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityLabel={`Open ${item.title}`}
|
|
onPress={() => router.push(buildContentReferenceRoute(item.ref) as never)}
|
|
style={({ pressed }) => [styles.row, pressed && styles.pressed]}
|
|
>
|
|
<View style={styles.iconWrapper}>
|
|
<Ionicons name="book-outline" size={22} color={Colors.primary} />
|
|
</View>
|
|
|
|
<View style={styles.textContainer}>
|
|
<Text style={styles.title}>{item.title}</Text>
|
|
{item.subtitle ? (
|
|
<Text numberOfLines={2} style={styles.subtitle}>
|
|
{item.subtitle}
|
|
</Text>
|
|
) : null}
|
|
</View>
|
|
|
|
<Ionicons name="chevron-forward" size={20} color="#9CA3AF" />
|
|
</Pressable>
|
|
</View>
|
|
))}
|
|
|
|
{items.length === 0 && !isLoading ? (
|
|
<View style={styles.emptyCard}>
|
|
<Text style={styles.emptyText}>No items found in this category.</Text>
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
content: {
|
|
paddingHorizontal: moderateScale(18),
|
|
paddingTop: verticalScale(16),
|
|
paddingBottom: verticalScale(32),
|
|
},
|
|
center: {
|
|
paddingVertical: verticalScale(40),
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
resultCount: {
|
|
fontSize: moderateScale(12),
|
|
fontWeight: "700",
|
|
color: Colors.textSecondary,
|
|
textTransform: "uppercase",
|
|
letterSpacing: 1,
|
|
marginBottom: verticalScale(12),
|
|
marginLeft: moderateScale(4),
|
|
},
|
|
list: {
|
|
gap: verticalScale(12),
|
|
},
|
|
card: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(18),
|
|
borderWidth: 1,
|
|
borderColor: "#E2EAE5",
|
|
overflow: "hidden",
|
|
},
|
|
row: {
|
|
minHeight: moderateScale(68),
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
padding: moderateScale(14),
|
|
},
|
|
pressed: {
|
|
opacity: 0.72,
|
|
},
|
|
iconWrapper: {
|
|
width: moderateScale(40),
|
|
height: moderateScale(40),
|
|
borderRadius: moderateScale(20),
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
backgroundColor: "#E8F5E9",
|
|
},
|
|
textContainer: {
|
|
flex: 1,
|
|
marginLeft: moderateScale(12),
|
|
marginRight: moderateScale(10),
|
|
},
|
|
title: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "600",
|
|
color: Colors.textPrimary,
|
|
},
|
|
subtitle: {
|
|
fontSize: moderateScale(12),
|
|
color: Colors.textSecondary,
|
|
marginTop: verticalScale(2),
|
|
lineHeight: moderateScale(16),
|
|
},
|
|
emptyCard: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(18),
|
|
borderWidth: 1,
|
|
borderColor: "#E2EAE5",
|
|
padding: moderateScale(24),
|
|
alignItems: "center",
|
|
},
|
|
emptyText: {
|
|
fontSize: moderateScale(14),
|
|
color: Colors.textSecondary,
|
|
},
|
|
});
|