'use client'; import { useState, useEffect, useRef } from 'react'; import { useRouter, usePathname } from 'next/navigation'; import { Locale, locales, localeNames, localeFlags } from '../lib/i18n'; interface FloatingLanguageSwitcherProps { locale: Locale; } export default function FloatingLanguageSwitcher({ locale }: FloatingLanguageSwitcherProps) { const [isOpen, setIsOpen] = useState(false); const [isVisible, setIsVisible] = useState(true); const [isMounted, setIsMounted] = useState(false); const router = useRouter(); const pathname = usePathname(); const switcherRef = useRef(null); const lastScrollY = useRef(0); // Ensure component is mounted before rendering to avoid hydration issues useEffect(() => { setIsMounted(true); }, []); // Handle scroll to show/hide the switcher (temporarily disabled for visibility) useEffect(() => { // Keep the switcher always visible for now setIsVisible(true); // Uncomment below to re-enable scroll hiding /* const handleScroll = () => { const currentScrollY = window.scrollY; if (currentScrollY > lastScrollY.current && currentScrollY > 100) { // Scrolling down setIsVisible(false); } else { // Scrolling up setIsVisible(true); } lastScrollY.current = currentScrollY; }; window.addEventListener('scroll', handleScroll, { passive: true }); return () => window.removeEventListener('scroll', handleScroll); */ }, []); // Close menu when clicking outside useEffect(() => { const handleClickOutside = (event: MouseEvent) => { // Add a small delay to ensure button click is processed first setTimeout(() => { if (switcherRef.current && !switcherRef.current.contains(event.target as Node)) { setIsOpen(false); } }, 10); }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [isOpen]); const switchLanguage = (newLocale: Locale) => { console.log('Switching language from', locale, 'to', newLocale); console.log('Current pathname:', pathname); try { // Get the current path segments const pathSegments = pathname.split('/').filter(Boolean); console.log('Path segments:', pathSegments); // Remove the current locale from the beginning if it exists if (pathSegments[0] && locales.includes(pathSegments[0] as Locale)) { pathSegments.shift(); } // Construct the new path with the new locale const newPath = `/${newLocale}${pathSegments.length > 0 ? '/' + pathSegments.join('/') : ''}`; console.log('New path:', newPath); // Use replace instead of push to avoid history issues router.replace(newPath); setIsOpen(false); } catch (error) { console.error('Error switching language:', error); // Fallback to simple navigation router.replace(`/${newLocale}`); setIsOpen(false); } }; // Don't render until mounted to avoid hydration issues if (!isMounted) { return null; } return (
{/* Main Switcher Button */} {/* Language Options */} {isOpen && (
Select Language
{locales.map((loc) => ( ))}
{/* Arrow pointing to the button */}
)} {/* Subtle glow effect */}
{/* Floating indicator */}
); }