"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useEffect, useState } from "react"; import AccountMenu from "@/components/AccountMenu"; import { useAuth } from "@/components/AuthProvider"; import { businessNavItems } from "@/data/business-nav"; import { en } from "@/lib/i18n/en"; import { getAdminDashboardOrigin, getCustomerDashboardOrigin, } from "@/lib/host"; import { customerDashboardHref } from "@/lib/session-handoff"; import { getDisplayName } from "@/lib/userName"; import { getBusinessProfile } from "@/services/meshkee-businesses"; import styles from "./DashboardShell.module.css"; const websiteUrl = process.env.NEXT_PUBLIC_WEBSITE_URL?.replace(/\/$/, "") || "http://localhost:3000"; export default function BusinessShell({ children, manageMeshkeeId, }: { children: React.ReactNode; /** When set, nav stays on admin host under /manage/[id]/… */ manageMeshkeeId?: string; }) { const pathname = usePathname(); const { user, logout, isSuperAdmin, activeBusinessId } = useAuth(); const [menuOpen, setMenuOpen] = useState(false); const [businessName, setBusinessName] = useState(""); const [businessTitle, setBusinessTitle] = useState(""); const [logoUrl, setLogoUrl] = useState(null); const displayName = getDisplayName(user); const linkedBusiness = user?.businesses.find((item) => item.isOwner) ?? user?.businesses[0]; const meshkeeId = manageMeshkeeId || activeBusinessId || null; const manageBase = manageMeshkeeId ? `/manage/${encodeURIComponent(manageMeshkeeId)}` : null; const publicPath = manageBase ? pathname.replace(manageBase, "") || "/profile" : pathname.startsWith("/business") ? pathname.replace(/^\/business/, "") || "/" : pathname; useEffect(() => { if (!meshkeeId) { setBusinessName(linkedBusiness?.name?.trim() || ""); setBusinessTitle(""); setLogoUrl(null); return; } let cancelled = false; void (async () => { try { const profile = await getBusinessProfile(meshkeeId); if (cancelled) return; const name = profile.profile.nameEn?.trim() || profile.profile.nameFa?.trim() || linkedBusiness?.name?.trim() || ""; setBusinessName(name); setBusinessTitle(profile.profile.title?.trim() || ""); setLogoUrl(profile.profile.logoUrl?.trim() || null); } catch { if (cancelled) return; setBusinessName(linkedBusiness?.name?.trim() || ""); setBusinessTitle(""); setLogoUrl(null); } })(); return () => { cancelled = true; }; }, [meshkeeId, linkedBusiness?.name]); function navHref(itemHref: string) { if (!manageBase) return itemHref; return `${manageBase}${itemHref === "/" ? "/profile" : itemHref}`; } const brandName = businessName || linkedBusiness?.name || "Business"; return (
{menuOpen ? (

{manageMeshkeeId ? "Admin · managing business" : en.workspaceEyebrow}

{en.workspaceHint}

{children}
); }