305 lines
8.7 KiB
TypeScript
305 lines
8.7 KiB
TypeScript
import React, { useCallback, useEffect, useMemo } from "react";
|
|
import {
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
TouchableOpacity,
|
|
} 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 {
|
|
ModuleIndexItem,
|
|
normalizeModuleListResponse,
|
|
toModuleIndexItem,
|
|
} from "@/features/modules/module-index";
|
|
|
|
export default function LibraryScreen() {
|
|
const {
|
|
access,
|
|
accessToken,
|
|
refreshAccess,
|
|
setModules,
|
|
setLastViewedModule,
|
|
} = useAuthStore();
|
|
|
|
const modulesQuery = useModulesQuery();
|
|
const modules = useMemo(
|
|
() => normalizeModuleListResponse(modulesQuery.data),
|
|
[modulesQuery.data],
|
|
);
|
|
|
|
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(() => {
|
|
if (accessToken) {
|
|
void refreshAccess().catch((error) => console.warn("Unable to refresh library access:", error));
|
|
}
|
|
}, [accessToken, refreshAccess]),
|
|
);
|
|
|
|
const items = useMemo(
|
|
() => modules.filter((module) => module.is_active).map(toModuleIndexItem),
|
|
[modules],
|
|
);
|
|
|
|
const unlockedModules = useMemo(() => {
|
|
if (!accessToken) return [];
|
|
|
|
return items.filter((item) =>
|
|
isModuleUnlockedByAccess(item.module, access),
|
|
);
|
|
}, [access, accessToken, items]);
|
|
|
|
const categories = useMemo(
|
|
() => Array.from(new Set(unlockedModules.map((item) => item.category))),
|
|
[unlockedModules],
|
|
);
|
|
|
|
const handleModulePress = useCallback(
|
|
(item: ModuleIndexItem) => {
|
|
setLastViewedModule({
|
|
moduleId: item.module.id,
|
|
moduleCode: item.module.code,
|
|
moduleSlug: item.module.slug,
|
|
entryTemplateCode: item.reference.target.template_code,
|
|
title: item.title,
|
|
description: item.description,
|
|
icon: item.icon,
|
|
color: item.color,
|
|
category: item.category,
|
|
});
|
|
|
|
router.push(buildContentReferenceRoute(item.reference) as never);
|
|
},
|
|
[setLastViewedModule],
|
|
);
|
|
|
|
return (
|
|
<SafeAreaView style={styles.safeArea}>
|
|
<View style={styles.container}>
|
|
<Text style={styles.title}>Your Library</Text>
|
|
|
|
<View style={styles.unlockedBox}>
|
|
<Text style={styles.unlockedCount}>{unlockedModules.length}</Text>
|
|
<Text style={styles.unlockedText}>Modules Unlocked</Text>
|
|
</View>
|
|
|
|
{unlockedModules.length === 0 ? (
|
|
<View style={styles.emptyContainer}>
|
|
<View style={styles.iconWrapper}>
|
|
<Ionicons name="book-outline" size={48} color={Colors.textSecondary} />
|
|
</View>
|
|
<Text style={styles.emptyTitle}>Your Library is Empty</Text>
|
|
<Text style={styles.emptySubtitle}>
|
|
Explore all modules on the Home dashboard to unlock premium content.
|
|
</Text>
|
|
<TouchableOpacity onPress={() => router.push("/modules")}>
|
|
<Text style={styles.exploreText}>Explore Modules</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
) : (
|
|
<ScrollView
|
|
showsVerticalScrollIndicator={false}
|
|
contentContainerStyle={styles.scrollContent}
|
|
>
|
|
{categories.map((category) => {
|
|
const categoryModules = unlockedModules.filter(
|
|
(item) => item.category === category,
|
|
);
|
|
|
|
return (
|
|
<View key={category} style={styles.categorySection}>
|
|
<Text style={styles.categoryTitle}>{category}</Text>
|
|
<View style={styles.card}>
|
|
{categoryModules.map((item, index) => (
|
|
<View key={item.module.id}>
|
|
<TouchableOpacity
|
|
style={styles.row}
|
|
onPress={() => handleModulePress(item)}
|
|
>
|
|
<View
|
|
style={[
|
|
styles.iconWrapperSmall,
|
|
{ backgroundColor: `${item.color}15` },
|
|
]}
|
|
>
|
|
<Ionicons
|
|
name={item.icon as never}
|
|
size={22}
|
|
color={item.color}
|
|
/>
|
|
</View>
|
|
<View style={styles.textContainer}>
|
|
<Text style={styles.moduleTitle}>{item.title}</Text>
|
|
<Text style={styles.moduleDesc} numberOfLines={1}>
|
|
{item.description}
|
|
</Text>
|
|
</View>
|
|
<Ionicons
|
|
name="chevron-forward"
|
|
size={18}
|
|
color={Colors.textSecondary}
|
|
/>
|
|
</TouchableOpacity>
|
|
{index < categoryModules.length - 1 ? (
|
|
<View style={styles.separator} />
|
|
) : null}
|
|
</View>
|
|
))}
|
|
</View>
|
|
</View>
|
|
);
|
|
})}
|
|
</ScrollView>
|
|
)}
|
|
</View>
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
safeArea: {
|
|
flex: 1,
|
|
backgroundColor: Colors.background,
|
|
},
|
|
container: {
|
|
flex: 1,
|
|
paddingHorizontal: moderateScale(24),
|
|
paddingTop: verticalScale(20),
|
|
},
|
|
title: {
|
|
fontSize: moderateScale(22),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
marginBottom: verticalScale(16),
|
|
},
|
|
unlockedBox: {
|
|
backgroundColor: "#F3F4F6",
|
|
borderRadius: moderateScale(14),
|
|
padding: moderateScale(14),
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
marginBottom: verticalScale(24),
|
|
},
|
|
unlockedCount: {
|
|
fontSize: moderateScale(22),
|
|
fontWeight: "800",
|
|
color: Colors.primary,
|
|
marginRight: moderateScale(12),
|
|
},
|
|
unlockedText: {
|
|
fontSize: moderateScale(14),
|
|
fontWeight: "600",
|
|
color: Colors.textPrimary,
|
|
},
|
|
emptyContainer: {
|
|
flex: 1,
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
paddingBottom: verticalScale(80),
|
|
},
|
|
iconWrapper: {
|
|
width: moderateScale(90),
|
|
height: moderateScale(90),
|
|
borderRadius: moderateScale(45),
|
|
backgroundColor: "#F3F4F6",
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
marginBottom: verticalScale(16),
|
|
},
|
|
emptyTitle: {
|
|
fontSize: moderateScale(18),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
marginBottom: verticalScale(6),
|
|
},
|
|
emptySubtitle: {
|
|
fontSize: moderateScale(13),
|
|
color: Colors.textSecondary,
|
|
textAlign: "center",
|
|
lineHeight: 18,
|
|
paddingHorizontal: moderateScale(16),
|
|
marginBottom: verticalScale(16),
|
|
},
|
|
exploreText: {
|
|
fontSize: moderateScale(14),
|
|
fontWeight: "700",
|
|
color: Colors.primary,
|
|
},
|
|
scrollContent: {
|
|
paddingBottom: verticalScale(24),
|
|
},
|
|
card: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(16),
|
|
borderWidth: 1,
|
|
borderColor: "#E5E7EB",
|
|
overflow: "hidden",
|
|
},
|
|
row: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
padding: moderateScale(14),
|
|
},
|
|
iconWrapperSmall: {
|
|
width: moderateScale(40),
|
|
height: moderateScale(40),
|
|
borderRadius: moderateScale(20),
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
},
|
|
textContainer: {
|
|
flex: 1,
|
|
marginLeft: moderateScale(12),
|
|
marginRight: moderateScale(12),
|
|
},
|
|
moduleTitle: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "600",
|
|
color: Colors.textPrimary,
|
|
marginBottom: 2,
|
|
},
|
|
moduleDesc: {
|
|
fontSize: moderateScale(12),
|
|
color: Colors.textSecondary,
|
|
},
|
|
separator: {
|
|
height: 1,
|
|
backgroundColor: "#F3F4F6",
|
|
},
|
|
categorySection: {
|
|
marginBottom: verticalScale(20),
|
|
},
|
|
categoryTitle: {
|
|
fontSize: moderateScale(12),
|
|
fontWeight: "700",
|
|
color: Colors.textSecondary,
|
|
textTransform: "uppercase",
|
|
letterSpacing: 1,
|
|
marginBottom: verticalScale(8),
|
|
marginLeft: moderateScale(4),
|
|
},
|
|
});
|