import React, { type ReactNode, useCallback } from "react";
import { BackHandler, Platform, Pressable, StyleSheet, Text, View } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { router, useFocusEffect } from "expo-router";
import { moderateScale, scale } from "react-native-size-matters";
import { Colors } from "@/theme";
import {
type PageTone,
toneColors,
usePageTone,
} from "../../context/PageToneContext";
export interface ContentPageHeaderProps {
title: string;
subtitle?: string;
right?: ReactNode;
fallbackRoute?: string;
onTitlePress?: () => void;
titleAccessibilityLabel?: string;
onBack?: () => void;
tone?: PageTone;
titleColor?: string;
}
export function ContentPageHeader({
title,
subtitle,
right,
fallbackRoute = "/modules",
onTitlePress,
titleAccessibilityLabel,
onBack,
tone,
titleColor,
}: ContentPageHeaderProps) {
const { tone: contextTone } = usePageTone();
const effectiveTone = tone || contextTone;
const resolvedTitleColor =
titleColor ||
Colors.textPrimary;
useFocusEffect(
useCallback(() => {
if (!onBack) return;
const subscription = BackHandler.addEventListener(
"hardwareBackPress",
() => {
onBack();
return true;
}
);
return () => {
subscription.remove();
};
}, [onBack])
);
const handleBack = () => {
if (onBack) {
onBack();
return;
}
if (router.canGoBack()) {
router.back();
return;
}
router.replace(fallbackRoute as never);
};
const titleContent = (
{title}
{subtitle ? (
{subtitle}
) : null}
);
return (
[
styles.backButton,
pressed && styles.backButtonPressed,
]}
>
{onTitlePress ? (
[
styles.titleButton,
pressed && styles.pressed,
]}
>
{titleContent}
) : (
{titleContent}
)}
{right}
);
}
const styles = StyleSheet.create({
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",
},
sideSlot: {
width: 82,
height: 40,
alignItems: "flex-start",
justifyContent: "center",
},
rightSlot: {
flexDirection: "row",
alignItems: "center",
justifyContent: "flex-end",
},
backButton: {
width: scale(38),
height: scale(38),
borderRadius: scale(19),
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F1F5F9",
},
backButtonPressed: {
backgroundColor: "#E2E8F0",
},
pressed: {
opacity: 0.65,
},
titleButton: {
flex: 1,
minHeight: 40,
marginHorizontal: moderateScale(12),
alignItems: "center",
justifyContent: "center",
},
titleContent: {
width: "100%",
alignItems: "center",
justifyContent: "center",
gap: 1,
},
title: {
width: "100%",
textAlign: "center",
fontSize: moderateScale(18),
fontWeight: "700",
color: Colors.textPrimary,
},
subtitle: {
width: "100%",
textAlign: "center",
fontSize: moderateScale(12),
lineHeight: moderateScale(16),
fontWeight: "500",
color: Colors.textSecondary,
},
});