import { Ionicons } from "@expo/vector-icons"; import * as ImagePicker from "expo-image-picker"; import { router } from "expo-router"; import { useState } from "react"; import { Alert, Linking, Pressable, ScrollView, View } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { useTranslation } from "react-i18next"; import { Avatar } from "@/components/ui/Avatar"; import { Badge } from "@/components/ui/Badge"; import { Button } from "@/components/ui/Button"; import { Card, MenuRow } from "@/components/ui/Card"; import { Field } from "@/components/ui/Field"; import { Screen, TAB_BAR_INSET } from "@/components/ui/Screen"; import { Text } from "@/components/ui/Text"; import { routes } from "@/lib/routes"; import { useAuthStore } from "@/lib/stores/authStore"; import { useLangStore } from "@/lib/stores/langStore"; import { useProfileModeStore } from "@/lib/stores/profileModeStore"; import { colors, gutter } from "@/lib/theme"; import { CALL_PHONE, CHATWOOT_BASE, WHATSAPP_NUMBER } from "@/lib/utils"; export default function ProfileScreen() { const { t } = useTranslation(); const insets = useSafeAreaInsets(); const user = useAuthStore((s) => s.user); const setAvatar = useAuthStore((s) => s.setAvatar); const updateProfile = useAuthStore((s) => s.updateProfile); const signOut = useAuthStore((s) => s.signOut); const setHasOnboarded = useAuthStore((s) => s.setHasOnboarded); const mode = useProfileModeStore((s) => s.mode); const membership = useProfileModeStore((s) => s.activeMembership()); const language = useLangStore((s) => s.language); const setLanguage = useLangStore((s) => s.setLanguage); const [editing, setEditing] = useState(false); const [name, setName] = useState(user?.name ?? ""); const [phone, setPhone] = useState(user?.phone ?? ""); const [email, setEmail] = useState(user?.email ?? ""); const pickAvatar = async () => { const permission = await ImagePicker.requestMediaLibraryPermissionsAsync(); if (!permission.granted) { Alert.alert( "Photo access needed", "Allow photo library access to set your profile picture." ); return; } const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ["images"], allowsEditing: true, aspect: [1, 1], quality: 0.85, }); if (!result.canceled && result.assets[0]?.uri) { setAvatar(result.assets[0].uri); } }; const takePhoto = async () => { const permission = await ImagePicker.requestCameraPermissionsAsync(); if (!permission.granted) { Alert.alert("Camera access needed", "Allow camera access to take a profile photo."); return; } const result = await ImagePicker.launchCameraAsync({ allowsEditing: true, aspect: [1, 1], quality: 0.85, }); if (!result.canceled && result.assets[0]?.uri) { setAvatar(result.assets[0].uri); } }; const changePhoto = () => { Alert.alert("Profile photo", "Choose how you'd like to update your photo.", [ { text: "Choose from library", onPress: pickAvatar }, { text: "Take photo", onPress: takePhoto }, ...(user?.avatarUri ? [{ text: "Remove photo", style: "destructive" as const, onPress: () => setAvatar(null) }] : []), { text: "Cancel", style: "cancel" }, ]); }; const links: { label: string; href: string; icon: keyof typeof Ionicons.glyphMap; subtitle?: string; }[] = [ { label: t("common.switchProfile"), href: routes.profileSwitch, icon: "swap-horizontal", subtitle: mode }, { label: t("common.joinOrg"), href: routes.profileJoin, icon: "business-outline" }, { label: t("common.orders"), href: routes.orders, icon: "receipt-outline", subtitle: "Track delivery" }, { label: t("common.prescriptions"), href: routes.rxList, icon: "document-text-outline" }, { label: t("common.loyalty"), href: routes.loyalty, icon: "star-outline", subtitle: `${user?.points ?? 0} pts` }, { label: t("common.family"), href: routes.family, icon: "people-outline" }, { label: t("common.subscriptions"), href: routes.subscriptions, icon: "repeat-outline" }, { label: t("common.emergencyContacts"), href: routes.emergencyContacts, icon: "alert-circle-outline" }, { label: t("common.branches"), href: routes.branches, icon: "location-outline" }, { label: t("common.addresses"), href: routes.addresses, icon: "home-outline" }, { label: t("common.language"), href: routes.language, icon: "language-outline", subtitle: language.toUpperCase() }, { label: t("common.about"), href: routes.about, icon: "information-circle-outline" }, { label: t("common.contact"), href: routes.contact, icon: "mail-outline" }, ]; return ( {t("common.profile")} {user?.name || "Guest"} {user?.phone || "—"} {user?.email ? ( {user.email} ) : null} {user?.avatarUri ? "Change photo" : "Add photo"} {!user?.isGuest ? ( { setName(user?.name ?? ""); setPhone(user?.phone ?? ""); setEmail(user?.email ?? ""); setEditing((v) => !v); }} > {editing ? "Close editor" : "Edit profile"} ) : null} {membership ? ( {membership.planName} · {membership.remainingAllowance} ETB allowance left ) : null} {editing && !user?.isGuest ? ( Edit profile