import type { Site } from "@cloudhal/core-contracts"; import { entryPath } from "@cloudhal/core-ts/permalinks"; import { DirectionalIcon, InsetPanel, LinkButton, Loader, Select, Text } from "@cloudhal/ui"; import { ArrowSquareOut } from "@phosphor-icons/react"; import { useQuery } from "@tanstack/react-query"; import { useState } from "react"; import { useI18n } from "../../i18n/index.tsx"; import { listingsApi } from "../../lib/api/listings.ts"; import { api } from "../../lib/api.ts"; import { CodeHighlighted } from "../CodeHighlighted.tsx"; import { ErrorBanner } from "../ErrorBanner.tsx"; const RICH_RESULTS = "https://search.google.com/test/rich-results?url="; const LOCAL = /^https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/; /** * The JSON-LD a chosen entry's page emits with the saved preset (`GET …/structured-data`, the * renderer's own builder), why it emits none, and — for a published entry on a site with a domain — * a link to Google's Rich Results Test for its public address. */ export function StructuredDataPreview({ site, collection }: { site: Site; collection: string }) { const { t } = useI18n("listings"); const sd = "listings.structuredData"; const entries = useQuery({ queryKey: ["entries", site.id, collection], queryFn: () => api.entries.list(site.id, collection), }); const [entryId, setEntryId] = useState(null); const preview = useQuery({ queryKey: ["structured-data", site.id, entryId, site.rev], queryFn: () => listingsApi.structuredData(site.id, entryId ?? ""), enabled: entryId !== null, }); const entry = entries.data?.find((e) => e.id === entryId); const list = (entries.data ?? []).slice(0, 100); const origin = preview.data?.origin; const testUrl = entry && origin && !LOCAL.test(origin) && entry.status === "published" ? `${RICH_RESULTS}${encodeURIComponent(`${origin}${entryPath(site, entry)}`)}` : undefined; const issues = preview.data?.issues ?? []; return (
{t(`${sd}.preview`)} {t(`${sd}.previewDescription`)}
{entries.data && list.length === 0 ? ( {t(`${sd}.previewEmpty`)} ) : (