516 lines
15 KiB
TypeScript
516 lines
15 KiB
TypeScript
import React, { useCallback, useEffect, useMemo, useRef } from "react";
|
|
import {
|
|
Platform,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
TouchableOpacity,
|
|
View,
|
|
} 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 { useSettingsStore } from "@/store/settings.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 ModulesIndexScreen() {
|
|
const isNavigatingRef = useRef(false);
|
|
|
|
const {
|
|
access,
|
|
accessToken,
|
|
user,
|
|
refreshAccess,
|
|
setModules,
|
|
setLastViewedModule,
|
|
} = useAuthStore();
|
|
|
|
const preview = useSettingsStore((state) => state.contentPreviewEnabled);
|
|
const previewFeatureEnabled =
|
|
process.env.EXPO_PUBLIC_CONTENT_PREVIEW_ENABLED === "true";
|
|
const setContentPreviewEnabled = useSettingsStore(
|
|
(state) => state.setContentPreviewEnabled,
|
|
);
|
|
const isPreviewAdmin = Boolean(
|
|
previewFeatureEnabled &&
|
|
user?.roles?.some((role) => role === "admin" || role === "super_admin"),
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!isPreviewAdmin && preview) {
|
|
setContentPreviewEnabled(false);
|
|
}
|
|
}, [isPreviewAdmin, preview, setContentPreviewEnabled]);
|
|
|
|
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(() => {
|
|
isNavigatingRef.current = false;
|
|
if (accessToken) {
|
|
void refreshAccess().catch((error) => {
|
|
console.warn("Unable to refresh module access:", error);
|
|
});
|
|
}
|
|
}, [accessToken, refreshAccess]),
|
|
);
|
|
|
|
const items = useMemo(
|
|
() => modules.filter((module) => module.is_active).map(toModuleIndexItem),
|
|
[modules],
|
|
);
|
|
|
|
const categories = useMemo(
|
|
() => Array.from(new Set(items.map((item) => item.category))),
|
|
[items],
|
|
);
|
|
|
|
const isUnlocked = useCallback(
|
|
(item: ModuleIndexItem) => {
|
|
if (!accessToken) return false;
|
|
if (isPreviewAdmin && preview) return true;
|
|
return isModuleUnlockedByAccess(item.module, access);
|
|
},
|
|
[access, accessToken, isPreviewAdmin, preview],
|
|
);
|
|
|
|
const handleModulePress = useCallback(
|
|
(item: ModuleIndexItem) => {
|
|
if (isNavigatingRef.current) return;
|
|
isNavigatingRef.current = true;
|
|
setTimeout(() => {
|
|
isNavigatingRef.current = false;
|
|
}, 1000);
|
|
|
|
if (!accessToken) {
|
|
router.push("/register");
|
|
return;
|
|
}
|
|
|
|
if (!isUnlocked(item)) {
|
|
router.push({
|
|
pathname: "/subscription",
|
|
params: {
|
|
moduleId: item.module.id,
|
|
moduleCode: item.module.code,
|
|
title: item.title,
|
|
category: item.category,
|
|
desc: item.description,
|
|
planCode: item.planCode,
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
|
|
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);
|
|
},
|
|
[accessToken, isUnlocked, setLastViewedModule],
|
|
);
|
|
|
|
return (
|
|
<SafeAreaView style={styles.safeArea} edges={["top", "left", "right"]}>
|
|
<View style={styles.container}>
|
|
<View style={styles.header}>
|
|
<TouchableOpacity
|
|
onPress={() => router.replace("/")}
|
|
style={styles.backButton}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Go to home"
|
|
>
|
|
<Ionicons name="chevron-back" size={24} color={Colors.textPrimary} />
|
|
</TouchableOpacity>
|
|
<Text style={styles.headerTitle}>All Modules</Text>
|
|
<View style={styles.headerActions}>
|
|
{isPreviewAdmin ? (
|
|
<TouchableOpacity
|
|
onPress={() => setContentPreviewEnabled(!preview)}
|
|
style={[
|
|
styles.previewButton,
|
|
preview && styles.previewButtonActive,
|
|
]}
|
|
accessibilityRole="button"
|
|
accessibilityLabel={
|
|
preview ? "Disable content preview" : "Enable content preview"
|
|
}
|
|
>
|
|
<Ionicons
|
|
name={preview ? "eye" : "eye-outline"}
|
|
size={21}
|
|
color={preview ? "#FFFFFF" : Colors.primaryDark}
|
|
/>
|
|
</TouchableOpacity>
|
|
) : null}
|
|
<TouchableOpacity
|
|
onPress={() => router.push("/search")}
|
|
style={styles.searchButton}
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Search all content"
|
|
>
|
|
<Ionicons name="search" size={22} color={Colors.primaryDark} />
|
|
</TouchableOpacity>
|
|
</View>
|
|
</View>
|
|
|
|
<ScrollView
|
|
showsVerticalScrollIndicator={false}
|
|
contentContainerStyle={styles.scrollContent}
|
|
>
|
|
<Text style={styles.subtitle}>
|
|
Explore all modules. Unlock to access complete content.
|
|
</Text>
|
|
|
|
{preview && isPreviewAdmin ? (
|
|
<View style={styles.previewBanner}>
|
|
<Ionicons name="eye" size={16} color={Colors.primaryDark} />
|
|
<Text style={styles.previewBannerText}>
|
|
Draft content preview is enabled.
|
|
</Text>
|
|
</View>
|
|
) : null}
|
|
|
|
{modulesQuery.isError ? (
|
|
<View style={styles.errorCard}>
|
|
<Ionicons
|
|
name="alert-circle-outline"
|
|
size={20}
|
|
color={Colors.textSecondary}
|
|
style={styles.errorIcon}
|
|
/>
|
|
<Text style={styles.errorText}>Unable to load modules. Please try again.</Text>
|
|
<TouchableOpacity onPress={() => void modulesQuery.refetch()}>
|
|
<Text style={styles.retryText}>Retry</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
) : null}
|
|
|
|
{categories.map((category) => {
|
|
const categoryModules = items.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) => {
|
|
const unlocked = isUnlocked(item);
|
|
|
|
return (
|
|
<View key={item.module.id}>
|
|
<TouchableOpacity
|
|
style={styles.row}
|
|
onPress={() => handleModulePress(item)}
|
|
>
|
|
<View
|
|
style={[
|
|
styles.iconWrapper,
|
|
{ backgroundColor: `${item.color}15` },
|
|
]}
|
|
>
|
|
<Ionicons
|
|
name={item.icon as never}
|
|
size={22}
|
|
color={item.color}
|
|
/>
|
|
</View>
|
|
|
|
<View style={styles.textContainer}>
|
|
<View style={styles.moduleTitleRow}>
|
|
<Text style={styles.moduleTitle}>{item.title}</Text>
|
|
{preview && item.module.status === "draft" ? (
|
|
<View style={styles.draftBadge}>
|
|
<Text style={styles.draftBadgeText}>Draft</Text>
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
<Text style={styles.moduleDesc} numberOfLines={1}>
|
|
{item.description}
|
|
</Text>
|
|
</View>
|
|
|
|
{unlocked ? (
|
|
<View style={styles.openButton}>
|
|
<Text style={styles.openButtonText}>Open</Text>
|
|
</View>
|
|
) : (
|
|
<Ionicons
|
|
name="lock-closed"
|
|
size={20}
|
|
color="#9CA3AF"
|
|
style={styles.lockIcon}
|
|
/>
|
|
)}
|
|
</TouchableOpacity>
|
|
|
|
{index < categoryModules.length - 1 ? (
|
|
<View style={styles.separator} />
|
|
) : null}
|
|
</View>
|
|
);
|
|
})}
|
|
</View>
|
|
</View>
|
|
);
|
|
})}
|
|
|
|
<View style={styles.footerNote}>
|
|
<Ionicons
|
|
name="bulb-outline"
|
|
size={20}
|
|
color={Colors.primary}
|
|
style={styles.footerNoteIcon}
|
|
/>
|
|
<Text style={styles.footerNoteText}>
|
|
Unlock any module to access complete knowledge and features.
|
|
</Text>
|
|
</View>
|
|
</ScrollView>
|
|
</View>
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
safeArea: {
|
|
flex: 1,
|
|
backgroundColor: "#FFFFFF",
|
|
},
|
|
container: {
|
|
flex: 1,
|
|
backgroundColor: Colors.background,
|
|
},
|
|
header: {
|
|
minHeight: 64,
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
paddingHorizontal: moderateScale(16),
|
|
paddingTop: Platform.OS === "web" ? 14 : moderateScale(8),
|
|
paddingBottom: moderateScale(8),
|
|
borderBottomWidth: 1,
|
|
borderBottomColor: "#ECECEC",
|
|
backgroundColor: "#FFFFFF",
|
|
},
|
|
backButton: {
|
|
padding: 4,
|
|
},
|
|
headerTitle: {
|
|
fontSize: moderateScale(18),
|
|
fontWeight: "700",
|
|
color: Colors.primary,
|
|
},
|
|
headerActions: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: 8,
|
|
},
|
|
previewButton: {
|
|
width: 32,
|
|
height: 32,
|
|
borderRadius: 16,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
backgroundColor: "#EEF2FF",
|
|
},
|
|
previewButtonActive: {
|
|
backgroundColor: Colors.primary,
|
|
},
|
|
searchButton: {
|
|
width: 32,
|
|
height: 32,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
scrollContent: {
|
|
paddingHorizontal: moderateScale(24),
|
|
paddingTop: verticalScale(16),
|
|
paddingBottom: verticalScale(32),
|
|
},
|
|
subtitle: {
|
|
fontSize: moderateScale(14),
|
|
color: Colors.textSecondary,
|
|
textAlign: "center",
|
|
marginBottom: verticalScale(24),
|
|
lineHeight: 20,
|
|
},
|
|
previewBanner: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: 8,
|
|
marginTop: -12,
|
|
marginBottom: verticalScale(18),
|
|
paddingHorizontal: moderateScale(12),
|
|
paddingVertical: verticalScale(8),
|
|
borderRadius: moderateScale(10),
|
|
backgroundColor: "#EEF2FF",
|
|
},
|
|
previewBannerText: {
|
|
color: Colors.primaryDark,
|
|
fontSize: moderateScale(12),
|
|
fontWeight: "600",
|
|
},
|
|
card: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(16),
|
|
borderWidth: 1,
|
|
borderColor: "#E5E7EB",
|
|
overflow: "hidden",
|
|
},
|
|
row: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
padding: moderateScale(14),
|
|
},
|
|
iconWrapper: {
|
|
width: moderateScale(40),
|
|
height: moderateScale(40),
|
|
borderRadius: moderateScale(20),
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
},
|
|
textContainer: {
|
|
flex: 1,
|
|
marginLeft: moderateScale(12),
|
|
marginRight: moderateScale(12),
|
|
},
|
|
moduleTitleRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: 8,
|
|
},
|
|
moduleTitle: {
|
|
fontSize: moderateScale(15),
|
|
fontWeight: "600",
|
|
color: Colors.textPrimary,
|
|
marginBottom: 2,
|
|
},
|
|
draftBadge: {
|
|
paddingHorizontal: moderateScale(7),
|
|
paddingVertical: 2,
|
|
borderRadius: moderateScale(8),
|
|
backgroundColor: "#FEF3C7",
|
|
},
|
|
draftBadgeText: {
|
|
fontSize: moderateScale(10),
|
|
fontWeight: "700",
|
|
color: "#92400E",
|
|
},
|
|
moduleDesc: {
|
|
fontSize: moderateScale(12),
|
|
color: Colors.textSecondary,
|
|
},
|
|
openButton: {
|
|
backgroundColor: "#4F46E5",
|
|
paddingHorizontal: moderateScale(16),
|
|
paddingVertical: verticalScale(6),
|
|
borderRadius: moderateScale(12),
|
|
},
|
|
openButtonText: {
|
|
color: "#FFFFFF",
|
|
fontSize: moderateScale(12),
|
|
fontWeight: "700",
|
|
},
|
|
lockIcon: {
|
|
paddingRight: 4,
|
|
},
|
|
separator: {
|
|
height: 1,
|
|
backgroundColor: "#F3F4F6",
|
|
},
|
|
footerNote: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
backgroundColor: "#E8F5E9",
|
|
padding: moderateScale(14),
|
|
borderRadius: moderateScale(14),
|
|
borderWidth: 1,
|
|
borderColor: "#C8E6C9",
|
|
marginTop: verticalScale(8),
|
|
},
|
|
footerNoteIcon: {
|
|
marginRight: moderateScale(10),
|
|
},
|
|
footerNoteText: {
|
|
flex: 1,
|
|
fontSize: moderateScale(13),
|
|
color: Colors.primaryDark,
|
|
lineHeight: 18,
|
|
},
|
|
categorySection: {
|
|
marginBottom: verticalScale(20),
|
|
},
|
|
categoryTitle: {
|
|
fontSize: moderateScale(12),
|
|
fontWeight: "700",
|
|
color: Colors.textSecondary,
|
|
textTransform: "uppercase",
|
|
letterSpacing: 1,
|
|
marginBottom: verticalScale(8),
|
|
marginLeft: moderateScale(4),
|
|
},
|
|
errorCard: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(14),
|
|
borderWidth: 1,
|
|
borderColor: "#E5E7EB",
|
|
padding: moderateScale(14),
|
|
marginBottom: verticalScale(20),
|
|
},
|
|
errorIcon: {
|
|
marginRight: moderateScale(8),
|
|
},
|
|
errorText: {
|
|
flex: 1,
|
|
fontSize: moderateScale(12),
|
|
color: Colors.textSecondary,
|
|
},
|
|
retryText: {
|
|
fontSize: moderateScale(12),
|
|
fontWeight: "700",
|
|
color: Colors.primary,
|
|
},
|
|
});
|