import { useEffect, useState } from "react"; import { Pressable, Switch, View } from "react-native"; import { ControlsData, SafetyControlValue, getControlsData, updateSafetyControl, } from "../../api"; import { useDevice } from "../../lib/device"; import { t } from "../../lib/locales"; import { colors } from "../../lib/theme"; import { Card, ConfirmDialog, DeviceSelector, Divider, H2, Muted, Row, Screen, SelectDialog, } from "../../lib/ui"; export default function ControlsScreen() { const [data, setData] = useState(null); const [state, setState] = useState>({}); const [communication, setCommunication] = useState<"scan" | "block">("scan"); const [communicationModalVisible, setCommunicationModalVisible] = useState(false); const [galleryScanningMode, setGalleryScanningMode] = useState< "delete" | "notify" | "none" >("none"); const [galleryScanningModalVisible, setGalleryScanningModalVisible] = useState(false); const [confirmModalVisible, setConfirmModalVisible] = useState(false); const { devices, selectedDevice, selectDevice, isLoading: isDeviceLoading, } = useDevice(); const normalizeGalleryScanningMode = ( value: SafetyControlValue | undefined, ): "delete" | "notify" | "none" => { if (value === "delete" || value === "notify" || value === "none") { return value; } return "none"; }; useEffect(() => { if (selectedDevice) { getControlsData().then((d) => { setData(d); setState( Object.fromEntries( d.safetyControls.map((c) => [c.key, c.defaultValue]), ), ); const blockDefault = d.safetyControls.find( (c) => c.key === "block_strangers", )?.defaultValue; setCommunication(blockDefault ? "block" : "scan"); const galleryDefault = d.safetyControls.find( (c) => c.key === "gallery_scanning_mode", )?.defaultValue; setGalleryScanningMode(normalizeGalleryScanningMode(galleryDefault)); }); } }, [selectedDevice]); const handleToggle = (key: string, value: SafetyControlValue) => { setState((prev) => ({ ...prev, [key]: value })); updateSafetyControl(key, value); }; const handleGalleryScanningModeChange = ( value: "delete" | "notify" | "none", ) => { setGalleryScanningMode(value); handleToggle("gallery_scanning_mode", value); setGalleryScanningModalVisible(false); }; const galleryScanningLabel = galleryScanningMode === "delete" ? t("galleryScanningDelete") : galleryScanningMode === "notify" ? t("galleryScanningNotify") : t("galleryScanningNone"); const openCommunicationOptions = () => { setCommunicationModalVisible(true); }; const chooseCommunication = (choice: "scan" | "block") => { if (choice === "scan") { setCommunication("scan"); handleToggle("block_strangers", false); setCommunicationModalVisible(false); } else { // block chosen - show confirmation setConfirmModalVisible(true); setCommunicationModalVisible(false); } }; const confirmBlock = () => { setCommunication("block"); handleToggle("block_strangers", true); setConfirmModalVisible(false); }; const cancelConfirm = () => { setConfirmModalVisible(false); }; if (!selectedDevice && !isDeviceLoading) return ( {t("noDeviceSelected")} ); if (!data) return ( ); return (

{t("disableBuddy")}

{t("disableBuddy")}

{t("temporarilyDisablesBuddy")} } right={ handleToggle("disable_buddy", value)} trackColor={{ false: colors.outline, true: colors.primary }} thumbColor={colors.onPrimary} /> } />

{t("contentBlocking")}

{t("adultSites")}

{t("blockAdultWebsites")} } right={ handleToggle("adult_sites", value)} trackColor={{ false: colors.outline, true: colors.primary }} thumbColor={colors.onPrimary} /> } />

{t("familyLink")}

{t("antiCircumvention")}

{t("preventFamilyLinkBypasses")} } right={ handleToggle("family_link_anti_circumvention", value) } trackColor={{ false: colors.outline, true: colors.primary }} thumbColor={colors.onPrimary} /> } />

{t("communication")}

{t("communicationWithStrangers")}} right={ {communication === "block" ? t("blockAllCommunications") : t("scanCommunicationsWithAI")} } /> {t("chooseHowBuddyShouldHandleStrangers")} chooseCommunication("scan"), }, { label: t("blockAllCommunications"), onPress: () => chooseCommunication("block"), destructive: true, }, ]} onClose={() => setCommunicationModalVisible(false)} />

{t("notifications")}

{t("dangerousMessages")}} right={ handleToggle("notify_dangerous_messages", value) } trackColor={{ false: colors.outline, true: colors.primary }} thumbColor={colors.onPrimary} /> } /> {t("newContactAdded")}} right={ handleToggle("notify_new_contact_added", value) } trackColor={{ false: colors.outline, true: colors.primary }} thumbColor={colors.onPrimary} /> } />

{t("galleryScanning")}

{t("galleryScanning")}

{t("scanGalleryForInappropriateImages")} } right={ setGalleryScanningModalVisible(true)}> {galleryScanningLabel} } /> handleGalleryScanningModeChange("none"), }, { label: t("galleryScanningNotify"), onPress: () => handleGalleryScanningModeChange("notify"), }, { label: t("galleryScanningDelete"), onPress: () => handleGalleryScanningModeChange("delete"), destructive: true, }, ]} onClose={() => setGalleryScanningModalVisible(false)} />
); }