@quantabit/recommend-sdk 1.0.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/LICENSE +21 -0
- package/README.md +85 -0
- package/dist/index.cjs +616 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.esm.js +584 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +87 -0
- package/types/index.d.ts +140 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 QuantaBit Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @quantabit/recommend-sdk
|
|
2
|
+
|
|
3
|
+
> QuantaBit Recommendation SDK - Personalized content/product recommendations and cold-start strategies
|
|
4
|
+
|
|
5
|
+
## ๐ฆ Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @quantabit/recommend-sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @quantabit/recommend-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## ๐ Quick Start
|
|
14
|
+
|
|
15
|
+
### Using Components
|
|
16
|
+
|
|
17
|
+
```jsx
|
|
18
|
+
import { RecommendCard, RecommendSection, GuessYouLike, SimilarItems } from '@quantabit/recommend-sdk';
|
|
19
|
+
|
|
20
|
+
<RecommendCard />
|
|
21
|
+
<RecommendSection />
|
|
22
|
+
<GuessYouLike />
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Using Hooks
|
|
26
|
+
|
|
27
|
+
```jsx
|
|
28
|
+
import { useRecommendations, useSimilar, useHomeFeed, useColdStart, useRecommend } from '@quantabit/recommend-sdk';
|
|
29
|
+
|
|
30
|
+
function MyComponent() {
|
|
31
|
+
const result = useRecommendations();
|
|
32
|
+
const result = useSimilar();
|
|
33
|
+
const result = useHomeFeed();
|
|
34
|
+
return <div>...</div>;
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### API Client
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
import { recommendApi } from '@quantabit/recommend-sdk';
|
|
42
|
+
|
|
43
|
+
// Use recommendApi for direct API calls
|
|
44
|
+
const data = await recommendApi.list();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## ๐ Components
|
|
48
|
+
|
|
49
|
+
| Component | Description |
|
|
50
|
+
| --------- | ----------- |
|
|
51
|
+
| `RecommendCard` | Recommend Card component |
|
|
52
|
+
| `RecommendSection` | Recommend Section component |
|
|
53
|
+
| `GuessYouLike` | Guess You Like component |
|
|
54
|
+
| `SimilarItems` | Similar Items component |
|
|
55
|
+
|
|
56
|
+
## ๐ช Hooks
|
|
57
|
+
|
|
58
|
+
| Hook | Description |
|
|
59
|
+
| ---- | ----------- |
|
|
60
|
+
| `useRecommendations` | Recommendations management |
|
|
61
|
+
| `useSimilar` | Similar management |
|
|
62
|
+
| `useHomeFeed` | Home Feed management |
|
|
63
|
+
| `useColdStart` | Cold Start management |
|
|
64
|
+
| `useRecommend` | Recommend management |
|
|
65
|
+
|
|
66
|
+
## ๐ Type Definitions
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
import { RecommendType, RecommendAlgorithm, RecommendScene, FeedbackType } from '@quantabit/recommend-sdk';
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## ๐ License
|
|
73
|
+
|
|
74
|
+
MIT License
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## ๐ Brand & Links
|
|
81
|
+
- Official Mainnet: [QuantaBit Chain](https://qbitchain.io/)
|
|
82
|
+
- Developer Platform: [Developer Platform](https://developer.quantabit.io/)
|
|
83
|
+
- Open Platform: [Open Platform](https://open.quantabit.io/)
|
|
84
|
+
- Payment Platform: [Pay Platform](https://pay.qbitwallet.io/)
|
|
85
|
+
- Feedback: [Feedback](https://xwin.live/qbit)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var sdkConfig = require('@quantabit/sdk-config');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Recommend SDK - API ๅฎขๆท็ซฏ
|
|
8
|
+
* ๆจ่็ณป็ปๅ็ซฏๆฅๅฃๅฐ่ฃ
|
|
9
|
+
*
|
|
10
|
+
* ไฝฟ็จ BaseApiClient ๅบ็ฑป็ฎๅไปฃ็
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* ๆจ่ API ๅฎขๆท็ซฏ
|
|
16
|
+
*/
|
|
17
|
+
class RecommendApiClient extends sdkConfig.BaseApiClient {
|
|
18
|
+
constructor(config = {}) {
|
|
19
|
+
super('/recommend', config);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ============ ไธชๆงๅๆจ่ ============
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* ่ทๅ้ฆ้กตๆจ่
|
|
26
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
27
|
+
*/
|
|
28
|
+
async getHomeRecommendations(params = {}) {
|
|
29
|
+
return this.get('/home', params);
|
|
30
|
+
}
|
|
31
|
+
async getHomeFeed(params = {}) {
|
|
32
|
+
return this.getHomeRecommendations(params);
|
|
33
|
+
}
|
|
34
|
+
async getRecommendations(type = 'home', params = {}) {
|
|
35
|
+
return this.get(`/${type}`, params);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* ่ทๅๅ
ๅฎนๆจ่
|
|
40
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
41
|
+
*/
|
|
42
|
+
async getContentRecommendations(params = {}) {
|
|
43
|
+
return this.get('/content', params);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* ่ทๅๅๅๆจ่
|
|
48
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
49
|
+
*/
|
|
50
|
+
async getProductRecommendations(params = {}) {
|
|
51
|
+
return this.get('/products', params);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* ่ทๅ็จๆทๆจ่๏ผๅฏ่ฝ่ฎค่ฏ็ไบบ๏ผ
|
|
56
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
57
|
+
*/
|
|
58
|
+
async getUserRecommendations(params = {}) {
|
|
59
|
+
return this.get('/users', params);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ============ ็ธๅ
ณๆจ่ ============
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* ่ทๅ็ธๅ
ณๅ
ๅฎน
|
|
66
|
+
* @param {string} contentId - ๅ
ๅฎน ID
|
|
67
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
68
|
+
*/
|
|
69
|
+
async getRelatedContent(contentId, params = {}) {
|
|
70
|
+
return this.get(`/related/content/${contentId}`, params);
|
|
71
|
+
}
|
|
72
|
+
async getSimilar(itemType, itemId, limit = 10) {
|
|
73
|
+
return this.get(`/related/${itemType}/${itemId}`, {
|
|
74
|
+
limit
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* ่ทๅ็ธๅ
ณๅๅ
|
|
80
|
+
* @param {string} productId - ๅๅ ID
|
|
81
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
82
|
+
*/
|
|
83
|
+
async getRelatedProducts(productId, params = {}) {
|
|
84
|
+
return this.get(`/related/product/${productId}`, params);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* ่ทๅ็ธไผผ็จๆท
|
|
89
|
+
* @param {string} userId - ็จๆท ID
|
|
90
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
91
|
+
*/
|
|
92
|
+
async getSimilarUsers(userId, params = {}) {
|
|
93
|
+
return this.get(`/related/user/${userId}`, params);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ============ ๅบๆฏๆจ่ ============
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* ่ทๅ็ไฝ ๅๆฌข
|
|
100
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
101
|
+
*/
|
|
102
|
+
async getGuessYouLike(params = {}) {
|
|
103
|
+
return this.get('/guess-you-like', params);
|
|
104
|
+
}
|
|
105
|
+
async getAlsoViewed(itemId, itemType = 'content', params = {}) {
|
|
106
|
+
return this.get('/also-viewed', {
|
|
107
|
+
item_id: itemId,
|
|
108
|
+
item_type: itemType,
|
|
109
|
+
...params
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async getAlsoBought(itemId, itemType = 'product', params = {}) {
|
|
113
|
+
return this.get('/also-bought', {
|
|
114
|
+
item_id: itemId,
|
|
115
|
+
item_type: itemType,
|
|
116
|
+
...params
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* ่ทๅ็ญ้จๆจ่
|
|
122
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
123
|
+
*/
|
|
124
|
+
async getHotRecommendations(params = {}) {
|
|
125
|
+
return this.get('/hot', params);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* ่ทๅๆฐๅๆจ่
|
|
130
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
131
|
+
*/
|
|
132
|
+
async getNewRecommendations(params = {}) {
|
|
133
|
+
return this.get('/new', params);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* ่ทๅไธ้ขๆจ่
|
|
138
|
+
* @param {string} topicId - ไธ้ข ID
|
|
139
|
+
* @param {Object} params - ๆจ่ๅๆฐ
|
|
140
|
+
*/
|
|
141
|
+
async getTopicRecommendations(topicId, params = {}) {
|
|
142
|
+
return this.get(`/topic/${topicId}`, params);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ============ ๅ้ฆ ============
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* ไธๆๅ
ด่ถฃ
|
|
149
|
+
* @param {string} itemId - ้กน็ฎ ID
|
|
150
|
+
* @param {string} itemType - ้กน็ฎ็ฑปๅ
|
|
151
|
+
* @param {string} reason - ๅๅ
|
|
152
|
+
*/
|
|
153
|
+
async notInterested(itemId, itemType, reason = '') {
|
|
154
|
+
return this.post('/feedback/not-interested', {
|
|
155
|
+
item_id: itemId,
|
|
156
|
+
item_type: itemType,
|
|
157
|
+
reason
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async markNotInterested(itemType, itemId, reason = '') {
|
|
161
|
+
return this.notInterested(itemId, itemType, reason);
|
|
162
|
+
}
|
|
163
|
+
async feedback(itemType, itemId, feedbackType, context = {}) {
|
|
164
|
+
return this.post('/feedback', {
|
|
165
|
+
item_type: itemType,
|
|
166
|
+
item_id: itemId,
|
|
167
|
+
feedback_type: feedbackType,
|
|
168
|
+
...context
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* ่ฎฐๅฝ็นๅป
|
|
174
|
+
* @param {string} itemId - ้กน็ฎ ID
|
|
175
|
+
* @param {string} itemType - ้กน็ฎ็ฑปๅ
|
|
176
|
+
* @param {Object} context - ไธไธๆ
|
|
177
|
+
*/
|
|
178
|
+
async trackClick(itemId, itemType, context = {}) {
|
|
179
|
+
return this.post('/feedback/click', {
|
|
180
|
+
item_id: itemId,
|
|
181
|
+
item_type: itemType,
|
|
182
|
+
...context
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* ่ฎฐๅฝๆๅ
|
|
188
|
+
* @param {Object[]} items - ๆๅ
้กน็ฎๅ่กจ
|
|
189
|
+
*/
|
|
190
|
+
async trackExposure(items) {
|
|
191
|
+
return this.post('/feedback/exposure', {
|
|
192
|
+
items
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ============ ๅๅฅฝ่ฎพ็ฝฎ ============
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* ่ทๅๆจ่ๅๅฅฝ
|
|
200
|
+
*/
|
|
201
|
+
async getPreferences() {
|
|
202
|
+
return this.get('/preferences');
|
|
203
|
+
}
|
|
204
|
+
async getUserPreferences() {
|
|
205
|
+
return this.getPreferences();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* ๆดๆฐๆจ่ๅๅฅฝ
|
|
210
|
+
* @param {Object} preferences - ๅๅฅฝ่ฎพ็ฝฎ
|
|
211
|
+
*/
|
|
212
|
+
async updatePreferences(preferences) {
|
|
213
|
+
return this.put('/preferences', preferences);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* ่ทๅๅ
ด่ถฃๆ ็ญพ
|
|
218
|
+
*/
|
|
219
|
+
async getInterestTags() {
|
|
220
|
+
return this.get('/interests');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* ๆดๆฐๅ
ด่ถฃๆ ็ญพ
|
|
225
|
+
* @param {string[]} tagIds - ๆ ็ญพ ID ๅ่กจ
|
|
226
|
+
*/
|
|
227
|
+
async updateInterestTags(tagIds) {
|
|
228
|
+
return this.put('/interests', {
|
|
229
|
+
tag_ids: tagIds
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
async getColdStartQuestions(params = {}) {
|
|
233
|
+
return this.get('/cold-start/questions', params);
|
|
234
|
+
}
|
|
235
|
+
async submitColdStartAnswers(answers) {
|
|
236
|
+
return this.post('/cold-start/answers', {
|
|
237
|
+
answers
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ๅๅปบ้ป่ฎคๅฎไพ
|
|
243
|
+
const recommendApi = new RecommendApiClient();
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Recommend SDK - ็ฑปๅๅฎไน
|
|
247
|
+
*/
|
|
248
|
+
|
|
249
|
+
// ๆจ่็ฑปๅ
|
|
250
|
+
const RecommendType = {
|
|
251
|
+
CONTENT: 'content',
|
|
252
|
+
PRODUCT: 'product',
|
|
253
|
+
USER: 'user',
|
|
254
|
+
COURSE: 'course',
|
|
255
|
+
GROUP: 'group',
|
|
256
|
+
ACTIVITY: 'activity'
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// ๆจ่็ฎๆณ
|
|
260
|
+
const RecommendAlgorithm = {
|
|
261
|
+
COLLABORATIVE: 'collaborative',
|
|
262
|
+
// ๅๅ่ฟๆปค
|
|
263
|
+
CONTENT_BASED: 'content_based',
|
|
264
|
+
// ๅบไบๅ
ๅฎน
|
|
265
|
+
HYBRID: 'hybrid',
|
|
266
|
+
// ๆททๅๆจ่
|
|
267
|
+
POPULARITY: 'popularity',
|
|
268
|
+
// ็ญ้จๆจ่
|
|
269
|
+
TRENDING: 'trending',
|
|
270
|
+
// ่ถๅฟๆจ่
|
|
271
|
+
SIMILAR: 'similar' // ็ธไผผๆจ่
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
// ๅบๆฏ
|
|
275
|
+
const RecommendScene = {
|
|
276
|
+
HOME_FEED: 'home_feed',
|
|
277
|
+
DETAIL_PAGE: 'detail_page',
|
|
278
|
+
SEARCH_RESULT: 'search_result',
|
|
279
|
+
CART: 'cart',
|
|
280
|
+
CHECKOUT: 'checkout',
|
|
281
|
+
PROFILE: 'profile'
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// ๅ้ฆ็ฑปๅ
|
|
285
|
+
const FeedbackType = {
|
|
286
|
+
CLICK: 'click',
|
|
287
|
+
VIEW: 'view',
|
|
288
|
+
LIKE: 'like',
|
|
289
|
+
DISLIKE: 'dislike',
|
|
290
|
+
SHARE: 'share',
|
|
291
|
+
PURCHASE: 'purchase',
|
|
292
|
+
NOT_INTERESTED: 'not_interested'
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Recommend SDK - React Hooks
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* useRecommendations - ๆจ่ๅ่กจHook
|
|
302
|
+
*/
|
|
303
|
+
function useRecommendations(type, options = {}) {
|
|
304
|
+
const [items, setItems] = react.useState([]);
|
|
305
|
+
const [loading, setLoading] = react.useState(false);
|
|
306
|
+
const [refreshing, setRefreshing] = react.useState(false);
|
|
307
|
+
const loadRecommendations = react.useCallback(async (isRefresh = false) => {
|
|
308
|
+
if (isRefresh) {
|
|
309
|
+
setRefreshing(true);
|
|
310
|
+
} else {
|
|
311
|
+
setLoading(true);
|
|
312
|
+
}
|
|
313
|
+
try {
|
|
314
|
+
const result = await recommendApi.getRecommendations(type, options);
|
|
315
|
+
setItems(result.items || []);
|
|
316
|
+
} finally {
|
|
317
|
+
setLoading(false);
|
|
318
|
+
setRefreshing(false);
|
|
319
|
+
}
|
|
320
|
+
}, [type, options.scene, options.limit, options.algorithm]);
|
|
321
|
+
|
|
322
|
+
// ๆไบคๅ้ฆ
|
|
323
|
+
const feedback = react.useCallback(async (itemId, feedbackType) => {
|
|
324
|
+
try {
|
|
325
|
+
await recommendApi.feedback(type, itemId, feedbackType);
|
|
326
|
+
return true;
|
|
327
|
+
} catch (e) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
}, [type]);
|
|
331
|
+
|
|
332
|
+
// ๆ ่ฎฐไธๆๅ
ด่ถฃ
|
|
333
|
+
const markNotInterested = react.useCallback(async (itemId, reason) => {
|
|
334
|
+
try {
|
|
335
|
+
await recommendApi.markNotInterested(type, itemId, reason);
|
|
336
|
+
// ไปๅ่กจไธญ็งป้ค
|
|
337
|
+
setItems(prev => prev.filter(item => item.id !== itemId));
|
|
338
|
+
return true;
|
|
339
|
+
} catch (e) {
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
}, [type]);
|
|
343
|
+
|
|
344
|
+
// ๅทๆฐ
|
|
345
|
+
const refresh = react.useCallback(() => {
|
|
346
|
+
return loadRecommendations(true);
|
|
347
|
+
}, [loadRecommendations]);
|
|
348
|
+
react.useEffect(() => {
|
|
349
|
+
loadRecommendations();
|
|
350
|
+
}, [type, options.scene]);
|
|
351
|
+
return {
|
|
352
|
+
items,
|
|
353
|
+
loading,
|
|
354
|
+
refreshing,
|
|
355
|
+
refresh,
|
|
356
|
+
feedback,
|
|
357
|
+
markNotInterested
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* useSimilar - ็ธไผผๆจ่Hook
|
|
363
|
+
*/
|
|
364
|
+
function useSimilar(itemType, itemId, limit = 10) {
|
|
365
|
+
const [items, setItems] = react.useState([]);
|
|
366
|
+
const [loading, setLoading] = react.useState(false);
|
|
367
|
+
const loadSimilar = react.useCallback(async () => {
|
|
368
|
+
if (!itemType || !itemId) return;
|
|
369
|
+
setLoading(true);
|
|
370
|
+
try {
|
|
371
|
+
const result = await recommendApi.getSimilar(itemType, itemId, limit);
|
|
372
|
+
setItems(result.items || []);
|
|
373
|
+
} finally {
|
|
374
|
+
setLoading(false);
|
|
375
|
+
}
|
|
376
|
+
}, [itemType, itemId, limit]);
|
|
377
|
+
react.useEffect(() => {
|
|
378
|
+
loadSimilar();
|
|
379
|
+
}, [itemType, itemId]);
|
|
380
|
+
return {
|
|
381
|
+
items,
|
|
382
|
+
loading,
|
|
383
|
+
refresh: loadSimilar
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* useHomeFeed - ้ฆ้กตFeed Hook
|
|
389
|
+
*/
|
|
390
|
+
function useHomeFeed() {
|
|
391
|
+
const [items, setItems] = react.useState([]);
|
|
392
|
+
const [loading, setLoading] = react.useState(false);
|
|
393
|
+
const [refreshing, setRefreshing] = react.useState(false);
|
|
394
|
+
const [page, setPage] = react.useState(1);
|
|
395
|
+
const [hasMore, setHasMore] = react.useState(true);
|
|
396
|
+
const loadFeed = react.useCallback(async (reset = false) => {
|
|
397
|
+
const currentPage = reset ? 1 : page;
|
|
398
|
+
if (reset) {
|
|
399
|
+
setRefreshing(true);
|
|
400
|
+
} else {
|
|
401
|
+
setLoading(true);
|
|
402
|
+
}
|
|
403
|
+
try {
|
|
404
|
+
const result = await recommendApi.getHomeFeed({
|
|
405
|
+
page: currentPage,
|
|
406
|
+
pageSize: 20
|
|
407
|
+
});
|
|
408
|
+
const newItems = result.items || [];
|
|
409
|
+
setItems(prev => reset ? newItems : [...prev, ...newItems]);
|
|
410
|
+
setHasMore(newItems.length >= 20);
|
|
411
|
+
if (reset) setPage(1);
|
|
412
|
+
} finally {
|
|
413
|
+
setLoading(false);
|
|
414
|
+
setRefreshing(false);
|
|
415
|
+
}
|
|
416
|
+
}, [page]);
|
|
417
|
+
const loadMore = react.useCallback(() => {
|
|
418
|
+
if (hasMore && !loading) {
|
|
419
|
+
setPage(p => p + 1);
|
|
420
|
+
}
|
|
421
|
+
}, [hasMore, loading]);
|
|
422
|
+
const refresh = react.useCallback(() => {
|
|
423
|
+
return loadFeed(true);
|
|
424
|
+
}, [loadFeed]);
|
|
425
|
+
react.useEffect(() => {
|
|
426
|
+
loadFeed(true);
|
|
427
|
+
}, []);
|
|
428
|
+
react.useEffect(() => {
|
|
429
|
+
if (page > 1) loadFeed();
|
|
430
|
+
}, [page]);
|
|
431
|
+
return {
|
|
432
|
+
items,
|
|
433
|
+
loading,
|
|
434
|
+
refreshing,
|
|
435
|
+
hasMore,
|
|
436
|
+
loadMore,
|
|
437
|
+
refresh
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* useColdStart - ๅทๅฏๅจๅผๅฏผHook
|
|
443
|
+
*/
|
|
444
|
+
function useColdStart() {
|
|
445
|
+
const [questions, setQuestions] = react.useState([]);
|
|
446
|
+
const [answers, setAnswers] = react.useState({});
|
|
447
|
+
const [loading, setLoading] = react.useState(false);
|
|
448
|
+
const [submitting, setSubmitting] = react.useState(false);
|
|
449
|
+
const [completed, setCompleted] = react.useState(false);
|
|
450
|
+
const loadQuestions = react.useCallback(async () => {
|
|
451
|
+
setLoading(true);
|
|
452
|
+
try {
|
|
453
|
+
const result = await recommendApi.getColdStartQuestions();
|
|
454
|
+
setQuestions(result.questions || []);
|
|
455
|
+
} finally {
|
|
456
|
+
setLoading(false);
|
|
457
|
+
}
|
|
458
|
+
}, []);
|
|
459
|
+
const setAnswer = react.useCallback((questionId, answer) => {
|
|
460
|
+
setAnswers(prev => ({
|
|
461
|
+
...prev,
|
|
462
|
+
[questionId]: answer
|
|
463
|
+
}));
|
|
464
|
+
}, []);
|
|
465
|
+
const submit = react.useCallback(async () => {
|
|
466
|
+
setSubmitting(true);
|
|
467
|
+
try {
|
|
468
|
+
await recommendApi.submitColdStartAnswers(answers);
|
|
469
|
+
setCompleted(true);
|
|
470
|
+
return true;
|
|
471
|
+
} catch (e) {
|
|
472
|
+
return false;
|
|
473
|
+
} finally {
|
|
474
|
+
setSubmitting(false);
|
|
475
|
+
}
|
|
476
|
+
}, [answers]);
|
|
477
|
+
react.useEffect(() => {
|
|
478
|
+
loadQuestions();
|
|
479
|
+
}, []);
|
|
480
|
+
return {
|
|
481
|
+
questions,
|
|
482
|
+
answers,
|
|
483
|
+
loading,
|
|
484
|
+
submitting,
|
|
485
|
+
completed,
|
|
486
|
+
setAnswer,
|
|
487
|
+
submit,
|
|
488
|
+
isComplete: Object.keys(answers).length >= questions.length
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function RecommendCard() {
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
function RecommendSection() {
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
function GuessYouLike() {
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
function SimilarItems() {
|
|
502
|
+
return null;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* QuantaBit Recommend SDK - i18n support
|
|
507
|
+
*/
|
|
508
|
+
|
|
509
|
+
const messages = {
|
|
510
|
+
en: {
|
|
511
|
+
recommendation: 'Recommendations',
|
|
512
|
+
guessYouLike: 'Guess You Like',
|
|
513
|
+
similarItems: 'Similar Items',
|
|
514
|
+
alsoViewed: 'People also viewed',
|
|
515
|
+
alsoBought: 'People also bought',
|
|
516
|
+
notInterested: 'Not Interested',
|
|
517
|
+
notInterestedFeedback: 'We will show you fewer items like this',
|
|
518
|
+
loading: 'Loading recommendations...',
|
|
519
|
+
noData: 'No recommendations available'
|
|
520
|
+
},
|
|
521
|
+
zh: {
|
|
522
|
+
recommendation: 'ไธชๆงๅๆจ่',
|
|
523
|
+
guessYouLike: '็ไฝ ๅๆฌข',
|
|
524
|
+
similarItems: '็ธไผผๆจ่',
|
|
525
|
+
alsoViewed: '็ไบ่ฟ็',
|
|
526
|
+
alsoBought: 'ไนฐไบ่ฟไนฐ',
|
|
527
|
+
notInterested: 'ไธๆๅ
ด่ถฃ',
|
|
528
|
+
notInterestedFeedback: 'ๅฐๅๅฐๆญค็ฑปๅ
ๅฎน็ๆจ่',
|
|
529
|
+
loading: 'ๆญฃๅจๅ ่ฝฝๆจ่ๅ
ๅฎน...',
|
|
530
|
+
noData: 'ๆๆ ๆจ่ๅ
ๅฎน'
|
|
531
|
+
},
|
|
532
|
+
ja: {
|
|
533
|
+
recommendation: 'ใใใใ',
|
|
534
|
+
guessYouLike: 'ใใชใใธใฎใใใใ',
|
|
535
|
+
similarItems: 'ไผผใใใใชๅๅ',
|
|
536
|
+
alsoViewed: 'ใใฎๅๅใ่ฆใไบบใฏใใใชๅๅใ่ฆใฆใใพใ',
|
|
537
|
+
alsoBought: 'ใใฎๅๅใ่ฒทใฃใไบบใฏใใใชๅๅใ่ฒทใฃใฆใใพใ',
|
|
538
|
+
notInterested: '่ๅณใใชใ',
|
|
539
|
+
notInterestedFeedback: 'ใใฎใใใชใขใคใใ ใฎ่กจ็คบใๆธใใใพใ',
|
|
540
|
+
loading: '่ชญใฟ่พผใฟไธญ...',
|
|
541
|
+
noData: 'ใใใใใฏใใใพใใ'
|
|
542
|
+
},
|
|
543
|
+
ko: {
|
|
544
|
+
recommendation: '์ถ์ฒ ์ฝํ
์ธ ',
|
|
545
|
+
guessYouLike: '์ด๋ฐ ์ฝํ
์ธ ๋ ์ด๋ ์ธ์?',
|
|
546
|
+
similarItems: '๋น์ทํ ์ฝํ
์ธ ',
|
|
547
|
+
alsoViewed: 'ํจ๊ป ๋ณธ ์ฝํ
์ธ ',
|
|
548
|
+
alsoBought: 'ํจ๊ป ๊ตฌ๋งคํ ์ํ',
|
|
549
|
+
notInterested: '๊ด์ฌ ์์',
|
|
550
|
+
notInterestedFeedback: '์ด์ ๊ฐ์ ์ฝํ
์ธ ์ถ์ฒ์ ์ค์
๋๋ค',
|
|
551
|
+
loading: '์ถ์ฒ ์ฝํ
์ธ ๋ฅผ ๋ถ๋ฌ์ค๋ ์ค...',
|
|
552
|
+
noData: '์ถ์ฒํ ์ฝํ
์ธ ๊ฐ ์์ต๋๋ค'
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
const SUPPORTED_LANGUAGES = ['en', 'zh', 'ja', 'ko'];
|
|
556
|
+
let currentLanguage = 'en';
|
|
557
|
+
function setLanguage(lang) {
|
|
558
|
+
if (SUPPORTED_LANGUAGES.includes(lang)) currentLanguage = lang;
|
|
559
|
+
}
|
|
560
|
+
function getLanguage() {
|
|
561
|
+
return currentLanguage;
|
|
562
|
+
}
|
|
563
|
+
function t(key) {
|
|
564
|
+
return (messages[currentLanguage] || messages.en)[key] || key;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* @quantabit/recommend-sdk
|
|
569
|
+
* Recommendation SDK - Full Version
|
|
570
|
+
*/
|
|
571
|
+
|
|
572
|
+
const getRecommendations = (...args) => recommendApi.getRecommendations(...args);
|
|
573
|
+
const getHomeFeed = (...args) => recommendApi.getHomeFeed(...args);
|
|
574
|
+
const getSimilar = (...args) => recommendApi.getSimilar(...args);
|
|
575
|
+
const getGuessYouLike = (...args) => recommendApi.getGuessYouLike(...args);
|
|
576
|
+
const getAlsoViewed = (...args) => recommendApi.getAlsoViewed(...args);
|
|
577
|
+
const getAlsoBought = (...args) => recommendApi.getAlsoBought(...args);
|
|
578
|
+
const feedback = (...args) => recommendApi.feedback(...args);
|
|
579
|
+
const markNotInterested = (...args) => recommendApi.markNotInterested(...args);
|
|
580
|
+
const getUserPreferences = (...args) => recommendApi.getUserPreferences(...args);
|
|
581
|
+
const updatePreferences = (...args) => recommendApi.updatePreferences(...args);
|
|
582
|
+
const getColdStartQuestions = (...args) => recommendApi.getColdStartQuestions(...args);
|
|
583
|
+
const submitColdStartAnswers = (...args) => recommendApi.submitColdStartAnswers(...args);
|
|
584
|
+
|
|
585
|
+
exports.FeedbackType = FeedbackType;
|
|
586
|
+
exports.GuessYouLike = GuessYouLike;
|
|
587
|
+
exports.RecommendAlgorithm = RecommendAlgorithm;
|
|
588
|
+
exports.RecommendApiClient = RecommendApiClient;
|
|
589
|
+
exports.RecommendCard = RecommendCard;
|
|
590
|
+
exports.RecommendScene = RecommendScene;
|
|
591
|
+
exports.RecommendSection = RecommendSection;
|
|
592
|
+
exports.RecommendType = RecommendType;
|
|
593
|
+
exports.SUPPORTED_LANGUAGES = SUPPORTED_LANGUAGES;
|
|
594
|
+
exports.SimilarItems = SimilarItems;
|
|
595
|
+
exports.feedback = feedback;
|
|
596
|
+
exports.getAlsoBought = getAlsoBought;
|
|
597
|
+
exports.getAlsoViewed = getAlsoViewed;
|
|
598
|
+
exports.getColdStartQuestions = getColdStartQuestions;
|
|
599
|
+
exports.getGuessYouLike = getGuessYouLike;
|
|
600
|
+
exports.getHomeFeed = getHomeFeed;
|
|
601
|
+
exports.getLanguage = getLanguage;
|
|
602
|
+
exports.getRecommendations = getRecommendations;
|
|
603
|
+
exports.getSimilar = getSimilar;
|
|
604
|
+
exports.getUserPreferences = getUserPreferences;
|
|
605
|
+
exports.markNotInterested = markNotInterested;
|
|
606
|
+
exports.messages = messages;
|
|
607
|
+
exports.recommendApi = recommendApi;
|
|
608
|
+
exports.setLanguage = setLanguage;
|
|
609
|
+
exports.submitColdStartAnswers = submitColdStartAnswers;
|
|
610
|
+
exports.t = t;
|
|
611
|
+
exports.updatePreferences = updatePreferences;
|
|
612
|
+
exports.useColdStart = useColdStart;
|
|
613
|
+
exports.useHomeFeed = useHomeFeed;
|
|
614
|
+
exports.useRecommendations = useRecommendations;
|
|
615
|
+
exports.useSimilar = useSimilar;
|
|
616
|
+
//# sourceMappingURL=index.cjs.map
|