import { Ionicons } from "@expo/vector-icons"; import { useRouter } from "expo-router"; import React, { useState, useEffect } from "react"; import { View, Text, TouchableOpacity, Image, ActivityIndicator, Platform, ActionSheetIOS, Alert, } from "react-native"; import * as Clipboard from "expo-clipboard"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { CalComAPIService, UserProfile } from "../services/calcom"; import { CalComLogo } from "./CalComLogo"; import { FullScreenModal } from "./FullScreenModal"; import { openInAppBrowser } from "../utils/browser"; 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); } }; // Build public page URL const publicPageUrl = userProfile?.username ? `https://cal.com/${userProfile.username}` : null; const handleViewPublicPage = () => { if (publicPageUrl) { openInAppBrowser(publicPageUrl, "Public page"); } }; const handleCopyPublicPageLink = async () => { if (!publicPageUrl) return; try { await Clipboard.setStringAsync(publicPageUrl); Alert.alert("Link Copied!", "Your public page link has been copied to clipboard."); } catch (error) { console.error("Failed to copy public page link:", error); Alert.alert("Error", "Failed to copy link. Please try again."); } }; const handleProfile = () => { if (Platform.OS === "ios") { const options = [ "Cancel", "My Profile", "My Settings", "Out of Office", "View public page", "Copy public page link", "Help", ]; ActionSheetIOS.showActionSheetWithOptions( { options, 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: // View public page handleViewPublicPage(); break; case 5: // Copy public page link handleCopyPublicPageLink(); break; case 6: // Help handleMenuOption("help"); break; } } ); } else { setShowProfileModal(true); } }; const handleMenuOption = (option: string) => { if (Platform.OS !== "ios") { setShowProfileModal(false); } switch (option) { case "profile": openInAppBrowser("https://app.cal.com/settings/my-account/profile", "Profile page"); break; case "settings": openInAppBrowser("https://app.cal.com/settings/my-account", "Settings page"); break; case "outOfOffice": openInAppBrowser( "https://app.cal.com/settings/my-account/out-of-office", "Out of Office page" ); break; case "roadmap": openInAppBrowser("https://cal.com/roadmap", "Roadmap"); break; case "help": openInAppBrowser("https://cal.com/help", "Help page"); 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 {/* View public page */} { setShowProfileModal(false); handleViewPublicPage(); }} > View public page {/* Copy public page link */} { setShowProfileModal(false); handleCopyPublicPageLink(); }} > Copy public page link handleMenuOption("roadmap")} > Roadmap handleMenuOption("help")} > Help {/* Cancel button */} setShowProfileModal(false)} > Cancel ); }