"use client" /** * The one scope (school > program / brand > site > location) picker menu. * * This body was previously copy-pasted into `UtilityBarSchoolSwitcher` and the * sidebar's `TeamSwitcher`, each with its own `useState` — so the two chrome * surfaces could show different programs at the same time. Both now render * this component over shared `useActiveScope` state, which the products home * writes to before the user has even entered the product. * * Only the *trigger* differs per surface: an avatar circle in the utility bar, * a full row in the sidebar, a card row on the products home. The menu itself * is identical everywhere, so it lives here once. */ import * as React from "react" import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" import { DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, } from "@/components/ui/dropdown-menu" import type { Product } from "@/contexts/product-context" import { useActiveScope } from "@/hooks/use-active-scope" import type { ScopeChild, ScopeConfig, ScopeParent } from "@/lib/scope-switcher" import { cn } from "@/lib/utils" export interface ScopeSwitcherBag { config: ScopeConfig parent: ScopeParent child: ScopeChild subView: "main" | "parents" /** Pass to `DropdownMenu` so the menu reopens on the main view. */ onOpenChange: (open: boolean) => void showParents: () => void showMain: () => void selectParent: (parent: ScopeParent) => void selectChild: (child: ScopeChild) => void /** `" · . "` — for `aria-label`. */ ariaLabel: string } /** Shared state + handlers for any trigger that opens `ScopeSwitcherMenuBody`. */ export function useScopeSwitcher(product: Product, customIndex?: number): ScopeSwitcherBag { const { config, parent, child, selectParent, selectChild } = useActiveScope( product, customIndex, ) const [subView, setSubView] = React.useState<"main" | "parents">("main") return { config, parent, child, subView, onOpenChange: open => { if (!open) setSubView("main") }, showParents: () => setSubView("parents"), showMain: () => setSubView("main"), selectParent: next => { selectParent(next) setSubView("main") }, selectChild, ariaLabel: `${parent.name} · ${child.name}. ${config.ariaSuffix}`, } } /** Menu contents. Render inside a `DropdownMenuContent`. */ export function ScopeSwitcherMenuBody({ scope }: { scope: ScopeSwitcherBag }) { const { config, parent, child, subView, showParents, showMain } = scope if (subView === "parents") { return ( <> { e.preventDefault() showMain() }} >