import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { ChevronDown, FileText, Globe, Search } from 'lucide-react'; import React, { useEffect, useMemo, useRef, useState } from 'react'; interface ScopePage { uuid: string; name: string; urlKey: string; urn: string; /** Published flag; `false` renders a "Draft" badge. Null/undefined = no badge. */ status?: boolean | null; } interface EntityScopeSelectorProps { pages: ScopePage[]; /** The active scope URN (`urn:evershop:cms:page:`), or null for All. */ currentEntityUrn: string | null; /** Label for the route-level ("all entities") option, e.g. "All CMS pages". */ allLabel: string; /** Builds the editor URL for a given entity uuid (or null for the All scope). */ scopeHref: (entityUuid: string | null) => string; loading?: boolean; } /** * Topbar dropdown that scopes the page-builder to a single entity (e.g. one * CMS page) vs. the whole route ("All CMS pages"). Sits beside the route * `PageSwitcher`: the route switcher changes *which route* is edited, this * changes *which entity within the route* placements are scoped to. * * Selection is plain `` navigation (like PageSwitcher) — the draft is * global (spec § 5.7), so switching scope preserves all pending work. Owns its * own open/search state + outside-click dismissal; the parent only supplies data. */ export function EntityScopeSelector({ pages, currentEntityUrn, allLabel, scopeHref, loading = false }: EntityScopeSelectorProps): React.ReactElement { const [open, setOpen] = useState(false); const [search, setSearch] = useState(''); const wrapperRef = useRef(null); useEffect(() => { if (!open) return; const onDown = (e: MouseEvent) => { const node = wrapperRef.current; if (!node) return; if (!node.contains(e.target as Node)) setOpen(false); }; window.addEventListener('mousedown', onDown); return () => window.removeEventListener('mousedown', onDown); }, [open]); const currentPage = useMemo( () => currentEntityUrn ? pages.find((p) => p.urn === currentEntityUrn) ?? null : null, [pages, currentEntityUrn] ); const filtered = useMemo(() => { const q = search.trim().toLowerCase(); if (!q) return pages; return pages.filter( (p) => p.name.toLowerCase().includes(q) || (p.urlKey ?? '').toLowerCase().includes(q) ); }, [pages, search]); // The label reflects the active scope: All, the matched page, or — when the // URN doesn't match any listed page (e.g. a draft page not in the published // list) — a neutral fallback so the control never renders blank. const activeLabel = currentEntityUrn ? currentPage?.name ?? _('This page') : allLabel; return (
{open && (
setSearch(e.target.value)} placeholder={_('Search pages…')} className="w-full text-sm pl-7 pr-2 py-1 rounded-md bg-muted/30 border border-divider focus:outline-none focus:ring-1 focus:ring-primary" aria-label={_('Search pages')} />
{/* Route-level ("all entities") option — always visible, ignores search. */}
{_('Pages')}
)}
); }