@pradip1995/segment-primitives 0.4.14 → 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 +55 -51
- package/src/Button.tsx +19 -19
- package/src/Card.tsx +10 -10
- package/src/cart-live.ts +21 -0
- package/src/coupon-apply-result.test.ts +73 -73
- package/src/coupon-apply-result.ts +41 -41
- package/src/coupon-apply.tsx +184 -184
- package/src/format-price.ts +12 -12
- package/src/index.ts +2 -0
- package/src/is-next-redirect.ts +86 -86
- package/src/localized-link.tsx +37 -37
- package/src/notify-wishlist.ts +12 -9
- package/src/optimized-image-src.ts +16 -0
- package/src/order-lines.test.ts +246 -246
- package/src/order-lines.ts +151 -151
- package/src/product-selection-context.tsx +56 -56
- package/src/product-variants.ts +381 -381
- package/src/resolve-media-url.test.ts +96 -0
- package/src/resolve-media-url.ts +202 -146
- package/src/revalidate-wishlist.ts +9 -8
- package/src/section-variants.ts +13 -13
- package/src/skip-image-optimization.test.ts +27 -0
- package/src/skip-image-optimization.ts +11 -0
- package/src/use-wishlist-count.ts +32 -32
- package/src/wishlist-count.test.ts +96 -96
- package/src/wishlist-count.ts +26 -26
- package/src/wishlist-ids.ts +83 -0
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
import assert from "node:assert/strict"
|
|
2
|
-
import { describe, it } from "node:test"
|
|
3
|
-
import { nextWishlistCount, type WishlistUpdatedDetail } from "./wishlist-count.ts"
|
|
4
|
-
|
|
5
|
-
const CASES: Array<{
|
|
6
|
-
name: string
|
|
7
|
-
current: number
|
|
8
|
-
detail?: WishlistUpdatedDetail | null
|
|
9
|
-
want: number
|
|
10
|
-
}> = [
|
|
11
|
-
{
|
|
12
|
-
name: "removing one item decrements the navbar badge",
|
|
13
|
-
current: 2,
|
|
14
|
-
detail: { delta: -1 },
|
|
15
|
-
want: 1,
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
name: "removing the last item clears the badge",
|
|
19
|
-
current: 1,
|
|
20
|
-
detail: { delta: -1 },
|
|
21
|
-
want: 0,
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
name: "removing from an empty wishlist stays at zero",
|
|
25
|
-
current: 0,
|
|
26
|
-
detail: { delta: -1 },
|
|
27
|
-
want: 0,
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
name: "adding an item increments the badge",
|
|
31
|
-
current: 0,
|
|
32
|
-
detail: { delta: 1 },
|
|
33
|
-
want: 1,
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
name: "adding onto an existing count increments the badge",
|
|
37
|
-
current: 2,
|
|
38
|
-
detail: { delta: 1 },
|
|
39
|
-
want: 3,
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
name: "an explicit count replaces the current badge",
|
|
43
|
-
current: 4,
|
|
44
|
-
detail: { count: 1 },
|
|
45
|
-
want: 1,
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
name: "an explicit count wins when delta is also present",
|
|
49
|
-
current: 4,
|
|
50
|
-
detail: { count: 0, delta: 2 },
|
|
51
|
-
want: 0,
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
name: "missing detail keeps the current badge",
|
|
55
|
-
current: 3,
|
|
56
|
-
want: 3,
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
name: "null detail keeps the current badge",
|
|
60
|
-
current: 3,
|
|
61
|
-
detail: null,
|
|
62
|
-
want: 3,
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
name: "empty detail keeps the current badge",
|
|
66
|
-
current: 3,
|
|
67
|
-
detail: {},
|
|
68
|
-
want: 3,
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: "non-finite current is treated as zero before applying delta",
|
|
72
|
-
current: Number.NaN,
|
|
73
|
-
detail: { delta: 2 },
|
|
74
|
-
want: 2,
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
name: "non-finite delta is ignored",
|
|
78
|
-
current: 5,
|
|
79
|
-
detail: { delta: Number.NaN },
|
|
80
|
-
want: 5,
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
name: "fractional values are truncated",
|
|
84
|
-
current: 2.9,
|
|
85
|
-
detail: { delta: -1.8 },
|
|
86
|
-
want: 1,
|
|
87
|
-
},
|
|
88
|
-
]
|
|
89
|
-
|
|
90
|
-
describe("nextWishlistCount", () => {
|
|
91
|
-
for (const row of CASES) {
|
|
92
|
-
it(row.name, () => {
|
|
93
|
-
assert.equal(nextWishlistCount(row.current, row.detail), row.want)
|
|
94
|
-
})
|
|
95
|
-
}
|
|
96
|
-
})
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import { nextWishlistCount, type WishlistUpdatedDetail } from "./wishlist-count.ts"
|
|
4
|
+
|
|
5
|
+
const CASES: Array<{
|
|
6
|
+
name: string
|
|
7
|
+
current: number
|
|
8
|
+
detail?: WishlistUpdatedDetail | null
|
|
9
|
+
want: number
|
|
10
|
+
}> = [
|
|
11
|
+
{
|
|
12
|
+
name: "removing one item decrements the navbar badge",
|
|
13
|
+
current: 2,
|
|
14
|
+
detail: { delta: -1 },
|
|
15
|
+
want: 1,
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: "removing the last item clears the badge",
|
|
19
|
+
current: 1,
|
|
20
|
+
detail: { delta: -1 },
|
|
21
|
+
want: 0,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "removing from an empty wishlist stays at zero",
|
|
25
|
+
current: 0,
|
|
26
|
+
detail: { delta: -1 },
|
|
27
|
+
want: 0,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: "adding an item increments the badge",
|
|
31
|
+
current: 0,
|
|
32
|
+
detail: { delta: 1 },
|
|
33
|
+
want: 1,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "adding onto an existing count increments the badge",
|
|
37
|
+
current: 2,
|
|
38
|
+
detail: { delta: 1 },
|
|
39
|
+
want: 3,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "an explicit count replaces the current badge",
|
|
43
|
+
current: 4,
|
|
44
|
+
detail: { count: 1 },
|
|
45
|
+
want: 1,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "an explicit count wins when delta is also present",
|
|
49
|
+
current: 4,
|
|
50
|
+
detail: { count: 0, delta: 2 },
|
|
51
|
+
want: 0,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "missing detail keeps the current badge",
|
|
55
|
+
current: 3,
|
|
56
|
+
want: 3,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "null detail keeps the current badge",
|
|
60
|
+
current: 3,
|
|
61
|
+
detail: null,
|
|
62
|
+
want: 3,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "empty detail keeps the current badge",
|
|
66
|
+
current: 3,
|
|
67
|
+
detail: {},
|
|
68
|
+
want: 3,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "non-finite current is treated as zero before applying delta",
|
|
72
|
+
current: Number.NaN,
|
|
73
|
+
detail: { delta: 2 },
|
|
74
|
+
want: 2,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: "non-finite delta is ignored",
|
|
78
|
+
current: 5,
|
|
79
|
+
detail: { delta: Number.NaN },
|
|
80
|
+
want: 5,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "fractional values are truncated",
|
|
84
|
+
current: 2.9,
|
|
85
|
+
detail: { delta: -1.8 },
|
|
86
|
+
want: 1,
|
|
87
|
+
},
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
describe("nextWishlistCount", () => {
|
|
91
|
+
for (const row of CASES) {
|
|
92
|
+
it(row.name, () => {
|
|
93
|
+
assert.equal(nextWishlistCount(row.current, row.detail), row.want)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
})
|
package/src/wishlist-count.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
export const WISHLIST_UPDATED_EVENT = "wishlist:updated"
|
|
2
|
-
|
|
3
|
-
export type WishlistUpdatedDetail = {
|
|
4
|
-
delta?: number
|
|
5
|
-
count?: number
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function nextWishlistCount(
|
|
9
|
-
current: number,
|
|
10
|
-
detail?: WishlistUpdatedDetail | null
|
|
11
|
-
): number {
|
|
12
|
-
const base = Number.isFinite(current) ? Math.max(0, Math.trunc(current)) : 0
|
|
13
|
-
if (!detail) return base
|
|
14
|
-
if (typeof detail.count === "number" && Number.isFinite(detail.count)) {
|
|
15
|
-
return Math.max(0, Math.trunc(detail.count))
|
|
16
|
-
}
|
|
17
|
-
if (typeof detail.delta === "number" && Number.isFinite(detail.delta)) {
|
|
18
|
-
return Math.max(0, base + Math.trunc(detail.delta))
|
|
19
|
-
}
|
|
20
|
-
return base
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function dispatchWishlistUpdated(detail: WishlistUpdatedDetail) {
|
|
24
|
-
if (typeof window === "undefined") return
|
|
25
|
-
window.dispatchEvent(new CustomEvent(WISHLIST_UPDATED_EVENT, { detail }))
|
|
26
|
-
}
|
|
1
|
+
export const WISHLIST_UPDATED_EVENT = "wishlist:updated"
|
|
2
|
+
|
|
3
|
+
export type WishlistUpdatedDetail = {
|
|
4
|
+
delta?: number
|
|
5
|
+
count?: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function nextWishlistCount(
|
|
9
|
+
current: number,
|
|
10
|
+
detail?: WishlistUpdatedDetail | null
|
|
11
|
+
): number {
|
|
12
|
+
const base = Number.isFinite(current) ? Math.max(0, Math.trunc(current)) : 0
|
|
13
|
+
if (!detail) return base
|
|
14
|
+
if (typeof detail.count === "number" && Number.isFinite(detail.count)) {
|
|
15
|
+
return Math.max(0, Math.trunc(detail.count))
|
|
16
|
+
}
|
|
17
|
+
if (typeof detail.delta === "number" && Number.isFinite(detail.delta)) {
|
|
18
|
+
return Math.max(0, base + Math.trunc(detail.delta))
|
|
19
|
+
}
|
|
20
|
+
return base
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function dispatchWishlistUpdated(detail: WishlistUpdatedDetail) {
|
|
24
|
+
if (typeof window === "undefined") return
|
|
25
|
+
window.dispatchEvent(new CustomEvent(WISHLIST_UPDATED_EVENT, { detail }))
|
|
26
|
+
}
|
|
@@ -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
|
+
}
|