@fantastic.dev/better-stripe 0.6.1 → 0.8.0

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.
Files changed (359) hide show
  1. package/CHANGELOG.md +28 -16
  2. package/README.md +130 -90
  3. package/dist/client/billing/checkout.d.ts +1 -1
  4. package/dist/client/billing/checkout.d.ts.map +1 -1
  5. package/dist/client/billing/checkout.js +7 -2
  6. package/dist/client/billing/checkout.js.map +1 -1
  7. package/dist/client/connect/paymentMethods.d.ts.map +1 -1
  8. package/dist/client/connect/paymentMethods.js.map +1 -1
  9. package/dist/client/connect/refundActor.d.ts.map +1 -1
  10. package/dist/client/connect/refundActor.js.map +1 -1
  11. package/dist/client/connect/refunds.d.ts.map +1 -1
  12. package/dist/client/connect/refunds.js.map +1 -1
  13. package/dist/client/connect/transfers.d.ts.map +1 -1
  14. package/dist/client/connect/transfers.js +15 -4
  15. package/dist/client/connect/transfers.js.map +1 -1
  16. package/dist/client/core/accountDefaults.d.ts +6 -0
  17. package/dist/client/core/accountDefaults.d.ts.map +1 -0
  18. package/dist/client/core/accountDefaults.js +15 -0
  19. package/dist/client/core/accountDefaults.js.map +1 -0
  20. package/dist/client/core/accounts.d.ts +1 -5
  21. package/dist/client/core/accounts.d.ts.map +1 -1
  22. package/dist/client/core/accounts.js +2 -14
  23. package/dist/client/core/accounts.js.map +1 -1
  24. package/dist/client/core/fees.d.ts.map +1 -1
  25. package/dist/client/core/fees.js +7 -2
  26. package/dist/client/core/fees.js.map +1 -1
  27. package/dist/client/index.d.ts +1 -1
  28. package/dist/client/index.d.ts.map +1 -1
  29. package/dist/client/products/prices.d.ts.map +1 -1
  30. package/dist/client/products/prices.js.map +1 -1
  31. package/dist/client/products/products.d.ts.map +1 -1
  32. package/dist/client/products/products.js +3 -1
  33. package/dist/client/products/products.js.map +1 -1
  34. package/dist/client/utils/owner.d.ts.map +1 -1
  35. package/dist/client/utils/owner.js +3 -1
  36. package/dist/client/utils/owner.js.map +1 -1
  37. package/dist/client/utils/stripeDashboardUrl.d.ts.map +1 -1
  38. package/dist/client/utils/stripeDashboardUrl.js.map +1 -1
  39. package/dist/client/webhooks/helpers.d.ts +10 -0
  40. package/dist/client/webhooks/helpers.d.ts.map +1 -1
  41. package/dist/client/webhooks/helpers.js +18 -0
  42. package/dist/client/webhooks/helpers.js.map +1 -1
  43. package/dist/client/webhooks/processors.d.ts.map +1 -1
  44. package/dist/client/webhooks/processors.js +10 -24
  45. package/dist/client/webhooks/processors.js.map +1 -1
  46. package/dist/component/_generated/component.d.ts +34 -0
  47. package/dist/component/_generated/component.d.ts.map +1 -1
  48. package/dist/component/billing/queries.d.ts +36 -0
  49. package/dist/component/billing/queries.d.ts.map +1 -1
  50. package/dist/component/billing/queries.js +10 -0
  51. package/dist/component/billing/queries.js.map +1 -1
  52. package/dist/component/billing/schema.d.ts +1 -0
  53. package/dist/component/billing/schema.d.ts.map +1 -1
  54. package/dist/component/billing/schema.js +1 -0
  55. package/dist/component/billing/schema.js.map +1 -1
  56. package/dist/component/connect/mutations.d.ts.map +1 -1
  57. package/dist/component/connect/mutations.js +28 -28
  58. package/dist/component/connect/mutations.js.map +1 -1
  59. package/dist/component/connect/schema.d.ts.map +1 -1
  60. package/dist/component/connect/schema.js.map +1 -1
  61. package/dist/component/lib/paymentSeller.d.ts +3 -0
  62. package/dist/component/lib/paymentSeller.d.ts.map +1 -0
  63. package/dist/component/lib/paymentSeller.js +10 -0
  64. package/dist/component/lib/paymentSeller.js.map +1 -0
  65. package/dist/component/lib/reversals.d.ts +10 -0
  66. package/dist/component/lib/reversals.d.ts.map +1 -1
  67. package/dist/component/lib/reversals.js +17 -0
  68. package/dist/component/lib/reversals.js.map +1 -1
  69. package/dist/component/schema.d.ts +1 -0
  70. package/dist/react/components/ConnectProvider.d.ts +1 -1
  71. package/dist/react/components/ConnectProvider.d.ts.map +1 -1
  72. package/dist/react/components/ConnectProvider.js +18 -4
  73. package/dist/react/components/ConnectProvider.js.map +1 -1
  74. package/dist/react/components/ConnectStatusBadge.d.ts.map +1 -1
  75. package/dist/react/components/ConnectStatusBadge.js.map +1 -1
  76. package/dist/react/components/EmbeddedDisputes.d.ts +1 -1
  77. package/dist/react/components/EmbeddedDisputes.d.ts.map +1 -1
  78. package/dist/react/components/EmbeddedDisputes.js +1 -1
  79. package/dist/react/components/EmbeddedDisputes.js.map +1 -1
  80. package/dist/react/components/SplitBreakdown.d.ts.map +1 -1
  81. package/dist/react/components/SplitBreakdown.js +12 -2
  82. package/dist/react/components/SplitBreakdown.js.map +1 -1
  83. package/dist/react/hooks/useCheckoutSession.d.ts.map +1 -1
  84. package/dist/react/hooks/useCheckoutSession.js.map +1 -1
  85. package/dist/react/hooks/useConfirmPayment.d.ts.map +1 -1
  86. package/dist/react/hooks/useConfirmPayment.js.map +1 -1
  87. package/dist/react/index.d.ts +1 -0
  88. package/dist/react/index.d.ts.map +1 -1
  89. package/dist/react/index.js +2 -0
  90. package/dist/react/index.js.map +1 -1
  91. package/dist/react/workflows/Account.d.ts +67 -0
  92. package/dist/react/workflows/Account.d.ts.map +1 -0
  93. package/dist/react/workflows/Account.js +74 -0
  94. package/dist/react/workflows/Account.js.map +1 -0
  95. package/dist/react/workflows/Billing.d.ts +60 -0
  96. package/dist/react/workflows/Billing.d.ts.map +1 -0
  97. package/dist/react/workflows/Billing.js +84 -0
  98. package/dist/react/workflows/Billing.js.map +1 -0
  99. package/dist/react/workflows/BillingPlans.d.ts +45 -0
  100. package/dist/react/workflows/BillingPlans.d.ts.map +1 -0
  101. package/dist/react/workflows/BillingPlans.js +51 -0
  102. package/dist/react/workflows/BillingPlans.js.map +1 -0
  103. package/dist/react/workflows/Checkout.d.ts +46 -0
  104. package/dist/react/workflows/Checkout.d.ts.map +1 -0
  105. package/dist/react/workflows/Checkout.js +61 -0
  106. package/dist/react/workflows/Checkout.js.map +1 -0
  107. package/dist/react/workflows/CheckoutChooser.d.ts +20 -0
  108. package/dist/react/workflows/CheckoutChooser.d.ts.map +1 -0
  109. package/dist/react/workflows/CheckoutChooser.js +24 -0
  110. package/dist/react/workflows/CheckoutChooser.js.map +1 -0
  111. package/dist/react/workflows/CheckoutForms.d.ts +26 -0
  112. package/dist/react/workflows/CheckoutForms.d.ts.map +1 -0
  113. package/dist/react/workflows/CheckoutForms.js +126 -0
  114. package/dist/react/workflows/CheckoutForms.js.map +1 -0
  115. package/dist/react/workflows/ClipboardButton.d.ts +17 -0
  116. package/dist/react/workflows/ClipboardButton.d.ts.map +1 -0
  117. package/dist/react/workflows/ClipboardButton.js +35 -0
  118. package/dist/react/workflows/ClipboardButton.js.map +1 -0
  119. package/dist/react/workflows/Collection.d.ts +30 -0
  120. package/dist/react/workflows/Collection.d.ts.map +1 -0
  121. package/dist/react/workflows/Collection.js +25 -0
  122. package/dist/react/workflows/Collection.js.map +1 -0
  123. package/dist/react/workflows/Collections.d.ts +56 -0
  124. package/dist/react/workflows/Collections.d.ts.map +1 -0
  125. package/dist/react/workflows/Collections.js +111 -0
  126. package/dist/react/workflows/Collections.js.map +1 -0
  127. package/dist/react/workflows/Connect.d.ts +19 -0
  128. package/dist/react/workflows/Connect.d.ts.map +1 -0
  129. package/dist/react/workflows/Connect.js +43 -0
  130. package/dist/react/workflows/Connect.js.map +1 -0
  131. package/dist/react/workflows/Costs.d.ts +48 -0
  132. package/dist/react/workflows/Costs.d.ts.map +1 -0
  133. package/dist/react/workflows/Costs.js +66 -0
  134. package/dist/react/workflows/Costs.js.map +1 -0
  135. package/dist/react/workflows/Disputes.d.ts +36 -0
  136. package/dist/react/workflows/Disputes.d.ts.map +1 -0
  137. package/dist/react/workflows/Disputes.js +74 -0
  138. package/dist/react/workflows/Disputes.js.map +1 -0
  139. package/dist/react/workflows/Earnings.d.ts +42 -0
  140. package/dist/react/workflows/Earnings.d.ts.map +1 -0
  141. package/dist/react/workflows/Earnings.js +38 -0
  142. package/dist/react/workflows/Earnings.js.map +1 -0
  143. package/dist/react/workflows/Financials.d.ts +43 -0
  144. package/dist/react/workflows/Financials.d.ts.map +1 -0
  145. package/dist/react/workflows/Financials.js +57 -0
  146. package/dist/react/workflows/Financials.js.map +1 -0
  147. package/dist/react/workflows/IntegrationSwitch.d.ts +23 -0
  148. package/dist/react/workflows/IntegrationSwitch.d.ts.map +1 -0
  149. package/dist/react/workflows/IntegrationSwitch.js +31 -0
  150. package/dist/react/workflows/IntegrationSwitch.js.map +1 -0
  151. package/dist/react/workflows/Onboarding.d.ts +56 -0
  152. package/dist/react/workflows/Onboarding.d.ts.map +1 -0
  153. package/dist/react/workflows/Onboarding.js +86 -0
  154. package/dist/react/workflows/Onboarding.js.map +1 -0
  155. package/dist/react/workflows/Overview.d.ts +51 -0
  156. package/dist/react/workflows/Overview.d.ts.map +1 -0
  157. package/dist/react/workflows/Overview.js +63 -0
  158. package/dist/react/workflows/Overview.js.map +1 -0
  159. package/dist/react/workflows/PaymentMethods.d.ts +35 -0
  160. package/dist/react/workflows/PaymentMethods.d.ts.map +1 -0
  161. package/dist/react/workflows/PaymentMethods.js +63 -0
  162. package/dist/react/workflows/PaymentMethods.js.map +1 -0
  163. package/dist/react/workflows/PaymentReceipt.d.ts +20 -0
  164. package/dist/react/workflows/PaymentReceipt.d.ts.map +1 -0
  165. package/dist/react/workflows/PaymentReceipt.js +24 -0
  166. package/dist/react/workflows/PaymentReceipt.js.map +1 -0
  167. package/dist/react/workflows/Payouts.d.ts +47 -0
  168. package/dist/react/workflows/Payouts.d.ts.map +1 -0
  169. package/dist/react/workflows/Payouts.js +87 -0
  170. package/dist/react/workflows/Payouts.js.map +1 -0
  171. package/dist/react/workflows/Products.d.ts +92 -0
  172. package/dist/react/workflows/Products.d.ts.map +1 -0
  173. package/dist/react/workflows/Products.js +179 -0
  174. package/dist/react/workflows/Products.js.map +1 -0
  175. package/dist/react/workflows/ReferralLinks.d.ts +26 -0
  176. package/dist/react/workflows/ReferralLinks.d.ts.map +1 -0
  177. package/dist/react/workflows/ReferralLinks.js +30 -0
  178. package/dist/react/workflows/ReferralLinks.js.map +1 -0
  179. package/dist/react/workflows/Refunds.d.ts +72 -0
  180. package/dist/react/workflows/Refunds.d.ts.map +1 -0
  181. package/dist/react/workflows/Refunds.js +95 -0
  182. package/dist/react/workflows/Refunds.js.map +1 -0
  183. package/dist/react/workflows/Setup.d.ts +50 -0
  184. package/dist/react/workflows/Setup.d.ts.map +1 -0
  185. package/dist/react/workflows/Setup.js +59 -0
  186. package/dist/react/workflows/Setup.js.map +1 -0
  187. package/dist/react/workflows/Storefront.d.ts +53 -0
  188. package/dist/react/workflows/Storefront.d.ts.map +1 -0
  189. package/dist/react/workflows/Storefront.js +44 -0
  190. package/dist/react/workflows/Storefront.js.map +1 -0
  191. package/dist/react/workflows/SubscriptionOverview.d.ts +13 -0
  192. package/dist/react/workflows/SubscriptionOverview.d.ts.map +1 -0
  193. package/dist/react/workflows/SubscriptionOverview.js +26 -0
  194. package/dist/react/workflows/SubscriptionOverview.js.map +1 -0
  195. package/dist/react/workflows/Subscriptions.d.ts +92 -0
  196. package/dist/react/workflows/Subscriptions.d.ts.map +1 -0
  197. package/dist/react/workflows/Subscriptions.js +149 -0
  198. package/dist/react/workflows/Subscriptions.js.map +1 -0
  199. package/dist/react/workflows/Tasks.d.ts +40 -0
  200. package/dist/react/workflows/Tasks.d.ts.map +1 -0
  201. package/dist/react/workflows/Tasks.js +113 -0
  202. package/dist/react/workflows/Tasks.js.map +1 -0
  203. package/dist/react/workflows/checkout-options.d.ts +37 -0
  204. package/dist/react/workflows/checkout-options.d.ts.map +1 -0
  205. package/dist/react/workflows/checkout-options.js +43 -0
  206. package/dist/react/workflows/checkout-options.js.map +1 -0
  207. package/dist/react/workflows/current-subscription.d.ts +11 -0
  208. package/dist/react/workflows/current-subscription.d.ts.map +1 -0
  209. package/dist/react/workflows/current-subscription.js +14 -0
  210. package/dist/react/workflows/current-subscription.js.map +1 -0
  211. package/dist/react/workflows/filters.d.ts +17 -0
  212. package/dist/react/workflows/filters.d.ts.map +1 -0
  213. package/dist/react/workflows/filters.js +23 -0
  214. package/dist/react/workflows/filters.js.map +1 -0
  215. package/dist/react/workflows/index.d.ts +70 -0
  216. package/dist/react/workflows/index.d.ts.map +1 -0
  217. package/dist/react/workflows/index.js +38 -0
  218. package/dist/react/workflows/index.js.map +1 -0
  219. package/dist/react/workflows/useWorkflowAction.d.ts +11 -0
  220. package/dist/react/workflows/useWorkflowAction.d.ts.map +1 -0
  221. package/dist/react/workflows/useWorkflowAction.js +68 -0
  222. package/dist/react/workflows/useWorkflowAction.js.map +1 -0
  223. package/dist/react/workflows/useWorkflowResource.d.ts +9 -0
  224. package/dist/react/workflows/useWorkflowResource.d.ts.map +1 -0
  225. package/dist/react/workflows/useWorkflowResource.js +67 -0
  226. package/dist/react/workflows/useWorkflowResource.js.map +1 -0
  227. package/docs/react-workflows-audit.md +66 -0
  228. package/docs/react-workflows.md +231 -0
  229. package/package.json +19 -3
  230. package/src/client/billing/checkout-ui-modes.test.ts +49 -0
  231. package/src/client/billing/checkout.ts +8 -3
  232. package/src/client/billing/invoices.test.ts +12 -6
  233. package/src/client/connect/paymentMethods.test.ts +3 -1
  234. package/src/client/connect/paymentMethods.ts +4 -1
  235. package/src/client/connect/refundActor.ts +1 -2
  236. package/src/client/connect/refunds.test.ts +35 -10
  237. package/src/client/connect/refunds.ts +4 -1
  238. package/src/client/connect/transfers.test.ts +66 -12
  239. package/src/client/connect/transfers.ts +32 -8
  240. package/src/client/core/accountDefaults.ts +15 -0
  241. package/src/client/core/accounts.ts +14 -17
  242. package/src/client/core/config.test.ts +2 -1
  243. package/src/client/core/descriptors.test.ts +6 -4
  244. package/src/client/core/fees.test.ts +20 -28
  245. package/src/client/core/fees.ts +9 -2
  246. package/src/client/index.test.ts +22 -22
  247. package/src/client/index.ts +1 -1
  248. package/src/client/products/prices.test.ts +8 -2
  249. package/src/client/products/prices.ts +4 -1
  250. package/src/client/products/products.test.ts +61 -20
  251. package/src/client/products/products.ts +3 -1
  252. package/src/client/utils/data.test.ts +3 -1
  253. package/src/client/utils/owner.test.ts +4 -1
  254. package/src/client/utils/owner.ts +5 -1
  255. package/src/client/utils/stripeDashboardUrl.ts +1 -4
  256. package/src/client/webhooks/handler.test.ts +16 -19
  257. package/src/client/webhooks/helpers.test.ts +26 -13
  258. package/src/client/webhooks/helpers.ts +23 -0
  259. package/src/client/webhooks/hooks.test.ts +1 -2
  260. package/src/client/webhooks/processors.test.ts +34 -34
  261. package/src/client/webhooks/processors.ts +9 -26
  262. package/src/client/webhooks.test.ts +1 -2
  263. package/src/component/_generated/api.ts +2 -0
  264. package/src/component/_generated/component.ts +46 -0
  265. package/src/component/billing/queries.ts +12 -0
  266. package/src/component/billing/schema.ts +1 -0
  267. package/src/component/connect/mutations.ts +34 -35
  268. package/src/component/connect/schema.ts +4 -3
  269. package/src/component/connect.test.ts +1 -5
  270. package/src/component/lib/paymentSeller.ts +15 -0
  271. package/src/component/lib/reversals.ts +30 -2
  272. package/src/component/seller-case-ownership.test.ts +61 -0
  273. package/src/component/store-subscriptions.test.ts +32 -0
  274. package/src/component/subscription-scoping.test.ts +5 -1
  275. package/src/component/triggers.test.ts +7 -4
  276. package/src/react/components/BuyerBillingView.render.test.tsx +7 -2
  277. package/src/react/components/ConnectProvider.live.test.tsx +61 -0
  278. package/src/react/components/ConnectProvider.tsx +18 -4
  279. package/src/react/components/ConnectStatusBadge.tsx +1 -4
  280. package/src/react/components/DisputeDetail.render.test.tsx +5 -1
  281. package/src/react/components/EarningsSummary.render.test.tsx +2 -6
  282. package/src/react/components/EmbeddedDisputes.render.test.tsx +7 -5
  283. package/src/react/components/EmbeddedDisputes.tsx +4 -1
  284. package/src/react/components/EvidenceForm.render.test.tsx +2 -8
  285. package/src/react/components/SplitBreakdown.render.test.tsx +28 -5
  286. package/src/react/components/SplitBreakdown.tsx +12 -2
  287. package/src/react/hooks/useCheckoutSession.ts +1 -2
  288. package/src/react/hooks/useConfirmPayment.ts +1 -2
  289. package/src/react/hooks/useEarnings.test.ts +43 -8
  290. package/src/react/hooks/useStripeMode.test.ts +5 -8
  291. package/src/react/index.ts +3 -0
  292. package/src/react/workflows/Account.test.tsx +31 -0
  293. package/src/react/workflows/Account.tsx +183 -0
  294. package/src/react/workflows/Billing.test.tsx +45 -0
  295. package/src/react/workflows/Billing.tsx +213 -0
  296. package/src/react/workflows/BillingPlans.test.tsx +31 -0
  297. package/src/react/workflows/BillingPlans.tsx +130 -0
  298. package/src/react/workflows/Checkout.test.tsx +118 -0
  299. package/src/react/workflows/Checkout.tsx +135 -0
  300. package/src/react/workflows/CheckoutChooser.test.tsx +44 -0
  301. package/src/react/workflows/CheckoutChooser.tsx +77 -0
  302. package/src/react/workflows/CheckoutForm.test.tsx +66 -0
  303. package/src/react/workflows/CheckoutForms.test.tsx +123 -0
  304. package/src/react/workflows/CheckoutForms.tsx +247 -0
  305. package/src/react/workflows/ClipboardButton.test.tsx +34 -0
  306. package/src/react/workflows/ClipboardButton.tsx +67 -0
  307. package/src/react/workflows/Collection.tsx +76 -0
  308. package/src/react/workflows/Collections.test.tsx +86 -0
  309. package/src/react/workflows/Collections.tsx +213 -0
  310. package/src/react/workflows/Connect.test.tsx +50 -0
  311. package/src/react/workflows/Connect.tsx +107 -0
  312. package/src/react/workflows/Costs.test.tsx +41 -0
  313. package/src/react/workflows/Costs.tsx +133 -0
  314. package/src/react/workflows/Disputes.test.tsx +25 -0
  315. package/src/react/workflows/Disputes.tsx +164 -0
  316. package/src/react/workflows/Earnings.test.ts +19 -0
  317. package/src/react/workflows/Earnings.tsx +84 -0
  318. package/src/react/workflows/Financials.test.tsx +35 -0
  319. package/src/react/workflows/Financials.tsx +117 -0
  320. package/src/react/workflows/IntegrationSwitch.test.tsx +36 -0
  321. package/src/react/workflows/IntegrationSwitch.tsx +63 -0
  322. package/src/react/workflows/Onboarding.test.tsx +75 -0
  323. package/src/react/workflows/Onboarding.tsx +171 -0
  324. package/src/react/workflows/Overview.test.tsx +57 -0
  325. package/src/react/workflows/Overview.tsx +123 -0
  326. package/src/react/workflows/PaymentMethods.test.tsx +60 -0
  327. package/src/react/workflows/PaymentMethods.tsx +117 -0
  328. package/src/react/workflows/PaymentReceipt.test.tsx +24 -0
  329. package/src/react/workflows/PaymentReceipt.tsx +46 -0
  330. package/src/react/workflows/Payouts.test.tsx +82 -0
  331. package/src/react/workflows/Payouts.tsx +212 -0
  332. package/src/react/workflows/Products.test.tsx +90 -0
  333. package/src/react/workflows/Products.tsx +377 -0
  334. package/src/react/workflows/ReferralLinks.test.tsx +39 -0
  335. package/src/react/workflows/ReferralLinks.tsx +75 -0
  336. package/src/react/workflows/Refunds.test.tsx +75 -0
  337. package/src/react/workflows/Refunds.tsx +223 -0
  338. package/src/react/workflows/Setup.test.tsx +25 -0
  339. package/src/react/workflows/Setup.tsx +166 -0
  340. package/src/react/workflows/Storefront.test.tsx +82 -0
  341. package/src/react/workflows/Storefront.tsx +138 -0
  342. package/src/react/workflows/SubscriptionOverview.test.tsx +27 -0
  343. package/src/react/workflows/SubscriptionOverview.tsx +48 -0
  344. package/src/react/workflows/Subscriptions.test.tsx +61 -0
  345. package/src/react/workflows/Subscriptions.tsx +282 -0
  346. package/src/react/workflows/Tasks.test.tsx +23 -0
  347. package/src/react/workflows/Tasks.tsx +199 -0
  348. package/src/react/workflows/checkout-options.ts +42 -0
  349. package/src/react/workflows/current-subscription.test.ts +29 -0
  350. package/src/react/workflows/current-subscription.ts +25 -0
  351. package/src/react/workflows/filters.test.ts +12 -0
  352. package/src/react/workflows/filters.ts +22 -0
  353. package/src/react/workflows/index.ts +238 -0
  354. package/src/react/workflows/native-interactions.test.tsx +73 -0
  355. package/src/react/workflows/public-workflows.test.tsx +216 -0
  356. package/src/react/workflows/useWorkflowAction.test.tsx +54 -0
  357. package/src/react/workflows/useWorkflowAction.ts +75 -0
  358. package/src/react/workflows/useWorkflowResource.test.tsx +41 -0
  359. package/src/react/workflows/useWorkflowResource.ts +78 -0
@@ -0,0 +1,77 @@
1
+ "use client";
2
+ import { useState, type ReactNode } from "react";
3
+ import { CHECKOUT_OPTIONS } from "./checkout-options.js";
4
+ import type { CheckoutMode } from "./Checkout.js";
5
+ export type CheckoutOption = (typeof CHECKOUT_OPTIONS)[number];
6
+ export type CheckoutChooserState = {
7
+ options: typeof CHECKOUT_OPTIONS;
8
+ selected: CheckoutOption;
9
+ unsupported: boolean;
10
+ select: (mode: CheckoutMode) => void;
11
+ };
12
+ export type CheckoutChooserProps = {
13
+ value?: CheckoutMode;
14
+ defaultValue?: CheckoutMode;
15
+ onValueChange?: (mode: CheckoutMode) => void;
16
+ recurring?: boolean;
17
+ children?: (state: CheckoutChooserState) => ReactNode;
18
+ };
19
+ /** Checkout tradeoffs and mode selection, independent of routing and session creation. */
20
+ export function CheckoutChooser({
21
+ value,
22
+ defaultValue = "embedded",
23
+ onValueChange,
24
+ recurring = false,
25
+ children,
26
+ }: CheckoutChooserProps) {
27
+ const [internal, setInternal] = useState(defaultValue);
28
+ const selected =
29
+ CHECKOUT_OPTIONS.find((option) => option.id === (value ?? internal)) ??
30
+ CHECKOUT_OPTIONS[1];
31
+ const select = (mode: CheckoutMode) => {
32
+ setInternal(mode);
33
+ onValueChange?.(mode);
34
+ };
35
+ const state = {
36
+ options: CHECKOUT_OPTIONS,
37
+ selected,
38
+ select,
39
+ unsupported: recurring && selected.id === "payment-intent",
40
+ };
41
+ if (children) return <>{children(state)}</>;
42
+ return (
43
+ <section data-better-stripe="checkout-chooser">
44
+ <h2>Choose your checkout type</h2>
45
+ <div role="group" aria-label="Checkout types">
46
+ {state.options.map((option) => (
47
+ <button
48
+ key={option.id}
49
+ type="button"
50
+ aria-pressed={selected.id === option.id}
51
+ onClick={() => select(option.id)}
52
+ >
53
+ {option.title}
54
+ </button>
55
+ ))}
56
+ </div>
57
+ <h3>{selected.title}</h3>
58
+ <p>{selected.api}</p>
59
+ <dl>
60
+ <dt>Pros</dt>
61
+ <dd>{selected.pros}</dd>
62
+ <dt>Cons</dt>
63
+ <dd>{selected.cons}</dd>
64
+ </dl>
65
+ <a href={selected.docs} target="_blank" rel="noreferrer">
66
+ Stripe documentation
67
+ </a>
68
+ {selected.id === "form" && <p>Preview</p>}
69
+ {state.unsupported && (
70
+ <p role="status">
71
+ Payment Intents alone do not create subscriptions. Choose a Checkout
72
+ Sessions integration for a recurring product.
73
+ </p>
74
+ )}
75
+ </section>
76
+ );
77
+ }
@@ -0,0 +1,66 @@
1
+ // @vitest-environment jsdom
2
+ import { act, cleanup, render, screen } from "@testing-library/react";
3
+ import { afterEach, expect, it, vi } from "vitest";
4
+ import type { ReactNode } from "react";
5
+ const mocks = vi.hoisted(() => ({
6
+ confirm: vi.fn(),
7
+ onConfirm: undefined as undefined | ((event: object) => unknown),
8
+ }));
9
+ vi.mock("@stripe/stripe-js", () => ({
10
+ loadStripe: () => Promise.resolve(null),
11
+ }));
12
+ vi.mock("@stripe/react-stripe-js/checkout", () => ({
13
+ CheckoutFormProvider: ({ children }: { children: ReactNode }) => children,
14
+ CheckoutForm: ({ onConfirm }: { onConfirm: (event: object) => unknown }) => {
15
+ mocks.onConfirm = onConfirm;
16
+ return <p>Stripe form</p>;
17
+ },
18
+ useCheckoutForm: () => ({
19
+ type: "success",
20
+ checkout: { id: "cs_form", confirm: mocks.confirm },
21
+ }),
22
+ }));
23
+ import { CheckoutForm } from "./CheckoutForms.js";
24
+ afterEach(cleanup);
25
+ it("returns a promise for the Stripe form confirmation lifecycle and uses the actual session ID", async () => {
26
+ mocks.confirm.mockResolvedValue({ type: "success" });
27
+ const navigate = vi.fn();
28
+ render(
29
+ <CheckoutForm
30
+ publishableKey="pk_test_example"
31
+ clientSecret="cs_form_secret"
32
+ returnUrl="https://app.test/receipt?session_id={CHECKOUT_SESSION_ID}"
33
+ onNavigate={navigate}
34
+ />,
35
+ );
36
+ let pending: unknown;
37
+ await act(async () => {
38
+ pending = mocks.onConfirm!({ type: "confirm" });
39
+ await pending;
40
+ });
41
+ expect(pending).toBeInstanceOf(Promise);
42
+ expect(mocks.confirm).toHaveBeenCalledWith({
43
+ formConfirmEvent: { type: "confirm" },
44
+ });
45
+ expect(navigate).toHaveBeenCalledWith(
46
+ "https://app.test/receipt?session_id=cs_form",
47
+ );
48
+ });
49
+ it("reports Stripe form confirmation failures", async () => {
50
+ mocks.confirm.mockResolvedValue({
51
+ type: "error",
52
+ error: { message: "Card declined" },
53
+ });
54
+ render(
55
+ <CheckoutForm
56
+ publishableKey="pk_test_example"
57
+ clientSecret="cs_form_secret"
58
+ returnUrl="/receipt"
59
+ onNavigate={vi.fn()}
60
+ />,
61
+ );
62
+ await act(async () => {
63
+ await mocks.onConfirm!({ type: "confirm" });
64
+ });
65
+ expect(screen.getByRole("alert").textContent).toContain("Card declined");
66
+ });
@@ -0,0 +1,123 @@
1
+ // @vitest-environment jsdom
2
+ import { cleanup, render, screen } from "@testing-library/react";
3
+ import userEvent from "@testing-library/user-event";
4
+ import { afterEach, beforeEach, expect, it, vi } from "vitest";
5
+ import type { ReactNode } from "react";
6
+ const mocks = vi.hoisted(() => ({
7
+ updateEmail: vi.fn(),
8
+ confirm: vi.fn(),
9
+ submit: vi.fn(),
10
+ confirmPayment: vi.fn(),
11
+ }));
12
+ vi.mock("../components/CheckoutSessionProvider.js", () => ({
13
+ CheckoutSessionProvider: ({ children }: { children: ReactNode }) => children,
14
+ }));
15
+ vi.mock("../components/StripeProvider.js", () => ({
16
+ StripeProvider: ({ children }: { children: ReactNode }) => children,
17
+ }));
18
+ vi.mock("@stripe/react-stripe-js/checkout", () => ({
19
+ PaymentElement: () => <p>Secure payment details</p>,
20
+ useCheckout: () => ({
21
+ type: "success",
22
+ checkout: {
23
+ id: "cs_test",
24
+ canConfirm: false,
25
+ updateEmail: mocks.updateEmail,
26
+ confirm: mocks.confirm,
27
+ },
28
+ }),
29
+ }));
30
+ vi.mock("@stripe/react-stripe-js", () => ({
31
+ PaymentElement: () => <p>Secure payment details</p>,
32
+ useStripe: () => ({ confirmPayment: mocks.confirmPayment }),
33
+ useElements: () => ({ submit: mocks.submit }),
34
+ }));
35
+ import { CheckoutElementsForm, PaymentIntentForm } from "./CheckoutForms.js";
36
+ afterEach(cleanup);
37
+ beforeEach(() => {
38
+ vi.clearAllMocks();
39
+ mocks.updateEmail.mockResolvedValue({ type: "success" });
40
+ mocks.confirm.mockResolvedValue({
41
+ type: "error",
42
+ error: { message: "Card declined" },
43
+ });
44
+ mocks.submit.mockResolvedValue({});
45
+ mocks.confirmPayment.mockResolvedValue({
46
+ error: { message: "Card declined" },
47
+ });
48
+ });
49
+ const props = {
50
+ publishableKey: "pk_test_example",
51
+ clientSecret: "cs_test_secret",
52
+ returnUrl: "https://app.test/receipt?session_id={CHECKOUT_SESSION_ID}",
53
+ email: "buyer@example.com",
54
+ onNavigate: vi.fn(),
55
+ };
56
+ it("sends the email before confirming, including when canConfirm is initially false", async () => {
57
+ render(<CheckoutElementsForm {...props} />);
58
+ await userEvent.click(screen.getByRole("button", { name: "Pay" }));
59
+ expect(mocks.updateEmail).toHaveBeenCalledWith("buyer@example.com");
60
+ expect(mocks.confirm).toHaveBeenCalledOnce();
61
+ expect(screen.getByRole("alert").textContent).toContain("Card declined");
62
+ });
63
+ it("does not confirm after Stripe rejects the email", async () => {
64
+ mocks.updateEmail.mockResolvedValue({
65
+ type: "error",
66
+ error: { message: "Invalid email" },
67
+ });
68
+ render(<CheckoutElementsForm {...props} />);
69
+ await userEvent.click(screen.getByRole("button", { name: "Pay" }));
70
+ expect(screen.getByRole("alert").textContent).toContain("Invalid email");
71
+ expect(mocks.confirm).not.toHaveBeenCalled();
72
+ });
73
+ it("disables confirmation while Stripe is processing and supports a new attempt after failure", async () => {
74
+ let resolve!: (value: unknown) => void;
75
+ mocks.confirm.mockImplementationOnce(
76
+ () =>
77
+ new Promise((done) => {
78
+ resolve = done;
79
+ }),
80
+ );
81
+ render(<CheckoutElementsForm {...props} />);
82
+ await userEvent.click(screen.getByRole("button", { name: "Pay" }));
83
+ expect(
84
+ (screen.getByRole("button", { name: "Processing…" }) as HTMLButtonElement)
85
+ .disabled,
86
+ ).toBe(true);
87
+ resolve({ type: "error", error: { message: "Try another card" } });
88
+ await screen.findByText("Try another card");
89
+ expect(
90
+ (screen.getByRole("button", { name: "Pay" }) as HTMLButtonElement).disabled,
91
+ ).toBe(false);
92
+ });
93
+ it("validates Elements before confirming a Payment Intent", async () => {
94
+ mocks.submit.mockResolvedValue({ error: { message: "Enter a valid card" } });
95
+ render(<PaymentIntentForm {...props} returnUrl="https://app.test/receipt" />);
96
+ await userEvent.click(screen.getByRole("button", { name: "Pay" }));
97
+ expect(screen.getByRole("alert").textContent).toContain("Enter a valid card");
98
+ expect(mocks.confirmPayment).not.toHaveBeenCalled();
99
+ });
100
+ it("includes the contact email and redirects to a server-confirmed receipt", async () => {
101
+ mocks.confirmPayment.mockResolvedValue({ paymentIntent: { id: "pi_test" } });
102
+ const navigate = vi.fn();
103
+ render(
104
+ <PaymentIntentForm
105
+ {...props}
106
+ returnUrl="https://app.test/receipt?ref=avery"
107
+ onNavigate={navigate}
108
+ />,
109
+ );
110
+ await userEvent.click(screen.getByRole("button", { name: "Pay" }));
111
+ expect(mocks.confirmPayment).toHaveBeenCalledWith(
112
+ expect.objectContaining({
113
+ confirmParams: expect.objectContaining({
114
+ payment_method_data: {
115
+ billing_details: { email: "buyer@example.com" },
116
+ },
117
+ }),
118
+ }),
119
+ );
120
+ expect(navigate).toHaveBeenCalledWith(
121
+ "https://app.test/receipt?ref=avery&payment_intent=pi_test",
122
+ );
123
+ });
@@ -0,0 +1,247 @@
1
+ "use client";
2
+ import {
3
+ useId,
4
+ useMemo,
5
+ useState,
6
+ type FormEvent,
7
+ type ReactNode,
8
+ } from "react";
9
+ import { loadStripe, type Appearance } from "@stripe/stripe-js";
10
+ import {
11
+ PaymentElement as IntentElement,
12
+ useElements,
13
+ useStripe,
14
+ } from "@stripe/react-stripe-js";
15
+ import {
16
+ PaymentElement,
17
+ useCheckout,
18
+ CheckoutFormProvider,
19
+ CheckoutForm as StripeCheckoutForm,
20
+ useCheckoutForm,
21
+ } from "@stripe/react-stripe-js/checkout";
22
+ import { CheckoutSessionProvider } from "../components/CheckoutSessionProvider.js";
23
+ import { StripeProvider } from "../components/StripeProvider.js";
24
+ import { useWorkflowAction } from "./useWorkflowAction.js";
25
+ export type CheckoutFormState = {
26
+ email: string;
27
+ setEmail: (email: string) => void;
28
+ emailInputId: string;
29
+ isPending: boolean;
30
+ error: string | null;
31
+ paymentElement: ReactNode;
32
+ submit: (event: FormEvent<HTMLFormElement>) => void;
33
+ };
34
+ export type CheckoutFormOptions = {
35
+ email?: string;
36
+ appearance?: Appearance;
37
+ renderForm?: (state: CheckoutFormState) => ReactNode;
38
+ };
39
+ export type CheckoutFormProps = CheckoutFormOptions & {
40
+ publishableKey: string;
41
+ clientSecret: string;
42
+ returnUrl: string;
43
+ onNavigate: (url: string) => void;
44
+ };
45
+ function NativeForm({
46
+ email,
47
+ setEmail,
48
+ emailInputId,
49
+ paymentElement,
50
+ submit,
51
+ isPending,
52
+ error,
53
+ }: CheckoutFormState) {
54
+ return (
55
+ <form
56
+ onSubmit={submit}
57
+ data-better-stripe="checkout-form"
58
+ aria-busy={isPending}
59
+ >
60
+ <label htmlFor={emailInputId}>Email for your receipt</label>
61
+ <input
62
+ id={emailInputId}
63
+ type="email"
64
+ name="email"
65
+ required
66
+ autoComplete="email"
67
+ value={email}
68
+ onChange={(event) => setEmail(event.target.value)}
69
+ />
70
+ {paymentElement}
71
+ {error && <p role="alert">{error}</p>}
72
+ <button type="submit" disabled={isPending}>
73
+ {isPending ? "Processing…" : "Pay"}
74
+ </button>
75
+ </form>
76
+ );
77
+ }
78
+ export function CheckoutElementsForm(props: CheckoutFormProps) {
79
+ return (
80
+ <CheckoutSessionProvider
81
+ publishableKey={props.publishableKey}
82
+ clientSecret={props.clientSecret}
83
+ appearance={props.appearance}
84
+ >
85
+ <SessionForm {...props} />
86
+ </CheckoutSessionProvider>
87
+ );
88
+ }
89
+ function SessionForm({
90
+ email: initialEmail = "",
91
+ returnUrl,
92
+ onNavigate,
93
+ renderForm,
94
+ }: CheckoutFormProps) {
95
+ const checkout = useCheckout();
96
+ const [email, setEmail] = useState(initialEmail);
97
+ const emailInputId = useId();
98
+ const action = useWorkflowAction(async () => {
99
+ if (checkout.type !== "success") throw new Error("Checkout is not ready.");
100
+ const updated = await checkout.checkout.updateEmail(email);
101
+ if (updated.type === "error") throw new Error(updated.error.message);
102
+ const confirmed = await checkout.checkout.confirm();
103
+ if (confirmed.type === "error") throw new Error(confirmed.error.message);
104
+ onNavigate(
105
+ returnUrl.replace("{CHECKOUT_SESSION_ID}", checkout.checkout.id),
106
+ );
107
+ });
108
+ if (checkout.type === "loading")
109
+ return <p role="status">Loading checkout…</p>;
110
+ if (checkout.type === "error")
111
+ return <p role="alert">{checkout.error.message}</p>;
112
+ const state: CheckoutFormState = {
113
+ email,
114
+ setEmail,
115
+ emailInputId,
116
+ paymentElement: <PaymentElement />,
117
+ error: action.error,
118
+ isPending: action.isPending,
119
+ submit: (event) => {
120
+ event.preventDefault();
121
+ void action.run();
122
+ },
123
+ };
124
+ return <>{renderForm ? renderForm(state) : <NativeForm {...state} />}</>;
125
+ }
126
+ export function PaymentIntentForm(props: CheckoutFormProps) {
127
+ return (
128
+ <StripeProvider
129
+ publishableKey={props.publishableKey}
130
+ elementsOptions={{
131
+ clientSecret: props.clientSecret,
132
+ ...(props.appearance ? { appearance: props.appearance } : {}),
133
+ }}
134
+ >
135
+ <IntentForm {...props} />
136
+ </StripeProvider>
137
+ );
138
+ }
139
+ function IntentForm({
140
+ email: initialEmail = "",
141
+ returnUrl,
142
+ onNavigate,
143
+ renderForm,
144
+ }: CheckoutFormProps) {
145
+ const stripe = useStripe();
146
+ const elements = useElements();
147
+ const emailInputId = useId();
148
+ const [email, setEmail] = useState(initialEmail);
149
+ const action = useWorkflowAction(async () => {
150
+ if (!stripe || !elements)
151
+ throw new Error("Payment details are still loading.");
152
+ const validation = await elements.submit();
153
+ if (validation.error)
154
+ throw new Error(
155
+ validation.error.message ?? "Check your payment details.",
156
+ );
157
+ const result = await stripe.confirmPayment({
158
+ elements,
159
+ confirmParams: {
160
+ return_url: returnUrl,
161
+ payment_method_data: { billing_details: { email } },
162
+ },
163
+ redirect: "if_required",
164
+ });
165
+ if (result.error)
166
+ throw new Error(result.error.message ?? "Payment failed.");
167
+ if (result.paymentIntent) {
168
+ const url = new URL(returnUrl, window.location.origin);
169
+ url.searchParams.set("payment_intent", result.paymentIntent.id);
170
+ onNavigate(url.toString());
171
+ }
172
+ });
173
+ const state: CheckoutFormState = {
174
+ email,
175
+ setEmail,
176
+ emailInputId,
177
+ paymentElement: <IntentElement />,
178
+ error: action.error,
179
+ isPending: action.isPending || !stripe || !elements,
180
+ submit: (event) => {
181
+ event.preventDefault();
182
+ void action.run();
183
+ },
184
+ };
185
+ return <>{renderForm ? renderForm(state) : <NativeForm {...state} />}</>;
186
+ }
187
+ export function CheckoutForm(props: CheckoutFormProps) {
188
+ const stripe = useMemo(
189
+ () => loadStripe(props.publishableKey),
190
+ [props.publishableKey],
191
+ );
192
+ return (
193
+ <CheckoutFormProvider
194
+ stripe={stripe}
195
+ options={{
196
+ clientSecret: props.clientSecret,
197
+ ...(props.appearance
198
+ ? { elementsOptions: { appearance: props.appearance } }
199
+ : {}),
200
+ }}
201
+ >
202
+ <CompactForm {...props} />
203
+ </CheckoutFormProvider>
204
+ );
205
+ }
206
+ function CompactForm({ returnUrl, onNavigate }: CheckoutFormProps) {
207
+ const checkout = useCheckoutForm();
208
+ const [loadError, setLoadError] = useState<string | null>(null);
209
+ const action = useWorkflowAction(
210
+ async (
211
+ event: Parameters<
212
+ NonNullable<
213
+ React.ComponentProps<typeof StripeCheckoutForm>["onConfirm"]
214
+ >
215
+ >[0],
216
+ ) => {
217
+ if (checkout.type !== "success")
218
+ throw new Error("Checkout is not ready.");
219
+ const result = await checkout.checkout.confirm({
220
+ formConfirmEvent: event,
221
+ });
222
+ if (result.type === "error") throw new Error(result.error.message);
223
+ onNavigate(
224
+ returnUrl.replace("{CHECKOUT_SESSION_ID}", checkout.checkout.id),
225
+ );
226
+ },
227
+ );
228
+ if (checkout.type === "loading")
229
+ return <p role="status">Loading embedded form…</p>;
230
+ if (checkout.type === "error")
231
+ return <p role="alert">{checkout.error.message}</p>;
232
+ return (
233
+ <div data-better-stripe="checkout-form" aria-busy={action.isPending}>
234
+ <StripeCheckoutForm
235
+ onLoadError={({ error }) =>
236
+ setLoadError(error.message ?? "Could not load checkout.")
237
+ }
238
+ onConfirm={async (event) => {
239
+ await action.run(event);
240
+ }}
241
+ />
242
+ {(loadError || action.error) && (
243
+ <p role="alert">{loadError || action.error}</p>
244
+ )}
245
+ </div>
246
+ );
247
+ }
@@ -0,0 +1,34 @@
1
+ // @vitest-environment jsdom
2
+ import {
3
+ act,
4
+ cleanup,
5
+ fireEvent,
6
+ render,
7
+ screen,
8
+ } from "@testing-library/react";
9
+ import { afterEach, expect, it, vi } from "vitest";
10
+ import { ClipboardButton } from "./ClipboardButton.js";
11
+ afterEach(() => {
12
+ cleanup();
13
+ vi.useRealTimers();
14
+ });
15
+ it("copies on demand, restores its label, and catches denied clipboard access", async () => {
16
+ vi.useFakeTimers();
17
+ const writeText = vi
18
+ .fn()
19
+ .mockResolvedValueOnce(undefined)
20
+ .mockRejectedValueOnce(new Error("Clipboard denied"));
21
+ render(<ClipboardButton text="pnpm check:all" writeText={writeText} />);
22
+ await act(async () => {
23
+ fireEvent.click(screen.getByRole("button", { name: "Copy" }));
24
+ });
25
+ expect(writeText).toHaveBeenCalledWith("pnpm check:all");
26
+ expect(screen.getByText("Copied!")).toBeTruthy();
27
+ act(() => {
28
+ vi.advanceTimersByTime(2000);
29
+ });
30
+ await act(async () => {
31
+ fireEvent.click(screen.getByRole("button", { name: "Copy" }));
32
+ });
33
+ expect(screen.getByRole("alert").textContent).toContain("Clipboard denied");
34
+ });
@@ -0,0 +1,67 @@
1
+ "use client";
2
+ import { useEffect, useRef, type ReactNode } from "react";
3
+ import { useWorkflowAction } from "./useWorkflowAction.js";
4
+ export type ClipboardButtonProps = {
5
+ text: string;
6
+ label?: string;
7
+ copiedLabel?: string;
8
+ resetAfterMs?: number;
9
+ writeText?: (text: string) => Promise<void>;
10
+ children?: (state: {
11
+ copy: () => Promise<void>;
12
+ copied: boolean;
13
+ isPending: boolean;
14
+ error: string | null;
15
+ label: string;
16
+ }) => ReactNode;
17
+ };
18
+ export function ClipboardButton({
19
+ text,
20
+ label = "Copy",
21
+ copiedLabel = "Copied!",
22
+ resetAfterMs = 2000,
23
+ writeText = (text) => navigator.clipboard.writeText(text),
24
+ children,
25
+ }: ClipboardButtonProps) {
26
+ const action = useWorkflowAction(async () => {
27
+ await writeText(text);
28
+ return text;
29
+ });
30
+ const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
31
+ useEffect(
32
+ () => () => {
33
+ clearTimeout(timer.current);
34
+ },
35
+ [],
36
+ );
37
+ const copied = action.result === text;
38
+ const copy = async () => {
39
+ const result = await action.run();
40
+ if (result !== undefined) {
41
+ clearTimeout(timer.current);
42
+ timer.current = setTimeout(action.reset, resetAfterMs);
43
+ }
44
+ };
45
+ const state = {
46
+ copy,
47
+ copied,
48
+ isPending: action.isPending,
49
+ error: action.error,
50
+ label: copied ? copiedLabel : label,
51
+ };
52
+ if (children) return <>{children(state)}</>;
53
+ return (
54
+ <span data-better-stripe="clipboard">
55
+ <button
56
+ type="button"
57
+ disabled={state.isPending}
58
+ onClick={() => {
59
+ void copy();
60
+ }}
61
+ >
62
+ {state.label}
63
+ </button>
64
+ {state.error && <span role="alert">{state.error}</span>}
65
+ </span>
66
+ );
67
+ }
@@ -0,0 +1,76 @@
1
+ "use client";
2
+ import type { Key, ReactNode } from "react";
3
+ export type WorkflowColumn<Row> = {
4
+ id: string;
5
+ label: string;
6
+ align?: "start" | "end";
7
+ cell: (row: Row) => ReactNode;
8
+ };
9
+ export type CollectionRenderProps<Row> = {
10
+ rows: Row[];
11
+ columns: WorkflowColumn<Row>[];
12
+ getKey: (row: Row) => Key;
13
+ isLoading: boolean;
14
+ isEmpty: boolean;
15
+ error: string | null;
16
+ emptyLabel: string;
17
+ loadingLabel: string;
18
+ };
19
+ export type CollectionProps<Row> = {
20
+ rows: Row[] | null | undefined;
21
+ columns: WorkflowColumn<Row>[];
22
+ getKey: (row: Row) => Key;
23
+ isLoading?: boolean;
24
+ error?: string | null;
25
+ emptyLabel?: string;
26
+ loadingLabel?: string;
27
+ children?: (state: CollectionRenderProps<Row>) => ReactNode;
28
+ };
29
+ /** Accessible native table by default. Render props let the application supply its design-system elements. */
30
+ export function Collection<Row>({
31
+ rows,
32
+ columns,
33
+ getKey,
34
+ isLoading = rows === undefined,
35
+ error = null,
36
+ emptyLabel = "No records yet.",
37
+ loadingLabel = "Loading…",
38
+ children,
39
+ }: CollectionProps<Row>) {
40
+ const state: CollectionRenderProps<Row> = {
41
+ rows: rows ?? [],
42
+ columns,
43
+ getKey,
44
+ isLoading,
45
+ isEmpty: !isLoading && !rows?.length,
46
+ error,
47
+ emptyLabel,
48
+ loadingLabel,
49
+ };
50
+ if (children) return <>{children(state)}</>;
51
+ if (error) return <p role="alert">{error}</p>;
52
+ if (isLoading) return <p role="status">{loadingLabel}</p>;
53
+ if (state.isEmpty) return <p>{emptyLabel}</p>;
54
+ return (
55
+ <table data-better-stripe="collection">
56
+ <thead>
57
+ <tr>
58
+ {columns.map((column) => (
59
+ <th key={column.id} scope="col">
60
+ {column.label}
61
+ </th>
62
+ ))}
63
+ </tr>
64
+ </thead>
65
+ <tbody>
66
+ {state.rows.map((row) => (
67
+ <tr key={getKey(row)}>
68
+ {columns.map((column) => (
69
+ <td key={column.id}>{column.cell(row)}</td>
70
+ ))}
71
+ </tr>
72
+ ))}
73
+ </tbody>
74
+ </table>
75
+ );
76
+ }