acupedia-mobile-ui/src/screens/home/HomeScreen.tsx

589 lines
17 KiB
TypeScript

import React, { useCallback, useEffect, useMemo, useRef } from "react";
import {
ScrollView,
StyleSheet,
Text,
View,
TouchableOpacity,
Alert,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { router, useFocusEffect } from "expo-router";
import { Ionicons } from "@expo/vector-icons";
import { moderateScale, verticalScale } from "react-native-size-matters";
import { Colors } from "@/theme";
import { useAuthStore } from "@/store/auth.store";
import { useModulesQuery } from "@/features/content/hooks/use-content-queries";
import { buildContentReferenceRoute } from "@/features/content/utils/content-reference";
import { isModuleUnlockedByAccess } from "@/features/access/module-access";
import {
normalizeModuleListResponse,
toModuleIndexItem,
} from "@/features/modules/module-index";
import type { LastViewedModule } from "@/store/auth.store";
export default function HomeScreen() {
const isNavigatingRef = useRef(false);
const {
user,
accessToken,
access,
lastViewedModule,
setModules,
refreshSession,
} = useAuthStore();
const modulesQuery = useModulesQuery();
const modules = useMemo(
() => normalizeModuleListResponse(modulesQuery.data),
[modulesQuery.data],
);
const moduleItems = useMemo(
() =>
modules
.filter((module) => module.is_active)
.map(toModuleIndexItem),
[modules],
);
useEffect(() => {
setModules(
modules.map((module) => {
const item = toModuleIndexItem(module);
return {
id: module.id,
code: module.code,
slug: module.slug,
isDefaultFree: item.isDefaultFree,
};
}),
);
}, [modules, setModules]);
useFocusEffect(
useCallback(() => {
void refreshSession().catch((error) => console.warn("Unable to refresh session:", error));
}, [refreshSession]),
);
const unlockedModules = useMemo(() => {
if (!accessToken) return [];
return moduleItems.filter((item) =>
isModuleUnlockedByAccess(item.module, access),
);
}, [access, accessToken, moduleItems]);
const displayContinueModule = useMemo(() => {
if (
!lastViewedModule?.moduleCode ||
!lastViewedModule?.entryTemplateCode
) {
return null;
}
const module = modules.find(
(candidate) => candidate.code === lastViewedModule.moduleCode,
);
if (!module || !isModuleUnlockedByAccess(module, access)) {
return null;
}
return lastViewedModule;
}, [access, lastViewedModule, modules]);
useFocusEffect(
useCallback(() => {
isNavigatingRef.current = false;
}, []),
);
const handleModulePress = useCallback((module: LastViewedModule) => {
if (isNavigatingRef.current) return;
isNavigatingRef.current = true;
setTimeout(() => {
isNavigatingRef.current = false;
}, 1000);
router.push(
buildContentReferenceRoute({
type: "template_ref",
label: module.title,
target: {
module_code: module.moduleCode,
template_code: module.entryTemplateCode,
},
}) as never,
);
}, []);
const handleNotificationPress = () => {
Alert.alert(
"Notifications",
"You have no new notifications at this time.",
);
};
return (
<SafeAreaView style={styles.safeArea}>
<ScrollView showsVerticalScrollIndicator={false} contentContainerStyle={styles.scrollContent}>
{/* Header */}
<View style={styles.header}>
<View>
<Text style={styles.welcomeText}>Hello,</Text>
<Text style={styles.userName}>{accessToken ? (user?.name || "User") : "Guest"}!</Text>
</View>
{accessToken && (
<TouchableOpacity style={styles.bellButton} onPress={handleNotificationPress}>
<Ionicons name="notifications-outline" size={24} color={Colors.textPrimary} />
</TouchableOpacity>
)}
</View>
{/* AcuPedia Intro Card */}
<View style={styles.infoCard}>
<View style={styles.infoTitleRow}>
<Ionicons name="school" size={18} color={Colors.primary} style={styles.infoIcon} />
<Text style={styles.infoAppTitle}>AcuPedia</Text>
</View>
<Text style={styles.infoHeadline}>The Complete Acupuncture Knowledge Platform</Text>
<Text style={styles.infoDesc}>
Access structured acupuncture content, detailed point information, TCM theories, diagnostic concepts, and clinical references in one trusted learning platform.
</Text>
</View>
{!accessToken ? (
<>
{/* Guest Library card */}
<TouchableOpacity
style={styles.libraryCard}
onPress={() => router.push("/register")}
>
<View style={styles.libraryIconWrapper}>
<Ionicons name="bookmark" size={24} color="#4F46E5" />
</View>
<View style={styles.libraryTextContainer}>
<Text style={styles.libraryTitle}>Your Library</Text>
<View style={styles.libraryCountRow}>
<Text style={styles.libraryCount}>0</Text>
<Text style={styles.libraryCountLabel}>Modules Unlocked</Text>
</View>
</View>
<Ionicons name="chevron-forward" size={20} color="#9CA3AF" />
</TouchableOpacity>
{/* Popular Categories */}
<Text style={styles.popularTitle}>Popular Categories</Text>
<View style={styles.categoriesGrid}>
<TouchableOpacity style={styles.categoryGridCard} onPress={() => router.push("/register")}>
<View style={[styles.categoryGridIconWrapper, { backgroundColor: "#3B82F615" }]}>
<Ionicons name="location-outline" size={20} color="#3B82F6" />
</View>
<View style={styles.categoryGridTextContainer}>
<Text style={styles.categoryGridTitle}>Points</Text>
</View>
</TouchableOpacity>
<TouchableOpacity style={styles.categoryGridCard} onPress={() => router.push("/register")}>
<View style={[styles.categoryGridIconWrapper, { backgroundColor: "#F9731615" }]}>
<Ionicons name="pulse-outline" size={20} color="#F97316" />
</View>
<View style={styles.categoryGridTextContainer}>
<Text style={styles.categoryGridTitle}>Patterns</Text>
</View>
</TouchableOpacity>
<TouchableOpacity style={styles.categoryGridCard} onPress={() => router.push("/register")}>
<View style={[styles.categoryGridIconWrapper, { backgroundColor: "#10B98115" }]}>
<Ionicons name="leaf-outline" size={20} color="#10B981" />
</View>
<View style={styles.categoryGridTextContainer}>
<Text style={styles.categoryGridTitle}>Nutrition</Text>
</View>
</TouchableOpacity>
<TouchableOpacity style={styles.categoryGridCard} onPress={() => router.push("/register")}>
<View style={[styles.categoryGridIconWrapper, { backgroundColor: "#06B6D415" }]}>
<Ionicons name="medkit-outline" size={20} color="#06B6D4" />
</View>
<View style={styles.categoryGridTextContainer}>
<Text style={styles.categoryGridTitle}>Therapeutics</Text>
</View>
</TouchableOpacity>
</View>
</>
) : (
<>
{/* Your Library Count Card */}
<TouchableOpacity
style={styles.libraryCard}
onPress={() => router.push("/library")}
>
<View style={styles.libraryIconWrapper}>
<Ionicons name="bookmark" size={24} color="#4F46E5" />
</View>
<View style={styles.libraryTextContainer}>
<Text style={styles.libraryTitle}>Your Library</Text>
<View style={styles.libraryCountRow}>
<Text style={styles.libraryCount}>{unlockedModules.length}</Text>
<Text style={styles.libraryCountLabel}>Modules Unlocked</Text>
</View>
</View>
<Ionicons name="chevron-forward" size={20} color="#9CA3AF" />
</TouchableOpacity>
{/* Continue Learning Section */}
{displayContinueModule && (
<>
<View style={styles.sectionHeader}>
<Text style={styles.sectionTitle}>Continue Learning</Text>
<TouchableOpacity onPress={() => router.push("/modules")}>
<Text style={styles.viewAllText}>View All</Text>
</TouchableOpacity>
</View>
<TouchableOpacity
style={styles.continueCard}
onPress={() => handleModulePress(displayContinueModule)}
>
<View style={[styles.continueIconWrapper, { backgroundColor: displayContinueModule.color + "15" }]}>
<Ionicons name={displayContinueModule.icon as any} size={28} color={displayContinueModule.color} />
</View>
<View style={styles.continueTextContainer}>
<Text style={styles.continueModuleTitle}>{displayContinueModule.title}</Text>
<Text style={styles.continueModuleDesc}>
{displayContinueModule.category} • Clinical Reference
</Text>
<Text style={styles.continueTime}>Last viewed today</Text>
</View>
<Ionicons name="chevron-forward" size={20} color="#9CA3AF" />
</TouchableOpacity>
</>
)}
</>
)}
{/* Explore More Modules Card */}
<View style={styles.exploreDashedCard}>
<View style={styles.exploreHeaderRow}>
<Text style={styles.exploreDashedTitle}>Explore More Modules</Text>
</View>
<Text style={styles.exploreDashedSubtitle}>
Explore all modules and features.
</Text>
<TouchableOpacity
style={styles.exploreButton}
onPress={() => router.push("/modules")}
>
<Text style={styles.exploreButtonText}>Explore All Modules</Text>
</TouchableOpacity>
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: Colors.background,
},
scrollContent: {
paddingHorizontal: moderateScale(24),
paddingTop: verticalScale(16),
paddingBottom: verticalScale(32),
},
header: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginBottom: verticalScale(20),
},
welcomeText: {
fontSize: moderateScale(16),
color: Colors.textSecondary,
fontWeight: "500",
},
userName: {
fontSize: moderateScale(26),
fontWeight: "800",
color: Colors.textPrimary,
},
bellButton: {
padding: 8,
backgroundColor: "#F3F4F6",
borderRadius: moderateScale(12),
},
libraryCard: {
flexDirection: "row",
alignItems: "center",
backgroundColor: "#EEF2FF",
borderWidth: 1,
borderColor: "#E0E7FF",
borderRadius: moderateScale(20),
padding: moderateScale(16),
marginBottom: verticalScale(24),
},
libraryIconWrapper: {
width: moderateScale(48),
height: moderateScale(48),
borderRadius: moderateScale(16),
backgroundColor: "#FFFFFF",
justifyContent: "center",
alignItems: "center",
marginRight: moderateScale(16),
boxShadow: '0px 2px 4px rgba(79, 70, 229, 0.1)',
},
libraryTextContainer: {
flex: 1,
},
libraryTitle: {
fontSize: moderateScale(13),
color: "#4F46E5",
fontWeight: "700",
textTransform: "uppercase",
letterSpacing: 0.5,
marginBottom: 2,
},
libraryCountRow: {
flexDirection: "row",
alignItems: "baseline",
},
libraryCount: {
fontSize: moderateScale(26),
fontWeight: "900",
color: Colors.textPrimary,
marginRight: 6,
},
libraryCountLabel: {
fontSize: moderateScale(13),
color: Colors.textSecondary,
fontWeight: "600",
},
sectionHeader: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginBottom: verticalScale(12),
},
sectionTitle: {
fontSize: moderateScale(17),
fontWeight: "800",
color: Colors.textPrimary,
},
viewAllText: {
fontSize: moderateScale(14),
fontWeight: "700",
color: "#4F46E5",
},
continueCard: {
flexDirection: "row",
alignItems: "center",
backgroundColor: "#FFFFFF",
borderWidth: 1,
borderColor: "#E5E7EB",
borderRadius: moderateScale(20),
padding: moderateScale(16),
marginBottom: verticalScale(28),
},
continueIconWrapper: {
width: moderateScale(56),
height: moderateScale(56),
borderRadius: moderateScale(16),
justifyContent: "center",
alignItems: "center",
marginRight: moderateScale(16),
},
continueTextContainer: {
flex: 1,
},
continueModuleTitle: {
fontSize: moderateScale(16),
fontWeight: "700",
color: Colors.textPrimary,
marginBottom: 2,
},
continueModuleDesc: {
fontSize: moderateScale(12),
color: Colors.textSecondary,
marginBottom: 4,
},
continueTime: {
fontSize: moderateScale(11),
color: Colors.primary,
fontWeight: "600",
},
sectionTitleUnlocked: {
fontSize: moderateScale(17),
fontWeight: "800",
color: Colors.textPrimary,
marginBottom: verticalScale(12),
},
unlockedListCard: {
backgroundColor: "#FFFFFF",
borderWidth: 1,
borderColor: "#E5E7EB",
borderRadius: moderateScale(20),
overflow: "hidden",
marginBottom: verticalScale(28),
},
unlockedRow: {
flexDirection: "row",
alignItems: "center",
padding: moderateScale(16),
},
rowIconWrapper: {
width: moderateScale(42),
height: moderateScale(42),
borderRadius: moderateScale(12),
justifyContent: "center",
alignItems: "center",
marginRight: moderateScale(14),
},
rowTextContainer: {
flex: 1,
},
rowTitle: {
fontSize: moderateScale(15),
fontWeight: "700",
color: Colors.textPrimary,
marginBottom: 2,
},
rowDesc: {
fontSize: moderateScale(12),
color: Colors.textSecondary,
},
rowSeparator: {
height: 1,
backgroundColor: "#F3F4F6",
},
exploreDashedCard: {
borderWidth: 2,
borderStyle: "dashed",
borderColor: "#818CF8",
borderRadius: moderateScale(20),
backgroundColor: "#EEF2FF30",
padding: moderateScale(18),
alignItems: "center",
},
exploreHeaderRow: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
width: "100%",
marginBottom: 4,
},
exploreDashedTitle: {
fontSize: moderateScale(15),
fontWeight: "700",
color: Colors.textPrimary,
},
exploreDashedSubtitle: {
fontSize: moderateScale(12),
color: Colors.textSecondary,
width: "100%",
marginBottom: verticalScale(14),
},
exploreButton: {
width: "100%",
backgroundColor: "#FFFFFF",
borderWidth: 1,
borderColor: "#E0E7FF",
borderRadius: moderateScale(14),
paddingVertical: verticalScale(11),
alignItems: "center",
boxShadow: '0px 1px 2px rgba(0, 0, 0, 0.05)',
},
exploreButtonText: {
fontSize: moderateScale(14),
fontWeight: "700",
color: "#4F46E5",
},
infoCard: {
backgroundColor: "#F4F9F4",
borderWidth: 1,
borderColor: "#E2EFE2",
borderRadius: moderateScale(20),
padding: moderateScale(16),
marginBottom: verticalScale(24),
},
infoTitleRow: {
flexDirection: "row",
alignItems: "center",
marginBottom: verticalScale(6),
},
infoIcon: {
marginRight: moderateScale(6),
},
infoAppTitle: {
fontSize: moderateScale(14),
fontWeight: "800",
color: Colors.primary,
textTransform: "uppercase",
letterSpacing: 1.5,
},
infoHeadline: {
fontSize: moderateScale(18),
fontWeight: "800",
color: Colors.textPrimary,
marginBottom: verticalScale(8),
lineHeight: verticalScale(22),
},
infoDesc: {
fontSize: moderateScale(13),
color: Colors.textSecondary,
lineHeight: verticalScale(18),
fontWeight: "400",
},
libraryGuestText: {
fontSize: moderateScale(13),
color: Colors.textSecondary,
lineHeight: 18,
marginTop: 4,
},
popularTitle: {
fontSize: moderateScale(17),
fontWeight: "800",
color: Colors.textPrimary,
marginBottom: verticalScale(12),
},
categoriesGrid: {
flexDirection: "row",
flexWrap: "wrap",
justifyContent: "space-between",
marginBottom: verticalScale(16),
},
categoryGridCard: {
width: "48%",
backgroundColor: "#FFFFFF",
borderRadius: moderateScale(16),
borderWidth: 1,
borderColor: "#E5E7EB",
padding: moderateScale(12),
marginBottom: verticalScale(12),
flexDirection: "row",
alignItems: "center",
},
categoryGridIconWrapper: {
width: moderateScale(36),
height: moderateScale(36),
borderRadius: moderateScale(10),
justifyContent: "center",
alignItems: "center",
marginRight: moderateScale(10),
},
categoryGridTextContainer: {
flex: 1,
},
categoryGridTitle: {
fontSize: moderateScale(14),
fontWeight: "700",
color: Colors.textPrimary,
},
});