@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.3
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.
- package/LICENSE +21 -0
- package/README.md +98 -10
- package/dist/abi/canvas-live.d.ts +49 -0
- package/dist/abi/canvas-live.js +88 -0
- package/dist/abi/cart.d.ts +48 -0
- package/dist/abi/cart.js +256 -0
- package/dist/abi/convert.d.ts +126 -0
- package/dist/abi/convert.js +493 -0
- package/dist/abi/data-api.d.ts +11 -0
- package/dist/abi/data-api.js +178 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/page-data.d.ts +198 -0
- package/dist/abi/page-data.js +577 -0
- package/dist/abi/page.d.ts +23 -0
- package/dist/abi/page.js +46 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +22 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/wire.d.ts +38 -0
- package/dist/abi/wire.js +84 -0
- package/dist/build.d.ts +113 -0
- package/dist/build.js +294 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +76 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +273 -0
- package/dist/headless/hooks.js +533 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +36 -0
- package/dist/headless/index.js +38 -0
- package/dist/headless/money.d.ts +25 -0
- package/dist/headless/money.js +61 -0
- package/dist/headless/page.d.ts +43 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +186 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +186 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/theme.d.ts +99 -0
- package/dist/headless/theme.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +18 -553
- package/dist/runtime/data/StoreProvider.d.ts +56 -0
- package/dist/runtime/data/StoreProvider.js +308 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +154 -0
- package/dist/runtime/data/to-ui.d.ts +237 -0
- package/dist/runtime/data/to-ui.js +759 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +233 -0
- package/dist/runtime/kit/cart.d.ts +167 -0
- package/dist/runtime/kit/cart.js +544 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +164 -0
- package/dist/runtime/kit/contract/content.js +83 -0
- package/dist/runtime/kit/contract/field-types.d.ts +290 -0
- package/dist/runtime/kit/contract/field-types.js +129 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +67 -0
- package/dist/runtime/kit/contract/validate.js +388 -0
- package/dist/{types → runtime/kit}/data.d.ts +276 -7
- package/dist/runtime/kit/data.js +1117 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/{types → runtime/kit}/dynamic-source.d.ts +4 -4
- package/dist/runtime/kit/dynamic-source.js +143 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/{types → runtime/kit}/storefront.d.ts +206 -7
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +142 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +111 -0
- package/dist/runtime/render/listing-query.d.ts +47 -0
- package/dist/runtime/render/listing-query.js +114 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +240 -0
- package/dist/runtime/render/section-actions.d.ts +44 -0
- package/dist/runtime/render/section-actions.js +169 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +81 -0
- package/dist/runtime/seo/head.d.ts +87 -0
- package/dist/runtime/seo/head.js +224 -0
- package/dist/runtime/seo/structured-data.d.ts +22 -0
- package/dist/runtime/seo/structured-data.js +115 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +113 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
- package/dist/runtime/shell/RuntimeBridge.js +82 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/index.d.ts +24 -0
- package/dist/sdk/index.js +24 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +120 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +197 -0
- package/dist/sdk/schema.js +121 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +27 -0
- package/dist/sdk/store-lookup.js +29 -0
- package/dist/sdk/store.d.ts +115 -0
- package/dist/sdk/store.js +1 -0
- package/dist/sdk/sx.d.ts +40 -0
- package/dist/sdk/sx.js +112 -0
- package/dist/sdk/theme.d.ts +152 -0
- package/dist/sdk/theme.js +10 -0
- package/dist/sdk/types.d.ts +395 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +65 -0
- package/dist/server.js +370 -0
- package/docs/headless-api.md +220 -0
- package/package.json +77 -23
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useRef, useState, } from 'react';
|
|
3
|
+
import { useData } from './data.js';
|
|
4
|
+
/**
|
|
5
|
+
* Client-side shopping cart for storefront themes.
|
|
6
|
+
*
|
|
7
|
+
* Backed by the Tanqory Storefront GraphQL cart API (commerce-standard:
|
|
8
|
+
* `cartCreate` / `cartLinesAdd` / `cartLinesUpdate` / `cartLinesRemove`,
|
|
9
|
+
* merchandise = Variant). The cart id is persisted in localStorage so the
|
|
10
|
+
* cart survives reloads; line data is always sourced from the backend (the
|
|
11
|
+
* single source of truth) and reconciled after every mutation.
|
|
12
|
+
*
|
|
13
|
+
* Two drivers, picked automatically:
|
|
14
|
+
* - LIVE — when `useData().graphql` exists (createLiveData). Real mutations.
|
|
15
|
+
* - MOCK — when it doesn't (createMockData: editor preview / offline dev).
|
|
16
|
+
* An in-memory cart so +/- / remove still demonstrate correctly.
|
|
17
|
+
*
|
|
18
|
+
* SSR/SSG-safe: the provider renders an empty cart on the server AND on the
|
|
19
|
+
* first client render (`ready === false`), then hydrates from localStorage +
|
|
20
|
+
* backend in a post-mount effect — so there's never a hydration mismatch.
|
|
21
|
+
*/
|
|
22
|
+
const STORAGE_KEY = 'tq-cart-id';
|
|
23
|
+
const DEFAULT_CURRENCY = 'USD';
|
|
24
|
+
// commerce-standard: single-variant products name their lone variant "Default Title".
|
|
25
|
+
// Hide it so single-variant lines don't show a meaningless variant subtitle.
|
|
26
|
+
const DEFAULT_VARIANT_TITLE = 'Default Title';
|
|
27
|
+
// ─── Money helpers (amounts are decimal strings) ──────────────────
|
|
28
|
+
/** The number of fraction digits a Money string carries — what the backend
|
|
29
|
+
* formats for that currency (0 for JPY/KRW, 3 for KWD/BHD, 2 for most). A
|
|
30
|
+
* fixed `toFixed(2)` printed "¥5000.00" and rounded a 3-decimal currency. */
|
|
31
|
+
function decimalsOf(amount) {
|
|
32
|
+
return (amount?.split('.')[1] ?? '').length;
|
|
33
|
+
}
|
|
34
|
+
function money(amount, currencyCode, decimals = 2) {
|
|
35
|
+
return { amount: amount.toFixed(decimals), currencyCode };
|
|
36
|
+
}
|
|
37
|
+
function multiply(price, qty) {
|
|
38
|
+
return money(Number(price.amount) * qty, price.currencyCode, decimalsOf(price.amount));
|
|
39
|
+
}
|
|
40
|
+
function sumLines(lines) {
|
|
41
|
+
const first = lines[0]?.lineSubtotal;
|
|
42
|
+
const currencyCode = first?.currencyCode ?? DEFAULT_CURRENCY;
|
|
43
|
+
const total = lines.reduce((acc, l) => acc + Number(l.lineSubtotal.amount), 0);
|
|
44
|
+
return money(total, currencyCode, first ? decimalsOf(first.amount) : 2);
|
|
45
|
+
}
|
|
46
|
+
function countQty(lines) {
|
|
47
|
+
return lines.reduce((acc, l) => acc + l.quantity, 0);
|
|
48
|
+
}
|
|
49
|
+
// ─── GraphQL (LIVE driver) ────────────────────────────────────────
|
|
50
|
+
const CART_FRAGMENT = /* GraphQL */ `
|
|
51
|
+
fragment CartFields on Cart {
|
|
52
|
+
id
|
|
53
|
+
checkoutUrl
|
|
54
|
+
totalQuantity
|
|
55
|
+
note
|
|
56
|
+
attributes { key value }
|
|
57
|
+
cost {
|
|
58
|
+
subtotalAmount { amount currencyCode }
|
|
59
|
+
totalAmount { amount currencyCode }
|
|
60
|
+
totalTaxAmount { amount currencyCode }
|
|
61
|
+
totalDutyAmount { amount currencyCode }
|
|
62
|
+
}
|
|
63
|
+
discountCodes { code applicable }
|
|
64
|
+
discountAllocations {
|
|
65
|
+
... on CartCodeDiscountAllocation { code discountedAmount { amount currencyCode } }
|
|
66
|
+
... on CartAutomaticDiscountAllocation { title discountedAmount { amount currencyCode } }
|
|
67
|
+
... on CartCustomDiscountAllocation { title discountedAmount { amount currencyCode } }
|
|
68
|
+
}
|
|
69
|
+
appliedGiftCards {
|
|
70
|
+
id
|
|
71
|
+
lastCharacters
|
|
72
|
+
amountUsed { amount currencyCode }
|
|
73
|
+
balance { amount currencyCode }
|
|
74
|
+
}
|
|
75
|
+
lines(first: 100) {
|
|
76
|
+
nodes {
|
|
77
|
+
id
|
|
78
|
+
quantity
|
|
79
|
+
attributes { key value }
|
|
80
|
+
discountAllocations {
|
|
81
|
+
... on CartCodeDiscountAllocation { code discountedAmount { amount currencyCode } }
|
|
82
|
+
... on CartAutomaticDiscountAllocation { title discountedAmount { amount currencyCode } }
|
|
83
|
+
... on CartCustomDiscountAllocation { title discountedAmount { amount currencyCode } }
|
|
84
|
+
}
|
|
85
|
+
sellingPlanAllocation {
|
|
86
|
+
sellingPlan { id name }
|
|
87
|
+
checkoutChargeAmount { amount currencyCode }
|
|
88
|
+
remainingBalanceChargeAmount { amount currencyCode }
|
|
89
|
+
}
|
|
90
|
+
cost {
|
|
91
|
+
subtotalAmount { amount currencyCode }
|
|
92
|
+
amountPerQuantity { amount currencyCode }
|
|
93
|
+
}
|
|
94
|
+
merchandise {
|
|
95
|
+
... on Variant {
|
|
96
|
+
id
|
|
97
|
+
title
|
|
98
|
+
image { url altText }
|
|
99
|
+
price { amount currencyCode }
|
|
100
|
+
product { handle title featuredImage { url altText } }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
`;
|
|
107
|
+
const CART_QUERY = /* GraphQL */ `
|
|
108
|
+
query Cart($id: ID!) { cart(id: $id) { ...CartFields } }
|
|
109
|
+
${CART_FRAGMENT}
|
|
110
|
+
`;
|
|
111
|
+
const CART_CREATE = /* GraphQL */ `
|
|
112
|
+
mutation CartCreate($lines: [CartLineInput!]!) {
|
|
113
|
+
cartCreate(input: { lines: $lines }) { cart { ...CartFields } userErrors { message } }
|
|
114
|
+
}
|
|
115
|
+
${CART_FRAGMENT}
|
|
116
|
+
`;
|
|
117
|
+
const CART_LINES_ADD = /* GraphQL */ `
|
|
118
|
+
mutation CartLinesAdd($cartId: ID!, $lines: [CartLineInput!]!) {
|
|
119
|
+
cartLinesAdd(cartId: $cartId, lines: $lines) { cart { ...CartFields } userErrors { message } }
|
|
120
|
+
}
|
|
121
|
+
${CART_FRAGMENT}
|
|
122
|
+
`;
|
|
123
|
+
const CART_LINES_UPDATE = /* GraphQL */ `
|
|
124
|
+
mutation CartLinesUpdate($cartId: ID!, $lines: [CartLineUpdateInput!]!) {
|
|
125
|
+
cartLinesUpdate(cartId: $cartId, lines: $lines) { cart { ...CartFields } userErrors { message } }
|
|
126
|
+
}
|
|
127
|
+
${CART_FRAGMENT}
|
|
128
|
+
`;
|
|
129
|
+
const CART_LINES_REMOVE = /* GraphQL */ `
|
|
130
|
+
mutation CartLinesRemove($cartId: ID!, $lineIds: [ID!]!) {
|
|
131
|
+
cartLinesRemove(cartId: $cartId, lineIds: $lineIds) { cart { ...CartFields } userErrors { message } }
|
|
132
|
+
}
|
|
133
|
+
${CART_FRAGMENT}
|
|
134
|
+
`;
|
|
135
|
+
const CART_DISCOUNT_CODES = /* GraphQL */ `
|
|
136
|
+
mutation CartDiscountCodesUpdate($cartId: ID!, $discountCodes: [String!]) {
|
|
137
|
+
cartDiscountCodesUpdate(cartId: $cartId, discountCodes: $discountCodes) {
|
|
138
|
+
cart { ...CartFields } userErrors { message }
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
${CART_FRAGMENT}
|
|
142
|
+
`;
|
|
143
|
+
const CART_GIFTCARD_UPDATE = /* GraphQL */ `
|
|
144
|
+
mutation CartGiftCardCodesUpdate($cartId: ID!, $giftCardCodes: [String!]!) {
|
|
145
|
+
cartGiftCardCodesUpdate(cartId: $cartId, giftCardCodes: $giftCardCodes) {
|
|
146
|
+
cart { ...CartFields } userErrors { message }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
${CART_FRAGMENT}
|
|
150
|
+
`;
|
|
151
|
+
const CART_GIFTCARD_REMOVE = /* GraphQL */ `
|
|
152
|
+
mutation CartGiftCardCodesRemove($cartId: ID!, $appliedGiftCardIds: [ID!]!) {
|
|
153
|
+
cartGiftCardCodesRemove(cartId: $cartId, appliedGiftCardIds: $appliedGiftCardIds) {
|
|
154
|
+
cart { ...CartFields } userErrors { message }
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
${CART_FRAGMENT}
|
|
158
|
+
`;
|
|
159
|
+
const CART_NOTE_UPDATE = /* GraphQL */ `
|
|
160
|
+
mutation CartNoteUpdate($cartId: ID!, $note: String) {
|
|
161
|
+
cartNoteUpdate(cartId: $cartId, note: $note) {
|
|
162
|
+
cart { ...CartFields } userErrors { message }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
${CART_FRAGMENT}
|
|
166
|
+
`;
|
|
167
|
+
const CART_ATTRIBUTES_UPDATE = /* GraphQL */ `
|
|
168
|
+
mutation CartAttributesUpdate($cartId: ID!, $attributes: [AttributeInput!]!) {
|
|
169
|
+
cartAttributesUpdate(cartId: $cartId, attributes: $attributes) {
|
|
170
|
+
cart { ...CartFields } userErrors { message }
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
${CART_FRAGMENT}
|
|
174
|
+
`;
|
|
175
|
+
function normalizeImage(img) {
|
|
176
|
+
if (!img || !img.url)
|
|
177
|
+
return null;
|
|
178
|
+
return { url: img.url, ...(img.altText ? { altText: img.altText } : {}) };
|
|
179
|
+
}
|
|
180
|
+
/** GqlCart → the cart fields we keep in state (loading/ready/error added by caller). */
|
|
181
|
+
function normalizeCart(c) {
|
|
182
|
+
const lines = c.lines.nodes.map((n) => {
|
|
183
|
+
const m = n.merchandise;
|
|
184
|
+
const variantTitle = m.title && m.title !== DEFAULT_VARIANT_TITLE ? m.title : undefined;
|
|
185
|
+
return {
|
|
186
|
+
id: n.id,
|
|
187
|
+
variantId: m.id,
|
|
188
|
+
quantity: n.quantity,
|
|
189
|
+
title: m.product?.title ?? m.title ?? 'Item',
|
|
190
|
+
...(variantTitle ? { variantTitle } : {}),
|
|
191
|
+
image: normalizeImage(m.image) ?? normalizeImage(m.product?.featuredImage),
|
|
192
|
+
price: n.cost.amountPerQuantity ?? m.price,
|
|
193
|
+
lineSubtotal: n.cost.subtotalAmount ?? multiply(m.price, n.quantity),
|
|
194
|
+
...(m.product?.handle ? { productHandle: m.product.handle } : {}),
|
|
195
|
+
...(n.attributes?.length ? { attributes: n.attributes.map((a) => ({ key: a.key, value: a.value ?? null })) } : {}),
|
|
196
|
+
...(n.discountAllocations?.length
|
|
197
|
+
? { discountAllocations: n.discountAllocations.map((d) => ({ title: d.title ?? null, code: d.code ?? null, amount: d.discountedAmount ?? money(0, m.price?.currencyCode ?? 'USD', decimalsOf(m.price?.amount)) })) }
|
|
198
|
+
: {}),
|
|
199
|
+
...(n.sellingPlanAllocation?.sellingPlan
|
|
200
|
+
? { sellingPlanAllocation: {
|
|
201
|
+
sellingPlanId: n.sellingPlanAllocation.sellingPlan.id,
|
|
202
|
+
sellingPlanName: n.sellingPlanAllocation.sellingPlan.name,
|
|
203
|
+
checkoutCharge: n.sellingPlanAllocation.checkoutChargeAmount ?? null,
|
|
204
|
+
remainingBalance: n.sellingPlanAllocation.remainingBalanceChargeAmount ?? null,
|
|
205
|
+
} }
|
|
206
|
+
: {}),
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
const subtotal = c.cost?.subtotalAmount ?? sumLines(lines);
|
|
210
|
+
// Sum every discount allocation (code + automatic) into one "you saved" figure.
|
|
211
|
+
const allocations = c.discountAllocations ?? [];
|
|
212
|
+
const discountAmount = allocations.length > 0
|
|
213
|
+
? money(allocations.reduce((acc, a) => acc + Number(a.discountedAmount.amount), 0), allocations[0].discountedAmount.currencyCode, decimalsOf(allocations[0].discountedAmount.amount))
|
|
214
|
+
: null;
|
|
215
|
+
return {
|
|
216
|
+
id: c.id,
|
|
217
|
+
lines,
|
|
218
|
+
subtotal,
|
|
219
|
+
total: c.cost?.totalAmount ?? subtotal,
|
|
220
|
+
tax: c.cost?.totalTaxAmount ?? null,
|
|
221
|
+
duty: c.cost?.totalDutyAmount ?? null,
|
|
222
|
+
note: c.note ?? null,
|
|
223
|
+
attributes: (c.attributes ?? []).map((a) => ({ key: a.key, value: a.value ?? null })),
|
|
224
|
+
totalQuantity: c.totalQuantity ?? countQty(lines),
|
|
225
|
+
checkoutUrl: c.checkoutUrl ?? null,
|
|
226
|
+
discountCodes: (c.discountCodes ?? []).map((d) => ({ code: d.code, applicable: d.applicable })),
|
|
227
|
+
discountAmount,
|
|
228
|
+
appliedGiftCards: (c.appliedGiftCards ?? []).map((g) => ({
|
|
229
|
+
id: g.id,
|
|
230
|
+
lastCharacters: g.lastCharacters,
|
|
231
|
+
amountUsed: g.amountUsed,
|
|
232
|
+
balance: g.balance,
|
|
233
|
+
})),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
// ─── Context ──────────────────────────────────────────────────────
|
|
237
|
+
const EMPTY = {
|
|
238
|
+
id: null,
|
|
239
|
+
lines: [],
|
|
240
|
+
subtotal: { amount: '0.00', currencyCode: DEFAULT_CURRENCY },
|
|
241
|
+
total: { amount: '0.00', currencyCode: DEFAULT_CURRENCY },
|
|
242
|
+
totalQuantity: 0,
|
|
243
|
+
checkoutUrl: null,
|
|
244
|
+
discountCodes: [],
|
|
245
|
+
discountAmount: null,
|
|
246
|
+
appliedGiftCards: [],
|
|
247
|
+
loading: false,
|
|
248
|
+
ready: false,
|
|
249
|
+
error: null,
|
|
250
|
+
};
|
|
251
|
+
/** Exported so another cart driver (lib/abi/cart.tsx, Theme ABI v2) can provide the same `useCart()`. */
|
|
252
|
+
export const CartContext = createContext(null);
|
|
253
|
+
function readStoredCartId() {
|
|
254
|
+
if (typeof window === 'undefined')
|
|
255
|
+
return null;
|
|
256
|
+
try {
|
|
257
|
+
return window.localStorage.getItem(STORAGE_KEY);
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function writeStoredCartId(id) {
|
|
264
|
+
if (typeof window === 'undefined')
|
|
265
|
+
return;
|
|
266
|
+
try {
|
|
267
|
+
if (id)
|
|
268
|
+
window.localStorage.setItem(STORAGE_KEY, id);
|
|
269
|
+
else
|
|
270
|
+
window.localStorage.removeItem(STORAGE_KEY);
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
/* private mode etc. */
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
export function CartProvider({ children }) {
|
|
277
|
+
const data = useData();
|
|
278
|
+
const gql = data.graphql;
|
|
279
|
+
const isLive = typeof gql === 'function';
|
|
280
|
+
const [state, setState] = useState(EMPTY);
|
|
281
|
+
// Mirror state in a ref so async callbacks read the current cart id / lines
|
|
282
|
+
// without re-creating the callbacks on every change.
|
|
283
|
+
const ref = useRef(state);
|
|
284
|
+
ref.current = state;
|
|
285
|
+
// The cart id as soon as the backend assigns it — `ref.current.id` only
|
|
286
|
+
// catches up on the next render, which is too late for a second `add()`
|
|
287
|
+
// that is already waiting on the first one's cartCreate.
|
|
288
|
+
const idRef = useRef(state.id);
|
|
289
|
+
// One cartCreate at a time: two adds racing before the first response would
|
|
290
|
+
// each create a cart and the last one to store its id would orphan the other.
|
|
291
|
+
const createInFlight = useRef(null);
|
|
292
|
+
const applyCart = useCallback((c) => {
|
|
293
|
+
const next = normalizeCart(c);
|
|
294
|
+
idRef.current = next.id;
|
|
295
|
+
writeStoredCartId(next.id);
|
|
296
|
+
setState((s) => ({ ...s, ...next, loading: false, error: null }));
|
|
297
|
+
}, []);
|
|
298
|
+
const setLocal = useCallback((lines) => {
|
|
299
|
+
setState((s) => ({
|
|
300
|
+
...s,
|
|
301
|
+
lines,
|
|
302
|
+
subtotal: sumLines(lines),
|
|
303
|
+
total: sumLines(lines),
|
|
304
|
+
totalQuantity: countQty(lines),
|
|
305
|
+
loading: false,
|
|
306
|
+
error: null,
|
|
307
|
+
}));
|
|
308
|
+
}, []);
|
|
309
|
+
// Hydrate after mount (client-only): restore the saved cart from the backend.
|
|
310
|
+
useEffect(() => {
|
|
311
|
+
let cancelled = false;
|
|
312
|
+
async function hydrate() {
|
|
313
|
+
const id = readStoredCartId();
|
|
314
|
+
if (isLive && gql && id) {
|
|
315
|
+
try {
|
|
316
|
+
const res = await gql(CART_QUERY, { id });
|
|
317
|
+
if (cancelled)
|
|
318
|
+
return;
|
|
319
|
+
if (res.cart) {
|
|
320
|
+
const next = normalizeCart(res.cart);
|
|
321
|
+
idRef.current = next.id;
|
|
322
|
+
setState((s) => ({ ...s, ...next, ready: true }));
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
// Cart expired / not found — drop the stale id.
|
|
326
|
+
writeStoredCartId(null);
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
// Leave the cart empty but mark ready so the UI stops loading.
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (!cancelled)
|
|
333
|
+
setState((s) => ({ ...s, ready: true }));
|
|
334
|
+
}
|
|
335
|
+
void hydrate();
|
|
336
|
+
return () => {
|
|
337
|
+
cancelled = true;
|
|
338
|
+
};
|
|
339
|
+
}, [isLive, gql]);
|
|
340
|
+
const add = useCallback(async (input) => {
|
|
341
|
+
const quantity = input.quantity ?? 1;
|
|
342
|
+
if (quantity < 1)
|
|
343
|
+
return false;
|
|
344
|
+
if (isLive && gql) {
|
|
345
|
+
setState((s) => ({ ...s, loading: true }));
|
|
346
|
+
try {
|
|
347
|
+
const lines = [{ merchandiseId: input.variantId, quantity }];
|
|
348
|
+
// Another add is creating the cart right now: wait for it, then add
|
|
349
|
+
// to THAT cart instead of creating a second one.
|
|
350
|
+
if (createInFlight.current) {
|
|
351
|
+
try {
|
|
352
|
+
await createInFlight.current;
|
|
353
|
+
}
|
|
354
|
+
catch {
|
|
355
|
+
/* the creator reports its own failure; we try again below */
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (!idRef.current) {
|
|
359
|
+
const create = (async () => {
|
|
360
|
+
const res = await gql(CART_CREATE, { lines });
|
|
361
|
+
const payload = res.cartCreate;
|
|
362
|
+
if (payload.cart)
|
|
363
|
+
applyCart(payload.cart);
|
|
364
|
+
else
|
|
365
|
+
throw new Error(payload.userErrors[0]?.message ?? 'Could not create cart');
|
|
366
|
+
})();
|
|
367
|
+
createInFlight.current = create;
|
|
368
|
+
try {
|
|
369
|
+
await create;
|
|
370
|
+
}
|
|
371
|
+
finally {
|
|
372
|
+
if (createInFlight.current === create)
|
|
373
|
+
createInFlight.current = null;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
const res = await gql(CART_LINES_ADD, {
|
|
378
|
+
cartId: idRef.current,
|
|
379
|
+
lines,
|
|
380
|
+
});
|
|
381
|
+
const payload = res.cartLinesAdd;
|
|
382
|
+
if (payload.cart)
|
|
383
|
+
applyCart(payload.cart);
|
|
384
|
+
else
|
|
385
|
+
throw new Error(payload.userErrors[0]?.message ?? 'Could not add to cart');
|
|
386
|
+
}
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
catch (e) {
|
|
390
|
+
setState((s) => ({ ...s, loading: false, error: e.message }));
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
// MOCK driver — merge by variant id using the supplied display data.
|
|
395
|
+
const p = input.product;
|
|
396
|
+
if (!p)
|
|
397
|
+
return false;
|
|
398
|
+
const existing = ref.current.lines.find((l) => l.variantId === input.variantId);
|
|
399
|
+
let lines;
|
|
400
|
+
if (existing) {
|
|
401
|
+
lines = ref.current.lines.map((l) => l.variantId === input.variantId
|
|
402
|
+
? { ...l, quantity: l.quantity + quantity, lineSubtotal: multiply(l.price, l.quantity + quantity) }
|
|
403
|
+
: l);
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
lines = [
|
|
407
|
+
...ref.current.lines,
|
|
408
|
+
{
|
|
409
|
+
id: `mock-${input.variantId}`,
|
|
410
|
+
variantId: input.variantId,
|
|
411
|
+
quantity,
|
|
412
|
+
title: p.title,
|
|
413
|
+
...(p.variantTitle ? { variantTitle: p.variantTitle } : {}),
|
|
414
|
+
image: p.image ?? null,
|
|
415
|
+
price: p.price,
|
|
416
|
+
lineSubtotal: multiply(p.price, quantity),
|
|
417
|
+
...(p.handle ? { productHandle: p.handle } : {}),
|
|
418
|
+
},
|
|
419
|
+
];
|
|
420
|
+
}
|
|
421
|
+
setLocal(lines);
|
|
422
|
+
return true;
|
|
423
|
+
}, [isLive, gql, applyCart, setLocal]);
|
|
424
|
+
// An optimistic edit the backend did not confirm is undone: the drawer must
|
|
425
|
+
// show what the cart actually holds, or a "removed" line comes back at checkout.
|
|
426
|
+
const rollback = useCallback((previous, message) => {
|
|
427
|
+
setLocal(previous);
|
|
428
|
+
setState((s) => ({ ...s, error: message }));
|
|
429
|
+
}, [setLocal]);
|
|
430
|
+
const remove = useCallback(async (lineId) => {
|
|
431
|
+
const previous = ref.current.lines;
|
|
432
|
+
const optimistic = previous.filter((l) => l.id !== lineId);
|
|
433
|
+
setLocal(optimistic);
|
|
434
|
+
if (isLive && gql && idRef.current) {
|
|
435
|
+
try {
|
|
436
|
+
const res = await gql(CART_LINES_REMOVE, {
|
|
437
|
+
cartId: idRef.current,
|
|
438
|
+
lineIds: [lineId],
|
|
439
|
+
});
|
|
440
|
+
const payload = res.cartLinesRemove;
|
|
441
|
+
if (payload.cart)
|
|
442
|
+
applyCart(payload.cart);
|
|
443
|
+
else
|
|
444
|
+
rollback(previous, payload.userErrors[0]?.message ?? 'Could not remove from cart');
|
|
445
|
+
}
|
|
446
|
+
catch (e) {
|
|
447
|
+
rollback(previous, e.message);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}, [isLive, gql, applyCart, setLocal, rollback]);
|
|
451
|
+
const updateQuantity = useCallback(async (lineId, quantity) => {
|
|
452
|
+
if (quantity <= 0) {
|
|
453
|
+
await remove(lineId);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
// Optimistic local update for a snappy stepper, reconciled below (live).
|
|
457
|
+
const previous = ref.current.lines;
|
|
458
|
+
const optimistic = previous.map((l) => l.id === lineId ? { ...l, quantity, lineSubtotal: multiply(l.price, quantity) } : l);
|
|
459
|
+
setLocal(optimistic);
|
|
460
|
+
if (isLive && gql && idRef.current) {
|
|
461
|
+
try {
|
|
462
|
+
const res = await gql(CART_LINES_UPDATE, {
|
|
463
|
+
cartId: idRef.current,
|
|
464
|
+
lines: [{ id: lineId, quantity }],
|
|
465
|
+
});
|
|
466
|
+
const payload = res.cartLinesUpdate;
|
|
467
|
+
if (payload.cart)
|
|
468
|
+
applyCart(payload.cart);
|
|
469
|
+
else
|
|
470
|
+
rollback(previous, payload.userErrors[0]?.message ?? 'Could not update quantity');
|
|
471
|
+
}
|
|
472
|
+
catch (e) {
|
|
473
|
+
rollback(previous, e.message);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}, [isLive, gql, applyCart, setLocal, remove, rollback]);
|
|
477
|
+
const clear = useCallback(async () => {
|
|
478
|
+
const previous = ref.current.lines;
|
|
479
|
+
const ids = previous.map((l) => l.id);
|
|
480
|
+
setLocal([]);
|
|
481
|
+
if (isLive && gql && idRef.current && ids.length > 0) {
|
|
482
|
+
try {
|
|
483
|
+
const res = await gql(CART_LINES_REMOVE, {
|
|
484
|
+
cartId: idRef.current,
|
|
485
|
+
lineIds: ids,
|
|
486
|
+
});
|
|
487
|
+
const payload = res.cartLinesRemove;
|
|
488
|
+
if (payload.cart)
|
|
489
|
+
applyCart(payload.cart);
|
|
490
|
+
else
|
|
491
|
+
rollback(previous, payload.userErrors[0]?.message ?? 'Could not clear cart');
|
|
492
|
+
}
|
|
493
|
+
catch (e) {
|
|
494
|
+
rollback(previous, e.message);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}, [isLive, gql, applyCart, setLocal, rollback]);
|
|
498
|
+
// Shared runner for the discount/gift-card mutations — all return the updated
|
|
499
|
+
// cart + userErrors, all reconcile via applyCart. LIVE-only (cart-level
|
|
500
|
+
// promotions need the backend; the MOCK driver has no concept of them).
|
|
501
|
+
const runCartMutation = useCallback(async (query, field, variables) => {
|
|
502
|
+
if (!isLive || !gql || !idRef.current)
|
|
503
|
+
return;
|
|
504
|
+
setState((s) => ({ ...s, loading: true }));
|
|
505
|
+
try {
|
|
506
|
+
const res = await gql(query, {
|
|
507
|
+
cartId: idRef.current,
|
|
508
|
+
...variables,
|
|
509
|
+
});
|
|
510
|
+
const payload = res[field];
|
|
511
|
+
if (payload?.cart)
|
|
512
|
+
applyCart(payload.cart);
|
|
513
|
+
else
|
|
514
|
+
throw new Error(payload?.userErrors[0]?.message ?? 'Cart update failed');
|
|
515
|
+
}
|
|
516
|
+
catch (e) {
|
|
517
|
+
setState((s) => ({ ...s, loading: false, error: e.message }));
|
|
518
|
+
}
|
|
519
|
+
}, [isLive, gql, applyCart]);
|
|
520
|
+
const applyDiscountCodes = useCallback((codes) => runCartMutation(CART_DISCOUNT_CODES, 'cartDiscountCodesUpdate', { discountCodes: codes }), [runCartMutation]);
|
|
521
|
+
const applyGiftCardCodes = useCallback((codes) => runCartMutation(CART_GIFTCARD_UPDATE, 'cartGiftCardCodesUpdate', { giftCardCodes: codes }), [runCartMutation]);
|
|
522
|
+
const removeGiftCards = useCallback((ids) => runCartMutation(CART_GIFTCARD_REMOVE, 'cartGiftCardCodesRemove', { appliedGiftCardIds: ids }), [runCartMutation]);
|
|
523
|
+
const updateNote = useCallback((note) => runCartMutation(CART_NOTE_UPDATE, 'cartNoteUpdate', { note }), [runCartMutation]);
|
|
524
|
+
const updateAttributes = useCallback((attributes) => runCartMutation(CART_ATTRIBUTES_UPDATE, 'cartAttributesUpdate', { attributes }), [runCartMutation]);
|
|
525
|
+
const value = {
|
|
526
|
+
...state,
|
|
527
|
+
add,
|
|
528
|
+
updateQuantity,
|
|
529
|
+
remove,
|
|
530
|
+
clear,
|
|
531
|
+
applyDiscountCodes,
|
|
532
|
+
applyGiftCardCodes,
|
|
533
|
+
removeGiftCards,
|
|
534
|
+
updateNote,
|
|
535
|
+
updateAttributes,
|
|
536
|
+
};
|
|
537
|
+
return _jsx(CartContext.Provider, { value: value, children: children });
|
|
538
|
+
}
|
|
539
|
+
export function useCart() {
|
|
540
|
+
const ctx = useContext(CartContext);
|
|
541
|
+
if (!ctx)
|
|
542
|
+
throw new Error('useCart must be used within <CartProvider>');
|
|
543
|
+
return ctx;
|
|
544
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { FC, ReactNode } from 'react';
|
|
2
|
-
import type { SectionDef } from './types';
|
|
2
|
+
import type { SectionDef } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* A section used inside a *page composition* takes `settings` (literal) + nested
|
|
5
5
|
* children — never `attributes`. `tag(SectionDef)` returns it typed for that
|
|
@@ -3,7 +3,7 @@ export interface ThemeConfig {
|
|
|
3
3
|
routes: Record<string, string>;
|
|
4
4
|
data: {
|
|
5
5
|
mode: 'mock' | 'live';
|
|
6
|
-
/** Cell store-api base, e.g. https://api
|
|
6
|
+
/** Cell store-api base, e.g. https://api.example.com (live mode). */
|
|
7
7
|
endpoint?: string | undefined;
|
|
8
8
|
/** Store UUID to preview against (live mode). */
|
|
9
9
|
storeId?: string | undefined;
|
|
@@ -13,7 +13,7 @@ export interface ThemeConfig {
|
|
|
13
13
|
tokens?: string;
|
|
14
14
|
}
|
|
15
15
|
export declare function defineTheme(config: ThemeConfig): ThemeConfig;
|
|
16
|
-
import type { AttrSpec } from './types';
|
|
16
|
+
import type { AttrSpec } from './types.js';
|
|
17
17
|
/** Global theme settings schema (drives the editor's "Theme settings" panel). */
|
|
18
18
|
export type SettingsSchema = Record<string, AttrSpec>;
|
|
19
19
|
export declare function defineSettings(schema: SettingsSchema): SettingsSchema;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface Consent {
|
|
2
|
+
analytics: boolean;
|
|
3
|
+
marketing: boolean;
|
|
4
|
+
}
|
|
5
|
+
export type ConsentMode = 'OPT_IN' | 'OPT_OUT' | 'NONE';
|
|
6
|
+
/** Set the mode from an authoritative (live) verdict. The ONLY way the gate opens — components must not call it. */
|
|
7
|
+
export declare function setConsentMode(m: ConsentMode): void;
|
|
8
|
+
/** Force the gate closed (OPT_IN) and un-armed — the SSG boot, until the live answer arrives. */
|
|
9
|
+
export declare function holdConsentClosed(): void;
|
|
10
|
+
export declare function getConsentMode(): ConsentMode;
|
|
11
|
+
export declare function isConsentArmed(): boolean;
|
|
12
|
+
/** Subscribe to mode/armed changes (banner display); returns an unsubscribe fn. */
|
|
13
|
+
export declare function onConsentModeChange(cb: () => void): () => void;
|
|
14
|
+
/** Back-compat for callers that only know a boolean: true → OPT_IN, false → NONE. */
|
|
15
|
+
export declare function setBannerRequired(v: boolean): void;
|
|
16
|
+
export declare function isBannerRequired(): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The mode for a loaded shop. The server's `cookieBanner.mode` is authoritative; an older API that only sends `enabled`
|
|
19
|
+
* is read as before (enabled → OPT_IN); anything missing or unrecognised is OPT_IN, never permissive.
|
|
20
|
+
*/
|
|
21
|
+
export declare function consentModeFromShop(shop: unknown): ConsentMode;
|
|
22
|
+
/** The stored decision, or null when the shopper hasn't chosen yet. */
|
|
23
|
+
export declare function getConsent(): Consent | null;
|
|
24
|
+
export declare function hasDecided(): boolean;
|
|
25
|
+
export declare function setConsent(c: Consent): void;
|
|
26
|
+
/** True when `purpose` may run right now (see gate semantics above). */
|
|
27
|
+
export declare function hasConsent(purpose: keyof Consent): boolean;
|
|
28
|
+
/** Subscribe to consent changes; returns an unsubscribe fn. */
|
|
29
|
+
export declare function onConsentChange(cb: (c: Consent) => void): () => void;
|