react-native-srschat 0.1.12 → 0.1.14

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.
@@ -1,82 +1,87 @@
1
- import React, { useState, useCallback, useContext } from 'react';
2
- import { SafeAreaView, Text, StyleSheet, View, TextInput, ScrollView, KeyboardAvoidingView,
3
- Platform, TouchableOpacity, RefreshControl, } from 'react-native';
1
+ import React, { useState, useEffect, useContext, useRef } from 'react';
2
+ import {
3
+ Text,
4
+ StyleSheet,
5
+ View,
6
+ TextInput,
7
+ TouchableOpacity,
8
+ Platform,
9
+ KeyboardAvoidingView,
10
+ Keyboard
11
+ } from 'react-native';
4
12
  import { Header } from '../components/header';
5
13
  import { AppContext } from '../contexts/AppContext';
6
- import { Testing } from '../components/testing';
7
14
  import Ionicons from 'react-native-vector-icons/Ionicons';
15
+ import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
16
+ import { Testing } from '../components/testing';
8
17
 
9
18
  const theme = {
10
19
  primaryColor: '#003764',
11
20
  backgroundColor: '#f6f6f6',
12
21
  textColor: '#000000',
13
- inputBackgroundColor: '#f6f6f6',
22
+ inputBackgroundColor: '#FFFFFF',
14
23
  };
15
24
 
16
25
  export const ChatWindow = ({ onProductCardClick, onAddToCartClick }) => {
17
- const { handleSend, messages, setMessages, onSendMessage, input, setInput } = useContext(AppContext)
18
-
19
- const [refreshing, setRefreshing] = useState(false);
26
+ const { handleSend, messages, input, setInput } = useContext(AppContext);
27
+ const scrollViewRef = useRef(null);
20
28
 
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);
29
+ useEffect(() => {
30
+ if (scrollViewRef.current) {
31
+ setTimeout(() => {
32
+ scrollViewRef.current.scrollToEnd({ animated: false });
33
+ }, 100);
30
34
  }
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
-
35
+ }, []);
36
+
44
37
  return (
45
- <SafeAreaView style={[styles.container, { backgroundColor: theme.backgroundColor }]}>
46
- <View style={styles.chatWindow}>
47
- <Header />
38
+ <View style={styles.container}>
39
+ <Header />
48
40
 
49
- <KeyboardAvoidingView
50
- behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
51
- style={styles.content}
52
- keyboardVerticalOffset={Platform.OS === 'ios' ? 60 : 0}
41
+ {/* Messages area */}
42
+ <KeyboardAwareScrollView
43
+ ref={scrollViewRef}
44
+ contentContainerStyle={styles.messagesContent}
45
+ enableOnAndroid
46
+ contentInsetAdjustmentBehavior="never"
47
+ automaticallyAdjustContentInsets={false}
48
+ contentInset={{ bottom: 0 }}
49
+ keyboardShouldPersistTaps="always"
50
+ showsVerticalScrollIndicator={false}
51
+ extraScrollHeight={0}
52
+ onContentSizeChange={() => {
53
+ scrollViewRef.current?.scrollToEnd({ animated: true });
54
+ }}
53
55
  >
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>
56
+ {messages.map((msg, i) => (
57
+ <View key={i} style={styles.messageWrapper}>
58
+ <View
59
+ style={[
60
+ styles.messageBubble,
61
+ msg.type === 'user' ? styles.userMessage : styles.aiMessage,
62
+ ]}
63
+ >
64
+ <Text
65
+ style={[
66
+ styles.messageText,
67
+ { color: msg.type === 'user' ? '#fff' : theme.textColor },
68
+ ]}
69
+ >
70
+ {msg.text}
71
+ </Text>
72
72
  </View>
73
- ))}
74
- </ScrollView>
75
-
76
- <Testing
77
- onProductCardClick={onProductCardClick}
78
- onAddToCartClick={onAddToCartClick}
79
- />
73
+ </View>
74
+ ))}
75
+ </KeyboardAwareScrollView>
76
+
77
+ <Testing
78
+ onProductCardClick={onProductCardClick}
79
+ onAddToCartClick={onAddToCartClick}
80
+ />
81
+ <KeyboardAvoidingView
82
+ behavior={Platform.OS === 'ios' ? 'padding' : undefined}
83
+ keyboardVerticalOffset={Platform.OS === 'ios' ? 80 : 60}
84
+ >
80
85
  <View style={styles.inputWrapper}>
81
86
  <View style={styles.inputContainer}>
82
87
  <TextInput
@@ -91,61 +96,55 @@ export const ChatWindow = ({ onProductCardClick, onAddToCartClick }) => {
91
96
  <Ionicons name="mic-outline" size={24} color="#8E8E93" />
92
97
  </TouchableOpacity>
93
98
  <TouchableOpacity
94
- style={[styles.sendButton, !input.trim() && styles.disabledButton]}
99
+ style={[
100
+ styles.sendButton,
101
+ !input.trim() && styles.disabledButton,
102
+ ]}
95
103
  onPress={handleSend}
96
104
  disabled={!input.trim()}
97
105
  >
98
- <Ionicons name="paper-plane-outline" size={24} color={input.trim() ? theme.primaryColor : '#8E8E93'} />
106
+ <Ionicons
107
+ name="paper-plane-outline"
108
+ size={24}
109
+ color={input.trim() ? theme.primaryColor : '#8E8E93'}
110
+ />
99
111
  </TouchableOpacity>
100
112
  </View>
101
113
  </View>
102
114
  </KeyboardAvoidingView>
103
- </View>
104
- </SafeAreaView>
115
+ </View>
105
116
  );
106
117
  };
107
118
 
108
119
  const styles = StyleSheet.create({
109
120
  container: {
110
121
  flex: 1,
111
- backgroundColor: '#FFFFFF',
112
- },
113
- chatWindow: {
114
- zIndex: 1,
115
- flex: 1,
116
- marginTop: 40,
122
+ backgroundColor: theme.backgroundColor,
123
+ position: 'absolute',
124
+ zIndex: 1000,
125
+ left: 0,
126
+ right: 0,
127
+ bottom: 0,
128
+ top: 100,
129
+ pointerEvents: 'box-none',
117
130
  borderTopWidth: 1,
118
131
  borderTopColor: '#DDD',
119
132
  borderTopLeftRadius: 16,
120
- borderTopRightRadius: 16,
121
- overflow: 'hidden',
122
- backgroundColor: theme.backgroundColor,
123
- },
124
- content: {
125
- flex: 1,
126
- },
127
- messagesContainer: {
128
- flex: 1,
133
+ borderTopRightRadius: 16,
134
+ overflow: 'hidden',
129
135
  },
130
136
  messagesContent: {
131
137
  padding: 16,
132
- paddingBottom: 32,
138
+ justifyContent: 'flex-end'
133
139
  },
134
140
  messageWrapper: {
135
- marginBottom: 16,
141
+ marginBottom: 0,
136
142
  },
137
143
  messageBubble: {
138
144
  maxWidth: '90%',
139
145
  padding: 12,
140
146
  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,
147
+ borderRadius: 12,
149
148
  },
150
149
  userMessage: {
151
150
  alignSelf: 'flex-end',
@@ -153,7 +152,8 @@ const styles = StyleSheet.create({
153
152
  },
154
153
  aiMessage: {
155
154
  alignSelf: 'flex-start',
156
- backgroundColor: '#F2F2F7',
155
+ backgroundColor: '#FFFFFF',
156
+ width: '90%',
157
157
  },
158
158
  messageText: {
159
159
  fontSize: 16,
@@ -161,10 +161,11 @@ const styles = StyleSheet.create({
161
161
  },
162
162
  inputWrapper: {
163
163
  backgroundColor: '#f6f6f6',
164
- paddingHorizontal: 8,
165
- paddingVertical: 8,
164
+ paddingHorizontal: 6,
165
+ paddingVertical: 6,
166
166
  borderTopWidth: 1,
167
167
  borderTopColor: 'rgba(0, 0, 0, 0.1)',
168
+ paddingBottom: 25,
168
169
  },
169
170
  inputContainer: {
170
171
  flexDirection: 'row',
@@ -190,6 +191,9 @@ const styles = StyleSheet.create({
190
191
  paddingHorizontal: 12,
191
192
  color: '#000000',
192
193
  },
194
+ inputButton: {
195
+ padding: 6,
196
+ },
193
197
  sendButton: {
194
198
  padding: 6,
195
199
  marginLeft: 'auto',
@@ -198,3 +202,10 @@ const styles = StyleSheet.create({
198
202
  opacity: 0.7,
199
203
  },
200
204
  });
205
+
206
+ /*
207
+ <Testing
208
+ onProductCardClick={onProductCardClick}
209
+ onAddToCartClick={onAddToCartClick}
210
+ />
211
+ */
@@ -9,14 +9,19 @@ export const Layout = ({ onProductCardClick, onAddToCartClick }) => {
9
9
  const { showModal } = useContext(AppContext);
10
10
 
11
11
  return (
12
- <View style={styles.container}>
12
+ <View style={styles.container} pointerEvents="box-none">
13
13
  {showModal === "Icon" ? <ChatIcon /> : <ChatWindow onProductCardClick={onProductCardClick} onAddToCartClick={onAddToCartClick}/>}
14
14
  </View>
15
15
  );
16
16
  };
17
17
 
18
18
  const styles = StyleSheet.create({
19
- container: {
20
- flex: 1
21
- }
19
+ container: {
20
+ flex: 1,
21
+ position: 'absolute',
22
+ top: 0,
23
+ left: 0,
24
+ right: 0,
25
+ bottom: 0,
26
+ },
22
27
  });