acupedia-mobile-ui/src/features/content/renderers/shared/DetailPageShell.tsx

125 lines
3.5 KiB
TypeScript

import React, { type PropsWithChildren, useState } from "react";
import { Pressable, StyleSheet, View } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { Ionicons } from "@expo/vector-icons";
import { router, useLocalSearchParams } from "expo-router";
import { Colors } from "@/theme";
import { type PageTone } from "../../context/PageToneContext";
import { ContentPageHeader } from "../../components/layout/ContentPageHeader";
import { useModuleDisplayName } from "../../hooks/use-module-display-name";
export interface DetailPageShellProps extends PropsWithChildren {
title: string;
subtitle?: string;
onBack?: () => void;
tone?: PageTone;
moduleCode?: string;
hideFavorite?: boolean;
rightAction?: React.ReactNode;
}
export function DetailPageShell({
children,
onBack,
tone,
moduleCode: propModuleCode,
hideFavorite,
rightAction,
}: DetailPageShellProps) {
const [isFavorite, setIsFavorite] = useState(false);
const params = useLocalSearchParams<{ moduleCode?: string }>();
const currentModuleCode =
typeof propModuleCode === "string" && propModuleCode.trim()
? propModuleCode.trim()
: typeof params.moduleCode === "string"
? params.moduleCode.trim()
: "";
const moduleDisplay = useModuleDisplayName(currentModuleCode);
const shouldHideFavorite = hideFavorite ?? moduleDisplay.isPointsModule;
const searchAction = currentModuleCode ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={`Search within ${moduleDisplay.name}`}
hitSlop={10}
onPress={() =>
router.push(`/modules/${encodeURIComponent(currentModuleCode)}/search` as never)
}
style={({ pressed }) => [styles.iconButton, pressed && styles.pressed]}
>
<Ionicons name="search" size={22} color={Colors.primaryDark} />
</Pressable>
) : null;
const favoriteAction = !shouldHideFavorite ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={isFavorite ? "Remove from favorites" : "Add to favorites"}
hitSlop={10}
onPress={() => setIsFavorite((value) => !value)}
style={({ pressed }) => [styles.iconButton, pressed && styles.pressed]}
>
<Ionicons
name={isFavorite ? "heart" : "heart-outline"}
size={22}
color={isFavorite ? Colors.error : Colors.textPrimary}
/>
</Pressable>
) : null;
const headerActions = searchAction || favoriteAction || rightAction ? (
<View style={styles.actions}>
{searchAction}
{favoriteAction}
{rightAction}
</View>
) : undefined;
return (
<SafeAreaView style={styles.screen} edges={["top", "left", "right"]}>
<ContentPageHeader
title={moduleDisplay.name}
tone={tone}
onBack={onBack}
onTitlePress={
currentModuleCode
? () => router.push(moduleDisplay.homeRoute as never)
: undefined
}
titleAccessibilityLabel={
currentModuleCode ? `Open ${moduleDisplay.name} home` : moduleDisplay.name
}
fallbackRoute={moduleDisplay.homeRoute}
right={headerActions}
/>
<View style={styles.body}>{children}</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
backgroundColor: "#FFFFFF",
},
body: {
flex: 1,
backgroundColor: "#F6F8F7",
},
actions: {
flexDirection: "row",
alignItems: "center",
},
iconButton: {
width: 36,
height: 40,
alignItems: "center",
justifyContent: "center",
},
pressed: {
opacity: 0.65,
},
});