import { FC, useEffect, useState } from 'react' import Input from '../components/Input' import { ArrowDown2, CloseCircle, HambergerMenu, Logout, MessageQuestion, ProfileCircle, SearchNormal, Setting2 } from 'iconsax-react' import { useTranslation } from 'react-i18next' import Notifications from '../pages/notification/Notification' import { useSharedStore } from './store/sharedStore' import { Popover, PopoverButton, PopoverPanel } from '@headlessui/react' import { Link, useLocation } from 'react-router-dom' import { Paths } from '@/utils/Paths' import SideBarItem from './SideBarItem' import AvatarImage from '@/assets/images/avatar_image.png' import { useGetProfile } from '@/pages/profile/hooks/useProfileData' import { useGetInbox } from '@/pages/received/hooks/useEmailData' import ThemeToggle from '@/components/ThemeToggle' import { getIconColor } from '@/utils/colorUtils' import RowActionsDropdown from '@/components/RowActionsDropdown' const Header: FC = () => { const [popoverKey, setPopoverKey] = useState(0); const { data } = useGetProfile() const [search, setSearch] = useState('') const [showSearchResults, setShowSearchResults] = useState(false) const [inputFocused, setInputFocused] = useState(false) const [showMobileSearch, setShowMobileSearch] = useState(false) const { pathname } = useLocation() const { data: inboxData } = useGetInbox({ limit: 10, search: search, enabled: !!search, }) const { t } = useTranslation('global') const { setOpenSidebar, openSidebar, setOpenReportBug, setEmailWidth } = useSharedStore() const [popoverButtonRef, setPopoverButtonRef] = useState(null) const [popoverButtonWidth, setPopoverButtonWidth] = useState(0) const handleSearchChange = (value: string) => { setSearch(value) if (value) { setShowSearchResults(true) } else { setShowSearchResults(false) } } const clearSearch = () => { setSearch('') setShowSearchResults(false) setInputFocused(false) } const clearSearchMobile = () => { setSearch('') setShowSearchResults(false) setInputFocused(false) setShowMobileSearch(false) } const handleInputFocus = () => { setInputFocused(true) if (search) { setShowSearchResults(true) } } const handleInputBlur = (e: React.FocusEvent) => { const searchContainer = document.getElementById('search-dropdown-container') const mobileSearchContainer = document.getElementById('mobile-search-dropdown-container') if ((searchContainer && searchContainer.contains(e.relatedTarget as Node)) || (mobileSearchContainer && mobileSearchContainer.contains(e.relatedTarget as Node))) { return } setInputFocused(false) setTimeout(() => { setShowSearchResults(false) }, 200) } const toggleMobileSearch = () => { setShowMobileSearch(prev => !prev) if (!showMobileSearch) { setTimeout(() => { const mobileSearchInput = document.getElementById('mobile-search-input') if (mobileSearchInput) { (mobileSearchInput as HTMLInputElement).focus() setInputFocused(true) } }, 100) } else { clearSearch() } } useEffect(() => { const handleClickOutside = (e: MouseEvent) => { const searchContainer = document.getElementById('search-dropdown-container') const mobileSearchContainer = document.getElementById('mobile-search-dropdown-container') const searchInput = document.getElementById('search-input-container') const mobileSearchInput = document.getElementById('mobile-search-input-container') const isClickOutsideDesktopSearch = searchContainer && !searchContainer.contains(e.target as Node) && searchInput && !searchInput.contains(e.target as Node); const isClickOutsideMobileSearch = mobileSearchContainer && !mobileSearchContainer.contains(e.target as Node) && mobileSearchInput && !mobileSearchInput.contains(e.target as Node); if (searchContainer && searchInput && isClickOutsideDesktopSearch) { clearSearch() } else if (mobileSearchContainer && mobileSearchInput && isClickOutsideMobileSearch) { clearSearchMobile() } } document.addEventListener('mousedown', handleClickOutside) return () => { document.removeEventListener('mousedown', handleClickOutside) } }, []) useEffect(() => { setPopoverKey((prevKey) => prevKey + 1) }, [pathname]) useEffect(() => { if (popoverButtonRef) { const rect = popoverButtonRef.getBoundingClientRect(); setPopoverButtonWidth(rect.width); console.log('popoverButtonRef width:', rect.width); console.log('popoverButtonRef height:', rect.height); console.log('popoverButtonRef dimensions:', { width: rect.width, height: rect.height }); console.log('Stored width in state:', popoverButtonWidth); setEmailWidth(rect.width); } }, [popoverButtonRef]) return (
{/* Desktop Search */}
{showSearchResults && search && inputFocused && (
{inboxData?.data?.results && inboxData.data.results.length > 0 ? (
{inboxData.data.results.slice(0, 10).map((message) => (
{!message.seen && (
)} {message.from.name || message.from.address}
{message.subject || 'بدون موضوع'}
{message.intro}
{new Date(message.date).toLocaleDateString('fa-IR')}
))}
) : (
نتیجه‌ای یافت نشد
)}
)}
{/* Mobile Layout */}
{!showMobileSearch && (
setOpenSidebar(!openSidebar)} className='xl:hidden block'>
)} {/* Full width mobile search when active */} {showMobileSearch && (
)}
{/* Mobile Search Results */} {showMobileSearch && showSearchResults && search && (
{inboxData?.data?.results && inboxData.data.results.length > 0 ? (
{inboxData.data.results.slice(0, 10).map((message) => (
{!message.seen && (
)} {message.from.name || message.from.address}
{message.subject || 'بدون موضوع'}
{message.intro}
{new Date(message.date).toLocaleDateString('fa-IR')}
))}
) : (
نتیجه‌ای یافت نشد
)}
)}
{/* Mobile search icon */} {!showMobileSearch && (
)} window.open( import.meta.env.VITE_HELP_URL + `/${import.meta.env.VITE_SERVICE_ID}`, '_blank' ), }, { label: 'گزارش اشکالات', onClick: () => setOpenReportBug(true), }, ]} trigger={} topOffset={20} leftOffset={70} topOffsetMobile={10} leftOffsetMobile={60} /> { data && (
{data?.data?.user?.emailAddress}
setPopoverKey((prevKey) => prevKey + 1)} size={20} color={getIconColor('primary')} />
{data?.data?.user?.emailAddress}
{data?.data?.user?.email}
} title={t('header.profile')} isActive link={Paths.profile} isWithoutLine /> } title={t('header.setting')} isActive link={Paths.setting} isWithoutLine />
} title={t('sidebar.logout')} isActive link={'#'} isWithoutLine isLogout />
) }
) } export default Header