Files
events-hub/frontend/src/components/customer/shared.ts
T
joshuaandClaude Opus 5.5 590757e040 Hub 2.14.0: customer page as a summary and tabs, popups for every edit
- Customer page: header tiles (products, owing, next invoice, address, AI) and three
  tabs (Overview, Products, Invoices and payments), the tab in the URL; #billing links
  open Invoices and payments and Billing's links use ?tab=invoices.
- Every add or edit is a popup (new Modal in components/ui.tsx: Esc / click outside,
  asks before discarding changes, focus kept inside, full screen on a phone). The page
  shows only read views. Record payment is a popup on Billing and the invoice page too.
- One card per product: licence and billing fields in one Edit with one Save (each
  endpoint called only if its fields changed); suspend/revoke/reactivate/release in a
  ⋯ menu. Contact and billing details edited together. One page-level message.
- History as one timeline with a filter; each kind on its own keeps its sortable table.
- CustomerBilling.tsx split into components/customer/; plan in docs/CUSTOMER-PAGE-PLAN.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 11:14:05 +02:00

64 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { BillingCycle, BillingEmail, Invoice, LicenceChange, Line, Payment, PlanChange } from "@/lib/billing";
import type { Product } from "@/lib/products";
// Hub 2.14.0: the shapes the customer page loads once and passes down to its cards and popups.
export type Msg = { type: "ok" | "err"; text: string } | null;
/** Each card reports its outcome to the one message under the page header, and asks for a reload. */
export type Done = (msg: Msg) => void;
/** A plan of a product: the Events tiers have an income ceiling (upTo), the App tiers a member band. */
export type Plan = { name: string; monthly: number | null; founding: number | null; upTo?: number | null; band?: string };
export type BillingLicence = {
id: string; key: string; product: Product; plan: string; status: string; customMonthlyCents: number | null;
billedFrom: string | null; billedUntil: string | null; billable: boolean; url: string | null;
incomeBand?: string | null; activeMembers?: string | null; storage?: { usedBytes: number; includedBytes: number } | null;
};
export type AddressType = "own_domain" | "crosscode" | "included";
export type CustomerBillingData = {
id: string; name: string; billingEmail: string | null; billingAddress: string | null; founding: boolean; foundingSince: string | null; foundingNote: string | null;
billingCycle: BillingCycle; billingStart: string | null; nextInvoiceDate: string | null; reviewDate: string | null;
addressType: AddressType; domain: string | null; addressCustomCents: number | null; addressMonthlyCents: number; domainRenewsOn: string | null;
sendsTo: string | null; balanceCents: number; licences: BillingLicence[]; invoices: Invoice[]; payments: Payment[]; licenceChanges: LicenceChange[]; emails: BillingEmail[];
plans: Record<Product, Plan[]>;
};
export type Preview = { billed: false } | { billed: true; empty: boolean; invoice: { periodStart: string; periodEnd: string; issueDate: string; lines: Line[]; totalCents: number; warnings: string[]; planChanges: PlanChange[] } };
export const ADDRESS_OPTIONS: Record<AddressType, { label: string; price: string; help: string }> = {
own_domain: { label: "Own domain", price: "R0", help: "The church (or whoever manages its domain) adds the DNS records we send. We look after the certificates." },
crosscode: { label: "Cross Code address", price: "R10/month", help: "<church>.crosscode.co.za, with events., app. and so on in front. Free during a trial." },
included: { label: "Domain included", price: "R25/month", help: "A .co.za or .org.za registered in the church's name; we renew it and manage the DNS. Other endings at cost: set a custom price." },
};
/** A date input's value from an ISO date. */
export const dateInput = (d: string | null | undefined) => (d ? d.slice(0, 10) : "");
/** End of the chosen day, South African time. */
export const endOfDay = (ymd: string) => (ymd ? `${ymd}T23:59:59+02:00` : null);
const gb = (bytes: number) => `${(bytes / 1024 ** 3).toFixed(1)} GB`;
/** The tier a product's report points to: an Events income band ("R100 001–R150 000") or an App member band. */
export function suggestedTier(licence: BillingLicence, plans: Plan[]) {
if (licence.product === "app") return plans.find(p => p.band && p.band === licence.activeMembers) || null;
const band = licence.incomeBand;
if (licence.product !== "events" || !band || band === "over R100 000") return null;
if (/^over /.test(band)) return plans.find(t => t.upTo == null) || null;
const top = Number((band.split("–")[1] || "").replace(/[^\d]/g, ""));
return plans.find(t => t.upTo == null || top <= (t.upTo ?? Infinity)) || null;
}
/** What the product reports that its price depends on. */
export function reportText(l: BillingLicence) {
if (l.product === "events") return l.incomeBand ? `12-month event income ${l.incomeBand}` : "No event income reported yet";
if (l.product === "app") return l.activeMembers ? `${l.activeMembers} active members (90 days)` : "No active members reported yet";
if (l.product === "website") return l.storage ? `${gb(l.storage.usedBytes)} of ${gb(l.storage.includedBytes)} storage used` : "No storage reported yet";
return "";
}
/** Whether a form differs from what it started with. */
export const changed = <T,>(a: T, b: T) => JSON.stringify(a) !== JSON.stringify(b);