@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,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `useThemeContext()` and `useThemeActions()` (theme SDK) return on a
|
|
3
|
+
* page: the platform's cart, overlay, consent and navigation, behind a stable
|
|
4
|
+
* API. One instance per page, so every section and overlay shares the same
|
|
5
|
+
* cart-line status.
|
|
6
|
+
*/
|
|
7
|
+
import { type ReactNode } from 'react';
|
|
8
|
+
import { type ThemeMode } from '../../sdk/index.js';
|
|
9
|
+
export declare function RuntimeBridge({ mode, locale, settings, children }: {
|
|
10
|
+
mode: ThemeMode;
|
|
11
|
+
locale: string;
|
|
12
|
+
settings: Record<string, unknown>;
|
|
13
|
+
children: ReactNode;
|
|
14
|
+
}): JSX.Element;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* What `useThemeContext()` and `useThemeActions()` (theme SDK) return on a
|
|
4
|
+
* page: the platform's cart, overlay, consent and navigation, behind a stable
|
|
5
|
+
* API. One instance per page, so every section and overlay shares the same
|
|
6
|
+
* cart-line status.
|
|
7
|
+
*/
|
|
8
|
+
import { useMemo, useState } from 'react';
|
|
9
|
+
import { ThemeRuntimeContext } from '../../sdk/index.js';
|
|
10
|
+
import { useData } from '../kit/data.js';
|
|
11
|
+
import { useCart } from '../kit/cart.js';
|
|
12
|
+
import { setConsent } from '../kit/consent.js';
|
|
13
|
+
import { useCartActions, navigate, searchHref, changeCountry, changeLocale } from '../render/section-actions.js';
|
|
14
|
+
import { useOverlays } from './overlay-state.js';
|
|
15
|
+
import { useStore } from '../data/StoreProvider.js';
|
|
16
|
+
import * as map from '../data/to-ui.js';
|
|
17
|
+
export function RuntimeBridge({ mode, locale, settings, children }) {
|
|
18
|
+
const cart = useCart();
|
|
19
|
+
const data = useData();
|
|
20
|
+
const store = useStore();
|
|
21
|
+
const overlays = useOverlays();
|
|
22
|
+
const cartActions = useCartActions();
|
|
23
|
+
// The shopper asked to change their cookie choice (footer link): show the banner again.
|
|
24
|
+
const [consentReopened, setConsentReopened] = useState(false);
|
|
25
|
+
const actions = useMemo(() => ({
|
|
26
|
+
cart: {
|
|
27
|
+
add: cartActions.addToCart,
|
|
28
|
+
setQuantity: cartActions.setQuantity,
|
|
29
|
+
remove: cartActions.removeLine,
|
|
30
|
+
setNote: cartActions.setNote,
|
|
31
|
+
open: () => (settings.enableCartDrawer === false ? navigate('/cart') : overlays.show('cart')),
|
|
32
|
+
},
|
|
33
|
+
search: {
|
|
34
|
+
open: () => (settings.enableSearchModal === false ? navigate('/search') : overlays.show('search')),
|
|
35
|
+
go: (q) => navigate(searchHref(q)),
|
|
36
|
+
// The storefront search query (the predictive endpoint asks for fields
|
|
37
|
+
// the storefront does not serve and fails as a whole).
|
|
38
|
+
suggest: async (q) => {
|
|
39
|
+
if (!data.search)
|
|
40
|
+
return { products: [], collections: [], articles: [], total: 0 };
|
|
41
|
+
const r = await data.search(q, { first: 6, types: ['PRODUCT', 'PAGE', 'ARTICLE'] });
|
|
42
|
+
// Search results carry no blog handle, so an article cannot be linked.
|
|
43
|
+
return { products: map.toProducts(r.products), collections: [], articles: [], total: r.totalCount ?? r.products.length };
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
localization: { changeCountry, changeLocale },
|
|
47
|
+
consent: {
|
|
48
|
+
decide: (c) => {
|
|
49
|
+
setConsent({ analytics: c.analytics, marketing: c.marketing });
|
|
50
|
+
// Theme ABI v2: the platform records the choice (POST /_tq/actions/consent.set).
|
|
51
|
+
void data.consent?.set({ analytics: c.analytics, marketing: c.marketing, functional: true }).catch(() => { });
|
|
52
|
+
setConsentReopened(false);
|
|
53
|
+
},
|
|
54
|
+
open: () => setConsentReopened(true),
|
|
55
|
+
},
|
|
56
|
+
overlays: { close: overlays.close },
|
|
57
|
+
toasts: { show: (m, tone = 'info') => overlays.toast(m, tone === 'error' ? 'error' : 'default'), dismiss: overlays.dismiss },
|
|
58
|
+
navigate,
|
|
59
|
+
}), [cartActions, data, overlays, settings.enableCartDrawer, settings.enableSearchModal]);
|
|
60
|
+
// The banner's privacy link: the merchant's own link (Settings › Customer privacy), else the privacy
|
|
61
|
+
// policy while it is published — loaded on this page, or listed in `Shop.policyLinks`.
|
|
62
|
+
const shop = store?.ctx.shop;
|
|
63
|
+
const privacyUrl = shop?.consentBanner?.privacyLinkUrl ?? store?.ctx.policies?.['privacy-policy']?.url ?? shop?.policyLinks?.find((p) => p.handle === 'privacy-policy')?.url ?? null;
|
|
64
|
+
const context = useMemo(() => ({
|
|
65
|
+
mode,
|
|
66
|
+
locale,
|
|
67
|
+
settings,
|
|
68
|
+
cart: map.toCart(cart),
|
|
69
|
+
checkoutUrl: cart.checkoutUrl ?? '/checkout',
|
|
70
|
+
overlay: overlays.open,
|
|
71
|
+
justAddedToCart: overlays.justAdded,
|
|
72
|
+
busyLines: [...cartActions.busyLines],
|
|
73
|
+
failedLines: [...cartActions.failedLines],
|
|
74
|
+
refusedLines: Object.fromEntries(cartActions.refusedLines),
|
|
75
|
+
toasts: overlays.toasts.map((t) => ({ id: t.id, message: t.message, tone: t.tone === 'error' ? 'error' : 'info' })),
|
|
76
|
+
consentRequired: Boolean(store?.ctx.consentEnabled && (store.ctx.consentAnswered === false || consentReopened)),
|
|
77
|
+
privacyPolicyUrl: privacyUrl,
|
|
78
|
+
...(shop ? { shop } : {}),
|
|
79
|
+
}), [mode, locale, settings, cart, overlays.open, overlays.justAdded, overlays.toasts, cartActions.busyLines, cartActions.failedLines, cartActions.refusedLines, store?.ctx.consentEnabled, store?.ctx.consentAnswered, consentReopened, privacyUrl, shop]);
|
|
80
|
+
const value = useMemo(() => ({ context, actions }), [context, actions]);
|
|
81
|
+
return _jsx(ThemeRuntimeContext.Provider, { value: value, children: children });
|
|
82
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page-level overlay state: which surface is open (cart drawer, search modal,
|
|
3
|
+
* account menu) and the toast queue. Sections never own an overlay the page
|
|
4
|
+
* shares — the header asks for the cart, the product form asks for the cart
|
|
5
|
+
* after an add, and both reach the same drawer through this context.
|
|
6
|
+
*/
|
|
7
|
+
import { type ReactNode, type RefObject } from 'react';
|
|
8
|
+
export type OverlayName = 'cart' | 'search' | 'account';
|
|
9
|
+
export interface Toast {
|
|
10
|
+
id: string;
|
|
11
|
+
message: string;
|
|
12
|
+
tone?: 'default' | 'error';
|
|
13
|
+
}
|
|
14
|
+
export interface OverlayApi {
|
|
15
|
+
open: OverlayName | null;
|
|
16
|
+
/** The element that opened the account menu (it anchors to it). */
|
|
17
|
+
anchor: RefObject<HTMLElement>;
|
|
18
|
+
show: (name: OverlayName, from?: HTMLElement | null) => void;
|
|
19
|
+
close: () => void;
|
|
20
|
+
/** The cart drawer was opened by an add, not by the shopper. */
|
|
21
|
+
justAdded: boolean;
|
|
22
|
+
toasts: readonly Toast[];
|
|
23
|
+
toast: (message: string, tone?: Toast['tone']) => void;
|
|
24
|
+
dismiss: (id: string) => void;
|
|
25
|
+
}
|
|
26
|
+
export declare function OverlayProvider({ children }: {
|
|
27
|
+
children: ReactNode;
|
|
28
|
+
}): JSX.Element;
|
|
29
|
+
/** Outside a provider (a section preview) every overlay request is a no-op. */
|
|
30
|
+
export declare function useOverlays(): OverlayApi;
|
|
31
|
+
export declare function useOpenCartAfterAdd(): () => void;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Page-level overlay state: which surface is open (cart drawer, search modal,
|
|
4
|
+
* account menu) and the toast queue. Sections never own an overlay the page
|
|
5
|
+
* shares — the header asks for the cart, the product form asks for the cart
|
|
6
|
+
* after an add, and both reach the same drawer through this context.
|
|
7
|
+
*/
|
|
8
|
+
import { createContext, useCallback, useContext, useMemo, useRef, useState } from 'react';
|
|
9
|
+
const OverlayContext = createContext(null);
|
|
10
|
+
let toastSeq = 0;
|
|
11
|
+
export function OverlayProvider({ children }) {
|
|
12
|
+
const [open, setOpen] = useState(null);
|
|
13
|
+
const [justAdded, setJustAdded] = useState(false);
|
|
14
|
+
const [toasts, setToasts] = useState([]);
|
|
15
|
+
const anchor = useRef(null);
|
|
16
|
+
const show = useCallback((name, from) => {
|
|
17
|
+
if (from !== undefined)
|
|
18
|
+
anchor.current = from;
|
|
19
|
+
setJustAdded(false);
|
|
20
|
+
setOpen(name);
|
|
21
|
+
}, []);
|
|
22
|
+
const close = useCallback(() => {
|
|
23
|
+
setOpen(null);
|
|
24
|
+
setJustAdded(false);
|
|
25
|
+
}, []);
|
|
26
|
+
const toast = useCallback((message, tone = 'default') => {
|
|
27
|
+
toastSeq += 1;
|
|
28
|
+
setToasts((ts) => [...ts, { id: `toast-${toastSeq}`, message, tone }]);
|
|
29
|
+
}, []);
|
|
30
|
+
const dismiss = useCallback((id) => setToasts((ts) => ts.filter((t) => t.id !== id)), []);
|
|
31
|
+
const value = useMemo(() => ({ open, anchor: anchor, show, close, justAdded, toasts, toast, dismiss }), [open, show, close, justAdded, toasts, toast, dismiss]);
|
|
32
|
+
return (_jsx(OverlayContext.Provider, { value: value, children: _jsx(JustAddedBridge, { set: setJustAdded, setOpen: setOpen, children: children }) }));
|
|
33
|
+
}
|
|
34
|
+
/** Lets `openCartAfterAdd` flip both flags in one place without widening the public API. */
|
|
35
|
+
const JustAddedContext = createContext(null);
|
|
36
|
+
function JustAddedBridge({ set, setOpen, children }) {
|
|
37
|
+
const openAfterAdd = useCallback(() => {
|
|
38
|
+
setOpen('cart');
|
|
39
|
+
set(true);
|
|
40
|
+
}, [set, setOpen]);
|
|
41
|
+
return _jsx(JustAddedContext.Provider, { value: openAfterAdd, children: children });
|
|
42
|
+
}
|
|
43
|
+
const NOOP = {
|
|
44
|
+
open: null,
|
|
45
|
+
anchor: { current: null },
|
|
46
|
+
show: () => { },
|
|
47
|
+
close: () => { },
|
|
48
|
+
justAdded: false,
|
|
49
|
+
toasts: [],
|
|
50
|
+
toast: () => { },
|
|
51
|
+
dismiss: () => { },
|
|
52
|
+
};
|
|
53
|
+
/** Outside a provider (a section preview) every overlay request is a no-op. */
|
|
54
|
+
export function useOverlays() {
|
|
55
|
+
return useContext(OverlayContext) ?? NOOP;
|
|
56
|
+
}
|
|
57
|
+
export function useOpenCartAfterAdd() {
|
|
58
|
+
return useContext(JustAddedContext) ?? (() => { });
|
|
59
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Is this render a PREVIEW (the editor canvas or a preview channel)? Under Theme ABI v2 that is
|
|
3
|
+
* `ctx.mode === 'preview'`, decided by the platform and handed to the tree by the entries — never
|
|
4
|
+
* read from the URL, so the server render and the hydrating browser agree.
|
|
5
|
+
*/
|
|
6
|
+
import { createContext, useContext } from 'react';
|
|
7
|
+
const PreviewModeContext = createContext(false);
|
|
8
|
+
export const PreviewModeProvider = PreviewModeContext.Provider;
|
|
9
|
+
export function usePreviewMode() {
|
|
10
|
+
return useContext(PreviewModeContext);
|
|
11
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare function setServerLocation(location: string): void;
|
|
2
|
+
/**
|
|
3
|
+
* SPA router — intercepts internal `<a>` clicks, pushes the new URL and
|
|
4
|
+
* re-renders, so a navigation swaps templates without a page load.
|
|
5
|
+
*
|
|
6
|
+
* Returns pathname + SEARCH: tracking the pathname alone left `/search?q=shirt`
|
|
7
|
+
* on screen after navigating to `/search?q=hat`.
|
|
8
|
+
*
|
|
9
|
+
* Skips external origins, new-tab / download / modified clicks, links marked
|
|
10
|
+
* `data-full-page-nav="true"`, and `/checkout`, `/account`, `/orders` — those
|
|
11
|
+
* are served by separate apps the edge router forwards to, so they need a real
|
|
12
|
+
* HTTP navigation.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useSoftRoute(enabled: boolean): string;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* The URL a server render is for. The SSR entry sets it before rendering (one
|
|
4
|
+
* synchronous render per request), so the server draws the same route the
|
|
5
|
+
* browser will hydrate — without it every page rendered as `/` on the server
|
|
6
|
+
* and hydration failed on every other URL.
|
|
7
|
+
*/
|
|
8
|
+
let serverLocation = '/';
|
|
9
|
+
export function setServerLocation(location) {
|
|
10
|
+
serverLocation = location || '/';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* SPA router — intercepts internal `<a>` clicks, pushes the new URL and
|
|
14
|
+
* re-renders, so a navigation swaps templates without a page load.
|
|
15
|
+
*
|
|
16
|
+
* Returns pathname + SEARCH: tracking the pathname alone left `/search?q=shirt`
|
|
17
|
+
* on screen after navigating to `/search?q=hat`.
|
|
18
|
+
*
|
|
19
|
+
* Skips external origins, new-tab / download / modified clicks, links marked
|
|
20
|
+
* `data-full-page-nav="true"`, and `/checkout`, `/account`, `/orders` — those
|
|
21
|
+
* are served by separate apps the edge router forwards to, so they need a real
|
|
22
|
+
* HTTP navigation.
|
|
23
|
+
*/
|
|
24
|
+
export function useSoftRoute(enabled) {
|
|
25
|
+
const here = () => (typeof window !== 'undefined' ? window.location.pathname + window.location.search : serverLocation);
|
|
26
|
+
const [location, setLocation] = useState(here);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!enabled || typeof window === 'undefined')
|
|
29
|
+
return;
|
|
30
|
+
const go = (next) => {
|
|
31
|
+
if (next !== window.location.pathname + window.location.search)
|
|
32
|
+
window.history.pushState({}, '', next);
|
|
33
|
+
setLocation(window.location.pathname + window.location.search);
|
|
34
|
+
if (!next.includes('#'))
|
|
35
|
+
window.scrollTo({ top: 0, behavior: 'auto' });
|
|
36
|
+
};
|
|
37
|
+
const onClick = (e) => {
|
|
38
|
+
if (e.defaultPrevented || e.button !== 0)
|
|
39
|
+
return;
|
|
40
|
+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
|
|
41
|
+
return;
|
|
42
|
+
const a = e.target?.closest?.('a[href]');
|
|
43
|
+
if (!a)
|
|
44
|
+
return;
|
|
45
|
+
if (a.target && a.target !== '_self')
|
|
46
|
+
return;
|
|
47
|
+
if (a.hasAttribute('download'))
|
|
48
|
+
return;
|
|
49
|
+
if (a.dataset.fullPageNav === 'true')
|
|
50
|
+
return;
|
|
51
|
+
let url;
|
|
52
|
+
try {
|
|
53
|
+
url = new URL(a.href, window.location.origin);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (url.origin !== window.location.origin)
|
|
59
|
+
return;
|
|
60
|
+
if (url.pathname === window.location.pathname && url.hash)
|
|
61
|
+
return;
|
|
62
|
+
if (/^\/(checkout|account|orders)(\/|$)/.test(url.pathname))
|
|
63
|
+
return;
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
go(url.pathname + url.search + url.hash);
|
|
66
|
+
};
|
|
67
|
+
const onPop = () => setLocation(window.location.pathname + window.location.search);
|
|
68
|
+
document.addEventListener('click', onClick);
|
|
69
|
+
window.addEventListener('popstate', onPop);
|
|
70
|
+
return () => {
|
|
71
|
+
document.removeEventListener('click', onClick);
|
|
72
|
+
window.removeEventListener('popstate', onPop);
|
|
73
|
+
};
|
|
74
|
+
}, [enabled]);
|
|
75
|
+
return location;
|
|
76
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The merchant's URL redirects (Settings → Navigation → URL redirects): when the
|
|
3
|
+
* current path matches one, go to its target. Client-side, after render, so a
|
|
4
|
+
* store with no redirects pays one small query and nothing else.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useUrlRedirect(pathname?: string): void;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { useData } from '../kit/data.js';
|
|
3
|
+
/**
|
|
4
|
+
* The merchant's URL redirects (Settings → Navigation → URL redirects): when the
|
|
5
|
+
* current path matches one, go to its target. Client-side, after render, so a
|
|
6
|
+
* store with no redirects pays one small query and nothing else.
|
|
7
|
+
*/
|
|
8
|
+
export function useUrlRedirect(pathname) {
|
|
9
|
+
const { graphql } = useData();
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (typeof window === 'undefined' || !graphql)
|
|
12
|
+
return;
|
|
13
|
+
const currentPath = pathname ?? window.location.pathname;
|
|
14
|
+
let cancelled = false;
|
|
15
|
+
// Server-side filter keeps the response small — only exact-path matches
|
|
16
|
+
// matter. The cap of 5 tolerates `query` substring false positives without
|
|
17
|
+
// paying for a full list.
|
|
18
|
+
void graphql(`query R($q: String) {
|
|
19
|
+
urlRedirects(first: 5, query: $q) {
|
|
20
|
+
nodes { path target }
|
|
21
|
+
}
|
|
22
|
+
}`, { q: currentPath })
|
|
23
|
+
.then((res) => {
|
|
24
|
+
if (cancelled)
|
|
25
|
+
return;
|
|
26
|
+
const match = res?.urlRedirects?.nodes.find((n) => n.path === currentPath);
|
|
27
|
+
if (match)
|
|
28
|
+
window.location.replace(match.target);
|
|
29
|
+
})
|
|
30
|
+
.catch(() => {
|
|
31
|
+
/* a failed redirect lookup should never break the page */
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
cancelled = true;
|
|
35
|
+
};
|
|
36
|
+
}, [pathname, graphql]);
|
|
37
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the runtime is handed at boot, instead of importing it:
|
|
3
|
+
*
|
|
4
|
+
* the theme bundle — the theme's definition and content (from .tanqory/, generated by the compiler)
|
|
5
|
+
* the editor runtime — Studio canvas behaviour (@tanqory/theme-editor-runtime), only where it applies
|
|
6
|
+
*
|
|
7
|
+
* One page (or one server render) runs one theme, so a module-level slot is
|
|
8
|
+
* enough; the entry sets both before it renders anything.
|
|
9
|
+
*/
|
|
10
|
+
import type { FC, ReactNode } from 'react';
|
|
11
|
+
import type { ThemeDefinition } from '../sdk/index.js';
|
|
12
|
+
import type { ContentNode } from './kit/types.js';
|
|
13
|
+
type GlobMap = Record<string, {
|
|
14
|
+
default: unknown;
|
|
15
|
+
}>;
|
|
16
|
+
export interface ThemeBundle {
|
|
17
|
+
theme: ThemeDefinition;
|
|
18
|
+
/** templates/*.json, groups/*.json, locales/*.json — keyed by their theme path. */
|
|
19
|
+
templates: GlobMap;
|
|
20
|
+
groups: GlobMap;
|
|
21
|
+
locales: GlobMap;
|
|
22
|
+
/** config/settings.json */
|
|
23
|
+
settings: Record<string, unknown>;
|
|
24
|
+
/** config/settings.schema.ts — the theme settings the editor offers. */
|
|
25
|
+
settingsSchema: Record<string, {
|
|
26
|
+
type: string;
|
|
27
|
+
options?: readonly {
|
|
28
|
+
value: string;
|
|
29
|
+
}[];
|
|
30
|
+
min?: number;
|
|
31
|
+
max?: number;
|
|
32
|
+
}>;
|
|
33
|
+
/** Custom sections (sections/*.tsx, `defineSection` from the old API) — registered after the theme's own. */
|
|
34
|
+
customSections: Record<string, {
|
|
35
|
+
default?: unknown;
|
|
36
|
+
}>;
|
|
37
|
+
/** The generated manifest the platform reads (served by the server entry). */
|
|
38
|
+
manifest: unknown;
|
|
39
|
+
}
|
|
40
|
+
export interface EditorRuntime {
|
|
41
|
+
/** This page is the editor canvas (preview plane): the bridge renders the tree. */
|
|
42
|
+
isPreviewPlane(location: {
|
|
43
|
+
hostname: string;
|
|
44
|
+
search: string;
|
|
45
|
+
}): boolean;
|
|
46
|
+
PreviewBridge: FC<{
|
|
47
|
+
pages: Record<string, ContentNode[]>;
|
|
48
|
+
initialPage: string;
|
|
49
|
+
Shell: FC<{
|
|
50
|
+
children: ReactNode;
|
|
51
|
+
}>;
|
|
52
|
+
allowedOrigins?: readonly string[];
|
|
53
|
+
}>;
|
|
54
|
+
Editor?: FC<{
|
|
55
|
+
pages: Record<string, ContentNode[]>;
|
|
56
|
+
initialPage: string;
|
|
57
|
+
}>;
|
|
58
|
+
/** Saved theme settings, plus the editor's unsaved live values on the canvas. */
|
|
59
|
+
useLiveSettings(saved: Record<string, unknown>, onCanvas: boolean): Record<string, unknown>;
|
|
60
|
+
/** The canvas's selection outline. */
|
|
61
|
+
usePreviewSelection(enabled: boolean): void;
|
|
62
|
+
/** The "Add section" preview endpoint (preview plane only); null = not that request. */
|
|
63
|
+
/** `page.data` is the store's live data when a store is attached; absent only offline (no store). */
|
|
64
|
+
sectionPreview?(request: Request, bundle: ThemeBundle, page: {
|
|
65
|
+
styles: string;
|
|
66
|
+
settings: Record<string, unknown>;
|
|
67
|
+
data?: import('./kit/index.js').DataApi;
|
|
68
|
+
}): Response | null;
|
|
69
|
+
}
|
|
70
|
+
export declare function setThemeBundle(b: ThemeBundle): void;
|
|
71
|
+
export declare function getThemeBundle(): ThemeBundle;
|
|
72
|
+
export declare function setEditorRuntime(e: EditorRuntime | null): void;
|
|
73
|
+
export declare function getEditorRuntime(): EditorRuntime | null;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
let bundle = null;
|
|
2
|
+
let editor = null;
|
|
3
|
+
export function setThemeBundle(b) {
|
|
4
|
+
bundle = b;
|
|
5
|
+
}
|
|
6
|
+
export function getThemeBundle() {
|
|
7
|
+
if (!bundle)
|
|
8
|
+
throw new Error('[tanqory] no theme bundle: the entry must call setThemeBundle() before rendering');
|
|
9
|
+
return bundle;
|
|
10
|
+
}
|
|
11
|
+
export function setEditorRuntime(e) {
|
|
12
|
+
editor = e;
|
|
13
|
+
}
|
|
14
|
+
export function getEditorRuntime() {
|
|
15
|
+
return editor;
|
|
16
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@tanqory/theme-kit/runtime` — the low-level runtime pieces `createRender` and `hydrate` are built
|
|
3
|
+
* from. For a theme's own tests (render a section tree under the real providers) and for custom
|
|
4
|
+
* entries. A theme's components never import this; they use `@tanqory/theme-kit`.
|
|
5
|
+
*
|
|
6
|
+
* Alpha surface: these names may still move between pre-releases.
|
|
7
|
+
*/
|
|
8
|
+
export { setThemeBundle, getThemeBundle, type ThemeBundle } from './runtime/slots.js';
|
|
9
|
+
export { sectionModules } from './runtime/entries/sections.js';
|
|
10
|
+
export { registerSections, getSection, allSections } from './runtime/kit/registry.js';
|
|
11
|
+
export { SectionTree } from './runtime/kit/SectionTree.js';
|
|
12
|
+
export type { ContentNode, SectionDef, AttrSpec, SectionProps } from './runtime/kit/types.js';
|
|
13
|
+
export { DataProvider, type DataApi } from './runtime/kit/data.js';
|
|
14
|
+
export { ThemeProvider } from './runtime/kit/theme-context.js';
|
|
15
|
+
export { PreviewModeProvider } from './runtime/shell/preview-mode.js';
|
|
16
|
+
export { localeStrings, themeLocaleOf } from './runtime/platform/locale.js';
|
|
17
|
+
export { AbiShell } from './abi/shell.js';
|
|
18
|
+
export { nodesOf, notFoundTree, locationOf } from './abi/page.js';
|
|
19
|
+
export { loadPageData, pageNeeds, pageParamsOf, variantOf, cartLinkOf, searchQueryOf, searchSortOf, listingQueryOfRequest, localizationOf, marketsOf, type PageData, type NativeFormResult, type NativeFormStatus, type LocaleMarket, } from './abi/page-data.js';
|
|
20
|
+
export { createDataApi, searchKey, searchOrder, SEARCH_SORTS } from './abi/data-api.js';
|
|
21
|
+
export * as convert from './abi/convert.js';
|
|
22
|
+
export { createAbiRemote, AbiWireError, parseRetryAfter, type AbiRemote, type WireEnvelope } from './abi/wire.js';
|
|
23
|
+
export { AbiCartProvider, refusalOf, wireCode, retryDelay } from './abi/cart.js';
|
|
24
|
+
export { AbiRewardsProvider, HOST_OFFERS_LOYALTY, rewardsStateOf } from './abi/rewards.js';
|
|
25
|
+
export { AbiReferralsProvider, keepRefCode, referralStateOf, friendOfferOf, REF_COOKIE, CLAIM_COOKIE } from './abi/referrals.js';
|
|
26
|
+
export { homeHead, heroImageOf, hreflangAlternates, type HomeHead, type HreflangAlternate } from './abi/seo.js';
|
|
27
|
+
export { canonicalize } from './abi/jcs.js';
|
|
28
|
+
export { CANVAS_EVENT, CANVAS_LIVE_EVENT, CANVAS_BRIDGE_EVENT, LIVE_TYPES, blocksOf, applyCanvasMessage, createCanvasStore, connectCanvas, type CanvasStore, } from './abi/canvas-live.js';
|
|
29
|
+
export { computeHead, headFrom, shopNameOf } from './runtime/seo/head.js';
|
|
30
|
+
export { jsonLdTag } from './runtime/seo/structured-data.js';
|
|
31
|
+
export { toLocalization, toShop, toCart, toProduct, consentBannerShows } from './runtime/data/to-ui.js';
|
|
32
|
+
export { localeHref } from './runtime/render/section-actions.js';
|
|
33
|
+
export { listingQueryOf, queryOfState, stateFromQuery, withListingQuery } from './runtime/render/listing-query.js';
|
|
34
|
+
export { copyPlanOf, localizeShippedCopy, shippedCopyOf, unknownCopyRefs } from './runtime/render/localized-copy.js';
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@tanqory/theme-kit/runtime` — the low-level runtime pieces `createRender` and `hydrate` are built
|
|
3
|
+
* from. For a theme's own tests (render a section tree under the real providers) and for custom
|
|
4
|
+
* entries. A theme's components never import this; they use `@tanqory/theme-kit`.
|
|
5
|
+
*
|
|
6
|
+
* Alpha surface: these names may still move between pre-releases.
|
|
7
|
+
*/
|
|
8
|
+
// the theme bundle slot + section registration
|
|
9
|
+
export { setThemeBundle, getThemeBundle } from './runtime/slots.js';
|
|
10
|
+
export { sectionModules } from './runtime/entries/sections.js';
|
|
11
|
+
export { registerSections, getSection, allSections } from './runtime/kit/registry.js';
|
|
12
|
+
export { SectionTree } from './runtime/kit/SectionTree.js';
|
|
13
|
+
// providers
|
|
14
|
+
export { DataProvider } from './runtime/kit/data.js';
|
|
15
|
+
export { ThemeProvider } from './runtime/kit/theme-context.js';
|
|
16
|
+
export { PreviewModeProvider } from './runtime/shell/preview-mode.js';
|
|
17
|
+
export { localeStrings, themeLocaleOf } from './runtime/platform/locale.js';
|
|
18
|
+
// the ABI v2 layer: page, data plan, data API, wire, cart, loyalty
|
|
19
|
+
export { AbiShell } from './abi/shell.js';
|
|
20
|
+
export { nodesOf, notFoundTree, locationOf } from './abi/page.js';
|
|
21
|
+
export { loadPageData, pageNeeds, pageParamsOf, variantOf, cartLinkOf, searchQueryOf, searchSortOf, listingQueryOfRequest, localizationOf, marketsOf, } from './abi/page-data.js';
|
|
22
|
+
export { createDataApi, searchKey, searchOrder, SEARCH_SORTS } from './abi/data-api.js';
|
|
23
|
+
export * as convert from './abi/convert.js';
|
|
24
|
+
export { createAbiRemote, AbiWireError, parseRetryAfter } from './abi/wire.js';
|
|
25
|
+
export { AbiCartProvider, refusalOf, wireCode, retryDelay } from './abi/cart.js';
|
|
26
|
+
export { AbiRewardsProvider, HOST_OFFERS_LOYALTY, rewardsStateOf } from './abi/rewards.js';
|
|
27
|
+
export { AbiReferralsProvider, keepRefCode, referralStateOf, friendOfferOf, REF_COOKIE, CLAIM_COOKIE } from './abi/referrals.js';
|
|
28
|
+
export { homeHead, heroImageOf, hreflangAlternates } from './abi/seo.js';
|
|
29
|
+
export { canonicalize } from './abi/jcs.js';
|
|
30
|
+
// live edit on the editor canvas
|
|
31
|
+
export { CANVAS_EVENT, CANVAS_LIVE_EVENT, CANVAS_BRIDGE_EVENT, LIVE_TYPES, blocksOf, applyCanvasMessage, createCanvasStore, connectCanvas, } from './abi/canvas-live.js';
|
|
32
|
+
// SEO
|
|
33
|
+
export { computeHead, headFrom, shopNameOf } from './runtime/seo/head.js';
|
|
34
|
+
export { jsonLdTag } from './runtime/seo/structured-data.js';
|
|
35
|
+
// store data → the shapes components read
|
|
36
|
+
export { toLocalization, toShop, toCart, toProduct, consentBannerShows } from './runtime/data/to-ui.js';
|
|
37
|
+
// render helpers
|
|
38
|
+
export { localeHref } from './runtime/render/section-actions.js';
|
|
39
|
+
export { listingQueryOf, queryOfState, stateFromQuery, withListingQuery } from './runtime/render/listing-query.js';
|
|
40
|
+
export { copyPlanOf, localizeShippedCopy, shippedCopyOf, unknownCopyRefs } from './runtime/render/localized-copy.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tanqory/theme-sdk — the public API a Tanqory theme is written against.
|
|
3
|
+
*
|
|
4
|
+
* schema defineSchema(), field.*, resolveSettings() — a section's persisted settings
|
|
5
|
+
* theme defineTheme(), defineSection() — how a theme describes itself
|
|
6
|
+
* runtime useThemeContext(), useThemeActions(), useSearchSuggestions()
|
|
7
|
+
* types Product, Collection, Cart, Menu, … — the data a theme renders
|
|
8
|
+
* rewards useRewards(), earnPoints(): the store's loyalty program, browser-only
|
|
9
|
+
* referrals useReferrals(), shareTargets(): the shopper's referral link and the friend offer, browser-only
|
|
10
|
+
* store StoreContext + imageFor / menuFor / collectionFor / productFor … —
|
|
11
|
+
* read what the runtime put in the context
|
|
12
|
+
*
|
|
13
|
+
* Nothing here fetches or knows how the platform works.
|
|
14
|
+
*/
|
|
15
|
+
export * from './schema.js';
|
|
16
|
+
export * from './types.js';
|
|
17
|
+
export * from './store.js';
|
|
18
|
+
export * from './store-lookup.js';
|
|
19
|
+
export * from './theme.js';
|
|
20
|
+
export * from './runtime-context.js';
|
|
21
|
+
export { ServerRenderProvider, useServerRender } from './server-render.js';
|
|
22
|
+
export * from './rewards.js';
|
|
23
|
+
export * from './referrals.js';
|
|
24
|
+
export { sx, cx as sxJoin, collectStyles, STYLE_RULES_ID } from './sx.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tanqory/theme-sdk — the public API a Tanqory theme is written against.
|
|
3
|
+
*
|
|
4
|
+
* schema defineSchema(), field.*, resolveSettings() — a section's persisted settings
|
|
5
|
+
* theme defineTheme(), defineSection() — how a theme describes itself
|
|
6
|
+
* runtime useThemeContext(), useThemeActions(), useSearchSuggestions()
|
|
7
|
+
* types Product, Collection, Cart, Menu, … — the data a theme renders
|
|
8
|
+
* rewards useRewards(), earnPoints(): the store's loyalty program, browser-only
|
|
9
|
+
* referrals useReferrals(), shareTargets(): the shopper's referral link and the friend offer, browser-only
|
|
10
|
+
* store StoreContext + imageFor / menuFor / collectionFor / productFor … —
|
|
11
|
+
* read what the runtime put in the context
|
|
12
|
+
*
|
|
13
|
+
* Nothing here fetches or knows how the platform works.
|
|
14
|
+
*/
|
|
15
|
+
export * from './schema.js';
|
|
16
|
+
export * from './types.js';
|
|
17
|
+
export * from './store.js';
|
|
18
|
+
export * from './store-lookup.js';
|
|
19
|
+
export * from './theme.js';
|
|
20
|
+
export * from './runtime-context.js';
|
|
21
|
+
export { ServerRenderProvider, useServerRender } from './server-render.js';
|
|
22
|
+
export * from './rewards.js';
|
|
23
|
+
export * from './referrals.js';
|
|
24
|
+
export { sx, cx as sxJoin, collectStyles, STYLE_RULES_ID } from './sx.js';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** PERCENTAGE: `value` is a percent; FIXED_AMOUNT: an amount in `currency`. */
|
|
2
|
+
export interface ReferralOffer {
|
|
3
|
+
type: 'PERCENTAGE' | 'FIXED_AMOUNT' | string;
|
|
4
|
+
value: string;
|
|
5
|
+
currency: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface ReferralReward {
|
|
8
|
+
type: 'PERCENTAGE_CODE' | 'FIXED_CODE' | 'STORE_CREDIT' | 'POINTS' | string;
|
|
9
|
+
value: string;
|
|
10
|
+
points: number;
|
|
11
|
+
currency: string | null;
|
|
12
|
+
}
|
|
13
|
+
/** The signed-in shopper's own link (they are the advocate). */
|
|
14
|
+
export interface ReferralState {
|
|
15
|
+
enabled: true;
|
|
16
|
+
code: string;
|
|
17
|
+
shareUrl: string;
|
|
18
|
+
offer: ReferralOffer;
|
|
19
|
+
reward: ReferralReward;
|
|
20
|
+
stats: {
|
|
21
|
+
clicks: number;
|
|
22
|
+
friendOrders: number;
|
|
23
|
+
pending: number;
|
|
24
|
+
rewarded: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** What the `?ref=` link the shopper arrived on offers them (a new customer). */
|
|
28
|
+
export interface FriendOffer {
|
|
29
|
+
enabled: true;
|
|
30
|
+
code: string;
|
|
31
|
+
offer: ReferralOffer;
|
|
32
|
+
minimumOrder: string | null;
|
|
33
|
+
advocateFirstName: string | null;
|
|
34
|
+
/** The friend's own code once claimed by email (kept for the session). */
|
|
35
|
+
claimedCode: string | null;
|
|
36
|
+
}
|
|
37
|
+
export type ClaimResult = {
|
|
38
|
+
ok: true;
|
|
39
|
+
code: string;
|
|
40
|
+
} | {
|
|
41
|
+
ok: false;
|
|
42
|
+
error: 'email' | 'self' | 'failed';
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* `state` is null until known, for a guest and with referrals off; `friend` without a `?ref=` code this session,
|
|
46
|
+
* for a returning customer and with referrals off. `claim` (checkout then applies the friend's code) and `invite`
|
|
47
|
+
* (emails the link to a friend) never reject.
|
|
48
|
+
*/
|
|
49
|
+
export interface ReferralsValue {
|
|
50
|
+
state: ReferralState | null;
|
|
51
|
+
friend: FriendOffer | null;
|
|
52
|
+
claim(email: string): Promise<ClaimResult>;
|
|
53
|
+
invite(email: string): Promise<boolean>;
|
|
54
|
+
}
|
|
55
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
56
|
+
export declare const ReferralsContext: import("react").Context<ReferralsValue>;
|
|
57
|
+
export declare const useReferrals: () => ReferralsValue;
|
|
58
|
+
/** One tap per app: each target carries the link (and the message where the app takes text). */
|
|
59
|
+
export declare function shareTargets(url: string, text: string): {
|
|
60
|
+
id: 'line' | 'whatsapp' | 'messenger' | 'x';
|
|
61
|
+
href: string;
|
|
62
|
+
}[];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The store's referral program as the shopper sees it (army #7751): the advocate's link, share targets and stats
|
|
3
|
+
* for the Referral tab, and the friend offer a `?ref=CODE` visit shows. The runtime provides it in the browser
|
|
4
|
+
* (lib/abi/referrals.tsx); on the server, in the editor and with referrals off both states are `null`.
|
|
5
|
+
*/
|
|
6
|
+
import { createContext, useContext } from 'react';
|
|
7
|
+
const OFF = { state: null, friend: null, claim: async () => ({ ok: false, error: 'failed' }), invite: async () => false };
|
|
8
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
9
|
+
export const ReferralsContext = createContext(OFF);
|
|
10
|
+
export const useReferrals = () => useContext(ReferralsContext);
|
|
11
|
+
/** One tap per app: each target carries the link (and the message where the app takes text). */
|
|
12
|
+
export function shareTargets(url, text) {
|
|
13
|
+
const u = encodeURIComponent(url);
|
|
14
|
+
return [
|
|
15
|
+
{ id: 'line', href: `https://social-plugins.line.me/lineit/share?url=${u}&text=${encodeURIComponent(text)}` },
|
|
16
|
+
{ id: 'whatsapp', href: `https://wa.me/?text=${encodeURIComponent(`${text} ${url}`)}` },
|
|
17
|
+
{ id: 'messenger', href: `fb-messenger://share/?link=${u}` },
|
|
18
|
+
{ id: 'x', href: `https://x.com/intent/post?text=${encodeURIComponent(text)}&url=${u}` },
|
|
19
|
+
];
|
|
20
|
+
}
|