@snowcone-app/ui 0.9.7 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -2925,6 +2925,23 @@ declare function useShop(): ShopContextValue;
2925
2925
  */
2926
2926
  declare function useShopOptional(): ShopContextValue | undefined;
2927
2927
 
2928
+ interface PictureView {
2929
+ /** Optional when the map is keyed by mockup id; set when keyed per element (a mockup may render twice). */
2930
+ mockupId?: string;
2931
+ /** In view or a neighbour of the picture in view (the set an edit renders). */
2932
+ inSet: boolean;
2933
+ /** Actually on screen now (the set Done renders at hero width). */
2934
+ onScreen: boolean;
2935
+ /** Displayed width in device pixels (CSS width x devicePixelRatio). */
2936
+ devicePx: number;
2937
+ }
2938
+ interface SessionShape {
2939
+ mockupIds: string[];
2940
+ width: number;
2941
+ /** Why, for traces and data attributes. */
2942
+ phase: "no-policy" | "editing" | "catch-up" | "done";
2943
+ }
2944
+
2928
2945
  interface MockupResult {
2929
2946
  mockupId: string;
2930
2947
  imageUrl: string;
@@ -3060,6 +3077,20 @@ interface RealtimeContextValue {
3060
3077
  * placement here, its frame's `versions[placement]` is at least this.
3061
3078
  */
3062
3079
  getLatestSentVersions: () => Record<string, number>;
3080
+ /**
3081
+ * Viewport policy (sessionShape.ts): a gallery picture reports whether it is
3082
+ * in the edit set (in view or a neighbour), on screen, and its displayed
3083
+ * width in device pixels; `null` when it unmounts. Only a gallery that opts
3084
+ * in reports (the phone carousel); without reports nothing changes.
3085
+ */
3086
+ reportPictureView: (elementKey: string, view: PictureView | null) => void;
3087
+ /** The shape the session renders now: its phase and width (for data attributes and checks). */
3088
+ getSessionShape: () => {
3089
+ phase: SessionShape["phase"];
3090
+ width: number;
3091
+ } | null;
3092
+ /** The design editor opened or closed (Done). Drives the viewport policy's phase; see `editorOpen`. */
3093
+ setEditorOpen: (open: boolean) => void;
3063
3094
  flushPendingBlobs: () => void;
3064
3095
  hasPendingBlobs: () => boolean;
3065
3096
  subscribePipelineSettled: (callback: () => void) => () => void;
@@ -3091,7 +3122,9 @@ interface RealtimeContextValue {
3091
3122
  */
3092
3123
  registerBlobUrl: (blobOrUrl: Blob | string, placementName?: string) => string;
3093
3124
  /** A picture's <img> loaded `url` (from getMockupUrl): its older composited frames are released. */
3094
- markBlobShown: (mockupId: string, url: string) => void;
3125
+ markBlobShown: (mockupId: string, url: string, consumer?: string) => void;
3126
+ /** An element that called markBlobShown unmounted: it no longer holds its frame. */
3127
+ releaseBlobConsumer: (mockupId: string, consumer: string) => void;
3095
3128
  }
3096
3129
  interface RealtimeProviderProps {
3097
3130
  children: React__default.ReactNode;
@@ -3131,13 +3164,22 @@ interface RealtimeProviderProps {
3131
3164
  * the rest of the session automatically.
3132
3165
  */
3133
3166
  delivery?: "cropped" | "full-frame";
3167
+ /**
3168
+ * Whether the design editor is open. With a gallery that opted into the
3169
+ * viewport policy (`reportPictureView`, the phone carousel), an open editor
3170
+ * renders the pictures in view at the width the screen shows them while an
3171
+ * edit is live, and every picture at that width when it stops; closing it
3172
+ * (Done) renders the picture on screen at `mockupWidth` (see sessionShape.ts).
3173
+ * Without such a gallery it changes nothing. Default true.
3174
+ */
3175
+ editorOpen?: boolean;
3134
3176
  }
3135
3177
  /**
3136
3178
  * RealtimeProvider - Manages realtime mockup WebSocket connection
3137
3179
  *
3138
3180
  * Must be used inside a <Product> provider to access product data.
3139
3181
  */
3140
- declare function RealtimeProvider({ children, wsUrl, mockupWidth, autoEnable, shop, grantUrl, initialGrant, delivery, }: RealtimeProviderProps): react_jsx_runtime.JSX.Element;
3182
+ declare function RealtimeProvider({ children, wsUrl, mockupWidth, autoEnable, shop, grantUrl, initialGrant, delivery, editorOpen, }: RealtimeProviderProps): react_jsx_runtime.JSX.Element;
3141
3183
  /**
3142
3184
  * Hook to access realtime context
3143
3185
  * Throws if not inside RealtimeProvider
@@ -4389,6 +4431,14 @@ interface HeroProductImageProps {
4389
4431
  errorText?: string;
4390
4432
  /** Label shown when the image fails to load with no stale image to keep. */
4391
4433
  unavailableText?: string;
4434
+ /**
4435
+ * Opt this picture into the realtime viewport policy (sessionShape.ts): it
4436
+ * reports whether it is in view (or a neighbour) and its displayed width,
4437
+ * so an edit renders only the pictures a shopper can see, at the width the
4438
+ * screen shows them. The phone carousel sets it; galleries that don't keep
4439
+ * today's behaviour.
4440
+ */
4441
+ viewportPolicy?: boolean;
4392
4442
  }
4393
4443
  /**
4394
4444
  * Hero-compatible version of ProductImage that renders as a simple img element
package/dist/index.d.ts CHANGED
@@ -2925,6 +2925,23 @@ declare function useShop(): ShopContextValue;
2925
2925
  */
2926
2926
  declare function useShopOptional(): ShopContextValue | undefined;
2927
2927
 
2928
+ interface PictureView {
2929
+ /** Optional when the map is keyed by mockup id; set when keyed per element (a mockup may render twice). */
2930
+ mockupId?: string;
2931
+ /** In view or a neighbour of the picture in view (the set an edit renders). */
2932
+ inSet: boolean;
2933
+ /** Actually on screen now (the set Done renders at hero width). */
2934
+ onScreen: boolean;
2935
+ /** Displayed width in device pixels (CSS width x devicePixelRatio). */
2936
+ devicePx: number;
2937
+ }
2938
+ interface SessionShape {
2939
+ mockupIds: string[];
2940
+ width: number;
2941
+ /** Why, for traces and data attributes. */
2942
+ phase: "no-policy" | "editing" | "catch-up" | "done";
2943
+ }
2944
+
2928
2945
  interface MockupResult {
2929
2946
  mockupId: string;
2930
2947
  imageUrl: string;
@@ -3060,6 +3077,20 @@ interface RealtimeContextValue {
3060
3077
  * placement here, its frame's `versions[placement]` is at least this.
3061
3078
  */
3062
3079
  getLatestSentVersions: () => Record<string, number>;
3080
+ /**
3081
+ * Viewport policy (sessionShape.ts): a gallery picture reports whether it is
3082
+ * in the edit set (in view or a neighbour), on screen, and its displayed
3083
+ * width in device pixels; `null` when it unmounts. Only a gallery that opts
3084
+ * in reports (the phone carousel); without reports nothing changes.
3085
+ */
3086
+ reportPictureView: (elementKey: string, view: PictureView | null) => void;
3087
+ /** The shape the session renders now: its phase and width (for data attributes and checks). */
3088
+ getSessionShape: () => {
3089
+ phase: SessionShape["phase"];
3090
+ width: number;
3091
+ } | null;
3092
+ /** The design editor opened or closed (Done). Drives the viewport policy's phase; see `editorOpen`. */
3093
+ setEditorOpen: (open: boolean) => void;
3063
3094
  flushPendingBlobs: () => void;
3064
3095
  hasPendingBlobs: () => boolean;
3065
3096
  subscribePipelineSettled: (callback: () => void) => () => void;
@@ -3091,7 +3122,9 @@ interface RealtimeContextValue {
3091
3122
  */
3092
3123
  registerBlobUrl: (blobOrUrl: Blob | string, placementName?: string) => string;
3093
3124
  /** A picture's <img> loaded `url` (from getMockupUrl): its older composited frames are released. */
3094
- markBlobShown: (mockupId: string, url: string) => void;
3125
+ markBlobShown: (mockupId: string, url: string, consumer?: string) => void;
3126
+ /** An element that called markBlobShown unmounted: it no longer holds its frame. */
3127
+ releaseBlobConsumer: (mockupId: string, consumer: string) => void;
3095
3128
  }
3096
3129
  interface RealtimeProviderProps {
3097
3130
  children: React__default.ReactNode;
@@ -3131,13 +3164,22 @@ interface RealtimeProviderProps {
3131
3164
  * the rest of the session automatically.
3132
3165
  */
3133
3166
  delivery?: "cropped" | "full-frame";
3167
+ /**
3168
+ * Whether the design editor is open. With a gallery that opted into the
3169
+ * viewport policy (`reportPictureView`, the phone carousel), an open editor
3170
+ * renders the pictures in view at the width the screen shows them while an
3171
+ * edit is live, and every picture at that width when it stops; closing it
3172
+ * (Done) renders the picture on screen at `mockupWidth` (see sessionShape.ts).
3173
+ * Without such a gallery it changes nothing. Default true.
3174
+ */
3175
+ editorOpen?: boolean;
3134
3176
  }
3135
3177
  /**
3136
3178
  * RealtimeProvider - Manages realtime mockup WebSocket connection
3137
3179
  *
3138
3180
  * Must be used inside a <Product> provider to access product data.
3139
3181
  */
3140
- declare function RealtimeProvider({ children, wsUrl, mockupWidth, autoEnable, shop, grantUrl, initialGrant, delivery, }: RealtimeProviderProps): react_jsx_runtime.JSX.Element;
3182
+ declare function RealtimeProvider({ children, wsUrl, mockupWidth, autoEnable, shop, grantUrl, initialGrant, delivery, editorOpen, }: RealtimeProviderProps): react_jsx_runtime.JSX.Element;
3141
3183
  /**
3142
3184
  * Hook to access realtime context
3143
3185
  * Throws if not inside RealtimeProvider
@@ -4389,6 +4431,14 @@ interface HeroProductImageProps {
4389
4431
  errorText?: string;
4390
4432
  /** Label shown when the image fails to load with no stale image to keep. */
4391
4433
  unavailableText?: string;
4434
+ /**
4435
+ * Opt this picture into the realtime viewport policy (sessionShape.ts): it
4436
+ * reports whether it is in view (or a neighbour) and its displayed width,
4437
+ * so an edit renders only the pictures a shopper can see, at the width the
4438
+ * screen shows them. The phone carousel sets it; galleries that don't keep
4439
+ * today's behaviour.
4440
+ */
4441
+ viewportPolicy?: boolean;
4392
4442
  }
4393
4443
  /**
4394
4444
  * Hero-compatible version of ProductImage that renders as a simple img element
package/dist/index.js CHANGED
@@ -5638,6 +5638,41 @@ function useMockupBackgroundOptional() {
5638
5638
  return useContext5(MockupBackgroundContext);
5639
5639
  }
5640
5640
 
5641
+ // src/patterns/sessionShape.ts
5642
+ var WIDTH_LADDER = [400, 600, 800, 1e3, 1200, 1600, 2e3, 3e3, 4e3];
5643
+ function ladderWidth(devicePx) {
5644
+ for (const w of WIDTH_LADDER) if (w >= devicePx) return w;
5645
+ return WIDTH_LADDER[WIDTH_LADDER.length - 1];
5646
+ }
5647
+ function sessionShape(input) {
5648
+ const { allMockupIds, views, heroWidth, editorOpen, editLive } = input;
5649
+ if (views.size === 0) return { mockupIds: [...allMockupIds], width: heroWidth, phase: "no-policy" };
5650
+ const byMockup = /* @__PURE__ */ new Map();
5651
+ views.forEach((v, key) => {
5652
+ const id = v.mockupId ?? key;
5653
+ const prev = byMockup.get(id);
5654
+ byMockup.set(id, prev ? { inSet: prev.inSet || v.inSet, onScreen: prev.onScreen || v.onScreen, devicePx: Math.max(prev.devicePx, v.devicePx) } : { inSet: v.inSet, onScreen: v.onScreen, devicePx: v.devicePx });
5655
+ });
5656
+ const ordered = (pick) => allMockupIds.filter((id) => {
5657
+ const v = byMockup.get(id);
5658
+ return v ? pick(v) : false;
5659
+ });
5660
+ const inSet = ordered((v) => v.inSet);
5661
+ const onScreen = ordered((v) => v.onScreen);
5662
+ const shown = [...byMockup.values()].filter((v) => v.inSet && v.devicePx > 0).map((v) => v.devicePx);
5663
+ const editingWidth = shown.length ? ladderWidth(Math.max(...shown)) : heroWidth;
5664
+ if (!editorOpen) {
5665
+ const set = onScreen.length ? onScreen : inSet;
5666
+ return { mockupIds: set.length ? set : [...allMockupIds], width: heroWidth, phase: "done" };
5667
+ }
5668
+ if (editLive && inSet.length) return { mockupIds: inSet, width: editingWidth, phase: "editing" };
5669
+ return { mockupIds: [...allMockupIds], width: editingWidth, phase: "catch-up" };
5670
+ }
5671
+ function sameShape(a, b) {
5672
+ if (!a) return false;
5673
+ return a.width === b.width && [...a.mockupIds].sort().join(",") === [...b.mockupIds].sort().join(",");
5674
+ }
5675
+
5641
5676
  // src/patterns/croppedFrameComposite.ts
5642
5677
  var COMPOSITE_QUALITY = 0.92;
5643
5678
  function isFullRect(rect) {
@@ -5706,6 +5741,7 @@ var MAX_CONCURRENT_BLOBS = 5;
5706
5741
  var MAX_FRAMES_PER_PICTURE = 3;
5707
5742
  var FRAME_SUPERSEDE_GRACE_MS = 2e3;
5708
5743
  var RERENDER_MIN_INTERVAL_MS = 550;
5744
+ var EDIT_QUIET_MS = 600;
5709
5745
  function mergeMockupIds(current, extra) {
5710
5746
  const out = [...current];
5711
5747
  for (const id of extra) if (!out.includes(id)) out.push(id);
@@ -5724,7 +5760,8 @@ function RealtimeProvider({
5724
5760
  shop,
5725
5761
  grantUrl,
5726
5762
  initialGrant,
5727
- delivery = "cropped"
5763
+ delivery = "cropped",
5764
+ editorOpen = true
5728
5765
  }) {
5729
5766
  const productContext = useProductOptional();
5730
5767
  const mockupBg = useMockupBackgroundOptional();
@@ -5822,8 +5859,11 @@ function RealtimeProvider({
5822
5859
  hasPendingBlobs,
5823
5860
  getLastSentVersion,
5824
5861
  getLatestSentVersions,
5825
- rerender: rerenderRaw
5862
+ rerender: rerenderRaw,
5863
+ isFrameSuperseded
5826
5864
  } = realtimeMockup;
5865
+ const isFrameSupersededRef = useRef12(isFrameSuperseded);
5866
+ isFrameSupersededRef.current = isFrameSuperseded;
5827
5867
  const mockupResultsRef = useRef12([]);
5828
5868
  const mockupSubscribersRef = useRef12(
5829
5869
  /* @__PURE__ */ new Set()
@@ -5862,6 +5902,18 @@ function RealtimeProvider({
5862
5902
  const compositeBusyRef = useRef12(/* @__PURE__ */ new Set());
5863
5903
  const compositeCanvasRef = useRef12(null);
5864
5904
  const requestedMockupIdsRef = useRef12([]);
5905
+ const pictureViewsRef = useRef12(/* @__PURE__ */ new Map());
5906
+ const editLiveRef = useRef12(false);
5907
+ const editQuietTimerRef = useRef12(null);
5908
+ const shapeApplyTimerRef = useRef12(null);
5909
+ const lastShapeRef = useRef12(null);
5910
+ const applySessionShapeRef = useRef12(null);
5911
+ const [editorOpenState, setEditorOpenState] = useState17(editorOpen);
5912
+ useEffect17(() => setEditorOpenState(editorOpen), [editorOpen]);
5913
+ const editorOpenRef = useRef12(editorOpenState);
5914
+ editorOpenRef.current = editorOpenState;
5915
+ const heroWidthRef = useRef12(mockupWidth);
5916
+ heroWidthRef.current = mockupWidth;
5865
5917
  const serverStampsVersionsRef = useRef12(false);
5866
5918
  const pendingRerenderRef = useRef12([]);
5867
5919
  const forceRerenderByConfigRef = useRef12(null);
@@ -5889,9 +5941,9 @@ function RealtimeProvider({
5889
5941
  const url = URL.createObjectURL(blobOrUrl);
5890
5942
  if (owner) {
5891
5943
  const list = [url, ...blobsByOwnerRef.current.get(owner) ?? []];
5892
- const shown = shownBlobByOwnerRef.current.get(owner);
5944
+ const shown = new Set(shownBlobByOwnerRef.current.get(owner)?.values() ?? []);
5893
5945
  while (list.length > MAX_FRAMES_PER_PICTURE) {
5894
- const victimIdx = [...list.keys()].reverse().find((i) => i > 0 && list[i] !== shown);
5946
+ const victimIdx = [...list.keys()].reverse().find((i) => i > 0 && !shown.has(list[i]));
5895
5947
  if (victimIdx === void 0) break;
5896
5948
  const [victim] = list.splice(victimIdx, 1);
5897
5949
  console.error(
@@ -5909,15 +5961,28 @@ function RealtimeProvider({
5909
5961
  registeredBlobUrlsRef.current.add(url);
5910
5962
  return url;
5911
5963
  }, [revokeBlobUrl]);
5912
- const markBlobShown = useCallback10((owner, url) => {
5964
+ const releaseOlderFrames = useCallback10((owner) => {
5913
5965
  const list = blobsByOwnerRef.current.get(owner);
5914
- if (!list) return;
5915
- const idx = list.indexOf(url);
5916
- if (idx < 0) return;
5917
- shownBlobByOwnerRef.current.set(owner, url);
5918
- list.slice(idx + 1).forEach(revokeBlobUrl);
5919
- blobsByOwnerRef.current.set(owner, list.slice(0, idx + 1));
5966
+ const shown = shownBlobByOwnerRef.current.get(owner);
5967
+ if (!list || !shown || shown.size === 0) return;
5968
+ const oldestShownIdx = Math.max(...[...shown.values()].map((u) => list.indexOf(u)));
5969
+ if (oldestShownIdx < 0) return;
5970
+ list.slice(oldestShownIdx + 1).forEach(revokeBlobUrl);
5971
+ blobsByOwnerRef.current.set(owner, list.slice(0, oldestShownIdx + 1));
5920
5972
  }, [revokeBlobUrl]);
5973
+ const markBlobShown = useCallback10((owner, url, consumer = "default") => {
5974
+ const list = blobsByOwnerRef.current.get(owner);
5975
+ if (!list || list.indexOf(url) < 0) return;
5976
+ const shown = shownBlobByOwnerRef.current.get(owner) ?? /* @__PURE__ */ new Map();
5977
+ shown.set(consumer, url);
5978
+ shownBlobByOwnerRef.current.set(owner, shown);
5979
+ releaseOlderFrames(owner);
5980
+ }, [releaseOlderFrames]);
5981
+ const releaseBlobConsumer = useCallback10((owner, consumer) => {
5982
+ const shown = shownBlobByOwnerRef.current.get(owner);
5983
+ if (!shown?.delete(consumer)) return;
5984
+ releaseOlderFrames(owner);
5985
+ }, [releaseOlderFrames]);
5921
5986
  const croppedDeliveryEnabled = delivery !== "full-frame";
5922
5987
  const sendCropConfig = useCallback10(
5923
5988
  (withCrop) => {
@@ -6039,6 +6104,64 @@ function RealtimeProvider({
6039
6104
  compositeQueueRef.current.clear();
6040
6105
  croppedActiveRef.current = false;
6041
6106
  }, []);
6107
+ const currentShape = useCallback10(
6108
+ () => sessionShape({
6109
+ allMockupIds: configuredMockupIdsRef.current,
6110
+ views: pictureViewsRef.current,
6111
+ heroWidth: heroWidthRef.current,
6112
+ editorOpen: editorOpenRef.current,
6113
+ editLive: editLiveRef.current
6114
+ }),
6115
+ []
6116
+ );
6117
+ const applySessionShape = useCallback10(() => {
6118
+ const base = lastConfigBaseRef.current;
6119
+ if (!base || !configSentRef.current) return;
6120
+ const shape = currentShape();
6121
+ if (sameShape(lastShapeRef.current, shape)) {
6122
+ lastShapeRef.current = shape;
6123
+ return;
6124
+ }
6125
+ if (lastShapeRef.current && lastShapeRef.current.width !== shape.width) invalidateBackgrounds();
6126
+ lastShapeRef.current = shape;
6127
+ base.mockupIds = shape.mockupIds;
6128
+ base.width = shape.width;
6129
+ requestedMockupIdsRef.current = [...shape.mockupIds];
6130
+ sendConfigRaw({
6131
+ ...base,
6132
+ placementSettings: { ...placementSettingsOverridesRef.current },
6133
+ ...croppedActiveRef.current ? { overlayCrop: "auto" } : {}
6134
+ });
6135
+ }, [currentShape, invalidateBackgrounds, sendConfigRaw]);
6136
+ applySessionShapeRef.current = applySessionShape;
6137
+ const markEditLive = useCallback10(() => {
6138
+ if (editQuietTimerRef.current) {
6139
+ clearTimeout(editQuietTimerRef.current);
6140
+ editQuietTimerRef.current = null;
6141
+ }
6142
+ if (editLiveRef.current) return;
6143
+ editLiveRef.current = true;
6144
+ applySessionShapeRef.current?.();
6145
+ }, []);
6146
+ const markEditSettled = useCallback10(() => {
6147
+ if (!editLiveRef.current) return;
6148
+ if (editQuietTimerRef.current) clearTimeout(editQuietTimerRef.current);
6149
+ editQuietTimerRef.current = setTimeout(() => {
6150
+ editQuietTimerRef.current = null;
6151
+ editLiveRef.current = false;
6152
+ applySessionShapeRef.current?.();
6153
+ }, EDIT_QUIET_MS);
6154
+ }, []);
6155
+ useEffect17(() => {
6156
+ applySessionShapeRef.current?.();
6157
+ }, [editorOpenState]);
6158
+ useEffect17(
6159
+ () => () => {
6160
+ if (editQuietTimerRef.current) clearTimeout(editQuietTimerRef.current);
6161
+ if (shapeApplyTimerRef.current) clearTimeout(shapeApplyTimerRef.current);
6162
+ },
6163
+ []
6164
+ );
6042
6165
  const deliverResults = useCallback10(
6043
6166
  (results) => {
6044
6167
  if (results.length === 0) return;
@@ -6104,13 +6227,15 @@ function RealtimeProvider({
6104
6227
  if (!pipelineResetCooldownRef.current) {
6105
6228
  hasBlobInFlightRef.current = false;
6106
6229
  for (let i = 0; i < settledVersions; i++) pipelineSettledSubscribersRef.current.forEach((cb) => cb());
6230
+ markEditSettled();
6107
6231
  }
6108
6232
  } else if (due.size === 0 && hasBlobInFlightRef.current && !hasPendingBlobs() && !pipelineResetCooldownRef.current) {
6109
6233
  hasBlobInFlightRef.current = false;
6110
6234
  pipelineSettledSubscribersRef.current.forEach((cb) => cb());
6235
+ markEditSettled();
6111
6236
  }
6112
6237
  },
6113
- [hasPendingBlobs]
6238
+ [hasPendingBlobs, markEditSettled]
6114
6239
  );
6115
6240
  const runComposite = useCallback10(
6116
6241
  async (result) => {
@@ -6141,6 +6266,7 @@ function RealtimeProvider({
6141
6266
  if (gen !== sceneGenRef.current || compositeQueueRef.current.has(result.mockupId) || processedFrameRef.current.get(result.mockupId) !== result.imageUrl) {
6142
6267
  return;
6143
6268
  }
6269
+ if (isFrameSupersededRef.current?.(result.imageUrl)) return;
6144
6270
  const message = err instanceof Error ? err.message : String(err);
6145
6271
  if (result.imageUrl.startsWith("blob:") && message.startsWith("E_OVERLAY_FRAME_LOAD") && message.includes(result.imageUrl.slice(0, 160))) {
6146
6272
  const failedUrl = result.imageUrl;
@@ -6236,10 +6362,11 @@ function RealtimeProvider({
6236
6362
  lastBlobSentTimeRef.current = (/* @__PURE__ */ new Date()).toLocaleTimeString();
6237
6363
  isPendingMockupsRef.current = true;
6238
6364
  pendingBlobSubscribersRef.current.forEach((cb) => cb(placement));
6365
+ markEditLive();
6239
6366
  }
6240
6367
  return result;
6241
6368
  },
6242
- [sendCanvasBlobRaw]
6369
+ [sendCanvasBlobRaw, markEditLive]
6243
6370
  );
6244
6371
  const sendCanvasState = useCallback10(
6245
6372
  (placement, state, mockupCount, baseThrottleMs) => {
@@ -6256,15 +6383,16 @@ function RealtimeProvider({
6256
6383
  isPendingMockupsRef.current = true;
6257
6384
  const version = getLastSentVersion();
6258
6385
  if (version > 0) {
6259
- const configured = configuredMockupIdsRef.current.length;
6386
+ const configured = requestedMockupIdsRef.current.length || configuredMockupIdsRef.current.length;
6260
6387
  const expected = Math.max(1, Math.min(configured || Infinity, mockupCount || Infinity));
6261
6388
  dueByVersionRef.current.set(version, { due: Number.isFinite(expected) ? expected : 1, at: Date.now() });
6262
6389
  }
6263
6390
  pendingBlobSubscribersRef.current.forEach((cb) => cb(placement));
6391
+ markEditLive();
6264
6392
  }
6265
6393
  return result;
6266
6394
  },
6267
- [sendCanvasStateRaw, getLastSentVersion]
6395
+ [sendCanvasStateRaw, getLastSentVersion, markEditLive]
6268
6396
  );
6269
6397
  const sendColorBlobTracked = useCallback10(
6270
6398
  (placement, color) => {
@@ -6329,22 +6457,24 @@ function RealtimeProvider({
6329
6457
  });
6330
6458
  }
6331
6459
  configuredMockupIdsRef.current = mockupIds;
6460
+ const shape = currentShape();
6461
+ lastShapeRef.current = shape;
6332
6462
  const configPayload = useSessionGrant ? {
6333
6463
  productId,
6334
- mockupIds,
6464
+ mockupIds: shape.mockupIds,
6335
6465
  variantId,
6336
6466
  shop,
6337
- width: mockupWidth,
6467
+ width: shape.width,
6338
6468
  ...mockupBg ? { bg: mockupBg } : {},
6339
6469
  ...spreadCropsRef.current ? { spreadCrops: spreadCropsRef.current } : {},
6340
6470
  placementSettings: { ...placementSettingsOverridesRef.current }
6341
6471
  } : {
6342
6472
  productId,
6343
- mockupIds,
6473
+ mockupIds: shape.mockupIds,
6344
6474
  variantId,
6345
6475
  shop: "hTidnpNnNa",
6346
6476
  seal: "bypass-sig-for-k6-load-test",
6347
- width: mockupWidth,
6477
+ width: shape.width,
6348
6478
  ...mockupBg ? { bg: mockupBg } : {},
6349
6479
  ...spreadCropsRef.current ? { spreadCrops: spreadCropsRef.current } : {},
6350
6480
  placementSettings: { ...placementSettingsOverridesRef.current }
@@ -6356,7 +6486,7 @@ function RealtimeProvider({
6356
6486
  });
6357
6487
  }
6358
6488
  lastConfigBaseRef.current = configPayload;
6359
- requestedMockupIdsRef.current = mockupIds;
6489
+ requestedMockupIdsRef.current = [...shape.mockupIds];
6360
6490
  sendConfigRaw(
6361
6491
  croppedActiveRef.current ? { ...configPayload, overlayCrop: "auto" } : configPayload
6362
6492
  );
@@ -6382,6 +6512,7 @@ function RealtimeProvider({
6382
6512
  sendConfigRaw,
6383
6513
  sendColorBlobTracked,
6384
6514
  invalidateBackgrounds,
6515
+ currentShape,
6385
6516
  mockupWidth,
6386
6517
  mockupBg,
6387
6518
  spreadCropsVersion
@@ -6571,8 +6702,9 @@ function RealtimeProvider({
6571
6702
  }
6572
6703
  console.log(`[RTP] notifyPendingExport("${placement}"): marked ${pendingIds.length} mockup(s) pending \u2014 [${pendingIds.slice(0, 5).join(", ")}${pendingIds.length > 5 ? "..." : ""}]`);
6573
6704
  pendingBlobSubscribersRef.current.forEach((cb) => cb(placement));
6705
+ markEditLive();
6574
6706
  },
6575
- [productContext?.product?.mockups]
6707
+ [productContext?.product?.mockups, markEditLive]
6576
6708
  );
6577
6709
  const notifyPendingExportCancelled = useCallback10(
6578
6710
  (placement) => {
@@ -6679,6 +6811,20 @@ function RealtimeProvider({
6679
6811
  const getLatestSentVersionsRef = useRef12(getLatestSentVersions);
6680
6812
  getLatestSentVersionsRef.current = getLatestSentVersions;
6681
6813
  const getLatestSentVersionsStable = useCallback10(() => getLatestSentVersionsRef.current?.() ?? {}, []);
6814
+ const reportPictureView = useCallback10((elementKey, view) => {
6815
+ const views = pictureViewsRef.current;
6816
+ if (view) views.set(elementKey, view);
6817
+ else views.delete(elementKey);
6818
+ if (shapeApplyTimerRef.current) return;
6819
+ shapeApplyTimerRef.current = setTimeout(() => {
6820
+ shapeApplyTimerRef.current = null;
6821
+ applySessionShapeRef.current?.();
6822
+ }, 100);
6823
+ }, []);
6824
+ const getSessionShape = useCallback10(() => {
6825
+ const s = lastShapeRef.current;
6826
+ return s ? { phase: s.phase, width: s.width } : null;
6827
+ }, []);
6682
6828
  const stableSubscribePipelineSettled = useCallback10(
6683
6829
  (callback) => {
6684
6830
  return subscribePipelineSettledRef.current(callback);
@@ -6729,9 +6875,13 @@ function RealtimeProvider({
6729
6875
  getMockupUrls,
6730
6876
  getCanvasVersion,
6731
6877
  getLatestSentVersions: getLatestSentVersionsStable,
6878
+ reportPictureView,
6879
+ getSessionShape,
6880
+ setEditorOpen: setEditorOpenState,
6732
6881
  isMockupLoading,
6733
6882
  registerBlobUrl,
6734
6883
  markBlobShown,
6884
+ releaseBlobConsumer,
6735
6885
  // WebRTC streaming
6736
6886
  subscribeRTCTiming
6737
6887
  }),
@@ -6763,9 +6913,12 @@ function RealtimeProvider({
6763
6913
  getMockupUrls,
6764
6914
  getCanvasVersion,
6765
6915
  getLatestSentVersionsStable,
6916
+ reportPictureView,
6917
+ getSessionShape,
6766
6918
  isMockupLoading,
6767
6919
  registerBlobUrl,
6768
- markBlobShown
6920
+ markBlobShown,
6921
+ releaseBlobConsumer
6769
6922
  ]
6770
6923
  );
6771
6924
  return /* @__PURE__ */ jsx52(RealtimeContext.Provider, { value: contextValue, children });
@@ -10356,7 +10509,8 @@ var HeroProductImage = memo3(function HeroProductImage2({
10356
10509
  darkShimmerFadeInDuration = 300,
10357
10510
  emptyText = "No artwork",
10358
10511
  errorText = "Error loading",
10359
- unavailableText = "Preview unavailable"
10512
+ unavailableText = "Preview unavailable",
10513
+ viewportPolicy = false
10360
10514
  }) {
10361
10515
  const context = useProductOptional();
10362
10516
  const designContext = useDesignOptional();
@@ -10562,6 +10716,15 @@ var HeroProductImage = memo3(function HeroProductImage2({
10562
10716
  const arrivedVersionRef = useRef23(0);
10563
10717
  const versionByUrlRef = useRef23(/* @__PURE__ */ new Map());
10564
10718
  const loadedUrlRef = useRef23(null);
10719
+ const blobConsumerRef = useRef23(`el-${Math.random().toString(36).slice(2, 10)}`);
10720
+ const releaseBlobConsumer = realtimeContext?.releaseBlobConsumer;
10721
+ useEffect32(() => {
10722
+ const id = actualMockupId;
10723
+ const consumer = blobConsumerRef.current;
10724
+ return () => {
10725
+ if (id) releaseBlobConsumer?.(id, consumer);
10726
+ };
10727
+ }, [actualMockupId, releaseBlobConsumer]);
10565
10728
  const frameErrorRef = useRef23(null);
10566
10729
  const showVersionOf = useCallback18((url) => {
10567
10730
  const e = versionByUrlRef.current.get(url);
@@ -10577,6 +10740,7 @@ var HeroProductImage = memo3(function HeroProductImage2({
10577
10740
  const sessionErrorCodeRef = useRef23(null);
10578
10741
  sessionErrorCodeRef.current = realtimeContext?.renderError?.code ?? null;
10579
10742
  const getCanvasVersion = realtimeContext?.getCanvasVersion;
10743
+ const getSessionShape = realtimeContext?.getSessionShape;
10580
10744
  const applyRenderStamp = useCallback18(() => {
10581
10745
  const node = containerRef.current;
10582
10746
  if (!node) return;
@@ -10597,7 +10761,14 @@ var HeroProductImage = memo3(function HeroProductImage2({
10597
10761
  if (frameErrorRef.current) node.dataset.frameError = frameErrorRef.current;
10598
10762
  else delete node.dataset.frameError;
10599
10763
  node.dataset.renderState = refusalCodeRef.current || behind && sessionErrorCodeRef.current ? "refused" : behind ? "stale" : "current";
10600
- }, [getCanvasVersion, getLatestSentVersions]);
10764
+ const img = node.querySelector("img");
10765
+ if (img?.naturalWidth) node.dataset.renderWidth = String(img.naturalWidth);
10766
+ const shape = getSessionShape?.();
10767
+ if (shape) {
10768
+ node.dataset.wantedWidth = String(shape.width);
10769
+ node.dataset.sessionPhase = shape.phase;
10770
+ }
10771
+ }, [getCanvasVersion, getLatestSentVersions, getSessionShape]);
10601
10772
  const applyRenderStampRef = useRef23(applyRenderStamp);
10602
10773
  applyRenderStampRef.current = applyRenderStamp;
10603
10774
  useEffect32(() => {
@@ -10629,6 +10800,39 @@ var HeroProductImage = memo3(function HeroProductImage2({
10629
10800
  realtimeContext?.subscribeMockupResultById,
10630
10801
  applyRenderStamp
10631
10802
  ]);
10803
+ const reportPictureView = realtimeContext?.reportPictureView;
10804
+ useEffect32(() => {
10805
+ if (!viewportPolicy || !reportPictureView || !actualMockupId) return;
10806
+ const node = containerRef.current;
10807
+ if (!node || typeof IntersectionObserver === "undefined") return;
10808
+ let inSet = false;
10809
+ const key = `${actualMockupId}#${Math.random().toString(36).slice(2, 9)}`;
10810
+ const report = () => {
10811
+ const r = node.getBoundingClientRect();
10812
+ const onScreen = r.width > 0 && r.right > 0 && r.left < window.innerWidth && r.bottom > 0 && r.top < window.innerHeight;
10813
+ reportPictureView(key, {
10814
+ mockupId: actualMockupId,
10815
+ inSet: inSet || onScreen,
10816
+ onScreen,
10817
+ devicePx: Math.round(r.width * (window.devicePixelRatio || 1))
10818
+ });
10819
+ };
10820
+ const io = new IntersectionObserver(
10821
+ (entries) => {
10822
+ inSet = entries.some((e) => e.isIntersecting);
10823
+ report();
10824
+ },
10825
+ { rootMargin: "0px 100% 0px 100%" }
10826
+ );
10827
+ io.observe(node);
10828
+ const onResize = () => report();
10829
+ window.addEventListener("resize", onResize);
10830
+ return () => {
10831
+ io.disconnect();
10832
+ window.removeEventListener("resize", onResize);
10833
+ reportPictureView(key, null);
10834
+ };
10835
+ }, [viewportPolicy, reportPictureView, actualMockupId]);
10632
10836
  useEffect32(() => {
10633
10837
  if (!priorityContext || !actualMockupId || !placement) return;
10634
10838
  priorityContext.registerMockup(actualMockupId, placement, initialPriority);
@@ -10931,7 +11135,7 @@ var HeroProductImage = memo3(function HeroProductImage2({
10931
11135
  if (renderedUrl) {
10932
11136
  loadedUrlRef.current = renderedUrl;
10933
11137
  showVersionOf(renderedUrl);
10934
- if (actualMockupId) realtimeContext?.markBlobShown?.(actualMockupId, renderedUrl);
11138
+ if (actualMockupId) realtimeContext?.markBlobShown?.(actualMockupId, renderedUrl, blobConsumerRef.current);
10935
11139
  applyRenderStampRef.current?.();
10936
11140
  }
10937
11141
  if (!onLoadCalledRef.current && onLoad) {
@@ -13028,7 +13232,8 @@ var MobileProductCarousel = memo4(function MobileProductCarousel2({
13028
13232
  onUrlGenerated: image.mockupId ? (url) => handleUrlGenerated(image.mockupId, url) : void 0,
13029
13233
  errorText,
13030
13234
  unavailableText,
13031
- className: "w-full h-full object-cover cursor-pointer pointer-events-none"
13235
+ className: "w-full h-full object-cover cursor-pointer pointer-events-none",
13236
+ viewportPolicy: true
13032
13237
  }
13033
13238
  ) : image.isRealMockup && !currentArtwork ? /* @__PURE__ */ jsx74(LoadingOverlayPrismCandyInline, { visible: true, variant: "light" }) : /* @__PURE__ */ jsx74(
13034
13239
  StaticCarouselImage,