acupedia-mobile-ui/src/app/_layout.tsx

185 lines
5.0 KiB
TypeScript

import { router, Stack } from "expo-router";
import React from "react";
// ============================================================================
// GLOBAL NAVIGATION THROTTLE (Generic Fix for Repeated Click / Double Navigation)
// ============================================================================
const NAVIGATION_THROTTLE_MS = 700;
let lastNavigationTimestamp = 0;
const originalPush = router.push.bind(router);
const originalNavigate = router.navigate.bind(router);
router.push = ((...args: Parameters<typeof originalPush>) => {
const now = Date.now();
if (now - lastNavigationTimestamp < NAVIGATION_THROTTLE_MS) {
return;
}
lastNavigationTimestamp = now;
return originalPush(...args);
}) as typeof router.push;
router.navigate = ((...args: Parameters<typeof originalNavigate>) => {
const now = Date.now();
if (now - lastNavigationTimestamp < NAVIGATION_THROTTLE_MS) {
return;
}
lastNavigationTimestamp = now;
return originalNavigate(...args);
}) as typeof router.navigate;
// ============================================================================
import * as RN from "react-native";
import {
useFonts,
Inter_400Regular,
Inter_500Medium,
Inter_600SemiBold,
Inter_700Bold,
} from "@expo-google-fonts/inter";
import { AppProvider } from "@/providers";
// Helper to resolve font family based on styles
const resolveCustomStyle = (style: any) => {
if (!style) {
return { fontFamily: "Inter-Regular" };
}
const flat = RN.StyleSheet.flatten(style);
// If fontFamily is already specified (like icons), don't override it!
if (flat && flat.fontFamily && flat.fontFamily !== "System" && flat.fontFamily !== "sans-serif") {
return style;
}
// Map font weight to Inter weights
const weight = flat?.fontWeight;
let fontFamily = "Inter-Regular";
if (weight === "bold" || weight === "700" || weight === "800" || weight === "900") {
fontFamily = "Inter-Bold";
} else if (weight === "600") {
fontFamily = "Inter-SemiBold";
} else if (weight === "500") {
fontFamily = "Inter-Medium";
}
// Merge the new fontFamily with the rest of the styles
return [
style,
{ fontFamily }
];
};
const OriginalText = RN.Text;
const OriginalTextInput = RN.TextInput;
// Custom Text HOC
const CustomText = React.forwardRef((props: any, ref: any) => {
const { style, allowFontScaling, ...rest } = props;
const newStyle = resolveCustomStyle(style);
return (
<OriginalText
{...rest}
ref={ref}
style={newStyle}
allowFontScaling={false}
/>
);
});
// Bind static props/methods if any
Object.assign(CustomText, OriginalText);
try {
const rnExports = require("react-native");
const descText = Object.getOwnPropertyDescriptor(rnExports, "Text");
if (descText && descText.configurable) {
Object.defineProperty(rnExports, "Text", {
get: () => CustomText,
configurable: true,
});
}
} catch (e) {
// Silently ignore if module exports are immutable in web bundle environment
}
// Custom TextInput HOC
const CustomTextInput = React.forwardRef((props: any, ref: any) => {
const { style, allowFontScaling, ...rest } = props;
const newStyle = resolveCustomStyle(style);
return (
<OriginalTextInput
{...rest}
ref={ref}
style={newStyle}
allowFontScaling={false}
/>
);
});
Object.assign(CustomTextInput, OriginalTextInput);
try {
const rnExports = require("react-native");
const descInput = Object.getOwnPropertyDescriptor(rnExports, "TextInput");
if (descInput && descInput.configurable) {
Object.defineProperty(rnExports, "TextInput", {
get: () => CustomTextInput,
configurable: true,
});
}
} catch (e) {
// Silently ignore if module exports are immutable in web bundle environment
}
import { useEffect } from "react";
import { Alert, Platform } from "react-native";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import * as ScreenCapture from "expo-screen-capture";
export default function RootLayout() {
const [fontsLoaded] = useFonts({
"Inter-Regular": Inter_400Regular,
"Inter-Medium": Inter_500Medium,
"Inter-SemiBold": Inter_600SemiBold,
"Inter-Bold": Inter_700Bold,
});
useEffect(() => {
if (Platform.OS === "web") return;
let isMounted = true;
void ScreenCapture.preventScreenCaptureAsync();
const subscription = ScreenCapture.addScreenshotListener(() => {
if (!isMounted) return;
Alert.alert(
"Confidential Clinical Content",
"Acupedia clinical reference materials and diagrams are proprietary. Capturing screenshots is restricted to protect clinical data."
);
});
return () => {
isMounted = false;
subscription?.remove();
};
}, []);
if (!fontsLoaded) {
return null;
}
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<AppProvider>
<Stack
screenOptions={{
headerShown: false,
}}
/>
</AppProvider>
</GestureHandlerRootView>
);
}