@schematichq/schematic-components 2.26.0 → 2.27.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,8 @@ import {
7
7
  useSchematicI18n as useSchematicI18n2,
8
8
  useSchematicLocale as useSchematicLocale2,
9
9
  useSchematicStrings,
10
- useSchematicTranslate
10
+ useSchematicTranslate,
11
+ useUpcomingInvoice as useUpcomingInvoice2
11
12
  } from "@schematichq/schematic-react";
12
13
 
13
14
  // src/elements/model/errors.ts
@@ -468,6 +469,12 @@ function formatConsumptionRate(rate, locale) {
468
469
  maximumFractionDigits: MAXIMUM_FRACTION_DIGITS
469
470
  });
470
471
  }
472
+ function formatPercent(fraction, locale) {
473
+ return new Intl.NumberFormat(usableLocale(locale), {
474
+ style: "percent",
475
+ maximumFractionDigits: 2
476
+ }).format(fraction);
477
+ }
471
478
  function usableDate(date) {
472
479
  if (date === null || date === void 0) {
473
480
  return void 0;
@@ -557,6 +564,64 @@ function deriveInvoiceList(page, options) {
557
564
  };
558
565
  }
559
566
 
567
+ // src/elements/model/upcomingBill.ts
568
+ function deriveUpcomingInvoice(invoice, options) {
569
+ const { format, locale } = options;
570
+ const currency = invoice.currency.toUpperCase();
571
+ const money = (amountMinor) => format?.amount?.(amountMinor, currency, locale) ?? formatCurrency(amountMinor, currency, locale, {
572
+ preserveSubUnitPrecision: false
573
+ });
574
+ const line = (amountMinor) => ({
575
+ amountMinor,
576
+ amountText: money(amountMinor)
577
+ });
578
+ const applied = invoice.customerBalanceApplied;
579
+ const remaining = invoice.customerBalanceRemaining;
580
+ const dueDate = usableDate(invoice.dueDate) ?? null;
581
+ return {
582
+ amountDueMinor: invoice.amountDue,
583
+ amountDueText: money(invoice.amountDue),
584
+ subtotalMinor: invoice.subtotal,
585
+ subtotalText: money(invoice.subtotal),
586
+ currency,
587
+ dueAt: dueDate === null ? null : {
588
+ date: dueDate,
589
+ text: format?.date?.(dueDate, locale) ?? formatDate(dueDate, locale)
590
+ },
591
+ // A deduction: the sign is the locale's, so a host never assembles one.
592
+ balanceApplied: applied > 0 ? line(-applied) : null,
593
+ // Zero remaining is worth saying when a balance was spent; no balance at
594
+ // all is not.
595
+ balanceRemaining: applied > 0 || remaining > 0 ? line(remaining) : null,
596
+ discounts: invoice.discounts.flatMap(
597
+ (discount) => discountLine(discount, currency, locale, money)
598
+ )
599
+ };
600
+ }
601
+ function discountLine(discount, invoiceCurrency, locale, money) {
602
+ const percentOff = discount.percentOff ?? null;
603
+ const amountOff = discount.amountOff ?? null;
604
+ const isPercent = percentOff !== null && percentOff > 0;
605
+ if (!isPercent && (amountOff === null || amountOff <= 0)) {
606
+ return [];
607
+ }
608
+ const currency = discount.currency?.toUpperCase() ?? invoiceCurrency;
609
+ return [
610
+ {
611
+ couponName: discount.couponName,
612
+ code: discount.customerFacingCode ?? null,
613
+ kind: isPercent ? "percent" : "amount",
614
+ valueText: isPercent ? formatPercent(percentOff / 100, locale) : currency === invoiceCurrency ? money(amountOff ?? 0) : formatCurrency(amountOff ?? 0, currency, locale, {
615
+ preserveSubUnitPrecision: false
616
+ }),
617
+ percentOff: isPercent ? percentOff : null,
618
+ amountOffMinor: isPercent ? null : amountOff,
619
+ months: discount.duration === "repeating" ? discount.durationInMonths ?? null : null,
620
+ duration: discount.duration
621
+ }
622
+ ];
623
+ }
624
+
560
625
  // src/elements/strings.ts
561
626
  var DEFAULT_STRINGS_LOCALE = "en";
562
627
  var DEFAULT_STRINGS = {
@@ -571,7 +636,21 @@ var DEFAULT_STRINGS = {
571
636
  invoicesLoadMore: "Load more",
572
637
  invoicesChargeTooltip: "Charge \u2014 you were billed this amount",
573
638
  invoicesCreditTooltip: "Credit \u2014 this amount was returned to your account, typically due to a plan change or proration",
574
- invoicesUndated: "View invoice"
639
+ invoicesUndated: "View invoice",
640
+ upcomingBillLoading: "Loading your next bill",
641
+ upcomingBillHeader: "Next bill due {{date}}",
642
+ upcomingBillError: "There was a problem retrieving your upcoming invoice.",
643
+ upcomingBillUnavailable: "Your upcoming invoice is not available for this account.",
644
+ upcomingBillEmpty: "No upcoming invoice",
645
+ upcomingBillEstimate: "Estimated bill",
646
+ upcomingBillBalanceApplied: "Applied balance towards next invoice",
647
+ upcomingBillBalanceRemaining: "Remaining balance after next invoice",
648
+ upcomingBillDiscount: "Discount",
649
+ upcomingBillDiscountValue: "{{value}} off",
650
+ // Looked up by the bare key with `{ count }`; English has two forms.
651
+ upcomingBillDiscountRepeating: "{{value}} off for next {{count}} months",
652
+ upcomingBillDiscountRepeating_one: "{{value}} off for next month",
653
+ upcomingBillDiscountRepeating_other: "{{value}} off for next {{count}} months"
575
654
  };
576
655
  var MISSING_STRING = "\0schematic:missing";
577
656
  function interpolate(template, vars) {
@@ -738,6 +817,83 @@ ${Object.entries(SCHEMATIC_TOKENS).map(([name, value]) => ` ${name}: ${value};`
738
817
  }
739
818
  `;
740
819
 
820
+ // src/elements/styles/upcoming-bill.ts
821
+ var upcomingBillCss = `
822
+ .schematic-upcoming-bill {
823
+ display: flex;
824
+ flex-direction: column;
825
+ gap: var(--schematic-space);
826
+ }
827
+
828
+ .schematic-upcoming-bill .schematic-header {
829
+ margin-bottom: 0;
830
+ }
831
+
832
+ .schematic-upcoming-bill .schematic-header__title {
833
+ margin-bottom: 0;
834
+ }
835
+
836
+ .schematic-upcoming-bill__amount {
837
+ align-items: flex-start;
838
+ display: flex;
839
+ gap: var(--schematic-space);
840
+ justify-content: space-between;
841
+ }
842
+
843
+ .schematic-upcoming-bill__total {
844
+ font-family: var(--schematic-font-heading);
845
+ font-size: 1.8125rem;
846
+ font-variant-numeric: tabular-nums;
847
+ font-weight: 800;
848
+ line-height: 1;
849
+ }
850
+
851
+ .schematic-upcoming-bill__estimate {
852
+ font-weight: 600;
853
+ max-width: 10rem;
854
+ text-align: end;
855
+ }
856
+
857
+ .schematic-upcoming-bill__rows {
858
+ display: flex;
859
+ flex-direction: column;
860
+ gap: var(--schematic-space);
861
+ }
862
+
863
+ .schematic-upcoming-bill .schematic-row__value {
864
+ font-variant-numeric: tabular-nums;
865
+ }
866
+
867
+ .schematic-upcoming-bill__discounts {
868
+ align-items: flex-end;
869
+ display: flex;
870
+ flex-direction: column;
871
+ gap: calc(var(--schematic-space) / 2);
872
+ list-style: none;
873
+ margin: 0;
874
+ padding: 0;
875
+ }
876
+
877
+ .schematic-upcoming-bill__discount {
878
+ align-items: center;
879
+ display: inline-flex;
880
+ gap: calc(var(--schematic-space) / 2);
881
+ }
882
+
883
+ .schematic-upcoming-bill__empty {
884
+ padding: calc(var(--schematic-space) / 2) 0;
885
+ }
886
+
887
+ .schematic-upcoming-bill .schematic-skeleton__cell[data-column="amount"] {
888
+ height: 1.8125rem;
889
+ width: 7rem;
890
+ }
891
+
892
+ .schematic-upcoming-bill .schematic-skeleton__cell[data-column="row"] {
893
+ width: 12rem;
894
+ }
895
+ `;
896
+
741
897
  // src/elements/styles/index.tsx
742
898
  import { jsx } from "react/jsx-runtime";
743
899
  var rulesCss = `
@@ -781,6 +937,12 @@ var rulesCss = `
781
937
  gap: var(--schematic-space);
782
938
  }
783
939
 
940
+ /* The message may run long; the action beside it stays one line. */
941
+ .schematic-status__retry {
942
+ flex-shrink: 0;
943
+ white-space: nowrap;
944
+ }
945
+
784
946
  /* Visually hidden, still read by assistive technology. */
785
947
  .schematic-hidden {
786
948
  border: 0;
@@ -1047,6 +1209,22 @@ var rulesCss = `
1047
1209
  margin: 0 0 calc(var(--schematic-space) / 2);
1048
1210
  }
1049
1211
 
1212
+ /* A label on the left, its value on the right. */
1213
+ .schematic-row {
1214
+ align-items: flex-start;
1215
+ display: flex;
1216
+ gap: var(--schematic-space);
1217
+ justify-content: space-between;
1218
+ }
1219
+
1220
+ .schematic-row__label {
1221
+ font-weight: 600;
1222
+ }
1223
+
1224
+ .schematic-row__value {
1225
+ text-align: end;
1226
+ }
1227
+
1050
1228
  .schematic-feature-list {
1051
1229
  display: flex;
1052
1230
  flex-direction: column;
@@ -1233,6 +1411,7 @@ var rulesCss = `
1233
1411
  }
1234
1412
 
1235
1413
  ${invoicesCss}
1414
+ ${upcomingBillCss}
1236
1415
  `;
1237
1416
  var schematicStylesCss = withTokenDefaults(rulesCss);
1238
1417
  var SchematicStyles = () => /* @__PURE__ */ jsx("style", { "data-schematic-styles": "", children: schematicStylesCss });
@@ -1249,6 +1428,17 @@ import {
1249
1428
  import { useCallback, useEffect, useSyncExternalStore } from "react";
1250
1429
  import { jsx as jsx2, jsxs } from "react/jsx-runtime";
1251
1430
  var cx = (...names) => names.filter((name) => typeof name === "string" && name !== "").join(" ");
1431
+ function billingResources(...elements) {
1432
+ const names = [];
1433
+ for (const element of elements) {
1434
+ for (const name of element.resources) {
1435
+ if (!names.includes(name)) {
1436
+ names.push(name);
1437
+ }
1438
+ }
1439
+ }
1440
+ return names;
1441
+ }
1252
1442
  function useResolvedLocale(locale) {
1253
1443
  const configured = useSchematicLocale();
1254
1444
  const viewer = useSyncExternalStore(
@@ -1503,6 +1693,126 @@ function InvoiceDate({ row, t }) {
1503
1693
  }
1504
1694
  );
1505
1695
  }
1696
+ Invoices.resources = ["invoices"];
1697
+
1698
+ // src/elements/UpcomingBill.tsx
1699
+ import { useUpcomingInvoice } from "@schematichq/schematic-react";
1700
+ import { useMemo as useMemo2 } from "react";
1701
+ import { Fragment as Fragment2, jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
1702
+ function UpcomingBill({
1703
+ className,
1704
+ headingLevel = 2,
1705
+ locale: localeProp,
1706
+ showAmount = true,
1707
+ showBalance = true,
1708
+ showDiscounts = true,
1709
+ showHeader = true,
1710
+ strings
1711
+ }) {
1712
+ const { data: invoice, error, isPending, refetch } = useUpcomingInvoice();
1713
+ const locale = useResolvedLocale(localeProp);
1714
+ const t = useTranslator(strings, localeProp);
1715
+ const bill = useMemo2(
1716
+ () => invoice === void 0 || invoice === null ? null : deriveUpcomingInvoice(invoice, { locale }),
1717
+ [invoice, locale]
1718
+ );
1719
+ const Heading = `h${headingLevel}`;
1720
+ const balanceRows = showBalance && bill !== null && (bill.balanceApplied !== null || bill.balanceRemaining !== null);
1721
+ const discountRow = showDiscounts && bill !== null && bill.discounts.length > 0;
1722
+ const unavailable = invoice === void 0 && httpStatus(error) === 404;
1723
+ const errorMessage = error === void 0 ? void 0 : unavailable ? t("upcomingBillUnavailable") : t("upcomingBillError");
1724
+ return /* @__PURE__ */ jsx4(
1725
+ StatusFrame,
1726
+ {
1727
+ className: cx("schematic-card", "schematic-upcoming-bill", className),
1728
+ error,
1729
+ errorMessage,
1730
+ hasData: invoice !== void 0,
1731
+ isPending,
1732
+ loadingLabel: t("upcomingBillLoading"),
1733
+ onRetry: refetch,
1734
+ retryText: t("retry"),
1735
+ skeleton: /* @__PURE__ */ jsx4(UpcomingBillSkeleton, { showHeader }),
1736
+ children: bill === null ? invoice !== void 0 && /* @__PURE__ */ jsx4("p", { className: "schematic-muted schematic-upcoming-bill__empty", children: t("upcomingBillEmpty") }) : /* @__PURE__ */ jsxs3(Fragment2, { children: [
1737
+ showHeader && bill.dueAt !== null && /* @__PURE__ */ jsx4("div", { className: "schematic-header", children: /* @__PURE__ */ jsx4(Heading, { className: "schematic-header__title", children: t("upcomingBillHeader", { date: bill.dueAt.text }) }) }),
1738
+ showAmount && /* @__PURE__ */ jsxs3("div", { className: "schematic-upcoming-bill__amount", children: [
1739
+ /* @__PURE__ */ jsx4(
1740
+ "span",
1741
+ {
1742
+ className: "schematic-upcoming-bill__total",
1743
+ "data-testid": "schematic-upcoming-total",
1744
+ children: bill.amountDueText
1745
+ }
1746
+ ),
1747
+ /* @__PURE__ */ jsx4("span", { className: "schematic-small schematic-upcoming-bill__estimate", children: t("upcomingBillEstimate") })
1748
+ ] }),
1749
+ (balanceRows || discountRow) && /* @__PURE__ */ jsxs3("div", { className: "schematic-upcoming-bill__rows", children: [
1750
+ balanceRows && bill.balanceApplied !== null && /* @__PURE__ */ jsxs3(
1751
+ "div",
1752
+ {
1753
+ className: "schematic-row schematic-upcoming-bill__balance-applied",
1754
+ "data-testid": "schematic-balance-applied",
1755
+ children: [
1756
+ /* @__PURE__ */ jsx4("span", { className: "schematic-row__label", children: t("upcomingBillBalanceApplied") }),
1757
+ /* @__PURE__ */ jsx4("span", { className: "schematic-row__value", children: bill.balanceApplied.amountText })
1758
+ ]
1759
+ }
1760
+ ),
1761
+ balanceRows && bill.balanceRemaining !== null && /* @__PURE__ */ jsxs3(
1762
+ "div",
1763
+ {
1764
+ className: "schematic-row schematic-upcoming-bill__balance-remaining",
1765
+ "data-testid": "schematic-balance-remaining",
1766
+ children: [
1767
+ /* @__PURE__ */ jsx4("span", { className: "schematic-row__label", children: t("upcomingBillBalanceRemaining") }),
1768
+ /* @__PURE__ */ jsx4("span", { className: "schematic-row__value", children: bill.balanceRemaining.amountText })
1769
+ ]
1770
+ }
1771
+ ),
1772
+ discountRow && /* @__PURE__ */ jsxs3(
1773
+ "div",
1774
+ {
1775
+ className: "schematic-row schematic-upcoming-bill__discount-row",
1776
+ "data-testid": "schematic-discounts",
1777
+ children: [
1778
+ /* @__PURE__ */ jsx4("span", { className: "schematic-row__label", children: t("upcomingBillDiscount") }),
1779
+ /* @__PURE__ */ jsx4("ul", { className: "schematic-row__value schematic-upcoming-bill__discounts", children: bill.discounts.map((discount, index) => /* @__PURE__ */ jsxs3(
1780
+ "li",
1781
+ {
1782
+ className: "schematic-upcoming-bill__discount",
1783
+ "data-testid": "schematic-discount",
1784
+ children: [
1785
+ discount.code !== null && /* @__PURE__ */ jsx4("span", { className: "schematic-chip schematic-upcoming-bill__code", children: discount.code }),
1786
+ /* @__PURE__ */ jsx4("span", { className: "schematic-upcoming-bill__discount-value", children: discountText(discount, t) })
1787
+ ]
1788
+ },
1789
+ `${discount.couponName}-${index}`
1790
+ )) })
1791
+ ]
1792
+ }
1793
+ )
1794
+ ] })
1795
+ ] })
1796
+ }
1797
+ );
1798
+ }
1799
+ function UpcomingBillSkeleton({ showHeader }) {
1800
+ return /* @__PURE__ */ jsxs3("div", { className: "schematic-skeleton", children: [
1801
+ showHeader && /* @__PURE__ */ jsx4("div", { className: "schematic-skeleton__heading" }),
1802
+ /* @__PURE__ */ jsx4("div", { className: "schematic-skeleton__row", children: /* @__PURE__ */ jsx4("div", { className: "schematic-skeleton__cell", "data-column": "amount" }) }),
1803
+ /* @__PURE__ */ jsx4("div", { className: "schematic-skeleton__row", children: /* @__PURE__ */ jsx4("div", { className: "schematic-skeleton__cell", "data-column": "row" }) })
1804
+ ] });
1805
+ }
1806
+ function discountText(discount, t) {
1807
+ if (discount.months === null) {
1808
+ return t("upcomingBillDiscountValue", { value: discount.valueText });
1809
+ }
1810
+ return t("upcomingBillDiscountRepeating", {
1811
+ count: discount.months,
1812
+ value: discount.valueText
1813
+ });
1814
+ }
1815
+ UpcomingBill.resources = ["upcomingInvoice"];
1506
1816
  export {
1507
1817
  BillingDataProvider,
1508
1818
  DEFAULT_STRINGS,
@@ -1510,12 +1820,16 @@ export {
1510
1820
  SCHEMATIC_TOKENS,
1511
1821
  SchematicI18nProvider,
1512
1822
  SchematicStyles,
1823
+ UpcomingBill,
1824
+ billingResources,
1513
1825
  deriveInvoiceList,
1826
+ deriveUpcomingInvoice,
1514
1827
  featureName,
1515
1828
  formatConsumptionRate,
1516
1829
  formatCurrency,
1517
1830
  formatDate,
1518
1831
  formatNumber,
1832
+ formatPercent,
1519
1833
  formatShortDate,
1520
1834
  httpStatus,
1521
1835
  interpolate,
@@ -1532,5 +1846,6 @@ export {
1532
1846
  useSchematicStrings,
1533
1847
  useSchematicTranslate,
1534
1848
  useTranslator,
1849
+ useUpcomingInvoice2 as useUpcomingInvoice,
1535
1850
  viewerLocale
1536
1851
  };