"use client"; import * as React from "react"; import { Loader2Icon } from "lucide-react"; import type { WidgetSettings } from "@accessibit/types"; import { useSiteAdmin } from "../context"; import { fill } from "../lib/fill"; import { STATEMENT_PAGE } from "../statement/layout"; import { WIDGET_TOUR_ANCHORS } from "../tour"; import { ActionBar } from "../ui/action-bar"; import { Button } from "../ui/button"; import { Notice } from "../ui/notice"; import { PageHeader } from "../ui/page-header"; import { SectionNav } from "../ui/section-nav"; import { WIDGET_SECTIONS, WIDGET_SECTION_PATH, widgetSectionOf, type WidgetSection, } from "./sections"; import { TOOL_UI_TO_OPTION_IDS } from "./settings-adapter"; import { useWidgetSettings } from "./WidgetSettingsProvider"; /** How many tools the panel has: the count beside "Strumenti". */ export const WIDGET_TOOL_COUNT = Object.keys(TOOL_UI_TO_OPTION_IDS).length; /** * What the three tabs of the Widget page share (ACC-547, boards W1, W2, W6, * V4): the title, the host's notice, the tabs, and the bar that holds the * changes not yet saved, whichever tab they were made on. * * A host may mount the real widget here too, on Strumenti only (board W6), * as the live preview of the tools being switched: a tool switched off is * gone from its panel at once. It sits at the bottom right whatever corner * the site uses, and is shown whatever device the site hides it on; Aspetto * draws its own preview of the corner, and Stato none. * The host renders it (`renderPreview`): WordPress frames the site's homepage * with the widget in preview mode (`wp/WpWidgetPreview.tsx`, ACC-404) and * sets it beside the switches (`previewBeside`, ACC-596). The dashboard * passes none: no tab of its loads the real widget (ACC-586, bugfix_6). * * The tab is read off the host's address (`pathname`), each tab's link being * `links.widget` and the tab's path after it. */ export function WidgetFrame({ notice = null, sections = WIDGET_SECTIONS, renderPreview, previewBeside = false, children, }: { /** * What stands under the title: the dashboard's Viewer notice * (`WidgetReaderNotice`) for a reader. WordPress passes its own: its * read-only state is the key's, not a role. */ notice?: React.ReactNode; /** The tabs the host offers, in order: WordPress has no status tab. */ sections?: readonly WidgetSection[]; /** The live widget, given the settings it should show now. */ renderPreview?: (settings: WidgetSettings) => React.ReactNode; /** * The preview is a page of its own to look at, not something the page * draws: on a wide window it takes the right half, pinned while the * switches scroll, so a switch flipped shows in the panel at once; on a * narrow one it comes after them. Otherwise it stands above the tab. */ previewBeside?: boolean; children: React.ReactNode; }) { const { messages, toast, pathname, links } = useSiteAdmin(); const t = messages.widget; const { site, canEdit, config, savedConfig, previewSettings, isDirty, isSaving, error, conflict, save, discard, loadLatest, } = useWidgetSettings(); const base = links?.widget?.(site.id) ?? ""; // The installation first while there is one to do; once the widget is on // the site, what is changed most comes first and the status last (ACC-586, // Widget_1). const tabs = site.isWidgetInstalled ? [ ...sections.filter((section) => section !== "status"), ...sections.filter((section) => section === "status"), ] : sections; // Read off the host's order, not the one shown: an address naming no tab // is the host's first, whatever the installation moved. const current = widgetSectionOf(pathname, sections); const toolsPreview = React.useMemo( () => ({ ...previewSettings, location: { desktop: "bottom-right" as const, mobile: "bottom-right" as const, }, // It is the tools that are previewed: the widget stays in the preview // on a device the site hides it on (ACC-695). visibility: { desktop: true, mobile: true }, }), [previewSettings], ); // The bar leaves the page with the changes it held, and the button that // was pressed with it: the focus goes to the tab the person is on, the // nearest thing still there, instead of falling to the top of the page. const navRef = React.useRef(null); const focusAfterBar = React.useRef(false); const showBar = canEdit && (isDirty || conflict); React.useEffect(() => { if (showBar || !focusAfterBar.current) return; focusAfterBar.current = false; navRef.current ?.querySelector('[aria-current="page"]') ?.focus(); }, [showBar]); const onSave = async () => { if (isSaving) return; focusAfterBar.current = true; const saved = await save(); if (saved) toast.success(t.savedToast); else focusAfterBar.current = false; }; const onDiscard = () => { if (isSaving) return; focusAfterBar.current = true; discard(); }; // The notice and the bar leave together: the focus goes where Discard's does. const onLoadLatest = () => { if (isSaving) return; focusAfterBar.current = true; loadLatest(); }; // What changed among the tools, in the bar's own words (board W6): the // switches are on another tab than the one the person may be looking at. const tools = Object.keys(config.enabledTools); const switchedOff = tools.filter( (tool) => savedConfig.enabledTools[tool] && !config.enabledTools[tool], ).length; const switchedOn = tools.filter( (tool) => !savedConfig.enabledTools[tool] && config.enabledTools[tool], ).length; const toolChanges = [ switchedOff === 1 ? t.unsaved.toolsOffOne : switchedOff > 1 && fill(t.unsaved.toolsOff, { count: switchedOff }), switchedOn === 1 ? t.unsaved.toolsOnOne : switchedOn > 1 && fill(t.unsaved.toolsOn, { count: switchedOn }), ].filter(Boolean); const unsavedTitle = toolChanges.length > 0 ? fill(t.unsaved.titleWithTools, { changes: toolChanges.join(", ") }) : t.unsaved.title; return (
{notice}
({ id: section, label: t.tabs[section], count: section === "tools" ? WIDGET_TOOL_COUNT : undefined, href: `${base}${WIDGET_SECTION_PATH[section]}`, }))} />
{showBar && ( {/* `aria-disabled`, not `disabled`: a button that goes off while it has the focus drops it to the top of the page. */} )} {conflict && ( {t.conflict.message} )} {error && ( {t.saveErrors[error]} )} {current === "tools" && renderPreview && previewBeside ? ( // Both cells as tall as the row, so the preview's own can pin it // inside its cell while the switches scroll (`WpWidgetPreview`).
{/* A container: the groups take one column when this half can't fit two (WidgetToolsTab). */}
{children}
{/* A hairline between the halves, not a border round each: the switches are cards already. */}
{renderPreview(toolsPreview)}
) : ( <> {current === "tools" && renderPreview?.(toolsPreview)} {children} )}
); }