import { Ionicons } from "@expo/vector-icons"; import { useRouter } from "expo-router"; import React, { useState, useEffect } from "react"; import { View, Text, TouchableOpacity, Image, ActivityIndicator, Modal, Alert, Linking, Platform, ActionSheetIOS, } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { CalComAPIService, UserProfile } from "../services/calcom"; import { CalComLogo } from "./CalComLogo"; import { FullScreenModal } from "./FullScreenModal"; export function Header() { const router = useRouter(); const insets = useSafeAreaInsets(); const [userProfile, setUserProfile] = useState(null); const [loading, setLoading] = useState(true); const [showProfileModal, setShowProfileModal] = useState(false); useEffect(() => { fetchUserProfile(); }, []); const fetchUserProfile = async () => { try { const profile = await CalComAPIService.getUserProfile(); setUserProfile(profile); } catch (error) { console.error("Failed to fetch user profile:", error); } finally { setLoading(false); } }; const handleProfile = () => { if (Platform.OS === "ios") { const options = ["Cancel", "My Profile", "My Settings", "Out of Office", "Help", "Sign Out"]; ActionSheetIOS.showActionSheetWithOptions( { options, destructiveButtonIndex: 6, // Sign Out cancelButtonIndex: 0, title: userProfile?.name || "Profile Menu", }, (buttonIndex) => { switch (buttonIndex) { case 1: // My Profile handleMenuOption("profile"); break; case 2: // My Settings handleMenuOption("settings"); break; case 3: // Out of Office handleMenuOption("outOfOffice"); break; case 4: // Support handleMenuOption("help"); break; case 5: // Sign Out handleMenuOption("signOut"); break; } } ); } else { setShowProfileModal(true); } }; const openExternalLink = async (url: string, fallbackMessage: string) => { try { const supported = await Linking.canOpenURL(url); if (supported) { await Linking.openURL(url); } else { Alert.alert("Error", `Cannot open ${fallbackMessage} on your device.`); } } catch (error) { console.error(`Failed to open ${url}:`, error); Alert.alert("Error", `Failed to open ${fallbackMessage}. Please try again.`); } }; const handleMenuOption = (option: string) => { if (Platform.OS !== "ios") { setShowProfileModal(false); } switch (option) { case "profile": openExternalLink("https://app.cal.com/settings/my-account/profile", "Profile page"); break; case "settings": openExternalLink("https://app.cal.com/settings/my-account", "Settings page"); break; case "outOfOffice": openExternalLink( "https://app.cal.com/settings/my-account/out-of-office", "Out of Office page" ); break; case "roadmap": openExternalLink("https://cal.com/roadmap", "Roadmap"); break; case "help": openExternalLink("https://cal.com/help", "Help page"); break; case "signOut": Alert.alert("Sign Out", "Are you sure you want to sign out?", [ { text: "Cancel", style: "cancel" }, { text: "Sign Out", style: "destructive", onPress: () => { // TODO: Implement sign out console.log("Sign Out pressed"); }, }, ]); break; } }; return ( <> {/* Left: Cal.com Logo */} {/* Right: Icons */} {/* Profile Picture */} {loading ? ( ) : userProfile?.avatarUrl ? ( ) : ( )} {/* Profile Menu Modal */} setShowProfileModal(false)} > setShowProfileModal(false)} > e.stopPropagation()} > {/* Header with profile info */} {userProfile?.avatarUrl ? ( ) : ( {userProfile?.name ? userProfile.name.charAt(0).toUpperCase() : userProfile?.email?.charAt(0).toUpperCase() || "?"} )} {userProfile?.name || "User"} {userProfile?.email && ( {userProfile.email} )} {/* Menu Items */} handleMenuOption("profile")} > My Profile handleMenuOption("settings")} > My Settings handleMenuOption("outOfOffice")} > Out of Office handleMenuOption("roadmap")} > Roadmap handleMenuOption("help")} > Help handleMenuOption("signOut")} > Sign Out {/* Cancel button */} setShowProfileModal(false)} > Cancel ); }