@akinon/pz-similar-products 1.92.0-rc.16
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/.gitattributes +15 -0
- package/.prettierrc +13 -0
- package/CHANGELOG.md +3 -0
- package/README.md +1372 -0
- package/package.json +21 -0
- package/src/data/endpoints.ts +122 -0
- package/src/hooks/index.ts +3 -0
- package/src/hooks/use-image-cropper.ts +264 -0
- package/src/hooks/use-image-search-feature.ts +32 -0
- package/src/hooks/use-similar-products.ts +939 -0
- package/src/index.ts +33 -0
- package/src/types/index.ts +419 -0
- package/src/utils/image-validation.ts +303 -0
- package/src/utils/index.ts +161 -0
- package/src/views/filters.tsx +858 -0
- package/src/views/header-image-search-feature.tsx +68 -0
- package/src/views/image-search-button.tsx +47 -0
- package/src/views/image-search.tsx +152 -0
- package/src/views/main.tsx +200 -0
- package/src/views/product-image-search-feature.tsx +48 -0
- package/src/views/results.tsx +591 -0
- package/src/views/search-button.tsx +38 -0
- package/src/views/search-modal.tsx +422 -0
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@akinon/pz-similar-products",
|
|
3
|
+
"version": "1.92.0-rc.16",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"react": "^18.0.0",
|
|
8
|
+
"react-dom": "^18.0.0"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"react-image-crop": "^11.0.5",
|
|
12
|
+
"clsx": "^2.0.0",
|
|
13
|
+
"tailwind-merge": "^2.0.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^18.7.8",
|
|
17
|
+
"@types/react": "^18.0.17",
|
|
18
|
+
"@types/react-dom": "^18.0.6",
|
|
19
|
+
"typescript": "^5.2.2"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { api } from '@akinon/next/data/client/api';
|
|
2
|
+
import {
|
|
3
|
+
Product,
|
|
4
|
+
Facet,
|
|
5
|
+
FacetChoice,
|
|
6
|
+
SortOption,
|
|
7
|
+
Pagination
|
|
8
|
+
} from '@akinon/next/types/commerce';
|
|
9
|
+
|
|
10
|
+
export interface SimilarProductsResponse {
|
|
11
|
+
product_ids?: number[];
|
|
12
|
+
productIds?: number[];
|
|
13
|
+
similar_products?: Array<{ product_id: number }>;
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SimilarProductsListResponse {
|
|
18
|
+
pagination: Pagination;
|
|
19
|
+
facets: Facet[];
|
|
20
|
+
sorters: SortOption[];
|
|
21
|
+
search_text: string | null;
|
|
22
|
+
products: Product[];
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const similarProductsApi = api.injectEndpoints({
|
|
27
|
+
endpoints: (build) => ({
|
|
28
|
+
getSimilarProductsByUrl: build.query<
|
|
29
|
+
SimilarProductsResponse,
|
|
30
|
+
{ url: string; limit?: number; excluded_product_ids?: number[] }
|
|
31
|
+
>({
|
|
32
|
+
query: ({ url, limit = 20, excluded_product_ids }) => {
|
|
33
|
+
const params = new URLSearchParams();
|
|
34
|
+
params.append('limit', String(limit));
|
|
35
|
+
params.append('url', url);
|
|
36
|
+
|
|
37
|
+
if (excluded_product_ids && excluded_product_ids.length > 0) {
|
|
38
|
+
params.append('excluded_product_ids', excluded_product_ids.join(','));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
url: `/api/similar-products?${params.toString()}`,
|
|
43
|
+
method: 'GET',
|
|
44
|
+
headers: {
|
|
45
|
+
Accept: 'application/json'
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}),
|
|
50
|
+
|
|
51
|
+
getSimilarProductsByImage: build.mutation<
|
|
52
|
+
SimilarProductsResponse,
|
|
53
|
+
{ image: string; limit?: number; excluded_product_ids?: number[] }
|
|
54
|
+
>({
|
|
55
|
+
query: ({ image, limit = 20, excluded_product_ids }) => {
|
|
56
|
+
const params = new URLSearchParams();
|
|
57
|
+
params.append('limit', String(limit));
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
url: `/api/similar-products?${params.toString()}`,
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: {
|
|
63
|
+
'Content-Type': 'application/json',
|
|
64
|
+
Accept: 'application/json'
|
|
65
|
+
},
|
|
66
|
+
body: JSON.stringify({ image, excluded_product_ids })
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}),
|
|
70
|
+
|
|
71
|
+
getSimilarProductsList: build.query<
|
|
72
|
+
SimilarProductsListResponse,
|
|
73
|
+
{
|
|
74
|
+
filter: string;
|
|
75
|
+
searchParams?: Record<string, string>;
|
|
76
|
+
isExclude?: boolean;
|
|
77
|
+
}
|
|
78
|
+
>({
|
|
79
|
+
query: ({ filter, searchParams = {}, isExclude = false }) => {
|
|
80
|
+
const params = new URLSearchParams(searchParams);
|
|
81
|
+
const queryString = params.toString();
|
|
82
|
+
|
|
83
|
+
const headerName = isExclude
|
|
84
|
+
? 'x-search-dynamic-exclude'
|
|
85
|
+
: 'x-search-dynamic-filter';
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
url: `/api/similar-product-list?${queryString}`,
|
|
89
|
+
headers: {
|
|
90
|
+
[headerName]: filter
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
serializeQueryArgs: ({ queryArgs }) => {
|
|
95
|
+
const { filter, searchParams = {}, isExclude = false } = queryArgs;
|
|
96
|
+
const sortedParams = Object.keys(searchParams)
|
|
97
|
+
.sort()
|
|
98
|
+
.reduce((acc, key) => {
|
|
99
|
+
acc[key] = searchParams[key];
|
|
100
|
+
return acc;
|
|
101
|
+
}, {} as Record<string, string>);
|
|
102
|
+
|
|
103
|
+
return JSON.stringify({
|
|
104
|
+
filter,
|
|
105
|
+
searchParams: sortedParams,
|
|
106
|
+
isExclude
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
}),
|
|
111
|
+
overrideExisting: true
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
export const {
|
|
115
|
+
useGetSimilarProductsByUrlQuery,
|
|
116
|
+
useLazyGetSimilarProductsByUrlQuery,
|
|
117
|
+
useGetSimilarProductsByImageMutation,
|
|
118
|
+
useGetSimilarProductsListQuery,
|
|
119
|
+
useLazyGetSimilarProductsListQuery
|
|
120
|
+
} = similarProductsApi;
|
|
121
|
+
|
|
122
|
+
export type { Product, Facet, FacetChoice, SortOption, Pagination };
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { useState, useRef, useCallback } from 'react';
|
|
2
|
+
import 'react-image-crop/dist/ReactCrop.css';
|
|
3
|
+
|
|
4
|
+
type CropType = {
|
|
5
|
+
unit: 'px' | '%';
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function useImageCropper(
|
|
13
|
+
setIsLoading: (loading: boolean) => void,
|
|
14
|
+
processImage: (base64: string) => void,
|
|
15
|
+
clearError?: () => void
|
|
16
|
+
) {
|
|
17
|
+
const [isCropping, setIsCropping] = useState(false);
|
|
18
|
+
const [crop, setCrop] = useState<CropType>({
|
|
19
|
+
unit: 'px',
|
|
20
|
+
x: 0,
|
|
21
|
+
y: 0,
|
|
22
|
+
width: 100,
|
|
23
|
+
height: 100
|
|
24
|
+
});
|
|
25
|
+
const [completedCrop, setCompletedCrop] = useState<CropType | null>(null);
|
|
26
|
+
const [cropProcessed, setCropProcessed] = useState(false);
|
|
27
|
+
const [lastSuccessfulCrop, setLastSuccessfulCrop] = useState<CropType | null>(
|
|
28
|
+
null
|
|
29
|
+
);
|
|
30
|
+
const debounceTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
31
|
+
|
|
32
|
+
const imageRef = useRef<HTMLImageElement>(null);
|
|
33
|
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
34
|
+
|
|
35
|
+
const handleCropComplete = (c: any) => {
|
|
36
|
+
setCompletedCrop(c);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const toggleCropMode = () => {
|
|
40
|
+
const newCroppingState = !isCropping;
|
|
41
|
+
setIsCropping(newCroppingState);
|
|
42
|
+
|
|
43
|
+
if (newCroppingState && clearError) {
|
|
44
|
+
clearError();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (newCroppingState) {
|
|
48
|
+
setCropProcessed(false);
|
|
49
|
+
if (completedCrop?.width && completedCrop?.height) {
|
|
50
|
+
setCrop(completedCrop);
|
|
51
|
+
} else {
|
|
52
|
+
const centeredCrop: CropType = {
|
|
53
|
+
unit: '%',
|
|
54
|
+
x: 25,
|
|
55
|
+
y: 25,
|
|
56
|
+
width: 50,
|
|
57
|
+
height: 50
|
|
58
|
+
};
|
|
59
|
+
setCrop(centeredCrop);
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
if (!cropProcessed) {
|
|
63
|
+
setCompletedCrop(lastSuccessfulCrop);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const processCompletedCropImmediate = async (crop: any) => {
|
|
69
|
+
if (!imageRef.current || !crop?.width || !crop?.height) {
|
|
70
|
+
setIsLoading(false);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (clearError) {
|
|
75
|
+
clearError();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
const canvas = document.createElement('canvas');
|
|
80
|
+
const image = imageRef.current;
|
|
81
|
+
|
|
82
|
+
const scaleX = image.naturalWidth / image.width;
|
|
83
|
+
const scaleY = image.naturalHeight / image.height;
|
|
84
|
+
|
|
85
|
+
canvas.width = crop.width * scaleX;
|
|
86
|
+
canvas.height = crop.height * scaleY;
|
|
87
|
+
|
|
88
|
+
const ctx = canvas.getContext('2d');
|
|
89
|
+
|
|
90
|
+
if (!ctx) {
|
|
91
|
+
setIsLoading(false);
|
|
92
|
+
throw new Error('No 2d context');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
ctx.drawImage(
|
|
97
|
+
image,
|
|
98
|
+
crop.x * scaleX,
|
|
99
|
+
crop.y * scaleY,
|
|
100
|
+
crop.width * scaleX,
|
|
101
|
+
crop.height * scaleY,
|
|
102
|
+
0,
|
|
103
|
+
0,
|
|
104
|
+
crop.width * scaleX,
|
|
105
|
+
crop.height * scaleY
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const base64Image = canvas.toDataURL('image/jpeg');
|
|
109
|
+
processImage(base64Image);
|
|
110
|
+
setCropProcessed(true);
|
|
111
|
+
setLastSuccessfulCrop(crop);
|
|
112
|
+
setIsLoading(false);
|
|
113
|
+
} catch (canvasError) {
|
|
114
|
+
try {
|
|
115
|
+
const originalUrl = image.src;
|
|
116
|
+
const proxyUrl = `/api/image-proxy?url=${encodeURIComponent(
|
|
117
|
+
originalUrl
|
|
118
|
+
)}`;
|
|
119
|
+
|
|
120
|
+
const proxiedImg = new window.Image();
|
|
121
|
+
|
|
122
|
+
proxiedImg.onload = () => {
|
|
123
|
+
try {
|
|
124
|
+
const corsCanvas = document.createElement('canvas');
|
|
125
|
+
corsCanvas.width = crop.width * scaleX;
|
|
126
|
+
corsCanvas.height = crop.height * scaleY;
|
|
127
|
+
|
|
128
|
+
const corsCtx = corsCanvas.getContext('2d');
|
|
129
|
+
if (!corsCtx) {
|
|
130
|
+
throw new Error('No 2d context for proxy canvas');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
corsCtx.drawImage(
|
|
134
|
+
proxiedImg,
|
|
135
|
+
crop.x * scaleX,
|
|
136
|
+
crop.y * scaleY,
|
|
137
|
+
crop.width * scaleX,
|
|
138
|
+
crop.height * scaleY,
|
|
139
|
+
0,
|
|
140
|
+
0,
|
|
141
|
+
crop.width * scaleX,
|
|
142
|
+
crop.height * scaleY
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const base64Image = corsCanvas.toDataURL('image/jpeg');
|
|
146
|
+
processImage(base64Image);
|
|
147
|
+
setCropProcessed(true);
|
|
148
|
+
setLastSuccessfulCrop(crop);
|
|
149
|
+
} catch (proxyError) {
|
|
150
|
+
console.error(
|
|
151
|
+
'Proxy image processing failed, using URL fallback:',
|
|
152
|
+
proxyError
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const fallbackData = JSON.stringify({
|
|
156
|
+
type: 'cors_fallback',
|
|
157
|
+
originalUrl,
|
|
158
|
+
crop: {
|
|
159
|
+
x: crop.x,
|
|
160
|
+
y: crop.y,
|
|
161
|
+
width: crop.width,
|
|
162
|
+
height: crop.height
|
|
163
|
+
},
|
|
164
|
+
scale: { x: scaleX, y: scaleY }
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const fallbackString =
|
|
168
|
+
'data:application/x-cors-fallback;base64,' + btoa(fallbackData);
|
|
169
|
+
processImage(fallbackString);
|
|
170
|
+
setCropProcessed(true);
|
|
171
|
+
setLastSuccessfulCrop(crop);
|
|
172
|
+
} finally {
|
|
173
|
+
setIsLoading(false);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
proxiedImg.onerror = (error) => {
|
|
178
|
+
console.error(
|
|
179
|
+
'Proxy image loading failed, using URL fallback:',
|
|
180
|
+
error
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const fallbackData = JSON.stringify({
|
|
184
|
+
type: 'cors_fallback',
|
|
185
|
+
originalUrl,
|
|
186
|
+
crop: {
|
|
187
|
+
x: crop.x,
|
|
188
|
+
y: crop.y,
|
|
189
|
+
width: crop.width,
|
|
190
|
+
height: crop.height
|
|
191
|
+
},
|
|
192
|
+
scale: { x: scaleX, y: scaleY }
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const fallbackString =
|
|
196
|
+
'data:application/x-cors-fallback;base64,' + btoa(fallbackData);
|
|
197
|
+
processImage(fallbackString);
|
|
198
|
+
setCropProcessed(true);
|
|
199
|
+
setLastSuccessfulCrop(crop);
|
|
200
|
+
setIsLoading(false);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
proxiedImg.src = proxyUrl;
|
|
204
|
+
} catch (proxyError) {
|
|
205
|
+
console.error('Image proxy setup failed:', proxyError);
|
|
206
|
+
setIsLoading(false);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.error('processCompletedCrop failed:', error);
|
|
211
|
+
setIsLoading(false);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const processCompletedCrop = useCallback((crop: any) => {
|
|
216
|
+
if (debounceTimeoutRef.current) {
|
|
217
|
+
clearTimeout(debounceTimeoutRef.current);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
debounceTimeoutRef.current = setTimeout(() => {
|
|
221
|
+
processCompletedCropImmediate(crop);
|
|
222
|
+
}, 250);
|
|
223
|
+
}, []);
|
|
224
|
+
|
|
225
|
+
const processManualCrop = useCallback((crop: any) => {
|
|
226
|
+
if (debounceTimeoutRef.current) {
|
|
227
|
+
clearTimeout(debounceTimeoutRef.current);
|
|
228
|
+
}
|
|
229
|
+
processCompletedCropImmediate(crop);
|
|
230
|
+
}, []);
|
|
231
|
+
|
|
232
|
+
const resetCrop = () => {
|
|
233
|
+
if (debounceTimeoutRef.current) {
|
|
234
|
+
clearTimeout(debounceTimeoutRef.current);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
setIsCropping(false);
|
|
238
|
+
setCompletedCrop(null);
|
|
239
|
+
setCropProcessed(false);
|
|
240
|
+
setLastSuccessfulCrop(null);
|
|
241
|
+
setCrop({
|
|
242
|
+
unit: 'px',
|
|
243
|
+
x: 0,
|
|
244
|
+
y: 0,
|
|
245
|
+
width: 100,
|
|
246
|
+
height: 100
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
isCropping,
|
|
252
|
+
crop,
|
|
253
|
+
setCrop,
|
|
254
|
+
completedCrop,
|
|
255
|
+
setCompletedCrop,
|
|
256
|
+
imageRef,
|
|
257
|
+
fileInputRef,
|
|
258
|
+
handleCropComplete,
|
|
259
|
+
toggleCropMode,
|
|
260
|
+
processCompletedCrop,
|
|
261
|
+
processManualCrop,
|
|
262
|
+
resetCrop
|
|
263
|
+
};
|
|
264
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const IMAGE_SEARCH_STORAGE_KEY = 'enable_image_search';
|
|
4
|
+
|
|
5
|
+
export function useImageSearchFeature() {
|
|
6
|
+
const [isEnabled, setIsEnabled] = useState(false);
|
|
7
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
8
|
+
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
const envEnabled = process.env.NEXT_PUBLIC_ENABLE_IMAGE_SEARCH === 'true';
|
|
11
|
+
|
|
12
|
+
if (envEnabled) {
|
|
13
|
+
setIsEnabled(true);
|
|
14
|
+
setIsLoading(false);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const localStorageValue = localStorage.getItem(IMAGE_SEARCH_STORAGE_KEY);
|
|
20
|
+
setIsEnabled(localStorageValue === 'true');
|
|
21
|
+
} catch (error) {
|
|
22
|
+
setIsEnabled(false);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
setIsLoading(false);
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
isEnabled,
|
|
30
|
+
isLoading
|
|
31
|
+
};
|
|
32
|
+
}
|