"use client"; import * as React from "react"; import type { TrackerTier } from "@accessibit/types"; import type { SiteAdminClient } from "./client"; import type { SiteAdminMessages, ViewsPurchaseWords } from "./messages"; import type { PageviewAddOnOffer, PageviewNotice, } from "./widget/PageviewLimitNotice"; /** Shows a short message to the user. The host decides how. */ export interface SiteAdminToast { success: (message: string) => unknown; error: (message: string) => unknown; warning: (message: string) => unknown; info: (message: string) => unknown; } export type DateInput = Date | string | number; /** * Dates and numbers, as the host writes them. The dashboard's come from its * `lib/locale` and its display timezone; the WordPress bundle's from `Intl`. */ export interface SiteAdminFormat { /** The UI language as an `Intl` tag (`it-IT`), for lists and numbers. */ intlLocale: string; /** * An instant, in the viewer's timezone unless `options.timeZone` names * one; a medium date ("12 ott 2026") unless `options` asks for more. */ date: (date: DateInput, options?: Intl.DateTimeFormatOptions) => string; /** A day the customer is told: Italy's calendar, whoever renders it. */ customerDate: ( date: DateInput, options?: Intl.DateTimeFormatOptions, ) => string; /** A count, its thousands grouped in every language. */ count: (value: number) => string; currency: ( value: number, currency: string, options?: Omit, ) => string; /** * A price in euros read at a glance: its cents only when there are any. * Only a host that names prices passes it (the dashboard, whose views * notice prices the blocks it sells); unset, `currency` writes them. */ euro?: (value: number) => string; } /** The two products a plan can be chosen for in the host's wizard. */ export type SiteAdminProduct = "widget" | "tracker"; /** * Where the pages link to. Each is the host's own address; one the host * leaves out is not drawn (WordPress has no support page, plans or billing). */ export interface SiteAdminLinks { support?: string; /** Subscriptions and invoices. */ billing?: string; /** What we sell, for a site. */ products?: (siteId: string) => string; /** The wizard, opened on a product for a site. */ choosePlan?: (product: SiteAdminProduct, siteId: string) => string; /** * Where a site's audits are listed (the statement page's picker): which * page that is depends on its Tracker tier. */ audits?: (siteId: string, trackerTier: TrackerTier | null) => string; /** * The widget page, whose tabs link to this address and `/status`, * `/appearance` or `/tools` after it (`WIDGET_SECTION_PATH`). Which tab shows is read off * the end of `pathname` alone (`widgetSectionOf`). The dashboard gives its * route; WordPress `#`, so its tabs are `#/tools`. */ widget?: (siteId: string) => string; } /** * The account looking at the page, as far as plans and billing go. Absent * (WordPress), nothing is offered that depends on it. */ export interface SiteAdminAccount { companyName: string | null; /** Administrators and owners: subscriptions and invoices. */ canSeeBilling: boolean; /** Whoever manages the subscription: the wizard. */ canChoosePlan: boolean; } /** How the account is offered a plan. */ export interface SiteAdminPlansCustomer { /** A customer from before the plans (ACC-542): offered a sales rep. */ prePlans: boolean; /** Offered the sales contact: a member and up. */ canAskSales: boolean; } export interface SiteAdminContactSalesProps { /** The sites the request is about. */ sites: string[]; variant?: "outline" | "default"; size?: "default" | "lg"; } /** What the host's view-block commands are drawn for. */ export interface SiteAdminViewsPurchaseProps { notice: PageviewNotice; /** The notice's `addOn`: the blocks offered, and how they are bought. */ offer: PageviewAddOnOffer; /** * Drawn as the notice's main command: false when the plan above, cheaper * than the blocks the year needs, comes first. */ primary: boolean; } /** * Selling view blocks from the views notice. Only the dashboard can (a card * on file, its own purchase route, its sales inbox), and only to whoever * manages the subscription, so it passes this for them alone. Unset * (WordPress, anyone else), the notice offers no block and its sentence * says nothing of them. */ export interface SiteAdminViewsPurchase { /** The sentence's words when it points at blocks bought by card. */ words: ViewsPurchaseWords; /** The command that buys blocks by card (`offer.buy === "card"`). */ renderBuy: (props: SiteAdminViewsPurchaseProps) => React.ReactNode; /** The command that asks sales for blocks, on a plan with no card. */ renderAskSales: (props: SiteAdminViewsPurchaseProps) => React.ReactNode; } /** * Everything host-specific, injected once. Nothing under this package imports * from the dashboard or from WordPress: the dashboard wraps the components * with its Next adapters, the WordPress bundle with its own. * * Internal links are drawn with the host's router through `ui/link.tsx` * (`UiLinkProvider`), which the dashboard mounts once at its root: the * primitives need it on pages that have no SiteAdminProvider. */ export interface SiteAdminAdapters { client: SiteAdminClient; messages: SiteAdminMessages; /** The UI language (`en`, `it`, `es`); the statement editor opens on it. */ locale: string; toast: SiteAdminToast; format: SiteAdminFormat; /** * Re-reads whatever the host rendered from server state: after a save, and * when the widget page checks the site again. */ refresh: () => void; /** The address shown, which tells the widget page its tab. */ pathname?: string; links?: SiteAdminLinks; account?: SiteAdminAccount; /** * A hook, called only by what offers a plan: the dashboard reads it off its * whole shell, which the rest of the pages do not need. `site` adds what * the page knows of its own site. Unset, nobody is a customer from before * the plans. */ usePlansCustomer?: (site?: { hasBoughtAudits?: boolean; }) => SiteAdminPlansCustomer; /** The host's "contact sales" button; unset, it is not offered. */ renderContactSales?: (props: SiteAdminContactSalesProps) => React.ReactNode; /** The host's view-block purchase; unset, none is offered. */ viewsPurchase?: SiteAdminViewsPurchase; /** * Where popovers and dialogs are portalled. Unset means `document.body`; * the WordPress bundle points it inside its scoped root, or its styles would * not reach them. */ portalContainer?: HTMLElement | null; } const SiteAdminContext = React.createContext(null); export function SiteAdminProvider({ children, ...adapters }: React.PropsWithChildren) { const { client, messages, locale, toast, format, refresh, pathname, links, account, usePlansCustomer, renderContactSales, viewsPurchase, portalContainer, } = adapters; // Memoized on the adapters themselves: effects in the pages depend on // `toast` and `messages`, and a fresh context object per render would // re-run them forever. const value = React.useMemo( () => ({ client, messages, locale, toast, format, refresh, pathname, links, account, usePlansCustomer, renderContactSales, viewsPurchase, portalContainer, }), [ client, messages, locale, toast, format, refresh, pathname, links, account, usePlansCustomer, renderContactSales, viewsPurchase, portalContainer, ], ); return ( {children} ); } export function useSiteAdmin(): SiteAdminAdapters { const ctx = React.useContext(SiteAdminContext); if (!ctx) { throw new Error("useSiteAdmin must be used inside a SiteAdminProvider"); } return ctx; } const NOT_PRE_PLANS: SiteAdminPlansCustomer = { prePlans: false, canAskSales: false, }; const useNoPlansCustomer = () => NOT_PRE_PLANS; /** The host's `usePlansCustomer`, or nobody from before the plans. */ export function usePlansCustomer(site?: { hasBoughtAudits?: boolean; }): SiteAdminPlansCustomer { const useHost = useSiteAdmin().usePlansCustomer ?? useNoPlansCustomer; return useHost(site); } export function useMessages(): SiteAdminMessages { return useSiteAdmin().messages; } /** * The portal container, or undefined outside a provider: the primitives under * `ui/` are also the dashboard's own, used on pages that never mount one. */ export function usePortalContainer(): HTMLElement | undefined { return React.useContext(SiteAdminContext)?.portalContainer ?? undefined; }