59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
"use client";
|
|
|
|
import { usePathname, useRouter } from "next/navigation";
|
|
import { DcNavigationLayout } from "@/shared/ui/dc-navigation-layout";
|
|
import type { MenuItem } from "@/entities/menu";
|
|
import { useMenuStore } from "@/shared/lib";
|
|
import { PageLayout } from "@/shared/ui/page-layout";
|
|
import { useEffect, useRef } from "react";
|
|
|
|
export default function WithBaseLayout({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const viewportRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
const container = viewportRef.current;
|
|
const viewport = window.visualViewport;
|
|
if (!container) return;
|
|
|
|
const updateViewport = () => {
|
|
// Preserve browser zoom; resize the mobile shell only at its normal scale.
|
|
if (viewport && viewport.scale !== 1) return;
|
|
const height = viewport
|
|
? viewport.height + viewport.offsetTop
|
|
: window.innerHeight;
|
|
container.style.setProperty("--dc-viewport-height", `${height}px`);
|
|
};
|
|
updateViewport();
|
|
window.addEventListener("resize", updateViewport);
|
|
viewport?.addEventListener("resize", updateViewport);
|
|
viewport?.addEventListener("scroll", updateViewport);
|
|
return () => {
|
|
window.removeEventListener("resize", updateViewport);
|
|
viewport?.removeEventListener("resize", updateViewport);
|
|
viewport?.removeEventListener("scroll", updateViewport);
|
|
container.style.removeProperty("--dc-viewport-height");
|
|
};
|
|
}, []);
|
|
|
|
const setCurrentMenuInfo = useMenuStore((state) => state.setCurrentMenuInfo);
|
|
|
|
const handleMenuClick = (item: MenuItem) => {
|
|
setCurrentMenuInfo(item);
|
|
router.push(item.urlPath);
|
|
};
|
|
|
|
return (
|
|
<div ref={viewportRef} className="dc-viewport-boundary">
|
|
<DcNavigationLayout pathname={pathname ?? ""} onMenuClick={handleMenuClick}>
|
|
<PageLayout>{children}</PageLayout>
|
|
</DcNavigationLayout>
|
|
</div>
|
|
);
|
|
}
|