"use client"; import * as React from "react"; import { TRACKER_TIER_NAMES, type TrackerTier } from "@accessibit/types"; import { usePlansCustomer, useSiteAdmin, type SiteAdminFormat, } from "../context"; import { fill } from "../lib/fill"; import type { SiteAdminMessages, ViewsPurchaseWords } from "../messages"; import { Notice, type NoticeProps } from "../ui/notice"; type WidgetDict = SiteAdminMessages["widget"]; /** The rung of a widget bought alone. */ export type WidgetTier = keyof WidgetDict["plan"]["widgetSizes"]; /** A plan by what it is: a Tracker tier, or a widget bought alone. */ export type AllowancePlan = | { kind: "tracker"; tier: TrackerTier } | { kind: "widget"; tier: WidgetTier }; /** * The view blocks a plan near or past its yearly views can buy, as the host * reads them on the server. The dashboard's `lib/addOns/viewsOffer` declares * its own type, held to this one where the notice is passed in; the host's * purchase command (`SiteAdminViewsPurchase`) gets it back whole. */ export type PageviewAddOnOffer = { /** By card, or by asking sales on a plan with no card to charge. */ buy: "card" | "ask_sales"; /** Views a block adds to every plan year. */ blockViews: number; /** * Euros a block costs today, excl. VAT: the plan's whole term at full * price, or by card on a 2- or 3-year widget plan only the plan years left. */ unitPriceEur: number; /** Euros a block costs for the plan's whole term; left out, `unitPriceEur`. */ termPriceEur?: number; /** The plan years `unitPriceEur` pays for; left out, `termYears`. */ yearsLeft?: number; /** The quantity to start from: the blocks the year's peak needs, at least 1. */ blocks: number; /** The blocks the year's peak needs: 0 when the pace stays within the limit. */ needed: number; /** Whether the plan above costs less than those blocks. */ pick: "blocks" | "plan_above"; /** What those blocks cost. */ blocksEur: number; /** What the plan above costs more, at the same term; null with none. */ stepEur: number | null; /** By card only: the card charged. */ cardLast4: string | null; /** * The plan's term in years: the term price covers all of them, * `unitPriceEur` only `yearsLeft` of them. */ termYears: number; /** * Which side of the limit the year's projection lands on, with the plan * year's last day (`YYYY-MM-DD`); `already_over` when the views so far are * past the limit already. Null with no projection yet; optional. */ pace?: { side: "over" | "within" | "already_over"; yearEnds: string; } | null; }; /** * The notice on a plan's yearly views, as the host reads it on the server. * The dashboard's `lib/widget/pageviewNotice` declares its own type, which * the type checker holds to this one where the dashboard passes it in. */ export type PageviewNotice = { state: "near" | "over"; /** The plan whose limit it is. */ plan: AllowancePlan; /** The views a year that plan includes. */ yearlyViews: number; /** The site the plan was bought for; a change of plan is made there. */ mainSiteId: string; /** The sites the limit is shared by, as hosts, the main domain first. */ sites: string[]; /** How many more share it, that this person may not see. */ otherSites: number; /** The day the plan year ends (`YYYY-MM-DD`), or null. */ yearEnds: string | null; /** The plan one step up, and the views a year it includes. */ upgrade: { plan: AllowancePlan; yearlyViews: number } | null; /** * The view blocks the plan can buy; null or left out with none to offer, * and then the notice keeps its own commands. Offered only through the * host's purchase command. */ addOn?: PageviewAddOnOffer | null; }; /** A plan by its name: "Professional", "Widget Medium". */ export function allowancePlanName(plan: AllowancePlan, t: WidgetDict): string { return plan.kind === "tracker" ? TRACKER_TIER_NAMES[plan.tier] : fill(t.plan.widgetBadge, { plan: t.plan.widgetSizes[plan.tier] }); } /** More sites than this are named by the first ones and a count. */ const SITES_NAMED = 3; /** * The words of the notice on a plan's yearly views (ACC-547, boards W5, W7, * O21): the limit of the plan, which side of it the widget is on, and what * can be done: write to us, or move to the plan above. Never how many views * there were: the notice does not have the number. * * `sales`: the customer is from before the plans and the widget was bought * alone (ACC-542), so a sales rep is offered instead of the rung above. * * `buy`: the viewer is offered the plan's view blocks by card, so the * sentence points at them; when the plan above costs less it says so first. * Left out (a host that does not sell them, a viewer who cannot buy, a plan * sold by sales), the sentence is the one that asks us to add them. */ export function pageviewNoticeText( notice: PageviewNotice, t: WidgetDict, format: SiteAdminFormat, sales = false, buy: ViewsPurchaseWords | null = null, ): { title: string; text: string } { const l = t.limit; const near = notice.state === "near"; const byViews = ( key: Key, views: number, ): string | undefined => (l[key] as Record)[String(views)]; const list = new Intl.ListFormat(format.intlLocale, { style: "long", type: "conjunction", }); // A plan can cover many subdomains: past three, the first two and how many // more, or the sentence would be a list. The ones this person may not open // are never named, only counted with the rest. const named = notice.sites.length > SITES_NAMED ? notice.sites.slice(0, SITES_NAMED - 1) : notice.sites; const more = notice.sites.length - named.length + notice.otherSites; const sites = more === 0 ? list.format(named) : fill(more === 1 ? l.sitesMoreOne : l.sitesMore, { sites: named.join(", "), count: more, }); const limit = byViews(near ? "nearOf" : "overOf", notice.yearlyViews) ?? // A number of views the catalog did not have when this was written. fill(near ? l.nearOfOther : l.overOfOther, { views: format.count(notice.yearlyViews), }); const plan = fill(notice.plan.kind === "tracker" ? l.inTracker : l.inWidget, { plan: allowancePlanName(notice.plan, t), }); const what = near ? notice.yearEnds ? fill(l.near, { sites, limit, plan, date: format.date(notice.yearEnds, { year: "numeric", month: "short", day: "numeric", // A day, not a moment: printed as written. timeZone: "UTC", }), }) : fill(l.nearNoDate, { sites, limit, plan }) : fill(l.over, { sites, limit, plan }); const next = notice.upgrade; // The plan above's views with their noun, never a bare number to end a // sentence on: «1,2 millones de visualizaciones» wants its «de». const amount = (views: number) => byViews("viewsOf", views) ?? fill(l.viewsCount, { count: format.count(views) }); const offer = !sales && buy ? notice.addOn : null; if (offer?.buy === "card" && buy) { const euros = (value: number) => format.euro ? format.euro(value) : format.currency(value, "EUR"); const why = offer.pick === "plan_above" && next && offer.stepEur !== null ? // The amounts are the term's: a year's, or a 2- or 3-year plan's. `${fill( offer.termYears > 1 ? buy.planAboveFirstTerm : buy.planAboveFirst, { plan: allowancePlanName(next.plan, t), step: euros(offer.stepEur), blocks: euros(offer.blocksEur), views: amount(next.yearlyViews), years: String(offer.termYears), }, )} ` : ""; const action = next ? near ? l.nearActionBuy : l.overActionBuy : near ? l.nearActionBuyLast : l.overActionBuyLast; return { title: near ? l.nearTitle : l.overTitle, text: `${what} ${why}${action}`, }; } const todo = sales ? near ? l.nearActionSales : l.overActionSales : next ? fill(near ? l.nearAction : l.overAction, { next: allowancePlanName(next.plan, t), views: amount(next.yearlyViews), }) : near ? l.nearActionLast : l.overActionLast; return { title: near ? l.nearTitle : l.overTitle, text: `${what} ${todo}`, }; } /** * What the notice offers, the same on the Widget page and on the site's * overview (boards W5, W7, O21): the plan's view blocks, by card or by asking * sales; the plan above, when there is one; write to us; and what we sell. * When the plan above costs less than the blocks the year needs, it comes * first. With no block to offer, writing to us comes first. Each is a page * or a command of the host: one it has not is not offered. * * The blocks and the plan above are for whoever manages the subscription: * the host passes its purchase command, and the right to choose a plan, for * them alone. On a subdomain they are the plan of its main domain. */ function usePageviewNoticeActions( notice: PageviewNotice, /** The site the page is about: its products page is the last link. */ domainId: string, ): NonNullable { const { messages, links, account, viewsPurchase } = useSiteAdmin(); const t = messages.widget; const upgrade = notice.upgrade && account?.canChoosePlan && links?.choosePlan ? { ...notice.upgrade, choosePlan: links.choosePlan } : null; const offer = viewsPurchase ? (notice.addOn ?? null) : null; const planAboveFirst = offer?.pick === "plan_above" && upgrade !== null; const addViews = offer && viewsPurchase ? ( // Keyed: the list changes order when the plan above comes first. {(offer.buy === "card" ? viewsPurchase.renderBuy : viewsPurchase.renderAskSales)({ notice, offer, primary: !planAboveFirst, })} ) : null; const up = upgrade ? [ { key: "upgrade", label: fill(t.limit.upgradeCta, { plan: allowancePlanName(upgrade.plan, t), }), variant: planAboveFirst ? ("primary" as const) : ("neutral" as const), href: upgrade.choosePlan(upgrade.plan.kind, notice.mainSiteId), }, ] : []; const contact = links?.support ? [ { key: "contact", label: t.limit.contactCta, // The blocks are the main command when there are any. variant: addViews ? ("neutral" as const) : ("primary" as const), href: links.support, }, ] : []; const products = links?.products ? [ { key: "products", label: t.limit.productsLink, variant: "link" as const, href: links.products(domainId), }, ] : []; if (!addViews) return [...contact, ...up, ...products]; return planAboveFirst ? [...up, addViews, ...contact, ...products] : [addViews, ...up, ...contact, ...products]; } /** * The notice itself, the same on the Widget page and on the site's overview: * its words and its commands are built here, once. * * A customer from before the plans (ACC-542) over a widget bought alone is * offered a sales rep instead, from a member up; a Lettore reads the * sentence. On a new plan the notice stays the plan's. */ export function PageviewLimitNotice({ notice, domainId, }: { notice: PageviewNotice; /** The site the page is about. */ domainId: string; }) { const { messages, format, renderContactSales, viewsPurchase } = useSiteAdmin(); const actions = usePageviewNoticeActions(notice, domainId); const { prePlans, canAskSales } = usePlansCustomer(); const sales = prePlans && notice.plan.kind === "widget"; const { title, text } = pageviewNoticeText( notice, messages.widget, format, sales, // The sentence points at the blocks only for whoever sees the command. viewsPurchase?.words ?? null, ); return ( {text} ); }