import type { ReactNode } from "react"; import { useEffect } from "react"; import { ArrowLeft } from "lucide-react"; import { useLocation, useNavigate } from "react-router-dom"; import { Button } from "../ui/button"; type MobileLocationState = { from?: string; }; type MobileDetailHeaderProps = { title: string; subtitle?: string; status?: ReactNode; fallbackTo: string; testId?: string; }; function isSafeMobileSource(value: unknown, currentPath: string) { return typeof value === "string" && value.startsWith("/mobile") && value !== currentPath; } export function MobileDetailHeader({ title, subtitle, status, fallbackTo, testId }: MobileDetailHeaderProps) { const location = useLocation(); const navigate = useNavigate(); const state = location.state as MobileLocationState | null; const currentPath = `${location.pathname}${location.search}`; const from = isSafeMobileSource(state?.from, currentPath) ? state?.from : null; useEffect(() => { const scrollToTop = () => { window.scrollTo({ top: 0, behavior: "auto" }); document.querySelector(".hyb-mobile-shell .overflow-y-auto")?.scrollTo({ top: 0, behavior: "auto" }); }; scrollToTop(); const frameId = window.requestAnimationFrame(scrollToTop); return () => window.cancelAnimationFrame(frameId); }, [currentPath]); const handleBack = () => { navigate(from ?? fallbackTo, { replace: true }); }; return (
{title}
{subtitle ?
{subtitle}
: null}
{status ?
{status}
: null}
); }