diff --git a/frontend/application/app/ClientLayout.tsx b/frontend/application/app/ClientLayout.tsx new file mode 100644 index 0000000..0034071 --- /dev/null +++ b/frontend/application/app/ClientLayout.tsx @@ -0,0 +1,106 @@ +"use client"; + +import { useState, useEffect } from "react"; +import Header from "../components/Header"; +import Footer from "../components/Footer"; +import { NetworkBanner } from "../components/NetworkBanner"; +import { Toaster } from "sonner"; +import { usePathname, useRouter } from 'next/navigation'; + +import CartDrawer from "../components/CartDrawer"; +import LoginModal from "../components/LoginModal"; +import B2BPortal from "../components/B2BPortal"; +import { useUserStore } from "../lib/store/userStore"; +import { useSettingsStore } from "../lib/store/settingsStore"; + +export default function ClientLayout({ children }: { children: React.ReactNode }) { + const [isCartOpen, setIsCartOpen] = useState(false); + const [showB2B, setShowB2B] = useState(false); + const [showLoginModal, setShowLoginModal] = useState(false); + const [advisorData, setAdvisorData] = useState(null); + + const pathname = usePathname(); + const router = useRouter(); + + const { fetchProfile } = useUserStore(); + const fetchSettings = useSettingsStore(state => state.fetchSettings); + + useEffect(() => { + fetchSettings(); + const token = localStorage.getItem('accessToken'); + if (token) { + fetchProfile().catch(e => console.error("Auth init failed:", e)); + } + }, [fetchProfile, fetchSettings]); + + // Derived currentView from pathname for Header + let currentView = "home"; + if (pathname?.includes("/shop")) currentView = "shop"; + if (pathname?.includes("/wiki")) currentView = "wiki"; + if (pathname?.includes("/profile")) currentView = "profile"; + if (pathname?.includes("/checkout")) currentView = "checkout"; + if (pathname?.includes("/dashboard")) currentView = "user-dashboard"; + + const handleNavigate = (v: any) => { + if (typeof v === 'object') { + if (v.view === 'profile') router.push('/profile'); + if (v.view === 'order-tracking') router.push('/order/tracking'); + if (v.view === 'home') router.push('/'); + } else { + if (v === 'home') router.push('/'); + else if (v === 'user-dashboard') router.push('/dashboard'); + else router.push(`/${v}`); + } + }; + + const navigateToShop = (category: string = "all", search: string = "") => { + const params = new URLSearchParams(); + if (category && category !== "all") params.set("category", category); + if (search) params.set("search", search); + const newUrl = params.toString() ? `/shop?${params.toString()}` : "/shop"; + router.push(newUrl); + }; + + const handleSearch = (q: string) => { + router.push(`/search?q=${encodeURIComponent(q)}`); + }; + + return ( + <> + +
setIsCartOpen(true)} + onSearch={handleSearch} + onB2BOpen={() => setShowB2B(true)} + /> +
+ {children} +
+