import { useState, useRef, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { ChevronDown, Lock, Settings } from "lucide-react"; import { useProfile } from "../../contexts/ProfileContext"; import PinDialog from "./PinDialog"; import ProfileFormModal from "./ProfileFormModal"; import type { Profile } from "../../services/profileService"; export default function ProfileSwitcher() { const { t } = useTranslation(); const { profiles, activeProfile, switchProfile } = useProfile(); const [open, setOpen] = useState(false); const [pinProfile, setPinProfile] = useState(null); const [showManage, setShowManage] = useState(false); const ref = useRef(null); // Close on outside click useEffect(() => { function handleClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) { setOpen(false); } } if (open) document.addEventListener("mousedown", handleClick); return () => document.removeEventListener("mousedown", handleClick); }, [open]); const handleSelect = (profile: Profile) => { setOpen(false); if (profile.id === activeProfile?.id) return; if (profile.pin_hash) { setPinProfile(profile); } else { switchProfile(profile.id); } }; const handlePinSuccess = () => { if (pinProfile) { switchProfile(pinProfile.id); setPinProfile(null); } }; return ( <>
{open && (
{profiles.map((profile) => ( ))}
)}
{pinProfile && ( setPinProfile(null)} /> )} {showManage && ( setShowManage(false)} /> )} ); }