kontenhumas-fe/components/auto-redirect.tsx

34 lines
851 B
TypeScript
Raw Normal View History

2025-09-23 15:37:43 +00:00
'use client';
2025-09-23 22:21:17 +00:00
import { useEffect, useState } from 'react';
2025-09-23 15:37:43 +00:00
import { useRouter } from '@/components/navigation';
export default function AutoRedirect() {
const router = useRouter();
2025-09-23 22:21:17 +00:00
const [mounted, setMounted] = useState(false);
2025-09-23 15:37:43 +00:00
useEffect(() => {
2025-09-23 22:21:17 +00:00
setMounted(true);
}, []);
useEffect(() => {
if (!mounted) return;
2025-09-23 15:37:43 +00:00
// Get current pathname without locale
const pathname = window.location.pathname;
const segments = pathname.split('/').filter(Boolean);
// Check if first segment is a locale
const locales = ['in', 'en', 'ar'];
const hasLocale = segments.length > 0 && locales.includes(segments[0]);
if (!hasLocale) {
// Redirect to default locale with current path
const newPath = `/in${pathname}`;
router.replace(newPath);
}
2025-09-23 22:21:17 +00:00
}, [mounted, router]);
2025-09-23 15:37:43 +00:00
return null;
}