201 lines
5.7 KiB
TypeScript
201 lines
5.7 KiB
TypeScript
import React, { useMemo } from "react";
|
|
import { Pressable, StyleSheet, Text, View } from "react-native";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
import { moderateScale, verticalScale } from "react-native-size-matters";
|
|
|
|
import { Colors } from "@/theme";
|
|
import { asNumber, asString } from "../helpers/value";
|
|
import { CollectionScaffold } from "../shared/CollectionScaffold";
|
|
import { useSingleTemplateContentQuery } from "../../hooks/use-single-template-content-query";
|
|
import type {
|
|
CollectionItemRendererProps,
|
|
CollectionRendererProps,
|
|
} from "../../types/renderer.types";
|
|
|
|
function extractOverviewFromData(data: Record<string, unknown> | undefined): string {
|
|
if (!data) return "";
|
|
const identification = data.identification;
|
|
if (identification && typeof identification === "object") {
|
|
const content = (identification as Record<string, unknown>).content;
|
|
if (typeof content === "string" && content.trim().length > 0) {
|
|
return content.trim();
|
|
}
|
|
}
|
|
if (typeof data.overview === "string" && data.overview.trim().length > 0) {
|
|
return data.overview.trim();
|
|
}
|
|
if (typeof data.content === "string" && data.content.trim().length > 0) {
|
|
return data.content.trim();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
export function QiBloodCollectionRenderer(props: CollectionRendererProps) {
|
|
// Dynamically load the overview from the module's identification template (m0013_ct0006)
|
|
const overviewQuery = useSingleTemplateContentQuery({
|
|
moduleCode: "m0013",
|
|
templateCode: "m0013_ct0006",
|
|
});
|
|
|
|
const overviewText = useMemo(() => {
|
|
if (overviewQuery.resolution.status === "ready") {
|
|
return extractOverviewFromData(overviewQuery.resolution.data);
|
|
}
|
|
return "";
|
|
}, [overviewQuery.resolution]);
|
|
|
|
// Filter out the identification entry from the category cards list, leaving only the 4 categories
|
|
const categoryItems = useMemo(() => {
|
|
return props.items
|
|
.filter((item) => {
|
|
const code = asString(item.attributes.code).toLowerCase();
|
|
const name = asString(item.attributes.name).toLowerCase();
|
|
return (
|
|
!code.includes("identification") &&
|
|
!name.includes("identification") &&
|
|
!code.includes("overview")
|
|
);
|
|
})
|
|
.sort((a, b) => {
|
|
const orderA = asNumber(a.attributes.sort_order) ?? 999;
|
|
const orderB = asNumber(b.attributes.sort_order) ?? 999;
|
|
return orderA - orderB;
|
|
});
|
|
}, [props.items]);
|
|
|
|
const header = (
|
|
<View style={styles.headerContainer}>
|
|
{overviewText ? (
|
|
<View style={styles.overviewCard}>
|
|
<View style={styles.overviewHeader}>
|
|
<Ionicons name="information-circle" size={20} color={Colors.primary} />
|
|
<Text style={styles.overviewTitle}>Overview</Text>
|
|
</View>
|
|
<Text style={styles.overviewText}>{overviewText}</Text>
|
|
</View>
|
|
) : null}
|
|
|
|
<Text style={styles.sectionHeading}>Categories</Text>
|
|
</View>
|
|
);
|
|
|
|
return <CollectionScaffold {...props} items={categoryItems} total={categoryItems.length} header={header} />;
|
|
}
|
|
|
|
export function QiBloodCardRenderer({
|
|
item,
|
|
onPress,
|
|
}: CollectionItemRendererProps) {
|
|
const a = item.attributes;
|
|
|
|
const title =
|
|
asString(a.name) ||
|
|
asString(a.title) ||
|
|
item.title;
|
|
|
|
const sortOrder = asNumber(a.sort_order);
|
|
const displayNumber =
|
|
typeof sortOrder === "number"
|
|
? sortOrder + 1
|
|
: undefined;
|
|
|
|
return (
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityLabel={title}
|
|
onPress={onPress}
|
|
style={({ pressed }) => [styles.cardRow, pressed && styles.pressed]}
|
|
>
|
|
<View style={styles.numberBadge}>
|
|
{displayNumber !== undefined ? (
|
|
<Text style={styles.numberText}>{displayNumber}</Text>
|
|
) : (
|
|
<Ionicons name="water-outline" size={18} color={Colors.primaryDark} />
|
|
)}
|
|
</View>
|
|
|
|
<View style={styles.titleContainer}>
|
|
<Text numberOfLines={2} style={styles.cardTitle}>
|
|
{title}
|
|
</Text>
|
|
</View>
|
|
|
|
<Ionicons name="chevron-forward" size={20} color="#9CA3AF" />
|
|
</Pressable>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
headerContainer: {
|
|
paddingHorizontal: moderateScale(16),
|
|
paddingTop: verticalScale(14),
|
|
paddingBottom: verticalScale(4),
|
|
gap: verticalScale(12),
|
|
},
|
|
overviewCard: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(16),
|
|
borderWidth: 1,
|
|
borderColor: "#E2EAE5",
|
|
padding: moderateScale(16),
|
|
},
|
|
overviewHeader: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
marginBottom: verticalScale(8),
|
|
gap: moderateScale(8),
|
|
},
|
|
overviewTitle: {
|
|
fontSize: moderateScale(16),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
},
|
|
overviewText: {
|
|
fontSize: moderateScale(13),
|
|
lineHeight: verticalScale(20),
|
|
color: Colors.textSecondary,
|
|
},
|
|
sectionHeading: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
marginLeft: moderateScale(4),
|
|
},
|
|
cardRow: {
|
|
minHeight: moderateScale(60),
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
padding: moderateScale(14),
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(14),
|
|
borderWidth: 1,
|
|
borderColor: "#E2EAE5",
|
|
},
|
|
pressed: {
|
|
opacity: 0.72,
|
|
},
|
|
numberBadge: {
|
|
width: moderateScale(36),
|
|
height: moderateScale(36),
|
|
borderRadius: moderateScale(18),
|
|
backgroundColor: "#E8F5E9",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
numberText: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "800",
|
|
color: Colors.primaryDark,
|
|
},
|
|
titleContainer: {
|
|
flex: 1,
|
|
marginLeft: moderateScale(12),
|
|
marginRight: moderateScale(8),
|
|
},
|
|
cardTitle: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "600",
|
|
color: Colors.textPrimary,
|
|
},
|
|
});
|