react-native-srschat 0.1.7 → 0.1.9

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 (62) hide show
  1. package/README.md +70 -0
  2. package/lib/commonjs/assets/heritage.png +0 -0
  3. package/lib/commonjs/components/header.js +105 -0
  4. package/lib/commonjs/components/header.js.map +1 -0
  5. package/lib/commonjs/components/testing.js +61 -0
  6. package/lib/commonjs/components/testing.js.map +1 -0
  7. package/lib/commonjs/contexts/AppContext.js +99 -0
  8. package/lib/commonjs/contexts/AppContext.js.map +1 -0
  9. package/lib/commonjs/hooks/Stream.js +202 -0
  10. package/lib/commonjs/hooks/Stream.js.map +1 -0
  11. package/lib/commonjs/index.js +13 -262
  12. package/lib/commonjs/index.js.map +1 -1
  13. package/lib/commonjs/layout/chatIcon.js +53 -0
  14. package/lib/commonjs/layout/chatIcon.js.map +1 -0
  15. package/lib/commonjs/layout/chatWindow.js +208 -0
  16. package/lib/commonjs/layout/chatWindow.js.map +1 -0
  17. package/lib/commonjs/layout/layout.js +35 -0
  18. package/lib/commonjs/layout/layout.js.map +1 -0
  19. package/lib/module/assets/heritage.png +0 -0
  20. package/lib/module/components/header.js +96 -0
  21. package/lib/module/components/header.js.map +1 -0
  22. package/lib/module/components/testing.js +53 -0
  23. package/lib/module/components/testing.js.map +1 -0
  24. package/lib/module/contexts/AppContext.js +90 -0
  25. package/lib/module/contexts/AppContext.js.map +1 -0
  26. package/lib/module/hooks/Stream.js +194 -0
  27. package/lib/module/hooks/Stream.js.map +1 -0
  28. package/lib/module/index.js +11 -261
  29. package/lib/module/index.js.map +1 -1
  30. package/lib/module/layout/chatIcon.js +44 -0
  31. package/lib/module/layout/chatIcon.js.map +1 -0
  32. package/lib/module/layout/chatWindow.js +199 -0
  33. package/lib/module/layout/chatWindow.js.map +1 -0
  34. package/lib/module/layout/layout.js +26 -0
  35. package/lib/module/layout/layout.js.map +1 -0
  36. package/lib/typescript/components/header.d.ts +3 -0
  37. package/lib/typescript/components/header.d.ts.map +1 -0
  38. package/lib/typescript/components/testing.d.ts +6 -0
  39. package/lib/typescript/components/testing.d.ts.map +1 -0
  40. package/lib/typescript/contexts/AppContext.d.ts +6 -0
  41. package/lib/typescript/contexts/AppContext.d.ts.map +1 -0
  42. package/lib/typescript/hooks/Stream.d.ts +2 -0
  43. package/lib/typescript/hooks/Stream.d.ts.map +1 -0
  44. package/lib/typescript/index.d.ts +5 -12
  45. package/lib/typescript/index.d.ts.map +1 -1
  46. package/lib/typescript/layout/chatIcon.d.ts +3 -0
  47. package/lib/typescript/layout/chatIcon.d.ts.map +1 -0
  48. package/lib/typescript/layout/chatWindow.d.ts +6 -0
  49. package/lib/typescript/layout/chatWindow.d.ts.map +1 -0
  50. package/lib/typescript/layout/layout.d.ts +6 -0
  51. package/lib/typescript/layout/layout.d.ts.map +1 -0
  52. package/package.json +1 -1
  53. package/src/assets/heritage.png +0 -0
  54. package/src/components/header.js +89 -0
  55. package/src/components/testing.js +47 -0
  56. package/src/contexts/AppContext.js +83 -0
  57. package/src/hooks/Stream.js +198 -0
  58. package/src/index.js +18 -0
  59. package/src/layout/chatIcon.js +38 -0
  60. package/src/layout/chatWindow.js +200 -0
  61. package/src/layout/layout.js +23 -0
  62. package/src/index.tsx +0 -323
@@ -0,0 +1,200 @@
1
+ import React, { useState, useCallback, useContext } from 'react';
2
+ import { SafeAreaView, Text, StyleSheet, View, TextInput, ScrollView, KeyboardAvoidingView,
3
+ Platform, TouchableOpacity, RefreshControl, } from 'react-native';
4
+ import { Ionicons as Icon } from '@expo/vector-icons';
5
+ import { Header } from '../components/header';
6
+ import { AppContext } from '../contexts/AppContext';
7
+ import { Testing } from '../components/testing';
8
+
9
+ const theme = {
10
+ primaryColor: '#003764',
11
+ backgroundColor: '#f6f6f6',
12
+ textColor: '#000000',
13
+ inputBackgroundColor: '#f6f6f6',
14
+ };
15
+
16
+ export const ChatWindow = ({ onProductCardClick, onAddToCartClick }) => {
17
+ const { handleSend, messages, setMessages, onSendMessage, input, setInput } = useContext(AppContext)
18
+
19
+ const [refreshing, setRefreshing] = useState(false);
20
+
21
+ const onRefresh = useCallback(async () => {
22
+ setRefreshing(true);
23
+ try {
24
+ const response = await onSendMessage("Hi, I'm refreshing the chat!");
25
+ setMessages((prev) => [...prev, { text: response, isUser: false }]);
26
+ } catch (error) {
27
+ console.error('Error refreshing chat:', error);
28
+ } finally {
29
+ setRefreshing(false);
30
+ }
31
+ }, []);
32
+
33
+ const getMessageStyle = (type) => [
34
+ styles.messageBubble,
35
+ type === "user" ? styles.userMessage : styles.aiMessage,
36
+ { backgroundColor: type === "user" ? theme.primaryColor : '#E8E8E8' },
37
+ ];
38
+
39
+ const getTextStyle = (type) => [
40
+ styles.messageText,
41
+ { color: type === "user" ? '#fff' : theme.textColor },
42
+ ];
43
+
44
+ return (
45
+ <SafeAreaView style={[styles.container, { backgroundColor: theme.backgroundColor }]}>
46
+ <View style={styles.chatWindow}>
47
+ <Header />
48
+
49
+ <KeyboardAvoidingView
50
+ behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
51
+ style={styles.content}
52
+ keyboardVerticalOffset={Platform.OS === 'ios' ? 60 : 0}
53
+ >
54
+ <ScrollView
55
+ style={styles.messagesContainer}
56
+ contentContainerStyle={styles.messagesContent}
57
+ refreshControl={
58
+ <RefreshControl
59
+ refreshing={refreshing}
60
+ onRefresh={onRefresh}
61
+ tintColor={theme.primaryColor}
62
+ colors={[theme.primaryColor]}
63
+ progressBackgroundColor="#ffffff"
64
+ />
65
+ }
66
+ >
67
+ {messages.map((msg, i) => (
68
+ <View key={i} style={styles.messageWrapper}>
69
+ <View style={[getMessageStyle(msg.type), styles.messageShadow]}>
70
+ <Text style={getTextStyle(msg.type)}>{msg.text}</Text>
71
+ </View>
72
+ </View>
73
+ ))}
74
+ </ScrollView>
75
+
76
+ <Testing
77
+ onProductCardClick={onProductCardClick}
78
+ onAddToCartClick={onAddToCartClick}
79
+ />
80
+ <View style={styles.inputWrapper}>
81
+ <View style={styles.inputContainer}>
82
+ <TextInput
83
+ style={styles.input}
84
+ value={input}
85
+ onChangeText={setInput}
86
+ placeholder="Ask a question..."
87
+ placeholderTextColor="#999"
88
+ multiline
89
+ />
90
+ <TouchableOpacity style={styles.inputButton}>
91
+ <Icon name="mic-outline" size={24} color="#8E8E93" />
92
+ </TouchableOpacity>
93
+ <TouchableOpacity
94
+ style={[styles.sendButton, !input.trim() && styles.disabledButton]}
95
+ onPress={handleSend}
96
+ disabled={!input.trim()}
97
+ >
98
+ <Icon name="paper-plane-outline" size={24} color={input.trim() ? theme.primaryColor : '#8E8E93'} />
99
+ </TouchableOpacity>
100
+ </View>
101
+ </View>
102
+ </KeyboardAvoidingView>
103
+ </View>
104
+ </SafeAreaView>
105
+ );
106
+ };
107
+
108
+ const styles = StyleSheet.create({
109
+ container: {
110
+ flex: 1,
111
+ backgroundColor: '#FFFFFF',
112
+ },
113
+ chatWindow: {
114
+ zIndex: 1,
115
+ flex: 1,
116
+ marginTop: 40,
117
+ borderTopWidth: 1,
118
+ borderTopColor: '#DDD',
119
+ borderTopLeftRadius: 16,
120
+ borderTopRightRadius: 16,
121
+ overflow: 'hidden',
122
+ backgroundColor: theme.backgroundColor,
123
+ },
124
+ content: {
125
+ flex: 1,
126
+ },
127
+ messagesContainer: {
128
+ flex: 1,
129
+ },
130
+ messagesContent: {
131
+ padding: 16,
132
+ paddingBottom: 32,
133
+ },
134
+ messageWrapper: {
135
+ marginBottom: 16,
136
+ },
137
+ messageBubble: {
138
+ maxWidth: '90%',
139
+ padding: 12,
140
+ paddingHorizontal: 16,
141
+ borderRadius: 20,
142
+ },
143
+ messageShadow: {
144
+ shadowColor: '#000',
145
+ shadowOffset: { width: 0, height: 1 },
146
+ shadowOpacity: 0.08,
147
+ shadowRadius: 2,
148
+ elevation: 2,
149
+ },
150
+ userMessage: {
151
+ alignSelf: 'flex-end',
152
+ backgroundColor: theme.primaryColor,
153
+ },
154
+ aiMessage: {
155
+ alignSelf: 'flex-start',
156
+ backgroundColor: '#F2F2F7',
157
+ },
158
+ messageText: {
159
+ fontSize: 16,
160
+ lineHeight: 22,
161
+ },
162
+ inputWrapper: {
163
+ backgroundColor: '#f6f6f6',
164
+ paddingHorizontal: 8,
165
+ paddingVertical: 8,
166
+ borderTopWidth: 1,
167
+ borderTopColor: 'rgba(0, 0, 0, 0.1)',
168
+ },
169
+ inputContainer: {
170
+ flexDirection: 'row',
171
+ alignItems: 'center',
172
+ paddingHorizontal: 8,
173
+ paddingVertical: 8,
174
+ backgroundColor: '#FFFFFF',
175
+ borderRadius: 16,
176
+ margin: 8,
177
+ shadowColor: '#000',
178
+ shadowOffset: {
179
+ width: 0,
180
+ height: 2,
181
+ },
182
+ shadowOpacity: 0.1,
183
+ shadowRadius: 4,
184
+ elevation: 3,
185
+ },
186
+ input: {
187
+ flex: 1,
188
+ fontSize: 16,
189
+ paddingVertical: 8,
190
+ paddingHorizontal: 12,
191
+ color: '#000000',
192
+ },
193
+ sendButton: {
194
+ padding: 6,
195
+ marginLeft: 'auto',
196
+ },
197
+ disabledButton: {
198
+ opacity: 0.7,
199
+ },
200
+ });
@@ -0,0 +1,23 @@
1
+ import React, { useState, useCallback, useContext } from 'react';
2
+ import { SafeAreaView, Text, StyleSheet, View, TextInput, ScrollView, KeyboardAvoidingView,
3
+ Platform, TouchableOpacity, RefreshControl, } from 'react-native';
4
+ import { Ionicons as Icon } from '@expo/vector-icons';
5
+ import { ChatWindow } from './chatWindow';
6
+ import { AppContext } from '../contexts/AppContext';
7
+ import { ChatIcon } from './chatIcon';
8
+
9
+ export const Layout = ({ onProductCardClick, onAddToCartClick }) => {
10
+ const { showModal } = useContext(AppContext);
11
+
12
+ return (
13
+ <View style={styles.container}>
14
+ {showModal === "Icon" ? <ChatIcon /> : <ChatWindow onProductCardClick={onProductCardClick} onAddToCartClick={onAddToCartClick}/>}
15
+ </View>
16
+ );
17
+ };
18
+
19
+ const styles = StyleSheet.create({
20
+ container: {
21
+ flex: 1
22
+ }
23
+ });
package/src/index.tsx DELETED
@@ -1,323 +0,0 @@
1
- import React from 'react';
2
- import {
3
- SafeAreaView,
4
- Text,
5
- StyleSheet,
6
- View,
7
- TextInput,
8
- ScrollView,
9
- KeyboardAvoidingView,
10
- Platform,
11
- TouchableOpacity,
12
- RefreshControl,
13
- } from 'react-native';
14
- import { Ionicons as Icon } from '@expo/vector-icons';
15
-
16
- interface ChatProps {
17
- onSendMessage: (
18
- message: string,
19
- onStream?: (chunk: string) => void
20
- ) => Promise<string>;
21
- placeholder?: string;
22
- theme?: {
23
- primaryColor?: string;
24
- backgroundColor?: string;
25
- textColor?: string;
26
- inputBackgroundColor?: string;
27
- };
28
- }
29
-
30
- const defaultTheme = {
31
- primaryColor: '#007AFF',
32
- backgroundColor: '#FFFFFF',
33
- textColor: '#000000',
34
- inputBackgroundColor: '#FFFFFF',
35
- };
36
-
37
- export const Chat: React.FC<ChatProps> = ({
38
- onSendMessage,
39
- placeholder = 'Type your message here...',
40
- theme = defaultTheme,
41
- }) => {
42
- const [message, setMessage] = React.useState('');
43
- const [messages, setMessages] = React.useState<Array<{
44
- text: string;
45
- isUser: boolean;
46
- timestamp: string;
47
- }>>([]);
48
- const [refreshing, setRefreshing] = React.useState(false);
49
-
50
- const onRefresh = React.useCallback(async () => {
51
- setRefreshing(true);
52
- try {
53
- const response = await onSendMessage("Hi, I'm refreshing the chat!");
54
- const currentTime = new Date().toLocaleTimeString([], {
55
- hour: 'numeric',
56
- minute: '2-digit',
57
- hour12: true
58
- });
59
- setMessages(prev => [...prev, {
60
- text: response,
61
- isUser: false,
62
- timestamp: currentTime
63
- }]);
64
- } catch (error) {
65
- console.error('Error refreshing chat:', error);
66
- } finally {
67
- setRefreshing(false);
68
- }
69
- }, [onSendMessage]);
70
-
71
- const handleSend = async () => {
72
- if (!message.trim()) return;
73
-
74
- const userMessage = message;
75
- setMessage('');
76
- const currentTime = new Date().toLocaleTimeString([], {
77
- hour: 'numeric',
78
- minute: '2-digit',
79
- hour12: true
80
- });
81
-
82
- setMessages(prev => [...prev, {
83
- text: userMessage,
84
- isUser: true,
85
- timestamp: currentTime
86
- }]);
87
-
88
- try {
89
- // Create bot message placeholder before streaming
90
- setMessages(prev => [...prev, {
91
- text: '',
92
- isUser: false,
93
- timestamp: currentTime
94
- }]);
95
-
96
- const response = await onSendMessage(
97
- userMessage,
98
- (chunk) => {
99
- setMessages(prev => {
100
- const newMessages = [...prev];
101
- const lastMessage = newMessages[newMessages.length - 1];
102
- if (lastMessage && !lastMessage.isUser) {
103
- lastMessage.text += chunk;
104
- }
105
- return newMessages;
106
- });
107
- }
108
- );
109
-
110
- // Only update if final response is different from streamed content
111
- setMessages(prev => {
112
- const newMessages = [...prev];
113
- const lastMessage = newMessages[newMessages.length - 1];
114
- if (lastMessage && !lastMessage.isUser && lastMessage.text !== response) {
115
- lastMessage.text = response;
116
- }
117
- return newMessages;
118
- });
119
- } catch (error) {
120
- console.error('Error sending message:', error);
121
- }
122
- };
123
-
124
- const getMessageStyle = (fromUser: boolean) => [
125
- styles.messageBubble,
126
- fromUser ? styles.userMessage : styles.botMessage,
127
- { backgroundColor: fromUser ? theme.primaryColor : '#E8E8E8' },
128
- ];
129
-
130
- const getTextStyle = (fromUser: boolean) => [
131
- styles.messageText,
132
- { color: fromUser ? '#fff' : theme.textColor },
133
- ];
134
-
135
- return (
136
- <SafeAreaView style={[styles.container, { backgroundColor: theme.backgroundColor }]}>
137
- <KeyboardAvoidingView
138
- behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
139
- style={styles.content}
140
- keyboardVerticalOffset={Platform.OS === 'ios' ? 60 : 0}
141
- >
142
- <ScrollView
143
- style={styles.messagesContainer}
144
- contentContainerStyle={styles.messagesContent}
145
- refreshControl={
146
- <RefreshControl
147
- refreshing={refreshing}
148
- onRefresh={onRefresh}
149
- tintColor={theme.primaryColor || defaultTheme.primaryColor}
150
- colors={[theme.primaryColor || defaultTheme.primaryColor]}
151
- progressBackgroundColor="#ffffff"
152
- />
153
- }
154
- >
155
- {messages.map((msg, i) => (
156
- <View key={i} style={styles.messageWrapper}>
157
- <View style={[getMessageStyle(msg.isUser), styles.messageShadow]}>
158
- <Text style={getTextStyle(msg.isUser)}>
159
- {msg.text}
160
- </Text>
161
- </View>
162
- <Text style={[
163
- styles.timestamp,
164
- msg.isUser ? styles.timestampRight : styles.timestampLeft
165
- ]}>
166
- {msg.timestamp}
167
- </Text>
168
- </View>
169
- ))}
170
- </ScrollView>
171
-
172
- <View style={styles.inputWrapper}>
173
- <View style={styles.inputContainer}>
174
- <TextInput
175
- style={styles.input}
176
- value={message}
177
- onChangeText={setMessage}
178
- placeholder={placeholder}
179
- placeholderTextColor="#999"
180
- multiline
181
- />
182
- </View>
183
- <View style={styles.inputButtonsContainer}>
184
- <TouchableOpacity style={styles.inputButton}>
185
- <Icon name="add" size={28} color="#8E8E93" />
186
- </TouchableOpacity>
187
- <TouchableOpacity style={styles.inputButton}>
188
- <Icon name="refresh-outline" size={28} color="#8E8E93" />
189
- </TouchableOpacity>
190
- <TouchableOpacity style={styles.inputButton}>
191
- <Icon name="mic-outline" size={28} color="#8E8E93" />
192
- </TouchableOpacity>
193
- <TouchableOpacity
194
- style={[
195
- styles.sendButton,
196
- !message.trim() && styles.disabledButton
197
- ]}
198
- onPress={handleSend}
199
- disabled={!message.trim()}
200
- >
201
- <Icon
202
- name="paper-plane-outline"
203
- size={28}
204
- color={message.trim() ? theme.primaryColor : '#8E8E93'}
205
- />
206
- </TouchableOpacity>
207
- </View>
208
- </View>
209
- </KeyboardAvoidingView>
210
- </SafeAreaView>
211
- );
212
- };
213
-
214
- const styles = StyleSheet.create({
215
- container: {
216
- flex: 1,
217
- backgroundColor: '#FFFFFF',
218
- },
219
- content: {
220
- flex: 1,
221
- },
222
- messagesContainer: {
223
- flex: 1,
224
- },
225
- messagesContent: {
226
- padding: 16,
227
- paddingBottom: 32,
228
- },
229
- messageWrapper: {
230
- marginBottom: 16,
231
- },
232
- timestamp: {
233
- fontSize: 12,
234
- color: '#8E8E93',
235
- marginTop: 4,
236
- paddingHorizontal: 4,
237
- },
238
- timestampRight: {
239
- textAlign: 'right',
240
- },
241
- timestampLeft: {
242
- textAlign: 'left',
243
- },
244
- messageBubble: {
245
- maxWidth: '70%',
246
- padding: 12,
247
- paddingHorizontal: 16,
248
- borderRadius: 20,
249
- backgroundColor: '#FFFFFF',
250
- },
251
- messageShadow: {
252
- shadowColor: '#000',
253
- shadowOffset: {
254
- width: 0,
255
- height: 1,
256
- },
257
- shadowOpacity: 0.08,
258
- shadowRadius: 2,
259
- elevation: 2,
260
- },
261
- userMessage: {
262
- alignSelf: 'flex-end',
263
- backgroundColor: '#007AFF',
264
- },
265
- botMessage: {
266
- alignSelf: 'flex-start',
267
- backgroundColor: '#F2F2F7',
268
- },
269
- messageText: {
270
- fontSize: 16,
271
- lineHeight: 22,
272
- },
273
- inputWrapper: {
274
- backgroundColor: '#FFFFFF',
275
- paddingHorizontal: 8,
276
- paddingVertical: 8,
277
- borderTopWidth: 1,
278
- borderTopColor: 'rgba(0, 0, 0, 0.1)',
279
- },
280
- inputContainer: {
281
- flexDirection: 'row',
282
- alignItems: 'center',
283
- paddingHorizontal: 8,
284
- paddingVertical: 8,
285
- backgroundColor: '#FFFFFF',
286
- borderRadius: 16,
287
- margin: 8,
288
- shadowColor: '#000',
289
- shadowOffset: {
290
- width: 0,
291
- height: 2,
292
- },
293
- shadowOpacity: 0.1,
294
- shadowRadius: 4,
295
- elevation: 3,
296
- },
297
- input: {
298
- flex: 1,
299
- fontSize: 16,
300
- paddingVertical: 8,
301
- paddingHorizontal: 12,
302
- maxHeight: 100,
303
- color: '#000000',
304
- },
305
- inputButtonsContainer: {
306
- flexDirection: 'row',
307
- alignItems: 'center',
308
- justifyContent: 'space-between',
309
- paddingHorizontal: 8,
310
- marginTop: 4,
311
- },
312
- inputButton: {
313
- padding: 6,
314
- marginHorizontal: 4,
315
- },
316
- sendButton: {
317
- padding: 6,
318
- marginLeft: 'auto',
319
- },
320
- disabledButton: {
321
- opacity: 0.7,
322
- },
323
- });