import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { ChevronDown, FileText, Search } from 'lucide-react'; import React, { useEffect, useMemo, useRef, useState } from 'react'; interface PageRoute { id: string; name: string; path: string; } interface PageSwitcherProps { pageRoutes: PageRoute[]; currentRouteId: string; currentRouteName: string; routesWithDraftOps: Set; editPathForRoute: (routeId: string) => string; } /** * Topbar dropdown that lists every editable storefront route. Used both as * a "switch page" affordance and as a quick visual of which routes have * pending draft work (the colored Draft / Live / Current pills). * * Owns its open/search state and the outside-click dismissal — parent only * supplies the data. Switching routes is plain `` navigation * because the user's draft is global (spec § 5.7) — the changeset persists * across routes, so there's no unsaved-work prompt to insert. */ export function PageSwitcher({ pageRoutes, currentRouteId, currentRouteName, routesWithDraftOps, editPathForRoute }: PageSwitcherProps): 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 filtered = useMemo(() => { const q = search.trim().toLowerCase(); if (!q) return pageRoutes; return pageRoutes.filter( (r) => r.name.toLowerCase().includes(q) || (r.path ?? '').toLowerCase().includes(q) ); }, [pageRoutes, search]); 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')} />
{_('Pages')}
)}
); }