'use client'; import React, { useEffect, useRef } from 'react'; import { useAppStore } from '@/lib/store'; import { fetchCurrentUser } from '@/lib/api'; import AuthView from '@/components/auth-view'; import AppLayout from '@/components/app-layout'; import CookieConsent from '@/components/cookie-consent'; export default function Home() { const currentUser = useAppStore((s) => s.currentUser); const isLoadingAuth = useAppStore((s) => s.isLoadingAuth); const setCurrentUser = useAppStore((s) => s.setCurrentUser); const hasCheckedRef = useRef(false); useEffect(() => { // Prevent duplicate auth checks: only run once per component lifecycle if (hasCheckedRef.current) return; hasCheckedRef.current = true; async function checkAuth() { try { const user = await fetchCurrentUser(); if (user && !user.error) { setCurrentUser(user); // Sync locale from user preference if (user.locale) { useAppStore.getState().setLocale(user.locale); } } else { setCurrentUser(null); } } catch { setCurrentUser(null); } } checkAuth(); }, []); if (isLoadingAuth) { return (