@snowcone-app/ui 0.10.22 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowcone-app/ui",
3
- "version": "0.10.22",
3
+ "version": "0.11.1",
4
4
  "description": "React components for merchandise visualization and customization",
5
5
  "keywords": [
6
6
  "react",
@@ -103,7 +103,7 @@
103
103
  "react-instantsearch": "^7.15.5",
104
104
  "react-zoom-pan-pinch": "^3.6.4",
105
105
  "tailwind-merge": "^3.0.0",
106
- "@snowcone-app/sdk": "0.31.6"
106
+ "@snowcone-app/sdk": "0.31.7"
107
107
  },
108
108
  "devDependencies": {
109
109
  "@chromatic-com/storybook": "^4.1.2",
@@ -21,57 +21,13 @@ import { LoadingOverlayPrismCandyInline } from "../components/LoadingOverlayPris
21
21
  import {
22
22
  createDesignForPlacements,
23
23
  type DesignElement,
24
- getMockupUrl,
25
- type Design,
26
24
  resolveVariantId,
27
25
  resolvePreviewVariantId,
28
26
  resolveMockupId,
29
27
  SNOWCONE_HOSTS,
30
28
  type MockupView,
31
29
  } from "@snowcone-app/sdk";
32
-
33
- /** Slugify a placement label into the catalog's `placements[].key` form. */
34
- function slugifyPlacementKey(label: string): string {
35
- return label
36
- .toLowerCase()
37
- .replace(/[^a-z0-9]+/g, "_")
38
- .replace(/^_+|_+$/g, "");
39
- }
40
-
41
- type PlacementLike = { label: string; key?: string; type?: string | null };
42
-
43
- /**
44
- * Convert the SDK's `DesignElement[]` (label-keyed, used by the realtime/canvas
45
- * path) into the public resolver's `Design` (placement-key → fill), so a static
46
- * `<img>` can be built via the **signing resolver** (`getMockupUrl`) rather than
47
- * the unsigned direct-renderer builder. The renderer requires a `seal`, which
48
- * only the resolver adds (ADR-0076).
49
- */
50
- function toPublicDesign(
51
- elements: DesignElement[],
52
- placements: PlacementLike[]
53
- ): Design {
54
- const design: Design = {};
55
- for (const el of elements) {
56
- if (!el.placement) continue;
57
- const match = placements.find((p) => p.label === el.placement);
58
- const key = match?.key || slugifyPlacementKey(el.placement);
59
- if (el.type === "color") {
60
- if (el.hex) design[key] = { color: el.hex };
61
- } else if (el.imageUrl) {
62
- const aligned = el.alignment && el.alignment !== "center";
63
- design[key] =
64
- aligned || el.tiles
65
- ? {
66
- src: el.imageUrl,
67
- ...(aligned ? { align: el.alignment } : {}),
68
- ...(el.tiles ? { tile: el.tiles } : {}),
69
- }
70
- : el.imageUrl;
71
- }
72
- }
73
- return design;
74
- }
30
+ import { heroMockupUrl, type HeroSize, type PlacementLike } from "./heroMockupUrl";
75
31
 
76
32
  /**
77
33
  * Build a static mockup `<img>` URL through the signing resolver (ADR-0076).
@@ -85,9 +41,7 @@ function buildHeroMockupUrl(args: {
85
41
  placements: PlacementLike[];
86
42
  mockupId: string;
87
43
  variantId: string;
88
- /** A render width (16:9), or a crop of the 8:3 strip (`view=`: at its own px, or downscaled to `width`). */
89
- size: { width: number } | { stripView: MockupView; width?: number };
90
- /** Canonical 6-hex background (see MockupBackground) — painted behind alpha scenes. */
44
+ size: HeroSize;
91
45
  bg?: string;
92
46
  }): string | null {
93
47
  const snow =
@@ -95,20 +49,8 @@ function buildHeroMockupUrl(args: {
95
49
  (window as unknown as { snowcone?: { shop?: string; resolver?: string } })
96
50
  .snowcone) ||
97
51
  {};
98
- if (!snow.shop) return null;
99
- const publicDesign = toPublicDesign(args.design, args.placements);
100
- if (Object.keys(publicDesign).length === 0) return null;
101
- return getMockupUrl(args.productCode, {
102
- shop: snow.shop,
103
- design: publicDesign,
104
- ...("stripView" in args.size
105
- ? { aspect: "8:3" as const, stripView: args.size.stripView, ...(args.size.width !== undefined ? { width: args.size.width } : {}) }
106
- : { width: args.size.width }),
107
- view: args.mockupId,
108
- variant: args.variantId,
109
- ...(args.bg ? { bg: args.bg } : {}),
110
- ...(snow.resolver ? { base: snow.resolver } : {}),
111
- });
52
+ // the one builder the server's first-hero preload uses too (heroMockupUrl.ts)
53
+ return heroMockupUrl({ ...args, shop: snow.shop, resolver: snow.resolver });
112
54
  }
113
55
  /** A crop of the strip needs no measured width: it comes at its own px (or the width the page asks). */
114
56
  function isCrop(stripView: MockupView | undefined): stripView is MockupView {
@@ -213,6 +155,12 @@ export interface HeroProductImageProps {
213
155
  stripView?: MockupView | { view: MockupView; width: number };
214
156
  /** Called with the URL of each picture once it is ON SCREEN (its <img> loaded), not when it is asked for. */
215
157
  onShown?: (url: string) => void;
158
+ /**
159
+ * URLs the SERVER already signed (the PDP's first-hero preload, heroMockupUrl.ts firstMobileHeroUrl): when the URL this
160
+ * image builds equals one's `url`, its `signed` is used at once (no /api/sign round trip) — the same bytes the page
161
+ * preloaded, so the hero is fetched once. Any other URL is signed as usual. Data: `data-hero-presigned`.
162
+ */
163
+ presigned?: ReadonlyArray<{ url: string; signed: string }>;
216
164
  }
217
165
 
218
166
  /**
@@ -265,6 +213,7 @@ export const HeroProductImage = memo(function HeroProductImage({
265
213
  fit = "cover",
266
214
  stripView: stripViewProp,
267
215
  onShown,
216
+ presigned,
268
217
  }: HeroProductImageProps) {
269
218
  // The strip ask as primitives (stable deps): the crop or the whole strip, and the crop's downscale width.
270
219
  const stripView = typeof stripViewProp === "object" ? stripViewProp.view : stripViewProp;
@@ -454,6 +403,8 @@ export const HeroProductImage = memo(function HeroProductImage({
454
403
  onUrlGeneratedRef.current = onUrlGenerated;
455
404
  const onShownRef = useRef(onShown);
456
405
  onShownRef.current = onShown;
406
+ // the server's signed URL was used (presigned): stamped data-hero-presigned for the preload gate
407
+ const presignedUsedRef = useRef(false);
457
408
 
458
409
  // Track layers.length in a ref so we can read it without depending on it
459
410
  // This prevents effect re-runs when layers change (which would cause duplicate requests)
@@ -1049,7 +1000,11 @@ export const HeroProductImage = memo(function HeroProductImage({
1049
1000
  snowcone?: { signMockupUrl?: (u: string) => Promise<string> };
1050
1001
  }).snowcone?.signMockupUrl
1051
1002
  : undefined;
1052
- if (signer) {
1003
+ const serverSigned = presigned?.find((p) => p.url === url)?.signed;
1004
+ if (serverSigned) {
1005
+ presignedUsedRef.current = true;
1006
+ render(serverSigned);
1007
+ } else if (signer) {
1053
1008
  signer(url)
1054
1009
  .then((signed) => render(signed || url))
1055
1010
  .catch(() => render(url));
@@ -1277,6 +1232,7 @@ export const HeroProductImage = memo(function HeroProductImage({
1277
1232
  data-fit={fit}
1278
1233
  data-strip-view={stripView}
1279
1234
  data-strip-width={cropWidth}
1235
+ data-hero-presigned={presignedUsedRef.current ? "server" : undefined}
1280
1236
  data-picture-failed={imgFailed ? "E_PICTURE_UNDISPLAYED" : undefined}
1281
1237
  data-picture-failed-src={failedSrc}
1282
1238
  data-mockup-unavailable={unavailable ? unavailable.code : undefined}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * The PDP's server preloads the phone's FIRST hero (firstMobileHeroUrl) so it downloads before the JS (lane shapes,
3
+ * 2026-11-19: Slow 4G hero requested at 3.8 s, painted at 9.1 s). The server must build EXACTLY the URL the client will
4
+ * request, or the phone downloads the hero twice. This renders the real client path (Product provider with the catalog
5
+ * record + MockupBackground + HeroProductImage with the phone carousel's props) on real catalog records and fails when
6
+ * its URL differs from the server's. Known cases: slides AKTGA7 (two placements, sizes), phone case BC43BE, note cube
7
+ * NBYVCX, notebook KVJVVA (three placements).
8
+ */
9
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10
+ import React from "react";
11
+ import { render, waitFor, cleanup } from "@testing-library/react";
12
+ import { ordinaryMockups, type CatalogProduct } from "@snowcone-app/sdk";
13
+ import { HeroProductImage } from "../HeroProductImage";
14
+ import { Product } from "../../patterns/Product";
15
+ import { MockupBackground } from "../../patterns/MockupBackground";
16
+ import { firstMobileHeroUrl as firstMobileHeroUrlInBrowser, MOBILE_HERO_WIDTH } from "../heroMockupUrl";
17
+
18
+ /** The SERVER's URL: built where there is no window, as page.tsx builds it (a window changes what the sdk does to a
19
+ * relative artwork; 2026-11-19 the jsdom-built "server" URL matched the client while the real server's did not). */
20
+ function firstMobileHeroUrl(args: Parameters<typeof firstMobileHeroUrlInBrowser>[0]): string | null {
21
+ vi.stubGlobal("window", undefined);
22
+ try { return firstMobileHeroUrlInBrowser(args); } finally { vi.unstubAllGlobals(); }
23
+ }
24
+ import AKTGA7 from "./fixtures/catalog-AKTGA7.json";
25
+ import BC43BE from "./fixtures/catalog-BC43BE.json";
26
+ import NBYVCX from "./fixtures/catalog-NBYVCX.json";
27
+ import KVJVVA from "./fixtures/catalog-KVJVVA.json";
28
+
29
+ const SHOP = "2e2c853b83";
30
+ const PRODUCTS = { AKTGA7, BC43BE, NBYVCX, KVJVVA } as unknown as Record<string, CatalogProduct & { mockups: Array<{ id: string }>; placements: Array<{ label: string }> }>;
31
+
32
+ /** The phone carousel's first slide, as page-client + MobileProductCarousel mount it. */
33
+ function FirstSlide({ product, bg, presigned }: { product: (typeof PRODUCTS)[string]; bg: string; presigned?: Array<{ url: string; signed: string }> }) {
34
+ const first = ordinaryMockups(product.mockups)[0];
35
+ return (
36
+ <Product productId={product.id} productData={product} initialPlacements={{ Front: { imageUrl: "default", alignment: "center" as never } }} dataMode="auto">
37
+ <MockupBackground bg={bg}>
38
+ <HeroProductImage
39
+ productId={product.id}
40
+ artwork={{ type: "regular", src: "default" } as never}
41
+ placement={product.placements[0].label}
42
+ mockupId={first.id}
43
+ width={MOBILE_HERO_WIDTH}
44
+ viewportPolicy
45
+ presigned={presigned}
46
+ />
47
+ </MockupBackground>
48
+ </Product>
49
+ );
50
+ }
51
+
52
+ async function requested(container: HTMLElement): Promise<string> {
53
+ await waitFor(() => expect(container.querySelector("img")?.getAttribute("src")).toBeTruthy());
54
+ return container.querySelector("img")!.getAttribute("src")!;
55
+ }
56
+
57
+ describe("the server's first phone hero URL is the URL the client requests (no double download)", () => {
58
+ let signer: ReturnType<typeof vi.fn>;
59
+ beforeEach(() => {
60
+ signer = vi.fn(async (u: string) => `${u}&signature=client`);
61
+ (window as unknown as { snowcone?: unknown }).snowcone = { shop: SHOP, signMockupUrl: signer };
62
+ });
63
+ afterEach(() => {
64
+ cleanup();
65
+ delete (window as unknown as { snowcone?: unknown }).snowcone;
66
+ });
67
+
68
+ for (const [id, product] of Object.entries(PRODUCTS)) {
69
+ for (const bg of ["f5f5f5", "0a0a0a"]) {
70
+ it(`${id} bg ${bg}: client URL === firstMobileHeroUrl`, async () => {
71
+ const server = firstMobileHeroUrl({ product, artworkSrc: "default", shop: SHOP, bg });
72
+ expect(server, "the server builds a URL for every live set product").toBeTruthy();
73
+ const { container } = render(<FirstSlide product={product} bg={bg} />);
74
+ const src = await requested(container);
75
+ expect(src).toBe(`${server}&signature=client`);
76
+ expect(new URL(server!).searchParams.get("width")).toBe(String(MOBILE_HERO_WIDTH));
77
+ });
78
+ }
79
+ }
80
+
81
+ it("a matching server-signed URL is used at once: no /api/sign round trip, stamped data-hero-presigned", async () => {
82
+ const product = PRODUCTS.AKTGA7;
83
+ const server = firstMobileHeroUrl({ product, artworkSrc: "default", shop: SHOP, bg: "f5f5f5" })!;
84
+ const { container } = render(<FirstSlide product={product} bg="f5f5f5" presigned={[{ url: server, signed: `${server}&signature=server` }]} />);
85
+ expect(await requested(container)).toBe(`${server}&signature=server`);
86
+ expect(signer).not.toHaveBeenCalled();
87
+ await waitFor(() => expect((container.querySelector("[data-hero-image]") as HTMLElement).dataset.heroPresigned).toBe("server"));
88
+ });
89
+
90
+ it("a server URL for another theme (bg) is not used: the client signs its own", async () => {
91
+ const product = PRODUCTS.AKTGA7;
92
+ const light = firstMobileHeroUrl({ product, artworkSrc: "default", shop: SHOP, bg: "f5f5f5" })!;
93
+ const { container } = render(<FirstSlide product={product} bg="0a0a0a" presigned={[{ url: light, signed: `${light}&signature=server` }]} />);
94
+ expect(await requested(container)).toMatch(/bg=0a0a0a.*&signature=client$/);
95
+ expect(signer).toHaveBeenCalled();
96
+ });
97
+
98
+ it("no shop or no artwork: no server URL (nothing to preload)", () => {
99
+ expect(firstMobileHeroUrl({ product: PRODUCTS.AKTGA7, artworkSrc: "default", shop: undefined })).toBeNull();
100
+ expect(firstMobileHeroUrl({ product: PRODUCTS.AKTGA7, artworkSrc: "", shop: SHOP })).toBeNull();
101
+ });
102
+ });
@@ -0,0 +1,298 @@
1
+ {
2
+ "id": "AKTGA7",
3
+ "name": "Custom Slide Sandals",
4
+ "slug": "custom-slide-sandals",
5
+ "tags": [
6
+ "Shoes"
7
+ ],
8
+ "price": 5900,
9
+ "mockups": [
10
+ {
11
+ "id": "9YTBBN",
12
+ "gvids": [
13
+ "dxwapt"
14
+ ],
15
+ "strip": {
16
+ "shape": "8:3",
17
+ "productX": 0.4998,
18
+ "heightPx": 2160
19
+ }
20
+ },
21
+ {
22
+ "id": "NK45AX",
23
+ "gvids": [
24
+ "dxwapt"
25
+ ],
26
+ "strip": {
27
+ "shape": "8:3",
28
+ "productX": 0.4998,
29
+ "heightPx": 2160
30
+ }
31
+ },
32
+ {
33
+ "id": "WJKMVE",
34
+ "gvids": [
35
+ "dxwapt"
36
+ ],
37
+ "strip": {
38
+ "shape": "8:3",
39
+ "productX": 0.5064,
40
+ "heightPx": 2160
41
+ }
42
+ },
43
+ {
44
+ "id": "MWAMXB",
45
+ "gvids": [
46
+ "dxwapt"
47
+ ],
48
+ "strip": {
49
+ "shape": "8:3",
50
+ "productX": 0.499,
51
+ "heightPx": 2160
52
+ }
53
+ },
54
+ {
55
+ "id": "MF7VJQ",
56
+ "gvids": [
57
+ "dxwapt"
58
+ ]
59
+ },
60
+ {
61
+ "id": "VEKUBC",
62
+ "gvids": [
63
+ "dxwapt"
64
+ ]
65
+ },
66
+ {
67
+ "id": "4PAQEX",
68
+ "gvids": [
69
+ "dxwapt"
70
+ ]
71
+ },
72
+ {
73
+ "id": "6YAWKD",
74
+ "gvids": [
75
+ "dxwapt"
76
+ ]
77
+ },
78
+ {
79
+ "id": "C79JMJ",
80
+ "gvids": [
81
+ "dxwapt"
82
+ ]
83
+ },
84
+ {
85
+ "id": "FRTFQX",
86
+ "gvids": [
87
+ "dxwapt"
88
+ ]
89
+ }
90
+ ],
91
+ "options": {
92
+ "attributes": {
93
+ "size": {
94
+ "choices": [
95
+ {
96
+ "label": "Youth 13",
97
+ "printableValueId": "Y13"
98
+ },
99
+ {
100
+ "label": "Youth 1",
101
+ "printableValueId": "Y1"
102
+ },
103
+ {
104
+ "label": "Youth 2",
105
+ "printableValueId": "Y2"
106
+ },
107
+ {
108
+ "label": "Youth 3",
109
+ "printableValueId": "Y3"
110
+ },
111
+ {
112
+ "label": "Youth 4",
113
+ "printableValueId": "Y4"
114
+ },
115
+ {
116
+ "label": "Youth 5",
117
+ "printableValueId": "Y5"
118
+ },
119
+ {
120
+ "label": "Women's 6",
121
+ "printableValueId": "W6"
122
+ },
123
+ {
124
+ "label": "Women's 7",
125
+ "printableValueId": "W7"
126
+ },
127
+ {
128
+ "label": "Women's 8",
129
+ "printableValueId": "W8"
130
+ },
131
+ {
132
+ "label": "Women's 9",
133
+ "printableValueId": "W9"
134
+ },
135
+ {
136
+ "label": "Women's 10",
137
+ "printableValueId": "W10"
138
+ },
139
+ {
140
+ "label": "Women's 11",
141
+ "printableValueId": "W11"
142
+ },
143
+ {
144
+ "label": "Women's 12",
145
+ "printableValueId": "W12"
146
+ },
147
+ {
148
+ "label": "Men's 6",
149
+ "printableValueId": "M6"
150
+ },
151
+ {
152
+ "label": "Men's 7",
153
+ "printableValueId": "M7"
154
+ },
155
+ {
156
+ "label": "Men's 8",
157
+ "printableValueId": "M8"
158
+ },
159
+ {
160
+ "label": "Men's 9",
161
+ "printableValueId": "M9"
162
+ },
163
+ {
164
+ "label": "Men's 10",
165
+ "printableValueId": "M10"
166
+ },
167
+ {
168
+ "label": "Men's 11",
169
+ "printableValueId": "M11"
170
+ },
171
+ {
172
+ "label": "Men's 12",
173
+ "printableValueId": "M12"
174
+ },
175
+ {
176
+ "label": "Men's 13",
177
+ "printableValueId": "M13"
178
+ },
179
+ {
180
+ "label": "Men's 14",
181
+ "printableValueId": "M14"
182
+ }
183
+ ],
184
+ "printableOption": "size",
185
+ "affectsCombinations": false
186
+ },
187
+ "color": {
188
+ "choices": [
189
+ {
190
+ "label": "White",
191
+ "printableValueId": "WHT"
192
+ }
193
+ ],
194
+ "printableOption": "color",
195
+ "affectsCombinations": false
196
+ }
197
+ },
198
+ "combinations": [
199
+ {
200
+ "price": 5900,
201
+ "variantId": "dxwapt",
202
+ "printableId": "NB3JMG"
203
+ }
204
+ ],
205
+ "attributesList": [
206
+ "size",
207
+ "color"
208
+ ]
209
+ },
210
+ "care_guide": null,
211
+ "description": [
212
+ "Slide sandals with your design printed edge to edge on both straps by dye sublimation. The straps are polyester canvas backed with SBR neoprene, and the polyurethane soles are not printed. Made to order in Alabama, in full US sizes for men, women and youth."
213
+ ],
214
+ "key_features": [
215
+ "<b>\u2b50\ufe0f Your Art on Both Straps</b> \u2013 Each strap is printed edge to edge by dye sublimation, and the soles stay plain.",
216
+ "",
217
+ "<b>\u2b50\ufe0f Cushioned Straps</b> \u2013 100% polyester canvas backed with SBR neoprene.",
218
+ "",
219
+ "<b>\u2b50\ufe0f White Polyurethane Soles</b> \u2013 Solid 100% polyurethane soles in white.",
220
+ "",
221
+ "<b>\u2b50\ufe0f True to Size</b> \u2013 Full US sizes: Youth 13 and 1 to 5, Women's 6 to 12, Men's 6 to 14.",
222
+ "",
223
+ "<b>\u2b50\ufe0f Made to Order in Alabama</b> \u2013 Printed and made in Alabama when you order."
224
+ ],
225
+ "days_to_deliver": "5 - 6",
226
+ "ships_from_country": "US",
227
+ "variants": [
228
+ {
229
+ "gvid": "dxwapt",
230
+ "gvidForMockup": null,
231
+ "placements": [
232
+ {
233
+ "label": "Left",
234
+ "width": 2669,
235
+ "height": 1068
236
+ },
237
+ {
238
+ "label": "Right",
239
+ "width": 2669,
240
+ "height": 1068
241
+ }
242
+ ]
243
+ }
244
+ ],
245
+ "placements": [
246
+ {
247
+ "label": "Left",
248
+ "key": "left",
249
+ "type": "image",
250
+ "width": 2669,
251
+ "height": 1068,
252
+ "defaultScaleMode": "fill",
253
+ "fitMarginTop": 0,
254
+ "fitMarginRight": 0,
255
+ "fitMarginBottom": 0,
256
+ "fitMarginLeft": 0,
257
+ "fitAlign": "c",
258
+ "align": "c",
259
+ "scale": 1,
260
+ "offsetX": 0,
261
+ "offsetY": 0
262
+ },
263
+ {
264
+ "label": "Right",
265
+ "key": "right",
266
+ "type": "image",
267
+ "width": 2669,
268
+ "height": 1068,
269
+ "defaultScaleMode": "fill",
270
+ "fitMarginTop": 0,
271
+ "fitMarginRight": 0,
272
+ "fitMarginBottom": 0,
273
+ "fitMarginLeft": 0,
274
+ "fitAlign": "c",
275
+ "align": "c",
276
+ "scale": 1,
277
+ "offsetX": 0,
278
+ "offsetY": 0
279
+ }
280
+ ],
281
+ "defaultGvid": "dxwapt",
282
+ "prices": {},
283
+ "currencies": {},
284
+ "ships_to": [],
285
+ "requiredPlacements": [
286
+ {
287
+ "label": "Left",
288
+ "type": "image",
289
+ "autoFilledByVariant": false
290
+ },
291
+ {
292
+ "label": "Right",
293
+ "type": "image",
294
+ "autoFilledByVariant": false
295
+ }
296
+ ],
297
+ "album": null
298
+ }