@pradip1995/segment-primitives 0.4.15 → 0.4.19

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,53 +1,55 @@
1
- {
2
- "name": "@pradip1995/segment-primitives",
3
- "version": "0.4.15",
4
- "license": "MIT",
5
- "publishConfig": {
6
- "access": "public"
7
- },
8
- "sideEffects": false,
9
- "files": [
10
- "src"
11
- ],
12
- "exports": {
13
- ".": "./src/index.ts",
14
- "./localized-link": "./src/localized-link.tsx",
15
- "./coupon-apply": "./src/coupon-apply.tsx",
16
- "./button": "./src/Button.tsx",
17
- "./card": "./src/Card.tsx",
18
- "./format-price": "./src/format-price.ts",
19
- "./resolve-media-url": "./src/resolve-media-url.ts",
20
- "./skip-image-optimization": "./src/skip-image-optimization.ts",
21
- "./optimized-image-src": "./src/optimized-image-src.ts",
22
- "./section-variants": "./src/section-variants.ts",
23
- "./product-variants": "./src/product-variants.ts",
24
- "./product-selection-context": "./src/product-selection-context.tsx",
25
- "./is-next-redirect": "./src/is-next-redirect.ts",
26
- "./order-lines": "./src/order-lines.ts",
27
- "./wishlist-count": "./src/wishlist-count.ts",
28
- "./use-wishlist-count": "./src/use-wishlist-count.ts",
29
- "./notify-wishlist": "./src/notify-wishlist.ts",
30
- "./revalidate-wishlist": "./src/revalidate-wishlist.ts"
31
- },
32
- "peerDependencies": {
33
- "@medusajs/types": "^2.0.0",
34
- "@pradip1995/commerce-core": "^4.0.0",
35
- "@pradip1995/plugin-sdk": "^0.2.0",
36
- "next": ">=15",
37
- "react": ">=19",
38
- "react-dom": ">=19"
39
- },
40
- "devDependencies": {
41
- "@medusajs/types": "^2.0.0",
42
- "@pradip1995/plugin-sdk": "^0.2.0",
43
- "@types/react": "^19",
44
- "next": ">=15",
45
- "react": "19.0.3",
46
- "typescript": "^5.7.2"
47
- },
48
- "scripts": {
49
- "typecheck": "tsc --noEmit",
50
- "lint": "tsc --noEmit",
51
- "test": "node --experimental-strip-types --test src/*.test.ts"
52
- }
53
- }
1
+ {
2
+ "name": "@pradip1995/segment-primitives",
3
+ "version": "0.4.19",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "sideEffects": false,
9
+ "files": [
10
+ "src"
11
+ ],
12
+ "exports": {
13
+ ".": "./src/index.ts",
14
+ "./localized-link": "./src/localized-link.tsx",
15
+ "./coupon-apply": "./src/coupon-apply.tsx",
16
+ "./button": "./src/Button.tsx",
17
+ "./card": "./src/Card.tsx",
18
+ "./format-price": "./src/format-price.ts",
19
+ "./resolve-media-url": "./src/resolve-media-url.ts",
20
+ "./skip-image-optimization": "./src/skip-image-optimization.ts",
21
+ "./optimized-image-src": "./src/optimized-image-src.ts",
22
+ "./section-variants": "./src/section-variants.ts",
23
+ "./product-variants": "./src/product-variants.ts",
24
+ "./product-selection-context": "./src/product-selection-context.tsx",
25
+ "./is-next-redirect": "./src/is-next-redirect.ts",
26
+ "./order-lines": "./src/order-lines.ts",
27
+ "./wishlist-count": "./src/wishlist-count.ts",
28
+ "./wishlist-ids": "./src/wishlist-ids.ts",
29
+ "./use-wishlist-count": "./src/use-wishlist-count.ts",
30
+ "./notify-wishlist": "./src/notify-wishlist.ts",
31
+ "./revalidate-wishlist": "./src/revalidate-wishlist.ts",
32
+ "./cart-live": "./src/cart-live.ts"
33
+ },
34
+ "scripts": {
35
+ "typecheck": "tsc --noEmit",
36
+ "lint": "tsc --noEmit",
37
+ "test": "node --experimental-strip-types --test src/*.test.ts"
38
+ },
39
+ "peerDependencies": {
40
+ "@medusajs/types": "^2.0.0",
41
+ "@pradip1995/commerce-core": "^4.0.0",
42
+ "@pradip1995/plugin-sdk": "^0.2.0",
43
+ "next": ">=15",
44
+ "react": ">=19",
45
+ "react-dom": ">=19"
46
+ },
47
+ "devDependencies": {
48
+ "@medusajs/types": "^2.0.0",
49
+ "@pradip1995/plugin-sdk": "^0.2.0",
50
+ "@types/react": "^19",
51
+ "next": ">=15",
52
+ "react": "19.0.3",
53
+ "typescript": "^5.7.2"
54
+ }
55
+ }
@@ -0,0 +1,21 @@
1
+ "use client"
2
+
3
+ import { retrieveCart } from "@pradip1995/commerce-core/client/actions/cart"
4
+ import type { HttpTypes } from "@medusajs/types"
5
+
6
+ export const CART_ITEM_ADDED_EVENT = "cart:item-added"
7
+
8
+ export type CartItemAddedDetail = {
9
+ message?: string
10
+ variant?: "error" | string
11
+ }
12
+
13
+ /** Fresh cart after add/update — bypasses waiting for router.refresh(). */
14
+ export async function fetchLiveCart(): Promise<HttpTypes.StoreCart | null> {
15
+ try {
16
+ const cart = await retrieveCart()
17
+ return (cart as HttpTypes.StoreCart | null) ?? null
18
+ } catch {
19
+ return null
20
+ }
21
+ }
@@ -1,9 +1,12 @@
1
1
  "use client"
2
2
 
3
3
  import { revalidateWishlistCache } from "./revalidate-wishlist"
4
+ import { clearSharedWishlistIdsCache } from "./wishlist-ids"
4
5
  import { dispatchWishlistUpdated, type WishlistUpdatedDetail } from "./wishlist-count"
5
6
 
6
7
  export async function notifyWishlistChanged(detail: WishlistUpdatedDetail) {
8
+ // Clear before dispatch so listeners refetch from an empty cache.
9
+ clearSharedWishlistIdsCache()
7
10
  dispatchWishlistUpdated(detail)
8
11
  await revalidateWishlistCache()
9
12
  }
@@ -0,0 +1,96 @@
1
+ import { describe, it } from "node:test"
2
+ import assert from "node:assert/strict"
3
+ import {
4
+ resolveProductImage,
5
+ resolveProductImages,
6
+ type ProductImageSource,
7
+ } from "./resolve-media-url.ts"
8
+
9
+ const PRIMARY_CASES: { name: string; product: ProductImageSource; want: string | null }[] = [
10
+ {
11
+ name: "uses thumbnail when present",
12
+ product: {
13
+ thumbnail: "https://cdn.example/thumb.jpg",
14
+ images: [{ url: "https://cdn.example/gallery.jpg" }],
15
+ },
16
+ want: "https://cdn.example/thumb.jpg",
17
+ },
18
+ {
19
+ name: "falls back to first gallery image when thumbnail is missing",
20
+ product: {
21
+ thumbnail: null,
22
+ images: [
23
+ { url: "https://cdn.example/gallery-1.jpg" },
24
+ { url: "https://cdn.example/gallery-2.jpg" },
25
+ ],
26
+ },
27
+ want: "https://cdn.example/gallery-1.jpg",
28
+ },
29
+ {
30
+ name: "skips empty thumbnail and empty gallery urls",
31
+ product: {
32
+ thumbnail: " ",
33
+ images: [{ url: "" }, { url: "https://cdn.example/gallery.jpg" }],
34
+ },
35
+ want: "https://cdn.example/gallery.jpg",
36
+ },
37
+ {
38
+ name: "falls back to first variant image when product media is missing",
39
+ product: {
40
+ thumbnail: null,
41
+ images: [],
42
+ variants: [
43
+ {
44
+ thumbnail: null,
45
+ images: [{ url: "https://cdn.example/variant.jpg" }],
46
+ },
47
+ ],
48
+ },
49
+ want: "https://cdn.example/variant.jpg",
50
+ },
51
+ {
52
+ name: "returns null when no media exists",
53
+ product: { thumbnail: null, images: [], variants: [] },
54
+ want: null,
55
+ },
56
+ ]
57
+
58
+ const GALLERY_CASES: { name: string; product: ProductImageSource; want: string[] }[] = [
59
+ {
60
+ name: "returns unique gallery urls with thumbnail first",
61
+ product: {
62
+ thumbnail: "https://cdn.example/thumb.jpg",
63
+ images: [
64
+ { url: "https://cdn.example/thumb.jpg" },
65
+ { url: "https://cdn.example/gallery.jpg" },
66
+ ],
67
+ },
68
+ want: ["https://cdn.example/thumb.jpg", "https://cdn.example/gallery.jpg"],
69
+ },
70
+ {
71
+ name: "uses gallery when thumbnail is missing",
72
+ product: {
73
+ images: [
74
+ { url: "https://cdn.example/gallery-1.jpg" },
75
+ { url: "https://cdn.example/gallery-2.jpg" },
76
+ ],
77
+ },
78
+ want: ["https://cdn.example/gallery-1.jpg", "https://cdn.example/gallery-2.jpg"],
79
+ },
80
+ ]
81
+
82
+ describe("resolveProductImage", () => {
83
+ for (const { name, product, want } of PRIMARY_CASES) {
84
+ it(name, () => {
85
+ assert.equal(resolveProductImage(product), want)
86
+ })
87
+ }
88
+ })
89
+
90
+ describe("resolveProductImages", () => {
91
+ for (const { name, product, want } of GALLERY_CASES) {
92
+ it(name, () => {
93
+ assert.deepEqual(resolveProductImages(product), want)
94
+ })
95
+ }
96
+ })
@@ -104,6 +104,15 @@ function rewriteStoredBackendUrl(url: string): string {
104
104
 
105
105
  type MediaRef = { url?: string | null } | string | null | undefined
106
106
 
107
+ export type ProductImageSource = {
108
+ thumbnail?: string | null
109
+ images?: Array<{ url?: string | null }> | null
110
+ variants?: Array<{
111
+ thumbnail?: string | null
112
+ images?: Array<MediaRef> | null
113
+ }> | null
114
+ }
115
+
107
116
  type CartLineImageSource = {
108
117
  thumbnail?: string | null
109
118
  product?: {
@@ -144,3 +153,50 @@ export function resolveCartLineItemImage(item: CartLineImageSource): string | nu
144
153
 
145
154
  return null
146
155
  }
156
+
157
+ function firstResolvedUrl(candidates: Array<string | null | undefined>): string | null {
158
+ for (const candidate of candidates) {
159
+ const resolved = resolveMediaUrl(candidate)
160
+ if (resolved) return resolved
161
+ }
162
+ return null
163
+ }
164
+
165
+ /** Product thumbnail, then first gallery image, then first variant image. */
166
+ export function resolveProductImage(product: ProductImageSource): string | null {
167
+ const variant = product.variants?.[0]
168
+ return firstResolvedUrl([
169
+ product.thumbnail,
170
+ ...(product.images ?? []).map((image) => image.url),
171
+ variant?.thumbnail,
172
+ ...(variant?.images ?? []).map((image) => mediaRefUrl(image)),
173
+ ])
174
+ }
175
+
176
+ /** Unique product media URLs for galleries, starting with the primary image. */
177
+ export function resolveProductImages(product: ProductImageSource): string[] {
178
+ const urls: string[] = []
179
+ const seen = new Set<string>()
180
+ const primary = resolveProductImage(product)
181
+ if (primary) {
182
+ urls.push(primary)
183
+ seen.add(primary)
184
+ }
185
+
186
+ const variantImages =
187
+ product.variants?.flatMap((variant) => variant.images ?? []) ?? []
188
+ const candidates = [
189
+ ...(product.images ?? []).map((image) => image.url),
190
+ ...(product.variants ?? []).map((variant) => variant.thumbnail),
191
+ ...variantImages.map((image) => mediaRefUrl(image)),
192
+ ]
193
+
194
+ for (const candidate of candidates) {
195
+ const resolved = resolveMediaUrl(candidate)
196
+ if (!resolved || seen.has(resolved)) continue
197
+ urls.push(resolved)
198
+ seen.add(resolved)
199
+ }
200
+
201
+ return urls
202
+ }
@@ -4,5 +4,6 @@ import { revalidatePath } from "next/cache"
4
4
 
5
5
  export async function revalidateWishlistCache() {
6
6
  revalidatePath("/", "layout")
7
+ revalidatePath("/store")
7
8
  revalidatePath("/wishlist")
8
9
  }
@@ -0,0 +1,83 @@
1
+ "use client"
2
+
3
+ import { getWishlistProductIds } from "@pradip1995/commerce-core/client/actions/wishlist"
4
+ import { WISHLIST_UPDATED_EVENT } from "./wishlist-count"
5
+
6
+ const WINDOW_KEY = "__storefrontWishlistIds"
7
+ const WINDOW_INFLIGHT = "__storefrontWishlistIdsInflight"
8
+
9
+ type WishlistIdsWindow = Window & {
10
+ [WINDOW_KEY]?: string[]
11
+ [WINDOW_INFLIGHT]?: Promise<string[]>
12
+ }
13
+
14
+ function win(): WishlistIdsWindow | null {
15
+ if (typeof window === "undefined") return null
16
+ return window as WishlistIdsWindow
17
+ }
18
+
19
+ function normalizeIds(ids: unknown): string[] {
20
+ if (!Array.isArray(ids)) return []
21
+ return ids.filter((id): id is string => typeof id === "string" && id.length > 0)
22
+ }
23
+
24
+ function fetchWishlistIds(w: WishlistIdsWindow): Promise<string[]> {
25
+ w[WINDOW_INFLIGHT] = getWishlistProductIds()
26
+ .then((ids) => {
27
+ const normalized = normalizeIds(ids)
28
+ w[WINDOW_KEY] = normalized
29
+ delete w[WINDOW_INFLIGHT]
30
+ return normalized
31
+ })
32
+ .catch(() => {
33
+ w[WINDOW_KEY] = []
34
+ delete w[WINDOW_INFLIGHT]
35
+ return [] as string[]
36
+ })
37
+ return w[WINDOW_INFLIGHT]!
38
+ }
39
+
40
+ /** Drop cached IDs so the next loadSharedWishlistIds() hits the server again. */
41
+ export function clearSharedWishlistIdsCache() {
42
+ const w = win()
43
+ if (!w) return
44
+ delete w[WINDOW_KEY]
45
+ delete w[WINDOW_INFLIGHT]
46
+ }
47
+
48
+ /**
49
+ * Shared wishlist ID loader (window-scoped so shop cards + wishlist page share
50
+ * one cache even when the bundler duplicates the module).
51
+ *
52
+ * `force: true` drops settled cache but reuses an in-flight fetch so N product
53
+ * cards mounting together only hit the server once.
54
+ */
55
+ export function loadSharedWishlistIds(options?: { force?: boolean }): Promise<string[]> {
56
+ const w = win()
57
+ if (!w) {
58
+ return getWishlistProductIds()
59
+ .then(normalizeIds)
60
+ .catch(() => [] as string[])
61
+ }
62
+
63
+ if (options?.force) {
64
+ delete w[WINDOW_KEY]
65
+ if (w[WINDOW_INFLIGHT]) return w[WINDOW_INFLIGHT]
66
+ return fetchWishlistIds(w)
67
+ }
68
+
69
+ if (w[WINDOW_KEY]) return Promise.resolve(w[WINDOW_KEY]!)
70
+ if (w[WINDOW_INFLIGHT]) return w[WINDOW_INFLIGHT]
71
+ return fetchWishlistIds(w)
72
+ }
73
+
74
+ let listening = false
75
+
76
+ /** Ensure wishlist:updated always invalidates the window cache (once per runtime). */
77
+ export function ensureWishlistIdsInvalidationListener() {
78
+ if (listening || typeof window === "undefined") return
79
+ listening = true
80
+ window.addEventListener(WISHLIST_UPDATED_EVENT, () => {
81
+ clearSharedWishlistIdsCache()
82
+ })
83
+ }