@tapcart/mobile-components 0.7.52 → 0.7.54
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/components/hooks/use-block-conditional-rendering.d.ts.map +1 -1
- package/dist/components/hooks/use-block-conditional-rendering.js +46 -38
- package/dist/components/hooks/use-click-outside.d.ts +6 -0
- package/dist/components/hooks/use-click-outside.d.ts.map +1 -0
- package/dist/components/hooks/use-click-outside.js +15 -0
- package/dist/components/hooks/use-nosto-recommendation.test.js +118 -25
- package/dist/components/hooks/use-outside-click.d.ts +7 -0
- package/dist/components/hooks/use-outside-click.d.ts.map +1 -0
- package/dist/components/hooks/use-outside-click.js +16 -0
- package/dist/components/hooks/use-tap.d.ts +8 -0
- package/dist/components/hooks/use-tap.d.ts.map +1 -0
- package/dist/components/hooks/use-tap.js +100 -0
- package/dist/components/ui/text.d.ts +9 -1
- package/dist/components/ui/text.d.ts.map +1 -1
- package/dist/components/ui/text.js +39 -2
- package/dist/components/ui/virtual-grid.d.ts +14 -0
- package/dist/components/ui/virtual-grid.d.ts.map +1 -0
- package/dist/components/ui/virtual-grid.js +55 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/lib/utils.d.ts +2 -0
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +50 -0
- package/dist/styles.css +182 -162
- package/package.json +26 -23
- package/dist/components/hooks/use-debug-dependencies.d.ts +0 -10
- package/dist/components/hooks/use-debug-dependencies.d.ts.map +0 -1
- package/dist/components/hooks/use-debug-dependencies.js +0 -18
- package/dist/components/hooks/use-layout.d.ts +0 -13
- package/dist/components/hooks/use-layout.d.ts.map +0 -1
- package/dist/components/hooks/use-layout.js +0 -23
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-block-conditional-rendering.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-block-conditional-rendering.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,YAAY,EAGb,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"use-block-conditional-rendering.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-block-conditional-rendering.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,YAAY,EAGb,MAAM,kBAAkB,CAAA;AAgBzB,eAAO,MAAM,4BAA4B,WAC/B;IACN,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf,UACO,YAAY;;;CA6KrB,CAAA"}
|
|
@@ -16,11 +16,11 @@ import { useCollection } from "./use-collection";
|
|
|
16
16
|
import { useSearchParams } from "next/navigation";
|
|
17
17
|
//@ts-expect-error
|
|
18
18
|
import { useVariables } from "@tapcart/webbridge-react";
|
|
19
|
-
import { gidFromId, countNumberOfTagsInState, getEnvState, shouldShowBlock, } from "../../lib/utils";
|
|
19
|
+
import { gidFromId, countNumberOfTagsInState, getEnvState, shouldShowBlock, evaluateConditions, } from "../../lib/utils";
|
|
20
20
|
export const useBlockConditionalRendering = (_props, _block) => {
|
|
21
|
-
var _a, _b, _c, _d, _e
|
|
22
|
-
const
|
|
23
|
-
|
|
21
|
+
var _a, _b, _c, _d, _e;
|
|
22
|
+
const _f = ((_a = _block === null || _block === void 0 ? void 0 : _block.visibilityConditions) === null || _a === void 0 ? void 0 : _a.conditions) || {}, { enabled: conditionalsV1IsEnabled = false, exclude: isInverse = false } = _f, restOfProps = __rest(_f, ["enabled", "exclude"]);
|
|
23
|
+
const { _version = 1, conditions, enabled: conditionalsV2IsEnabled = false, } = (_block === null || _block === void 0 ? void 0 : _block.visibilityConditions) || {};
|
|
24
24
|
const blockState = restOfProps;
|
|
25
25
|
const { appId = "", apiUrl = "" } = _props;
|
|
26
26
|
const searchParams = useSearchParams();
|
|
@@ -29,7 +29,7 @@ export const useBlockConditionalRendering = (_props, _block) => {
|
|
|
29
29
|
const customerVariables = variables === null || variables === void 0 ? void 0 : variables.customer;
|
|
30
30
|
// locale is provided in this form en_US
|
|
31
31
|
const [language, country] = ((_b = deviceVariables === null || deviceVariables === void 0 ? void 0 : deviceVariables.locale) === null || _b === void 0 ? void 0 : _b.split("_")) || [];
|
|
32
|
-
const productId = (_c = searchParams.get("productId")) !== null && _c !== void 0 ? _c : undefined;
|
|
32
|
+
const productId = (_c = searchParams === null || searchParams === void 0 ? void 0 : searchParams.get("productId")) !== null && _c !== void 0 ? _c : undefined;
|
|
33
33
|
const location = {
|
|
34
34
|
country: country || "US",
|
|
35
35
|
language: language || "en",
|
|
@@ -45,7 +45,7 @@ export const useBlockConditionalRendering = (_props, _block) => {
|
|
|
45
45
|
};
|
|
46
46
|
}), [blockProductMetafields]);
|
|
47
47
|
// Fetch product if productId is present
|
|
48
|
-
const { products, error: useProductsError, isLoading:
|
|
48
|
+
const { products, error: useProductsError, isLoading: isProductsLoading, } = useProducts(productId !== undefined && conditionalsV1IsEnabled
|
|
49
49
|
? {
|
|
50
50
|
productIds: [gidFromId(productId)],
|
|
51
51
|
baseURL: apiUrl,
|
|
@@ -68,9 +68,9 @@ export const useBlockConditionalRendering = (_props, _block) => {
|
|
|
68
68
|
.map((m) => `${m.namespace}.${m.key}`)
|
|
69
69
|
.join(",")
|
|
70
70
|
: undefined, [collectionMetafieldsQuery]);
|
|
71
|
-
const collectionId = (_d = searchParams.get("collectionId")) !== null && _d !== void 0 ? _d : undefined;
|
|
72
|
-
const collectionHandle = (_e = searchParams.get("collectionHandle")) !== null && _e !== void 0 ? _e : undefined;
|
|
73
|
-
const { specificCollection = {}, error: useCollectionError, loading:
|
|
71
|
+
const collectionId = (_d = searchParams === null || searchParams === void 0 ? void 0 : searchParams.get("collectionId")) !== null && _d !== void 0 ? _d : undefined;
|
|
72
|
+
const collectionHandle = (_e = searchParams === null || searchParams === void 0 ? void 0 : searchParams.get("collectionHandle")) !== null && _e !== void 0 ? _e : undefined;
|
|
73
|
+
const { specificCollection = {}, error: useCollectionError, loading: isCollectionLoading, } = useCollection({
|
|
74
74
|
appId,
|
|
75
75
|
apiUrl,
|
|
76
76
|
collectionId,
|
|
@@ -78,42 +78,50 @@ export const useBlockConditionalRendering = (_props, _block) => {
|
|
|
78
78
|
language: location === null || location === void 0 ? void 0 : location.language,
|
|
79
79
|
metafields: memoizedMetafields,
|
|
80
80
|
});
|
|
81
|
+
const isLoading = isProductsLoading || isCollectionLoading;
|
|
81
82
|
if (useProductsError || useCollectionError) {
|
|
82
83
|
console.error("Unable to load products in conditional block rendering hook: ", useProductsError !== null && useProductsError !== void 0 ? useProductsError : useCollectionError);
|
|
83
84
|
}
|
|
85
|
+
let shouldShow = true;
|
|
86
|
+
const blockConditionalsIsEnabled = conditionalsV1IsEnabled || conditionalsV2IsEnabled;
|
|
87
|
+
const blockHasTags = countNumberOfTagsInState(blockState) > 0;
|
|
88
|
+
const product = products.length === 1 ? products[0] : undefined;
|
|
89
|
+
const envState = getEnvState({
|
|
90
|
+
location,
|
|
91
|
+
product,
|
|
92
|
+
collection: specificCollection,
|
|
93
|
+
customer: customerVariables,
|
|
94
|
+
});
|
|
95
|
+
const evaluateV1Conditions = () => {
|
|
96
|
+
if (!blockHasTags) {
|
|
97
|
+
// No tags configured - default to showing
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
//@ts-expect-error: envState typing mismatch
|
|
101
|
+
const shouldShowPreInverse = shouldShowBlock(envState, blockState);
|
|
102
|
+
// TODO: Remove when inverse is available for all blocks
|
|
103
|
+
return isInverse ? !shouldShowPreInverse : shouldShowPreInverse;
|
|
104
|
+
};
|
|
105
|
+
const evaluateV2Conditions = () => {
|
|
106
|
+
return evaluateConditions(conditions, envState);
|
|
107
|
+
};
|
|
84
108
|
try {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
});
|
|
96
|
-
//@ts-expect-errors
|
|
97
|
-
const shouldShowPreInverse = shouldShowBlock(envState, blockState);
|
|
98
|
-
// TODO: Currently negation of properties is not available but is required for migration of certain merchants
|
|
99
|
-
// This code is to be removed and updated when inverse feature is available for all blocks
|
|
100
|
-
// If exclude tag is applied then block should show inversely
|
|
101
|
-
if (isInverse) {
|
|
102
|
-
shouldShow = !shouldShowPreInverse;
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
shouldShow = shouldShowPreInverse;
|
|
106
|
-
}
|
|
109
|
+
if (blockConditionalsIsEnabled) {
|
|
110
|
+
switch (_version) {
|
|
111
|
+
case 2:
|
|
112
|
+
shouldShow = evaluateV2Conditions();
|
|
113
|
+
break;
|
|
114
|
+
case 1:
|
|
115
|
+
default:
|
|
116
|
+
// Handles v1 and any unexpected versions
|
|
117
|
+
shouldShow = evaluateV1Conditions();
|
|
118
|
+
break;
|
|
107
119
|
}
|
|
108
120
|
}
|
|
109
|
-
// if conditionals is disabled, show the block.
|
|
110
|
-
if (!isEnabled) {
|
|
111
|
-
return { shouldShow: true, isLoading: false };
|
|
112
|
-
}
|
|
113
|
-
return { shouldShow, isLoading: useProductsLoading || useCollectionLoading };
|
|
114
121
|
}
|
|
115
122
|
catch (e) {
|
|
116
|
-
console.error("
|
|
117
|
-
|
|
123
|
+
console.error("Error evaluating block visibility conditions:", e);
|
|
124
|
+
shouldShow = true; // Fail-safe to show block
|
|
118
125
|
}
|
|
126
|
+
return { shouldShow, isLoading };
|
|
119
127
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-click-outside.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-click-outside.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,wBAAgB,eAAe,CAAC,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAE,IAAI,CAa5F"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import React from "react"
|
|
3
|
+
export function useClickOutside(ref, callback) {
|
|
4
|
+
React.useEffect(() => {
|
|
5
|
+
const handleClickOutside = (event) => {
|
|
6
|
+
if (ref.current && !ref.current.contains(event.target)) {
|
|
7
|
+
callback()
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
document.addEventListener("mousedown", handleClickOutside)
|
|
11
|
+
return () => {
|
|
12
|
+
document.removeEventListener("mousedown", handleClickOutside)
|
|
13
|
+
}
|
|
14
|
+
}, [ref, callback])
|
|
15
|
+
}
|
|
@@ -7,7 +7,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { renderHook } from "@testing-library/react
|
|
10
|
+
import { renderHook } from "@testing-library/react";
|
|
11
|
+
import { act } from "react-dom/test-utils";
|
|
11
12
|
import { NostoRecommendationsType, useNostoRecommendations, } from "./use-nosto-recommendation";
|
|
12
13
|
import { useProducts } from "./use-products";
|
|
13
14
|
jest.mock("./use-products", () => ({
|
|
@@ -111,8 +112,14 @@ describe("useNostoRecommendations", () => {
|
|
|
111
112
|
});
|
|
112
113
|
it("should return front page recommendations if productIds is empty and slotId is provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
113
114
|
const slotId = "mockSlotId";
|
|
114
|
-
const { result
|
|
115
|
-
|
|
115
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, slotId, undefined, undefined, undefined));
|
|
116
|
+
// Initial state check
|
|
117
|
+
expect(result.current.isLoading).toBe(true);
|
|
118
|
+
// Wait for all promises with act
|
|
119
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
120
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
121
|
+
}));
|
|
122
|
+
// Check final state
|
|
116
123
|
expect(result.current.isLoading).toBe(false);
|
|
117
124
|
expect(result.current.error).toBe(null);
|
|
118
125
|
expect(useProducts).toHaveBeenCalledWith({
|
|
@@ -127,8 +134,15 @@ describe("useNostoRecommendations", () => {
|
|
|
127
134
|
}));
|
|
128
135
|
it("should return best sellers recommendations if productIds is empty and slotId is not provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
129
136
|
const slotId = "";
|
|
130
|
-
const { result
|
|
131
|
-
|
|
137
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, slotId, undefined, undefined));
|
|
138
|
+
// Initial state check
|
|
139
|
+
expect(result.current.isLoading).toBe(true);
|
|
140
|
+
// Wait for all promises with act
|
|
141
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
142
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
143
|
+
}));
|
|
144
|
+
// Check final state
|
|
145
|
+
expect(result.current.isLoading).toBe(false);
|
|
132
146
|
expect(result.current.error).toBe(null);
|
|
133
147
|
expect(useProducts).toHaveBeenCalledWith({
|
|
134
148
|
productIds: ["best-seller-1"],
|
|
@@ -143,8 +157,15 @@ describe("useNostoRecommendations", () => {
|
|
|
143
157
|
it("should return best sellers recommendations if productIds is not empty, Best Sellers is the recommendation type and slotId is not provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
144
158
|
const slotId = "";
|
|
145
159
|
const productIds = ["mockProductId"];
|
|
146
|
-
const { result
|
|
147
|
-
|
|
160
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, slotId, NostoRecommendationsType.BEST_SELLERS, productIds));
|
|
161
|
+
// Initial state check
|
|
162
|
+
expect(result.current.isLoading).toBe(true);
|
|
163
|
+
// Wait for all promises with act
|
|
164
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
165
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
166
|
+
}));
|
|
167
|
+
// Check final state
|
|
168
|
+
expect(result.current.isLoading).toBe(false);
|
|
148
169
|
expect(result.current.error).toBe(null);
|
|
149
170
|
expect(useProducts).toHaveBeenCalledWith({
|
|
150
171
|
productIds: ["best-seller-1"],
|
|
@@ -158,8 +179,15 @@ describe("useNostoRecommendations", () => {
|
|
|
158
179
|
}));
|
|
159
180
|
it("should return default front page recommendations if slotId is provided but layoutType is not", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
160
181
|
const slotId = "mockSlotId";
|
|
161
|
-
const { result
|
|
162
|
-
|
|
182
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, slotId));
|
|
183
|
+
// Initial state check
|
|
184
|
+
expect(result.current.isLoading).toBe(true);
|
|
185
|
+
// Wait for all promises with act
|
|
186
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
187
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
188
|
+
}));
|
|
189
|
+
// Check final state
|
|
190
|
+
expect(result.current.isLoading).toBe(false);
|
|
163
191
|
expect(result.current.error).toBe(null);
|
|
164
192
|
expect(useProducts).toHaveBeenCalledWith({
|
|
165
193
|
productIds: ["front-page-id-1"],
|
|
@@ -172,8 +200,15 @@ describe("useNostoRecommendations", () => {
|
|
|
172
200
|
});
|
|
173
201
|
}));
|
|
174
202
|
it("should return the correct recommendations for best sellers", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
175
|
-
const { result
|
|
176
|
-
|
|
203
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", NostoRecommendationsType.BEST_SELLERS, undefined, undefined));
|
|
204
|
+
// Initial state check
|
|
205
|
+
expect(result.current.isLoading).toBe(true);
|
|
206
|
+
// Wait for all promises with act
|
|
207
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
208
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
209
|
+
}));
|
|
210
|
+
// Check final state
|
|
211
|
+
expect(result.current.isLoading).toBe(false);
|
|
177
212
|
expect(result.current.error).toBe(null);
|
|
178
213
|
expect(useProducts).toHaveBeenCalledWith({
|
|
179
214
|
productIds: ["best-seller-1"],
|
|
@@ -187,8 +222,15 @@ describe("useNostoRecommendations", () => {
|
|
|
187
222
|
}));
|
|
188
223
|
it("should return the related recommendations when only productIds and a recommendation type are provided on a non-home layout", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
189
224
|
const productIds = ["mockProductId"];
|
|
190
|
-
const { result
|
|
191
|
-
|
|
225
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", NostoRecommendationsType.VIEWED_TOGETHER, productIds, undefined));
|
|
226
|
+
// Initial state check
|
|
227
|
+
expect(result.current.isLoading).toBe(true);
|
|
228
|
+
// Wait for all promises with act
|
|
229
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
230
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
231
|
+
}));
|
|
232
|
+
// Check final state
|
|
233
|
+
expect(result.current.isLoading).toBe(false);
|
|
192
234
|
expect(result.current.error).toBe(null);
|
|
193
235
|
expect(useProducts).toHaveBeenCalledWith({
|
|
194
236
|
productIds: ["related-1"],
|
|
@@ -203,8 +245,15 @@ describe("useNostoRecommendations", () => {
|
|
|
203
245
|
it("should return the correct recommendations when productIds and slotIds are provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
204
246
|
const slotId = "mockSlotId";
|
|
205
247
|
const productIds = ["mockProductId"];
|
|
206
|
-
const { result
|
|
207
|
-
|
|
248
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, slotId, NostoRecommendationsType.VIEWED_TOGETHER, productIds, undefined));
|
|
249
|
+
// Initial state check
|
|
250
|
+
expect(result.current.isLoading).toBe(true);
|
|
251
|
+
// Wait for all promises with act
|
|
252
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
253
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
254
|
+
}));
|
|
255
|
+
// Check final state
|
|
256
|
+
expect(result.current.isLoading).toBe(false);
|
|
208
257
|
expect(result.current.error).toBe(null);
|
|
209
258
|
expect(useProducts).toHaveBeenCalledWith({
|
|
210
259
|
productIds: ["product-page-id-1"],
|
|
@@ -218,8 +267,15 @@ describe("useNostoRecommendations", () => {
|
|
|
218
267
|
}));
|
|
219
268
|
it("should return the related products fallback recommendations when productIds are provided and no slotId and a type other than viewed_category and best_sellers is provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
220
269
|
const productIds = ["mockProductId"];
|
|
221
|
-
const { result
|
|
222
|
-
|
|
270
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", NostoRecommendationsType.VIEWED_TOGETHER, productIds, undefined));
|
|
271
|
+
// Initial state check
|
|
272
|
+
expect(result.current.isLoading).toBe(true);
|
|
273
|
+
// Wait for all promises with act
|
|
274
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
275
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
276
|
+
}));
|
|
277
|
+
// Check final state
|
|
278
|
+
expect(result.current.isLoading).toBe(false);
|
|
223
279
|
expect(result.current.error).toBe(null);
|
|
224
280
|
expect(useProducts).toHaveBeenCalledWith({
|
|
225
281
|
productIds: ["related-1"],
|
|
@@ -233,8 +289,15 @@ describe("useNostoRecommendations", () => {
|
|
|
233
289
|
}));
|
|
234
290
|
it("should return the category fallback recommendations when no slotId, productIds are provided and a recommendation type of viewed_category is provided", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
235
291
|
const productIds = ["mockProductId"];
|
|
236
|
-
const { result
|
|
237
|
-
|
|
292
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", NostoRecommendationsType.VIEWED_CATEGORY, productIds, undefined));
|
|
293
|
+
// Initial state check
|
|
294
|
+
expect(result.current.isLoading).toBe(true);
|
|
295
|
+
// Wait for all promises with act
|
|
296
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
297
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
298
|
+
}));
|
|
299
|
+
// Check final state
|
|
300
|
+
expect(result.current.isLoading).toBe(false);
|
|
238
301
|
expect(result.current.error).toBe(null);
|
|
239
302
|
expect(useProducts).toHaveBeenCalledWith({
|
|
240
303
|
productIds: ["category-page-id-1"],
|
|
@@ -248,7 +311,15 @@ describe("useNostoRecommendations", () => {
|
|
|
248
311
|
}));
|
|
249
312
|
it("should handle loading state", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
250
313
|
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, ""));
|
|
314
|
+
// Check initial loading state
|
|
251
315
|
expect(result.current.isLoading).toBe(true);
|
|
316
|
+
// Wait for all state updates to complete
|
|
317
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
318
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
319
|
+
}));
|
|
320
|
+
// Verify loading has finished
|
|
321
|
+
expect(result.current.isLoading).toBe(false);
|
|
322
|
+
expect(result.current.error).toBe(null);
|
|
252
323
|
}));
|
|
253
324
|
it("should handle error state", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
254
325
|
const mockError = () => "Failed to fetch products";
|
|
@@ -257,14 +328,29 @@ describe("useNostoRecommendations", () => {
|
|
|
257
328
|
isLoading: false,
|
|
258
329
|
error: mockError,
|
|
259
330
|
});
|
|
260
|
-
const { result
|
|
261
|
-
|
|
331
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL));
|
|
332
|
+
// Initial state check
|
|
333
|
+
expect(result.current.isLoading).toBe(true);
|
|
334
|
+
// Wait for all promises and state updates to resolve
|
|
335
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
336
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
337
|
+
}));
|
|
338
|
+
// Verify final error state
|
|
339
|
+
expect(result.current.isLoading).toBe(false);
|
|
262
340
|
expect(result.current.error).toBe(mockError);
|
|
341
|
+
expect(result.current.recommendations).toEqual([]);
|
|
263
342
|
}));
|
|
264
343
|
it("should default to best sellers if productIds are empty but a recommendation type other than Best Sellers is used", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
265
344
|
const productIds = [];
|
|
266
|
-
const { result
|
|
267
|
-
|
|
345
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", NostoRecommendationsType.VIEWED_TOGETHER, productIds, undefined));
|
|
346
|
+
// Initial state check
|
|
347
|
+
expect(result.current.isLoading).toBe(true);
|
|
348
|
+
// Wait for all promises with act
|
|
349
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
350
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
351
|
+
}));
|
|
352
|
+
// Check final state
|
|
353
|
+
expect(result.current.isLoading).toBe(false);
|
|
268
354
|
expect(result.current.error).toBe(null);
|
|
269
355
|
expect(useProducts).toHaveBeenCalledWith({
|
|
270
356
|
productIds: ["best-seller-1"],
|
|
@@ -277,8 +363,15 @@ describe("useNostoRecommendations", () => {
|
|
|
277
363
|
});
|
|
278
364
|
}));
|
|
279
365
|
it("should return an empty array if skip is true", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
280
|
-
const { result
|
|
281
|
-
|
|
366
|
+
const { result } = renderHook(() => useNostoRecommendations(mockIntegrations, mockBaseURL, "", NostoRecommendationsType.BEST_SELLERS, undefined, true));
|
|
367
|
+
// Initial state check
|
|
368
|
+
expect(result.current.isLoading).toBe(true);
|
|
369
|
+
// Wait for all promises with act
|
|
370
|
+
yield act(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
371
|
+
yield new Promise((resolve) => setTimeout(resolve, 0));
|
|
372
|
+
}));
|
|
373
|
+
// Check final state
|
|
374
|
+
expect(result.current.isLoading).toBe(false);
|
|
282
375
|
expect(useProducts).toHaveBeenCalledWith({
|
|
283
376
|
productIds: [],
|
|
284
377
|
baseURL: mockBaseURL,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-outside-click.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-outside-click.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,QAAA,MAAM,eAAe,QAAS,MAAM,SAAS,CAAC,WAAW,CAAC,YAAY,MAAM,IAAI,SAa/E,CAAC;AAEF,eAAe,eAAe,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import React from "react"
|
|
3
|
+
const useClickOutside = (ref, callback) => {
|
|
4
|
+
React.useEffect(() => {
|
|
5
|
+
const handleClickOutside = (event) => {
|
|
6
|
+
if (ref.current && !ref.current.contains(event.target)) {
|
|
7
|
+
callback()
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
document.addEventListener("mousedown", handleClickOutside)
|
|
11
|
+
return () => {
|
|
12
|
+
document.removeEventListener("mousedown", handleClickOutside)
|
|
13
|
+
}
|
|
14
|
+
}, [ref, callback])
|
|
15
|
+
}
|
|
16
|
+
export default useClickOutside
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tap.d.ts","sourceRoot":"","sources":["../../../components/hooks/use-tap.ts"],"names":[],"mappings":"AACA,OAAO,KAAmD,MAAM,OAAO,CAAA;AAuFvE,QAAA,MAAM,MAAM;6BAuBkC,GAAG,KAAK,IAAI,aACvC,GAAG;;;CAerB,CAAA;AAED,OAAO,EAAE,MAAM,EAAE,CAAA"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import { useState, useEffect, useCallback, useRef } from "react"
|
|
3
|
+
// Shared manager for all instances of the hook
|
|
4
|
+
const tapManager = (() => {
|
|
5
|
+
const elements = new Map()
|
|
6
|
+
let isListening = false
|
|
7
|
+
const startListening = () => {
|
|
8
|
+
if (isListening) return
|
|
9
|
+
const handleTouchStart = (e) => {
|
|
10
|
+
const touch = e.touches[0]
|
|
11
|
+
elements.forEach((data, el) => {
|
|
12
|
+
if (el.contains(touch.target)) {
|
|
13
|
+
data.touchStarted = true
|
|
14
|
+
data.touchMoved = false
|
|
15
|
+
data.startPosition = { x: touch.clientX, y: touch.clientY }
|
|
16
|
+
// Don't set isPressed here, wait to determine if it's a tap or drag
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
const handleTouchMove = (e) => {
|
|
21
|
+
const touch = e.touches[0]
|
|
22
|
+
elements.forEach((data, el) => {
|
|
23
|
+
if (data.touchStarted) {
|
|
24
|
+
const deltaX = Math.abs(touch.clientX - data.startPosition.x)
|
|
25
|
+
const deltaY = Math.abs(touch.clientY - data.startPosition.y)
|
|
26
|
+
if (deltaX > data.tapThreshold || deltaY > data.tapThreshold) {
|
|
27
|
+
data.touchMoved = true
|
|
28
|
+
data.setIsPressed(false)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
const handleTouchEnd = () => {
|
|
34
|
+
elements.forEach((data) => {
|
|
35
|
+
if (data.touchStarted) {
|
|
36
|
+
data.touchStarted = false
|
|
37
|
+
if (!data.touchMoved) {
|
|
38
|
+
// It's a tap, set isPressed briefly
|
|
39
|
+
data.setIsPressed(true)
|
|
40
|
+
setTimeout(() => data.setIsPressed(false), 100)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
document.addEventListener("touchstart", (e) => handleTouchStart(e), {
|
|
46
|
+
passive: true,
|
|
47
|
+
})
|
|
48
|
+
document.addEventListener("touchmove", (e) => handleTouchMove(e), {
|
|
49
|
+
passive: true,
|
|
50
|
+
})
|
|
51
|
+
document.addEventListener("touchend", () => handleTouchEnd(), {
|
|
52
|
+
passive: true,
|
|
53
|
+
})
|
|
54
|
+
isListening = true
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
register: (el, data) => {
|
|
58
|
+
elements.set(el, data)
|
|
59
|
+
startListening()
|
|
60
|
+
},
|
|
61
|
+
unregister: (el) => {
|
|
62
|
+
elements.delete(el)
|
|
63
|
+
},
|
|
64
|
+
elements,
|
|
65
|
+
}
|
|
66
|
+
})()
|
|
67
|
+
const useTap = (tapThreshold = 10) => {
|
|
68
|
+
const [isPressed, setIsPressed] = useState(false)
|
|
69
|
+
const elementRef = useRef(null)
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
const element = elementRef.current
|
|
72
|
+
if (!element) return
|
|
73
|
+
const data = {
|
|
74
|
+
touchStarted: false,
|
|
75
|
+
touchMoved: false,
|
|
76
|
+
startPosition: { x: 0, y: 0 },
|
|
77
|
+
setIsPressed,
|
|
78
|
+
tapThreshold,
|
|
79
|
+
}
|
|
80
|
+
tapManager.register(element, data)
|
|
81
|
+
return () => {
|
|
82
|
+
tapManager.unregister(element)
|
|
83
|
+
}
|
|
84
|
+
}, [tapThreshold])
|
|
85
|
+
const onTap = useCallback((handler) => {
|
|
86
|
+
return (event) => {
|
|
87
|
+
const data = tapManager.elements.get(elementRef.current)
|
|
88
|
+
if (!data) return
|
|
89
|
+
if (event.type === "touchend" && !data.touchMoved) {
|
|
90
|
+
handler(event)
|
|
91
|
+
} else if (event.type === "click" && !data.touchStarted) {
|
|
92
|
+
handler(event)
|
|
93
|
+
setIsPressed(true)
|
|
94
|
+
setTimeout(() => setIsPressed(false), 100)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}, [])
|
|
98
|
+
return { onTap, isPressed, ref: elementRef }
|
|
99
|
+
}
|
|
100
|
+
export { useTap }
|
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { type VariantProps } from "class-variance-authority";
|
|
3
|
+
type Injections = Record<string, {
|
|
4
|
+
payload: string;
|
|
5
|
+
currencyMetadata?: {
|
|
6
|
+
currency: string;
|
|
7
|
+
locale: string;
|
|
8
|
+
};
|
|
9
|
+
}>;
|
|
3
10
|
declare const textVariants: (props?: ({
|
|
4
11
|
type?: "h1" | "h2" | "label" | "body-primary" | "body-secondary" | null | undefined;
|
|
5
12
|
} & import("class-variance-authority/dist/types").ClassProp) | undefined) => string;
|
|
6
13
|
export interface TextProps extends React.HTMLAttributes<HTMLParagraphElement>, VariantProps<typeof textVariants> {
|
|
7
14
|
fontColor?: string | null;
|
|
15
|
+
injections?: Injections;
|
|
8
16
|
children?: React.ReactNode;
|
|
9
17
|
}
|
|
10
|
-
declare function Text({ className, type, fontColor, children, ...props }: TextProps): import("react/jsx-runtime").JSX.Element;
|
|
18
|
+
declare function Text({ className, type, fontColor, injections, children, ...props }: TextProps): import("react/jsx-runtime").JSX.Element;
|
|
11
19
|
export { Text, textVariants };
|
|
12
20
|
//# sourceMappingURL=text.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../../components/ui/text.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAO,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAA;
|
|
1
|
+
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../../components/ui/text.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAO,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAQjE,KAAK,UAAU,GAAG,MAAM,CACtB,MAAM,EACN;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,gBAAgB,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAC7E,CAAA;AAmDD,QAAA,MAAM,YAAY;;mFAgBhB,CAAA;AAEF,MAAM,WAAW,SACf,SAAQ,KAAK,CAAC,cAAc,CAAC,oBAAoB,CAAC,EAChD,YAAY,CAAC,OAAO,YAAY,CAAC;IACnC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACzB,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAA;CAC3B;AAED,iBAAS,IAAI,CAAC,EACZ,SAAS,EACT,IAAI,EACJ,SAAS,EACT,UAAU,EACV,QAAQ,EACR,GAAG,KAAK,EACT,EAAE,SAAS,2CAoBX;AAED,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -14,6 +14,41 @@ import * as React from "react";
|
|
|
14
14
|
import { cva } from "class-variance-authority";
|
|
15
15
|
import { cn } from "../../lib/utils";
|
|
16
16
|
import { useTranslation, getTextTranslation, } from "../contexts/translation-context";
|
|
17
|
+
import { Money } from "./money";
|
|
18
|
+
function injectString(template, injections) {
|
|
19
|
+
if (injections === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (!Object.entries(injections).length)
|
|
22
|
+
return undefined;
|
|
23
|
+
const parts = [];
|
|
24
|
+
let lastIndex = 0;
|
|
25
|
+
// Find all matches and their positions
|
|
26
|
+
const matches = Array.from(template.matchAll(/\{\{(.*?)\}\}/g));
|
|
27
|
+
matches.forEach((match, index) => {
|
|
28
|
+
const key = match[1];
|
|
29
|
+
const injection = injections[key];
|
|
30
|
+
// Add text before the match
|
|
31
|
+
if (match.index && match.index > lastIndex) {
|
|
32
|
+
parts.push(template.slice(lastIndex, match.index));
|
|
33
|
+
}
|
|
34
|
+
// Add the injection
|
|
35
|
+
if (injection) {
|
|
36
|
+
if (injection.currencyMetadata) {
|
|
37
|
+
parts.push(_jsx(Money, { price: Number(injection.payload), currency: "USD" // These could be made configurable if needed
|
|
38
|
+
, locale: "en-US" }, `price-${index}`));
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
parts.push(injection.payload);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
lastIndex = (match.index || 0) + match[0].length;
|
|
45
|
+
});
|
|
46
|
+
// Add remaining text after last match
|
|
47
|
+
if (lastIndex < template.length) {
|
|
48
|
+
parts.push(template.slice(lastIndex));
|
|
49
|
+
}
|
|
50
|
+
return parts.length ? parts : undefined;
|
|
51
|
+
}
|
|
17
52
|
const textVariants = cva("", {
|
|
18
53
|
variants: {
|
|
19
54
|
type: {
|
|
@@ -29,7 +64,7 @@ const textVariants = cva("", {
|
|
|
29
64
|
},
|
|
30
65
|
});
|
|
31
66
|
function Text(_a) {
|
|
32
|
-
var { className, type, fontColor, children } = _a, props = __rest(_a, ["className", "type", "fontColor", "children"]);
|
|
67
|
+
var { className, type, fontColor, injections, children } = _a, props = __rest(_a, ["className", "type", "fontColor", "injections", "children"]);
|
|
33
68
|
const fontColorOverride = fontColor ? { color: fontColor } : {};
|
|
34
69
|
const translations = useTranslation();
|
|
35
70
|
const stringContents = React.Children.toArray(children).join("");
|
|
@@ -37,6 +72,8 @@ function Text(_a) {
|
|
|
37
72
|
text: stringContents,
|
|
38
73
|
translations,
|
|
39
74
|
});
|
|
40
|
-
|
|
75
|
+
const injectContents = injectString(stringContents, injections);
|
|
76
|
+
const content = injectContents || textContents || children;
|
|
77
|
+
return (_jsx("p", Object.assign({ className: cn(textVariants({ type }), className), style: fontColorOverride }, props, { children: content })));
|
|
41
78
|
}
|
|
42
79
|
export { Text, textVariants };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type VariantProps } from "class-variance-authority";
|
|
3
|
+
interface GridType {
|
|
4
|
+
className: any;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
declare const virtualGridVariants: (props?: ({
|
|
8
|
+
columns?: 1 | 2 | 3 | 4 | null | undefined;
|
|
9
|
+
} & import("class-variance-authority/dist/types").ClassProp) | undefined) => string;
|
|
10
|
+
export interface VirtualGridProps extends GridType, VariantProps<typeof virtualGridVariants> {
|
|
11
|
+
}
|
|
12
|
+
declare function VirtualGrid({ className, columns, children, ...props }: VirtualGridProps): import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
export { VirtualGrid, virtualGridVariants };
|
|
14
|
+
//# sourceMappingURL=virtual-grid.d.ts.map
|