/**
* The Advanced section's rows about how search results may show the page:
* its image preview size, the text under its link, its images in image
* search, a date to drop from search, and, closed away, the controls only
* other search engines read. Each is Google's, from its robots meta tag
* page; the words say what each costs, since a mistake here is quiet.
*
* @module admin/display-rows
*/
import type { PageImagePreview } from "@nextlyhq/plugin-sdk";
import {
Input,
navigateTo,
SegmentedChoice,
Switch,
} from "@nextlyhq/plugin-sdk/admin";
import { ChevronRight } from "lucide-react";
import { useId, useState, type ReactNode } from "react";
import { SETTINGS_PAGE_HREF } from "../settings/contract";
import type { AdvancedProps } from "./advanced";
import { Chip, cx, LinkAction, Note, SourceRow } from "./parts";
import { keyOf, Row, RowButton, useOutsideChange } from "./rows";
/** Where the site's default for image previews is set. */
const SITE_DEFAULT_HREF = `${SETTINGS_PAGE_HREF}?tab=crawlers`;
/** The names of the `seo` group's display fields, which the rows are offered for. */
export const DISPLAY_FIELDS = [
"maxImagePreview",
"maxSnippet",
"nosnippet",
"noimageindex",
"unavailableAfter",
"noarchive",
"nocache",
] as const;
/** Whether the group declares `name`, or is not known yet. */
function declares(props: AdvancedProps, name: string): boolean {
return props.context === undefined || props.context.fields.includes(name);
}
/** The rows, under their heading, for the display fields the group declares. */
export function DisplayRows(props: AdvancedProps): ReactNode {
const has = (name: string) => declares(props, name);
return (
<>
How search results may show this page
{has("maxImagePreview") ? : null}
{has("maxSnippet") || has("nosnippet") ? : null}
{has("noimageindex") ? : null}
{has("unavailableAfter") ? : null}
{has("noarchive") || has("nocache") ? (
) : null}
>
);
}
/** A link within the admin, opened as the admin's own links are. */
function AdminLink({
href,
children,
}: {
href: string;
children: ReactNode;
}): ReactNode {
return (
{
// A new tab or window is the browser's to open.
if (event.metaKey || event.ctrlKey || event.shiftKey) return;
event.preventDefault();
navigateTo(href);
}}
>
{children}
);
}
const SIZE_OPTIONS = [
{ value: "large", label: "Large" },
{ value: "standard", label: "Standard" },
{ value: "none", label: "None" },
] as const;
/** What each size the page chose for itself means. */
const SIZE_MEANS: Readonly> = {
large:
"Google may show a big picture from this page in results and in Discover.",
standard:
"Google may show a picture of the usual size from this page in results and in Discover.",
none: "Google shows no picture from this page in results or in Discover.",
};
function ImagePreviewRow({
answer,
context,
writable,
errors,
onSet,
}: AdvancedProps): ReactNode {
const id = useId();
const preview = answer?.display?.imagePreview;
// The size the site's default gives, which a page need not store.
const siteSize =
context?.levels.site.largeImagePreviews === false ? undefined : "large";
return (
onSet("maxImagePreview", next === siteSize ? null : next)
}
/>
}
>
{preview?.from === "page" ? (
Set for this page{SIZE_MEANS[preview.value]} onSet("maxImagePreview", null)}
>
Go back to the site's default
) : (
Automatic
{siteSize === undefined
? "Google's own size: large previews are off in "
: "the site's default, set in "}
Settings › Search engines and AI
)}
);
}
const SNIPPET_OPTIONS = [
{ value: "any", label: "Any length" },
{ value: "shorter", label: "Shorter" },
{ value: "none", label: "None" },
] as const;
/** Why typed characters are not a limit, or none where they are one. */
function snippetProblem(text: string): string | undefined {
if (text.trim() === "") return undefined;
if (!/^\d+$/.test(text.trim())) return "Type a whole number of characters.";
if (Number(text) === 0) {
return '0 means no text at all. Choose "None" for that, so the warning about AI Overviews shows.';
}
return undefined;
}
/** The limit typed, as stored: a whole number above 0, else none. */
function limitOf(text: string): number | null {
return snippetProblem(text) === undefined && text.trim() !== ""
? Number(text)
: null;
}
/**
* The text under the link: any length, a limit in characters, or none. A
* limit of 0 is refused and points to None, the one choice that says what
* hiding the text costs.
*/
function SnippetRow(props: AdvancedProps): ReactNode {
const { values, writable, errors, onSet } = props;
const id = useId();
// Each choice only where the group declares the field it writes, so the
// tab never sets a directive a site's own `seo` fields leave out.
const offered = SNIPPET_OPTIONS.filter(
option =>
option.value === "any" ||
declares(props, option.value === "shorter" ? "maxSnippet" : "nosnippet")
);
const stored =
typeof values.maxSnippet === "number" ? values.maxSnippet : undefined;
const none = values.nosnippet === true;
// Shorter stays chosen while its number is being typed, before one is kept.
const [shorter, setShorter] = useState(stored !== undefined);
const [text, setText] = useState(stored === undefined ? "" : String(stored));
const wrote = useOutsideChange(
keyOf(values.maxSnippet, values.nosnippet),
() => {
setShorter(typeof values.maxSnippet === "number");
setText(
typeof values.maxSnippet === "number" ? String(values.maxSnippet) : ""
);
}
);
const write = (maxSnippet: number | null, nosnippet: boolean) => {
wrote(keyOf(maxSnippet, nosnippet));
if (declares(props, "maxSnippet") && maxSnippet !== (stored ?? null)) {
onSet("maxSnippet", maxSnippet);
}
if (declares(props, "nosnippet") && nosnippet !== none) {
onSet("nosnippet", nosnippet);
}
};
const choose = (next: string) => {
setShorter(next === "shorter");
if (next === "any") setText("");
write(next === "shorter" ? limitOf(text) : null, next === "none");
};
const chosen = none ? "none" : shorter ? "shorter" : "any";
return (
}
>
{chosen === "shorter" ? (
{
setText(next);
// Only a limit is kept: what is not one stays in the field with
// its reason, and the stored limit stays until a valid one, or
// Any length, replaces it.
if (snippetProblem(next) === undefined) write(limitOf(next), false);
}}
/>
) : null}
{chosen === "none" ? : null}
);
}
/** The number of characters a shorter text may hold, and what it means. */
function ShorterLimit({
text,
writable,
onType,
}: {
text: string;
writable: boolean;
onType: (text: string) => void;
}): ReactNode {
const id = useId();
const problem = snippetProblem(text);
return (
<>
onType(event.target.value)}
/>
characters at most
{problem ??
"Google may still show less. Any length is right for most pages."}
>
);
}
/**
* What no text under the link costs. Google's robots meta tag page: nosnippet
* applies to every result, AI Overviews and AI Mode included, and keeps the
* page from being a direct input to them.
*/
function NoSnippetWarning(): ReactNode {
return (
No text will show under this page's link in any Google result.
{" "}
Google also won't use the page as a direct source in AI Overviews or
AI Mode. The page itself stays in search. To keep one passage out instead,
turn on "Keep out of search previews" for that section in Page
Builder.
);
}
function ImageSearchRow({
values,
writable,
errors,
onSet,
}: AdvancedProps): ReactNode {
const label = "Show this page's images in image search";
return (
onSet("noimageindex", !shown)}
/>
}
/>
);
}
/** A stored moment as a `datetime-local` value in UTC: `2026-12-31T23:59`. */
function inputValueOf(value: unknown): string {
const at =
value instanceof Date
? value.getTime()
: typeof value === "string"
? Date.parse(value)
: Number.NaN;
return Number.isNaN(at) ? "" : new Date(at).toISOString().slice(0, 16);
}
/** A `datetime-local` value, read as UTC, as the moment stored; none for blank. */
function storedMomentOf(input: string): string | null {
if (input === "") return null;
const at = Date.parse(`${input}:00.000Z`);
return Number.isNaN(at) ? null : new Date(at).toISOString();
}
/**
* A date and time, in UTC, after which Google stops showing the page. The
* page stays online; once the date passes it is hidden from search, which
* the row and the page's problems say.
*/
function DropDateRow({
values,
answer,
writable,
errors,
onSet,
}: AdvancedProps): ReactNode {
const id = useId();
const held = inputValueOf(values.unavailableAfter);
const [setting, setSetting] = useState(held !== "");
const wrote = useOutsideChange(keyOf(values.unavailableAfter), () =>
setSetting(inputValueOf(values.unavailableAfter) !== "")
);
const write = (next: string | null) => {
wrote(keyOf(next));
onSet("unavailableAfter", next);
};
const passed = answer?.display?.unavailableAfter?.passed === true;
return (
{
setSetting(false);
write(null);
}}
>
Remove the date
) : (
setSetting(true)}>
Set a date
)
}
>
{setting ? (
<>
write(storedMomentOf(event.target.value))}
/>
{passed ? (
That date has passed. Google may already have dropped the page.
Remove the date to bring it back.
) : (
Times are UTC. To take the page down, unpublish it instead.
)}
>
) : undefined}
);
}
/** What is switched on among the controls for other search engines. */
function otherEnginesSummary(noarchive: boolean, nocache: boolean): string {
if (noarchive && nocache) return "noarchive and nocache on";
if (noarchive) return "noarchive on";
if (nocache) return "nocache on";
return "Both off. Google ignores them.";
}
/**
* `noarchive` and `nocache`, closed away and off by default, each its own
* switch. Google uses neither; what they do elsewhere is not described here.
*/
function OtherEnginesRows({
values,
context,
writable,
errors,
onSet,
}: AdvancedProps): ReactNode {
const id = useId();
const noarchive = values.noarchive === true;
const nocache = values.nocache === true;
// Open where one is on, so what is set is in view.
const [open, setOpen] = useState(noarchive || nocache);
const has = (name: string) =>
context === undefined || context.fields.includes(name);
return (