@snowcone-app/ui 0.11.13 → 0.11.14
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/CHANGELOG.md +6 -0
- package/dist/index.cjs +704 -672
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +352 -324
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/composed/HeroProductImage.tsx +22 -11
- package/src/composed/__tests__/HeroProductImage.placement-art.test.tsx +102 -0
- package/src/index.ts +8 -0
- package/src/patterns/PlacementArt.tsx +49 -0
package/package.json
CHANGED
|
@@ -17,6 +17,7 @@ import { useShopOptional } from "../patterns/ShopProvider";
|
|
|
17
17
|
import { useRealtimeOptional } from "../patterns/RealtimeProvider";
|
|
18
18
|
import { useMockupPriorityOptional } from "../patterns/MockupPriorityProvider";
|
|
19
19
|
import { useMockupBackgroundOptional } from "../patterns/MockupBackground";
|
|
20
|
+
import { placementArtFor, usePlacementArtOptional } from "../patterns/PlacementArt";
|
|
20
21
|
import { LoadingOverlayPrismCandyInline } from "../components/LoadingOverlayPrismCandy";
|
|
21
22
|
import {
|
|
22
23
|
createDesignForPlacements,
|
|
@@ -52,6 +53,10 @@ function buildHeroMockupUrl(args: {
|
|
|
52
53
|
// the one builder the server's first-hero preload uses too (heroMockupUrl.ts)
|
|
53
54
|
return heroMockupUrl({ ...args, shop: snow.shop, resolver: snow.resolver });
|
|
54
55
|
}
|
|
56
|
+
/** The art a picture is of: its artwork src, and its per-placement map (PlacementArt) when there is one. */
|
|
57
|
+
function artIdentity(src: string | undefined, placementArtKey: string): string | undefined {
|
|
58
|
+
return placementArtKey ? `${src ?? ""}|${placementArtKey}` : src;
|
|
59
|
+
}
|
|
55
60
|
/** A crop of the strip needs no measured width: it comes at its own px (or the width the page asks). */
|
|
56
61
|
function isCrop(stripView: MockupView | undefined): stripView is MockupView {
|
|
57
62
|
return stripView !== undefined;
|
|
@@ -241,6 +246,10 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
241
246
|
const priorityContext = useMockupPriorityOptional();
|
|
242
247
|
// Page background baked into static mockup URLs (see MockupBackground).
|
|
243
248
|
const mockupBg = useMockupBackgroundOptional();
|
|
249
|
+
// A picture per placement (PlacementArt: a product printed piece by piece), else the one artwork on every placement.
|
|
250
|
+
const placementArt = usePlacementArtOptional();
|
|
251
|
+
// What the picture shows: the artwork, and the per-placement map when there is one (a new map is new art).
|
|
252
|
+
const placementArtKey = placementArt ? JSON.stringify(placementArt) : "";
|
|
244
253
|
const priorityContextRef = useRef(priorityContext);
|
|
245
254
|
priorityContextRef.current = priorityContext;
|
|
246
255
|
|
|
@@ -313,13 +322,13 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
313
322
|
// Build placementDesigns with alignment info from design context
|
|
314
323
|
// This ensures alignment changes trigger URL regeneration
|
|
315
324
|
const placementDesigns: Record<string, PlacementDesign> = {};
|
|
316
|
-
if (designContext?.getPlacementDesign && allPlacements) {
|
|
325
|
+
if ((designContext?.getPlacementDesign || placementArt) && allPlacements) {
|
|
317
326
|
for (const p of allPlacements) {
|
|
318
327
|
// Skip color placements
|
|
319
328
|
if (p.type === "color") continue;
|
|
320
|
-
const design = designContext
|
|
329
|
+
const design = designContext?.getPlacementDesign(p.label);
|
|
321
330
|
placementDesigns[p.label] = {
|
|
322
|
-
imageUrl: effectiveArtworkSrc,
|
|
331
|
+
imageUrl: placementArtFor(placementArt, p, effectiveArtworkSrc),
|
|
323
332
|
alignment: design?.alignment || "center",
|
|
324
333
|
} as PlacementDesign;
|
|
325
334
|
}
|
|
@@ -360,6 +369,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
360
369
|
images,
|
|
361
370
|
width,
|
|
362
371
|
effects,
|
|
372
|
+
placementArt,
|
|
363
373
|
// Include placements to trigger recomputation when alignment changes
|
|
364
374
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
365
375
|
JSON.stringify(designContext?.placements),
|
|
@@ -464,7 +474,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
464
474
|
|
|
465
475
|
lastUrlRef.current = realtimeUrl;
|
|
466
476
|
// Track which artwork this realtimeUrl is for (used to detect stale realtime URLs)
|
|
467
|
-
realtimeArtworkSrcRef.current = artworkSrc || selectedArtworkSrc;
|
|
477
|
+
realtimeArtworkSrcRef.current = artIdentity(artworkSrc || selectedArtworkSrc, placementArtKey);
|
|
468
478
|
// addLayerDirectly handles shimmer timing internally (500ms delay)
|
|
469
479
|
// SAFARI FIX: Skip transition for initial load to prevent flash
|
|
470
480
|
const isInitialLoad = layersLengthRef.current === 0;
|
|
@@ -776,7 +786,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
776
786
|
// Detect artwork changes and reset local URL tracking
|
|
777
787
|
// This ensures we don't skip updates when artwork changes
|
|
778
788
|
useEffect(() => {
|
|
779
|
-
const currentSrc = artworkSrc || selectedArtworkSrc;
|
|
789
|
+
const currentSrc = artIdentity(artworkSrc || selectedArtworkSrc, placementArtKey);
|
|
780
790
|
if (currentSrc !== prevArtworkSrcRef.current) {
|
|
781
791
|
// Artwork changed - reset local tracking so next update is applied
|
|
782
792
|
lastUrlRef.current = null;
|
|
@@ -788,7 +798,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
788
798
|
// Reset realtime artwork tracking so stale URLs are detected correctly
|
|
789
799
|
realtimeArtworkSrcRef.current = undefined;
|
|
790
800
|
}
|
|
791
|
-
}, [artworkSrc, selectedArtworkSrc]);
|
|
801
|
+
}, [artworkSrc, selectedArtworkSrc, placementArtKey]);
|
|
792
802
|
|
|
793
803
|
// Keep refs to objects we need to pass through (these don't need to trigger re-creation)
|
|
794
804
|
const contextSelectionRef = useRef(context?.selection);
|
|
@@ -814,15 +824,15 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
814
824
|
}
|
|
815
825
|
|
|
816
826
|
const placementDesigns: Record<string, PlacementDesign> = {};
|
|
817
|
-
if (getPlacementDesign && placements) {
|
|
827
|
+
if ((getPlacementDesign || placementArt) && placements) {
|
|
818
828
|
for (const p of placements) {
|
|
819
829
|
// Skip color placements - treat null/undefined/missing type as "image" (default)
|
|
820
830
|
if (p.type === "color") continue;
|
|
821
|
-
const design = getPlacementDesign(p.label);
|
|
822
|
-
// Always create a placement design with current artwork
|
|
831
|
+
const design = getPlacementDesign?.(p.label);
|
|
832
|
+
// Always create a placement design with current artwork (its own picture under PlacementArt)
|
|
823
833
|
// Use alignment from context if available, otherwise default to center
|
|
824
834
|
placementDesigns[p.label] = {
|
|
825
|
-
imageUrl: effectiveArtworkSrc,
|
|
835
|
+
imageUrl: placementArtFor(placementArt, p, effectiveArtworkSrc),
|
|
826
836
|
alignment: design?.alignment || "center",
|
|
827
837
|
};
|
|
828
838
|
}
|
|
@@ -866,6 +876,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
866
876
|
},
|
|
867
877
|
[
|
|
868
878
|
getPlacementDesign,
|
|
879
|
+
placementArt,
|
|
869
880
|
artworkSrc,
|
|
870
881
|
artworkType,
|
|
871
882
|
artworkTileCount,
|
|
@@ -969,7 +980,7 @@ export const HeroProductImage = memo(function HeroProductImage({
|
|
|
969
980
|
|
|
970
981
|
// Skip adding layer if realtimeUrl is for the CURRENT artwork
|
|
971
982
|
// If artwork changed, the realtimeUrl is stale and we should show the new static URL
|
|
972
|
-
const currentArtworkSrc = artworkSrc || selectedArtworkSrc;
|
|
983
|
+
const currentArtworkSrc = artIdentity(artworkSrc || selectedArtworkSrc, placementArtKey);
|
|
973
984
|
const isRealtimeUrlFresh = realtimeUrl && realtimeArtworkSrcRef.current === currentArtworkSrc;
|
|
974
985
|
|
|
975
986
|
if (isRealtimeUrlFresh) {
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PlacementArt: a picture per placement (a product printed piece by piece, www lib/chat-pieces.ts). Without it a hero
|
|
3
|
+
* sends its ONE artwork to every image placement; inside it each placement asks for its own picture (`asset.<key>`).
|
|
4
|
+
* Known case: notebook KVJVVA (three placements: back_cover, front_cover, spine).
|
|
5
|
+
*/
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
7
|
+
import React from "react";
|
|
8
|
+
import { render, waitFor, cleanup } from "@testing-library/react";
|
|
9
|
+
import { ordinaryMockups, type CatalogProduct } from "@snowcone-app/sdk";
|
|
10
|
+
import { HeroProductImage } from "../HeroProductImage";
|
|
11
|
+
import { Product } from "../../patterns/Product";
|
|
12
|
+
import { PlacementArt, placementArtFor } from "../../patterns/PlacementArt";
|
|
13
|
+
import { MOBILE_HERO_WIDTH } from "../heroMockupUrl";
|
|
14
|
+
import KVJVVA from "./fixtures/catalog-KVJVVA.json";
|
|
15
|
+
|
|
16
|
+
const SHOP = "2e2c853b83";
|
|
17
|
+
const product = KVJVVA as unknown as CatalogProduct & { id: string; mockups: Array<{ id: string }>; placements: Array<{ label: string; key?: string }> };
|
|
18
|
+
const ONE = "https://agentdraft.storage.snowcone.app/agent-drafts/drf_ZA_IM71v916y.webp";
|
|
19
|
+
const ART = {
|
|
20
|
+
back_cover: "https://agentdraft.storage.snowcone.app/agent-drafts/drf_PLACEDcopy01.webp",
|
|
21
|
+
front_cover: "https://agentdraft.storage.snowcone.app/agent-drafts/drf_PLACEDcopy01-mark.webp",
|
|
22
|
+
spine: "https://agentdraft.storage.snowcone.app/agent-drafts/drf_PLACEDcopy01-ground.png",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function Hero({ art, onUrl, realtimeUrl }: { art: Record<string, string> | null | undefined; onUrl?: (url: string) => void; realtimeUrl?: string }) {
|
|
26
|
+
const hero = (
|
|
27
|
+
<HeroProductImage
|
|
28
|
+
productId={product.id}
|
|
29
|
+
artwork={{ type: "regular", src: ONE }}
|
|
30
|
+
placement={product.placements[0].label}
|
|
31
|
+
mockupId={ordinaryMockups(product.mockups)[0].id}
|
|
32
|
+
width={MOBILE_HERO_WIDTH}
|
|
33
|
+
onUrlGenerated={onUrl}
|
|
34
|
+
realtimeUrl={realtimeUrl}
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
return (
|
|
38
|
+
<Product productId={product.id} productData={product} initialPlacements={{ Front: { imageUrl: ONE, alignment: "center" as never } }} dataMode="auto">
|
|
39
|
+
{art === undefined ? hero : <PlacementArt art={art}>{hero}</PlacementArt>}
|
|
40
|
+
</Product>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function requested(container: HTMLElement): Promise<URL> {
|
|
45
|
+
await waitFor(() => expect(container.querySelector("img")?.getAttribute("src")).toBeTruthy());
|
|
46
|
+
return new URL(container.querySelector("img")!.getAttribute("src")!);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe("PlacementArt: each placement its own picture", () => {
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
(window as unknown as { snowcone?: unknown }).snowcone = { shop: SHOP, signMockupUrl: vi.fn(async (u: string) => `${u}&signature=client`) };
|
|
52
|
+
});
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
cleanup();
|
|
55
|
+
delete (window as unknown as { snowcone?: unknown }).snowcone;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("inside it, every placement asks for its own picture", async () => {
|
|
59
|
+
const url = await requested(render(<Hero art={ART} />).container);
|
|
60
|
+
for (const [key, src] of Object.entries(ART)) expect(url.searchParams.get(`asset.${key}`)).toBe(src);
|
|
61
|
+
expect([...url.searchParams.values()]).not.toContain(ONE);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("without it (or with art null), the one artwork goes on every placement, as before (wrong case for a pieced product)", async () => {
|
|
65
|
+
for (const art of [undefined, null]) {
|
|
66
|
+
const url = await requested(render(<Hero art={art} />).container);
|
|
67
|
+
for (const key of Object.keys(ART)) expect(url.searchParams.get(`asset.${key}`)).toBe(ONE);
|
|
68
|
+
cleanup();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("a map that arrives after the first picture (the page asked for it) asks for the pieced picture", async () => {
|
|
73
|
+
// the URL the hero asks for next (its new layer: jsdom never loads it, so it is read where the hero reports it)
|
|
74
|
+
const asked: string[] = [];
|
|
75
|
+
const onUrl = (u: string) => void asked.push(new URL(u).searchParams.get("asset.spine") ?? "");
|
|
76
|
+
const { container, rerender } = render(<Hero art={null} onUrl={onUrl} />);
|
|
77
|
+
expect((await requested(container)).searchParams.get("asset.spine")).toBe(ONE);
|
|
78
|
+
rerender(<Hero art={ART} onUrl={onUrl} />);
|
|
79
|
+
await waitFor(() => expect(asked.at(-1)).toBe(ART.spine));
|
|
80
|
+
expect(asked[0]).toBe(ONE);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("the phone carousel hands back the spread picture it was given (realtimeUrl): the map still replaces it", async () => {
|
|
84
|
+
const urls: string[] = [];
|
|
85
|
+
const onUrl = (u: string) => void urls.push(u);
|
|
86
|
+
const { container, rerender } = render(<Hero art={null} onUrl={onUrl} />);
|
|
87
|
+
await requested(container);
|
|
88
|
+
await waitFor(() => expect(urls.length).toBeGreaterThan(0));
|
|
89
|
+
const spread = urls.at(-1)!;
|
|
90
|
+
// MobileProductCarousel passes the URL the hero generated back as realtimeUrl on its next render
|
|
91
|
+
rerender(<Hero art={null} onUrl={onUrl} realtimeUrl={spread} />);
|
|
92
|
+
rerender(<Hero art={ART} onUrl={onUrl} realtimeUrl={spread} />);
|
|
93
|
+
await waitFor(() => expect(new URL(urls.at(-1)!).searchParams.get("asset.spine")).toBe(ART.spine));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("placementArtFor: by key, else the slugified label, else the hero's own artwork", () => {
|
|
97
|
+
expect(placementArtFor(ART, { label: "Back Cover", key: "back_cover" }, ONE)).toBe(ART.back_cover);
|
|
98
|
+
expect(placementArtFor(ART, { label: "Front Cover" }, ONE)).toBe(ART.front_cover);
|
|
99
|
+
expect(placementArtFor(ART, { label: "Inside", key: "inside" }, ONE)).toBe(ONE);
|
|
100
|
+
expect(placementArtFor(null, { label: "Spine", key: "spine" }, ONE)).toBe(ONE);
|
|
101
|
+
});
|
|
102
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -177,6 +177,14 @@ export {
|
|
|
177
177
|
} from "./patterns/MockupBackground";
|
|
178
178
|
export type { MockupBackgroundProps } from "./patterns/MockupBackground";
|
|
179
179
|
|
|
180
|
+
// PlacementArt - a picture per placement for the heroes inside it (asset.<key>)
|
|
181
|
+
export {
|
|
182
|
+
PlacementArt,
|
|
183
|
+
usePlacementArtOptional,
|
|
184
|
+
placementArtFor,
|
|
185
|
+
} from "./patterns/PlacementArt";
|
|
186
|
+
export type { PlacementArtProps, PlacementArtMap } from "./patterns/PlacementArt";
|
|
187
|
+
|
|
180
188
|
// ProductPageProvider - Quick-start providers for product pages
|
|
181
189
|
export {
|
|
182
190
|
ProductPageProvider,
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* PlacementArt — a picture per placement for every `HeroProductImage` inside it.
|
|
5
|
+
*
|
|
6
|
+
* By default a hero sends its ONE artwork to every image placement of the product. On a product printed piece by
|
|
7
|
+
* piece (an all-over zip hoodie) that spreads one picture over every cut piece, so the zip runs through it. Wrap the
|
|
8
|
+
* heroes in `<PlacementArt art={{ back: url1, front_left: url2, … }}>` and each placement gets its own picture
|
|
9
|
+
* (`asset.<key>` on the mockup URL), keyed by the catalog's placement key (`placements[].key`).
|
|
10
|
+
*
|
|
11
|
+
* `art={null}` (or no provider) leaves heroes as they are. A placement the map does not name keeps the hero's own
|
|
12
|
+
* artwork; a caller that needs every piece covered checks that before it passes the map.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import React, { createContext, useContext, useMemo } from "react";
|
|
16
|
+
import { slugifyPlacementKey } from "../composed/heroMockupUrl";
|
|
17
|
+
|
|
18
|
+
/** Placement key → picture URL. */
|
|
19
|
+
export type PlacementArtMap = Readonly<Record<string, string>>;
|
|
20
|
+
|
|
21
|
+
const PlacementArtContext = createContext<PlacementArtMap | null>(null);
|
|
22
|
+
|
|
23
|
+
export interface PlacementArtProps {
|
|
24
|
+
/** Placement key → picture, or null to leave every hero on its own artwork. */
|
|
25
|
+
art: PlacementArtMap | null;
|
|
26
|
+
children: React.ReactNode;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function PlacementArt({ art, children }: PlacementArtProps) {
|
|
30
|
+
// a stable value for an unchanged map, so heroes do not rebuild their URLs on every parent render
|
|
31
|
+
const key = art ? JSON.stringify(art) : "";
|
|
32
|
+
const value = useMemo(() => (key ? (JSON.parse(key) as PlacementArtMap) : null), [key]);
|
|
33
|
+
return <PlacementArtContext.Provider value={value}>{children}</PlacementArtContext.Provider>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The nearest PlacementArt's map, or null outside one. */
|
|
37
|
+
export function usePlacementArtOptional(): PlacementArtMap | null {
|
|
38
|
+
return useContext(PlacementArtContext);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The picture a placement shows: the map's (by the placement's key, else its slugified label), else `fallback`. */
|
|
42
|
+
export function placementArtFor(
|
|
43
|
+
art: PlacementArtMap | null,
|
|
44
|
+
placement: { label: string; key?: string | null },
|
|
45
|
+
fallback: string,
|
|
46
|
+
): string {
|
|
47
|
+
if (!art) return fallback;
|
|
48
|
+
return art[placement.key || slugifyPlacementKey(placement.label)] ?? fallback;
|
|
49
|
+
}
|