react-native-chatbot-ai 0.1.27 → 0.1.29

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.
Files changed (50) hide show
  1. package/lib/module/components/chat/footer/SuggestionsBar.js +208 -0
  2. package/lib/module/components/chat/footer/SuggestionsBar.js.map +1 -0
  3. package/lib/module/components/chat/footer/index.js +89 -153
  4. package/lib/module/components/chat/footer/index.js.map +1 -1
  5. package/lib/module/components/chat/item/ChatAIAnswerMessageItem.js +38 -19
  6. package/lib/module/components/chat/item/ChatAIAnswerMessageItem.js.map +1 -1
  7. package/lib/module/components/chat/item/ShimmerBlock.js +52 -0
  8. package/lib/module/components/chat/item/ShimmerBlock.js.map +1 -0
  9. package/lib/module/components/product/CardHorizontal.js +30 -9
  10. package/lib/module/components/product/CardHorizontal.js.map +1 -1
  11. package/lib/module/hooks/message/useSendMessage.js +0 -2
  12. package/lib/module/hooks/message/useSendMessage.js.map +1 -1
  13. package/lib/module/hooks/message/useStreamMessage.js +8 -21
  14. package/lib/module/hooks/message/useStreamMessage.js.map +1 -1
  15. package/lib/module/hooks/product/useSearchProduct.js +8 -5
  16. package/lib/module/hooks/product/useSearchProduct.js.map +1 -1
  17. package/lib/module/store/products.js +13 -4
  18. package/lib/module/store/products.js.map +1 -1
  19. package/lib/module/store/streamMessage.js +18 -0
  20. package/lib/module/store/streamMessage.js.map +1 -1
  21. package/lib/module/types/chat.js.map +1 -1
  22. package/lib/typescript/src/components/chat/footer/SuggestionsBar.d.ts +10 -0
  23. package/lib/typescript/src/components/chat/footer/SuggestionsBar.d.ts.map +1 -0
  24. package/lib/typescript/src/components/chat/footer/index.d.ts.map +1 -1
  25. package/lib/typescript/src/components/chat/item/ChatAIAnswerMessageItem.d.ts.map +1 -1
  26. package/lib/typescript/src/components/chat/item/ShimmerBlock.d.ts +9 -0
  27. package/lib/typescript/src/components/chat/item/ShimmerBlock.d.ts.map +1 -0
  28. package/lib/typescript/src/components/product/CardHorizontal.d.ts +2 -2
  29. package/lib/typescript/src/components/product/CardHorizontal.d.ts.map +1 -1
  30. package/lib/typescript/src/hooks/message/useSendMessage.d.ts +0 -1
  31. package/lib/typescript/src/hooks/message/useSendMessage.d.ts.map +1 -1
  32. package/lib/typescript/src/hooks/message/useStreamMessage.d.ts +0 -1
  33. package/lib/typescript/src/hooks/message/useStreamMessage.d.ts.map +1 -1
  34. package/lib/typescript/src/hooks/product/useSearchProduct.d.ts.map +1 -1
  35. package/lib/typescript/src/store/products.d.ts.map +1 -1
  36. package/lib/typescript/src/store/streamMessage.d.ts.map +1 -1
  37. package/lib/typescript/src/types/chat.d.ts +5 -1
  38. package/lib/typescript/src/types/chat.d.ts.map +1 -1
  39. package/package.json +3 -1
  40. package/src/components/chat/footer/SuggestionsBar.tsx +247 -0
  41. package/src/components/chat/footer/index.tsx +90 -168
  42. package/src/components/chat/item/ChatAIAnswerMessageItem.tsx +56 -20
  43. package/src/components/chat/item/ShimmerBlock.tsx +60 -0
  44. package/src/components/product/CardHorizontal.tsx +333 -305
  45. package/src/hooks/message/useSendMessage.ts +1 -2
  46. package/src/hooks/message/useStreamMessage.ts +9 -24
  47. package/src/hooks/product/useSearchProduct.ts +10 -3
  48. package/src/store/products.ts +8 -2
  49. package/src/store/streamMessage.ts +13 -0
  50. package/src/types/chat.ts +5 -1
@@ -19,7 +19,7 @@ import { SearchSessionsResponse } from '../../types/dto';
19
19
  import { useChatContext } from '../../context/ChatContext';
20
20
 
21
21
  export const useSendMessage = () => {
22
- const { startStream, stopStream, retryStream } = useStreamMessage();
22
+ const { startStream, retryStream } = useStreamMessage();
23
23
  const { mutateAsync: createSession } = useCreateSession();
24
24
  const queryClient = useQueryClient();
25
25
  const logGA = useChatContext().logGA;
@@ -117,7 +117,6 @@ export const useSendMessage = () => {
117
117
 
118
118
  return {
119
119
  onSendMessage,
120
- stopStream,
121
120
  retryStream,
122
121
  };
123
122
  };
@@ -15,7 +15,6 @@ import useSessionStore from '../../store/session';
15
15
 
16
16
  export const useStreamMessage = () => {
17
17
  const sessionId = useSessionStore((state) => state.sessionId);
18
- const esRef = useRef<EventSource | null>(null);
19
18
  const sessionIdRef = useRef<string | undefined>(sessionId);
20
19
  const { apiAddress, logGA } = useChatContext();
21
20
  const setStreamMessage = useStreamMessageStore(
@@ -38,9 +37,12 @@ export const useStreamMessage = () => {
38
37
 
39
38
  if (!currentSessionId || !token) return;
40
39
 
41
- if (esRef.current) {
42
- esRef.current.close();
43
- esRef.current = null;
40
+ const eventSource = useStreamMessageStore.getState().eventSource;
41
+ const setEventSource = useStreamMessageStore.getState().setEventSource;
42
+
43
+ if (eventSource) {
44
+ eventSource.close();
45
+ setEventSource(undefined);
44
46
  }
45
47
 
46
48
  const url = `${apiAddress}${ENDPOINTS.assistantService.getStreamMessage(currentSessionId)}`;
@@ -53,6 +55,7 @@ export const useStreamMessage = () => {
53
55
  },
54
56
  body: JSON.stringify(payload),
55
57
  });
58
+ setEventSource(es);
56
59
 
57
60
  es.addEventListener('open', () => {
58
61
  console.log('🔵 Stream opened');
@@ -70,7 +73,7 @@ export const useStreamMessage = () => {
70
73
  );
71
74
  setStreamMessage(buffers);
72
75
  },
73
- 300,
76
+ 150,
74
77
  { leading: true, trailing: true }
75
78
  );
76
79
 
@@ -78,7 +81,7 @@ export const useStreamMessage = () => {
78
81
  isError: boolean,
79
82
  messageId?: string
80
83
  ) => {
81
- await new Promise((resolve) => setTimeout(resolve, 600));
84
+ await new Promise((resolve) => setTimeout(resolve, 500));
82
85
  if (isError) {
83
86
  DeviceEventEmitter.emit(events.updateMessageError, [
84
87
  {
@@ -148,21 +151,10 @@ export const useStreamMessage = () => {
148
151
  es.addEventListener('close', async () => {
149
152
  console.log('✅ Stream closed');
150
153
  });
151
-
152
- esRef.current = es;
153
154
  },
154
155
  [apiAddress, setIsStreaming, setStreamMessage, logGA, sessionId]
155
156
  );
156
157
 
157
- const stopStream = useCallback(() => {
158
- if (esRef.current) {
159
- console.log('🛑 Closing stream');
160
- esRef.current.close();
161
- esRef.current = null;
162
- setIsStreaming(false);
163
- }
164
- }, [setIsStreaming]);
165
-
166
158
  const retryStream = useCallback(
167
159
  (messageItem: IMessageItem) => {
168
160
  DeviceEventEmitter.emit(events.updateOneMessage, {
@@ -193,15 +185,8 @@ export const useStreamMessage = () => {
193
185
  [startStream, logGA, sessionId]
194
186
  );
195
187
 
196
- useEffect(() => {
197
- return () => {
198
- stopStream();
199
- };
200
- }, [stopStream]);
201
-
202
188
  return {
203
189
  startStream,
204
- stopStream,
205
190
  retryStream,
206
191
  };
207
192
  };
@@ -11,19 +11,26 @@ interface IUseFetchProductProps {
11
11
  }
12
12
  export const useSearchProduct = ({ productIds }: IUseFetchProductProps) =>
13
13
  useQuery({
14
- queryKey: [QUERY_KEYS.SEARCH_PRODUCT],
14
+ queryKey: [QUERY_KEYS.SEARCH_PRODUCT, productIds],
15
15
  queryFn: async () => {
16
+ const products = useProductsStore.getState().products;
17
+ const notExistProductIds = productIds.filter((id) => !products[id]);
18
+
19
+ if (notExistProductIds.length === 0) {
20
+ return [];
21
+ }
22
+
16
23
  const res = await apiInstance?.post<BaseResponse<IProductItem[]>>(
17
24
  ENDPOINTS.productService.search,
18
25
  {
19
26
  ReturnPromotionTag: true,
20
- productIds,
27
+ productIds: notExistProductIds,
21
28
  }
22
29
  );
23
30
  return res?.data?.data;
24
31
  },
25
32
  enabled: !!apiInstance && productIds?.length > 0,
26
33
  onSuccess: (data: IProductItem[]) => {
27
- useProductsStore.setState({ products: data || [] });
34
+ useProductsStore.getState().setProducts(data || []);
28
35
  },
29
36
  });
@@ -2,8 +2,14 @@ import { ProductsStore } from '../types';
2
2
  import { create } from 'zustand';
3
3
 
4
4
  const useProductsStore = create<ProductsStore>((set) => ({
5
- products: [],
6
- setProducts: (products) => set({ products }),
5
+ products: {},
6
+ setProducts: (products) => {
7
+ const productsMap: Record<string, any> = {};
8
+ products.forEach((product) => {
9
+ productsMap[product.id] = product;
10
+ });
11
+ set((state) => ({ products: { ...state.products, ...productsMap } }));
12
+ },
7
13
  }));
8
14
 
9
15
  export default useProductsStore;
@@ -19,6 +19,19 @@ const useStreamMessageStore = create<StreamMessageStore>((set) => ({
19
19
  return { streamMessage: updated };
20
20
  });
21
21
  },
22
+ setEventSource(es) {
23
+ set({ eventSource: es });
24
+ },
25
+ stopStream() {
26
+ set((state) => {
27
+ if (state.eventSource) {
28
+ console.log('🛑 Closing stream');
29
+ state.eventSource.close();
30
+ return { isStreaming: false, eventSource: undefined };
31
+ }
32
+ return {};
33
+ });
34
+ },
22
35
  }));
23
36
 
24
37
  export default useStreamMessageStore;
package/src/types/chat.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { JSX } from 'react';
2
2
  import { IMessageItem, IProductItem } from './dto';
3
+ import EventSource from 'react-native-sse';
3
4
 
4
5
  export interface ChatContextType {
5
6
  apiAddress: string;
@@ -57,9 +58,12 @@ export interface StreamMessageStore {
57
58
  setIsStreaming: (isStreaming: boolean) => void;
58
59
  streamMessage: Record<string, IMessageItem>;
59
60
  setStreamMessage: (streamMessage: Record<string, IMessageItem>) => void;
61
+ eventSource?: EventSource;
62
+ setEventSource: (es?: EventSource) => void;
63
+ stopStream: () => void;
60
64
  }
61
65
 
62
66
  export interface ProductsStore {
63
- products: IProductItem[];
67
+ products: Record<string, IProductItem | undefined>;
64
68
  setProducts: (products: IProductItem[]) => void;
65
69
  }