acupedia-mobile-ui/src/screens/settings/SettingsScreen.tsx

175 lines
4.9 KiB
TypeScript

import React, { useState } from "react";
import {
Alert,
Platform,
ScrollView,
StyleSheet,
View,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
import { router } from "expo-router";
import { moderateScale, verticalScale } from "react-native-size-matters";
import { Colors } from "@/theme";
import SettingsHeader from "@/features/settings/components/SettingsHeader";
import SettingsSection from "@/features/settings/components/SettingsSection";
import LogoutButton from "@/features/settings/components/LogoutButton";
import {
ACCOUNT_SETTINGS,
PREFERENCE_SETTINGS,
SUPPORT_SETTINGS,
} from "@/features/settings/constants/settingsData";
import { SettingsItem } from "@/features/settings/types";
import { useAuthStore } from "@/store/auth.store";
import { useSettingsStore } from "@/store/settings.store";
import { ENABLE_ENV_SWITCHER } from "@/config/environments";
import { useEnvironmentStore } from "@/store/environment.store";
import EnvironmentSelectorModal from "@/components/EnvironmentSelectorModal";
export default function SettingsScreen() {
const [envModalVisible, setEnvModalVisible] = useState(false);
const activeConfig = useEnvironmentStore((state) => state.getActiveConfig());
const user = useAuthStore((state) => state.user);
const logout = useAuthStore((state) => state.logout);
const accordionDefaultExpanded = useSettingsStore(
(state) => state.accordionDefaultExpanded
);
const setAccordionDefaultExpanded = useSettingsStore(
(state) => state.setAccordionDefaultExpanded
);
const contentPreviewEnabled = useSettingsStore(
(state) => state.contentPreviewEnabled
);
const setContentPreviewEnabled = useSettingsStore(
(state) => state.setContentPreviewEnabled
);
// Clinical Preview Mode is strictly gated to admin role
const isAdmin = user?.roles?.includes("admin") ?? false;
const appPreferenceItems: SettingsItem[] = [
{
id: "expand-accordions",
title: "Expand Sections by Default",
subtitle: "Keep all detail accordions open while reading",
icon: "list-outline",
isSwitch: true,
switchValue: accordionDefaultExpanded,
onSwitchChange: (val) => setAccordionDefaultExpanded(val),
},
...(isAdmin
? [
{
id: "clinical-preview",
title: "Clinical Draft / Preview Mode",
subtitle: "View unpublished modules & draft revisions",
icon: "flask-outline" as const,
isSwitch: true,
switchValue: contentPreviewEnabled,
onSwitchChange: (val: boolean) => setContentPreviewEnabled(val),
},
]
: []),
...PREFERENCE_SETTINGS,
...(ENABLE_ENV_SWITCHER
? [
{
id: "server-environment",
title: "Server Environment",
subtitle: `${activeConfig.label} (${activeConfig.badge})`,
icon: "server-outline" as const,
value: activeConfig.badge,
onPress: () => setEnvModalVisible(true),
},
]
: []),
];
const handleLogout = () => {
const doLogout = () => {
logout();
router.replace("/login");
};
if (Platform.OS === "web") {
const confirmed =
typeof window !== "undefined"
? window.confirm("Are you sure you want to log out?")
: true;
if (confirmed) {
doLogout();
}
} else {
Alert.alert(
"Log Out",
"Are you sure you want to log out of Acupedia?",
[
{ text: "Cancel", style: "cancel" },
{
text: "Log Out",
style: "destructive",
onPress: doLogout,
},
],
{ cancelable: true }
);
}
};
return (
<SafeAreaView style={styles.safeArea}>
<ScrollView
showsVerticalScrollIndicator={false}
contentContainerStyle={styles.contentContainer}
keyboardShouldPersistTaps="handled"
>
<View style={styles.container}>
<SettingsHeader title="Settings" />
<SettingsSection
title="Account Settings"
items={ACCOUNT_SETTINGS}
/>
<SettingsSection
title="App Preferences"
items={appPreferenceItems}
/>
<SettingsSection
title="Support & Info"
items={SUPPORT_SETTINGS}
/>
<LogoutButton onPress={handleLogout} />
</View>
</ScrollView>
{ENABLE_ENV_SWITCHER && (
<EnvironmentSelectorModal
visible={envModalVisible}
onClose={() => setEnvModalVisible(false)}
/>
)}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: Colors.background,
},
contentContainer: {
flexGrow: 1,
paddingBottom: verticalScale(32),
},
container: {
flex: 1,
paddingHorizontal: moderateScale(20),
paddingTop: verticalScale(12),
},
});