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 { useTranslation } from "react-i18next"; import { BiometricRow, LoyaltyStrip, QuickActions } from "@/components/profile"; 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 { AppHeader } from "@/components/ui/Header"; import { Screen } from "@/components/ui/Screen"; import { Text } from "@/components/ui/Text"; import { routes } from "@/lib/routes"; import { useAuthStore, type AuthProvider } from "@/lib/stores/authStore"; import { useLangStore } from "@/lib/stores/langStore"; import { useLocationStore } from "@/lib/stores/locationStore"; import { usePrefsStore } from "@/lib/stores/prefsStore"; import { useProfileModeStore } from "@/lib/stores/profileModeStore"; import { paymentLabel } from "@/components/checkout/PaymentMethodList"; import { colors, gutter } from "@/lib/theme"; import { CALL_PHONE, CHATWOOT_BASE, WHATSAPP_NUMBER } from "@/lib/utils"; export default function ProfileScreen() { const { t } = useTranslation(); const user = useAuthStore((s) => s.user); const setAvatar = useAuthStore((s) => s.setAvatar); const updateProfile = useAuthStore((s) => s.updateProfile); const setBiometric = useAuthStore((s) => s.setBiometric); 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 payment = useLocationStore((s) => s.paymentPreference); const prefs = usePrefsStore(); 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: "Payment methods", href: routes.paymentMethods, icon: "card-outline", subtitle: paymentLabel(payment), }, { label: "Notification preferences", href: routes.notificationPrefs, icon: "notifications-outline", subtitle: [ prefs.push ? "Push" : null, prefs.sms ? "SMS" : null, prefs.email ? "Email" : null, ] .filter(Boolean) .join(" · ") || "All off", }, { label: t("common.notifications"), href: routes.notifications, icon: "mail-unread-outline", subtitle: "Alerts inbox" }, { label: t("common.prescriptions"), href: routes.rxList, icon: "medkit-outline", subtitle: "Rx + family" }, { label: t("common.loyalty"), href: routes.loyalty, icon: "star-outline", subtitle: `${user?.points ?? 0} pts` }, { 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.about"), href: routes.about, icon: "information-circle-outline" }, { label: t("common.contact"), href: routes.contact, icon: "mail-outline" }, ]; return ( {user?.name || "Guest"} {user?.phone || "—"} {user?.email ? ( {user.email} ) : null} {user?.authProvider && user.authProvider !== "guest" ? ( ) : null} {user?.isGuest ? : 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"} ) : ( { signOut(); setHasOnboarded(false); router.replace(routes.signIn as never); }} > Sign in / create account )} {membership ? ( {membership.planName} · {membership.remainingAllowance} ETB allowance left ) : null} {!user?.isGuest ? ( ) : null} {editing && !user?.isGuest ? ( Edit profile