import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; export interface SearchSnippetPreviewProps { /** URL slug (the url_key). */ urlKey?: string; /** Meta title. Falls back to `name`, then a placeholder. */ metaTitle?: string; /** Meta description. */ metaDescription?: string; /** Path prefix before the slug, e.g. "/blog/" or "/blog/category/". */ pathPrefix?: string; /** Entity name, used to derive the slug/title when those are blank. */ name?: string; } function slugify(text: string): string { return text .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); } /** * A Google-style search-result (SERP) snippet preview for SEO editing. Shows the * breadcrumb URL, title and description as a search engine would render them. * The title links to the live page in a new tab. */ export function SearchSnippetPreview({ urlKey, metaTitle, metaDescription, pathPrefix = '/', name }: SearchSnippetPreviewProps) { // Resolve the origin on the client so the link points at this store and the // breadcrumb shows the real domain (SSR-safe: empty until mounted). const [origin, setOrigin] = React.useState(''); React.useEffect(() => { setOrigin(window.location.origin); }, []); const slug = (urlKey || '').trim() || slugify(name || ''); const path = `${pathPrefix}${slug}`; const href = `${origin}${path}`; const host = origin ? origin.replace(/^https?:\/\//, '') : ''; const title = (metaTitle || '').trim() || (name || '').trim() || _('Untitled page'); const description = (metaDescription || '').trim(); const crumbs = path.split('/').filter(Boolean); return (
{description || _('Add a meta description to control the text shown here.')}