import { drawerInputClass } from '@components/common/page-builder/drawer/index.js'; import { BlogPicker } from '@components/common/page-builder/pickers/BlogPicker.js'; import { CategoryPicker } from '@components/common/page-builder/pickers/CategoryPicker.js'; import { LandingPagePicker } from '@components/common/page-builder/pickers/LandingPagePicker.js'; import { PagePicker } from '@components/common/page-builder/pickers/PagePicker.js'; import { ProductPicker } from '@components/common/page-builder/pickers/ProductPicker.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { BlogUrn, CatalogUrn, CmsUrn, PromotionUrn, UrnService } from '@evershop/evershop/lib/urn'; import { isSafeUrl } from '@evershop/evershop/lib/util/safeUrl'; import React, { useState } from 'react'; /** * Composite "where does this link go?" picker for widget CTAs and tile * links. Tabs: Page · Category · Product · Collection · Custom URL. * * Storage format: * - Internal entities → URN: `urn:evershop::` (id is uuid * for page/category/product, code for collection). The storefront * resolves URNs at request time via per-request batched loaders, * so the rendered link reflects the entity's *current* URL — if a * category is renamed, every widget linking to it follows along. * - Custom URL → plain string (unchanged passthrough). * * `kind` is an admin-only hint helping the picker re-open on the tab * the merchant last used; the URN encodes it too, so kind is derivable * on re-edit even when not stored separately. */ type ParsedLinkUrn = { kind: Exclude; id: string }; // Map a URN's (service, type) to the LinkPicker tab kind. Returns null // for non-link URNs (e.g. widget_instance) so they fall back to custom-URL. function parseUrn(value: string | undefined | null): ParsedLinkUrn | null { if (!value || !UrnService.isValid(value)) return null; const { service, type, uuid } = UrnService.parse(value); if (service === 'catalog' && type === 'product') return { kind: 'product', id: uuid }; if (service === 'catalog' && type === 'category') return { kind: 'category', id: uuid }; if (service === 'cms' && type === 'page') return { kind: 'page', id: uuid }; if (service === 'promotion' && type === 'landing_page') return { kind: 'landingPage', id: uuid }; if (service === 'blog' && type === 'post') return { kind: 'blogPost', id: uuid }; if (service === 'blog' && type === 'category') return { kind: 'blogCategory', id: uuid }; if (service === 'blog' && type === 'tag') return { kind: 'blogTag', id: uuid }; return null; } export type LinkKind = | 'page' | 'landingPage' | 'category' | 'product' | 'blogPost' | 'blogCategory' | 'blogTag' | 'custom'; const TABS: { value: LinkKind; label: string }[] = [ { value: 'page', label: _('Page') }, { value: 'landingPage', label: _('Landing page') }, { value: 'category', label: _('Category') }, { value: 'product', label: _('Product') }, { value: 'blogPost', label: _('Blog post') }, { value: 'blogCategory', label: _('Blog category') }, { value: 'blogTag', label: _('Blog tag') }, { value: 'custom', label: _('Custom URL') } ]; // The blog home is a fixed storefront route (`/blog`) with no entity id, so it // can't be a URN like the other blog links — it's stored as this static path // and surfaced as a pinned shortcut at the top of the Blog category picker. const BLOG_HOME_PATH = '/blog'; const BLOG_HOME_ID = '__blog_home__'; // Same shape for the all-products listing: a fixed route, no entity, so no URN. // Pinned at the top of the Category picker because "every product in the store" // is what a shopper reads as the widest category. const ALL_PRODUCTS_PATH = '/products'; const ALL_PRODUCTS_ID = '__all_products__'; // Emitted as `custom`, NOT `category`, even though the row lives on the Category // tab. Consumers map `kind` onto a legacy `{type, uuid}` pair — BasicMenuSetting // does — and its `toLinkValue` turns `type: 'category'` plus any truthy uuid // back into `CatalogUrn.category(uuid)`. With a path instead of a uuid that // synthesizes `urn:evershop:catalog:category:/products`: a URN that parses, so // the picker stops highlighting the row (it looks unselectable) and the // storefront resolves it to a dead link. `custom` is also simply true — the // stored value is a plain path. The `/blog` home shortcut escapes this only // because `blogCategory` is not one of the three legacy types. export const ALL_PRODUCTS_KIND: LinkKind = 'custom'; export interface LinkPickerProps { value: string; onChange: (next: { url: string; kind: LinkKind; label?: string }) => void; /** Initial tab. Defaults to "custom" so the freeform path is the fastest. */ initialKind?: LinkKind; /** When non-collection links are out of scope (e.g. a collection-only CTA), * hide the other tabs. */ allowedKinds?: LinkKind[]; } export function LinkPicker({ value, onChange, initialKind = 'custom', allowedKinds }: LinkPickerProps) { const visibleTabs = allowedKinds ? TABS.filter((t) => allowedKinds.includes(t.value)) : TABS; // If the stored value is a URN, the kind is derivable from it — open // on that tab regardless of the explicit `initialKind` hint. const parsed = parseUrn(value); const effectiveInitial: LinkKind = parsed ? parsed.kind : value === BLOG_HOME_PATH ? 'blogCategory' : value === ALL_PRODUCTS_PATH ? 'category' : initialKind; const [tab, setTab] = useState( visibleTabs.find((t) => t.value === effectiveInitial)?.value ?? visibleTabs[0].value ); return (
{visibleTabs.map((t) => { const active = tab === t.value; return ( ); })}
{tab === 'page' && ( onChange({ url: CmsUrn.page(r.uuid), kind: 'page', label: r.name }) } /> )} {tab === 'landingPage' && ( onChange({ url: PromotionUrn.landingPage(r.uuid), kind: 'landingPage', label: r.name }) } /> )} {tab === 'category' && ( onChange({ url: ALL_PRODUCTS_PATH, kind: ALL_PRODUCTS_KIND, label: _('All products') }) } onPick={(r) => onChange({ url: CatalogUrn.category(r.uuid), kind: 'category', label: r.name }) } /> )} {tab === 'product' && ( onChange({ url: CatalogUrn.product(r.uuid), kind: 'product', label: r.name }) } /> )} {tab === 'blogPost' && ( onChange({ url: BlogUrn.post(r.uuid), kind: 'blogPost', label: r.name }) } /> )} {tab === 'blogCategory' && ( onChange({ url: BLOG_HOME_PATH, kind: 'blogCategory', label: _('Blog Home') }) } onPick={(r) => onChange({ url: BlogUrn.category(r.uuid), kind: 'blogCategory', label: r.name }) } /> )} {tab === 'blogTag' && ( onChange({ url: BlogUrn.tag(r.uuid), kind: 'blogTag', label: r.name }) } /> )} {tab === 'custom' && (
onChange({ url: e.target.value, kind: 'custom' }) } placeholder="/c/sale or https://example.com" className={drawerInputClass} /> {!parsed && value && !isSafeUrl(value) ? (
{_( 'Only http(s), mailto, tel or relative links are allowed — this link will be ignored on the storefront.' )}
) : (
{_('Paste a URL or a relative path starting with ')} /.
)}
)}
); }