34 lines
851 B
TypeScript
34 lines
851 B
TypeScript
'use client';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
import { useRouter } from '@/components/navigation';
|
|
|
|
export default function AutoRedirect() {
|
|
const router = useRouter();
|
|
const [mounted, setMounted] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setMounted(true);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!mounted) return;
|
|
|
|
// 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);
|
|
}
|
|
}, [mounted, router]);
|
|
|
|
return null;
|
|
}
|