'use client' import { PageContainer } from '@/components/layout/FeaturePage' import { useEffect, useRef, useState } from 'react' import { browserStorage } from '@/shared/storage' import { usePathname, useRouter } from 'next/navigation' import { ArrowLeft, Menu } from 'lucide-react' import { useTranslation } from 'react-i18next' import SettingsNav from './SettingsNav' import { SettingsToolbar } from './SettingsToolbar' import { SettingsLoadStatusBanner } from './SettingsLoadStatusBanner' import { SETTINGS_RETURN_KEY } from './SettingsReturnTracker' import Modal from '@/components/common/Modal' import { useSettings } from '@/features/settings/store/SettingsStore' export default function SettingsMain({ children }: { children: React.ReactNode }) { const { t } = useTranslation() const pathname = usePathname() const router = useRouter() const { hasUnsavedChanges, saveDraft, saving, applying, toast, setActiveSection } = useSettings() const [mobileOpen, setMobileOpen] = useState(false) const [leaving, setLeaving] = useState(false) const [leaveFailed, setLeaveFailed] = useState(false) const scroller = useRef(null) const content = useRef(null) const priorPath = useRef(pathname) const section = pathname.split('/')[2] || 'general' useEffect(() => { setActiveSection(section) scroller.current?.scrollTo({ top: 0, behavior: 'instant' }) if (priorPath.current !== pathname) content.current?.focus({ preventScroll: true }) priorPath.current = pathname }, [pathname, section, setActiveSection]) const returnToApp = () => { let destination = '/' try { const stored = browserStorage.readRaw('session', SETTINGS_RETURN_KEY) if (stored?.startsWith('/') && !stored.startsWith('//') && !stored.startsWith('/settings')) destination = stored } catch { /* Home is a safe fallback. */ } router.push(destination) } const returnButton = ( ) return (
{returnButton}
{children}
setMobileOpen(false)} title={t('Settings')} width="sm" >
setMobileOpen(false)} />
{ if (!saving) setLeaving(false) }} title={t('Keep your changes?')} width="sm" >
{leaveFailed && (

{toast}

)}

{t( 'Save a draft before leaving. You can return and apply it later; your current settings will keep working.' )}

) }