@nafzo/react-native-sdk 0.0.0-stage → 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.
@@ -0,0 +1,46 @@
1
+ import React from "react";
2
+ import { useNafzoContext } from "./NafzoProvider.js";
3
+ import { useNafzoChat } from "./hooks/useNafzoChat.js";
4
+
5
+ import FloatingChat from "./components/FloatingChat.js";
6
+ import FullscreenChat from "./components/FullscreenChat.js";
7
+
8
+ export function NafzoAssistant({
9
+ mode = "floating",
10
+ children,
11
+ onClose,
12
+ }) {
13
+ const { config } = useNafzoContext();
14
+ const chat = useNafzoChat();
15
+
16
+ if (!["floating", "fullscreen"].includes(mode)) {
17
+ throw new Error(
18
+ 'NafzoAssistant: mode must be "floating" or "fullscreen"'
19
+ );
20
+ }
21
+
22
+ if (typeof children === "function") {
23
+ return children({
24
+ mode,
25
+ ...chat,
26
+ });
27
+ }
28
+
29
+ if (mode === "fullscreen") {
30
+ return (
31
+ <FullscreenChat
32
+ chat={chat}
33
+ config={config}
34
+ onClose={onClose}
35
+ />
36
+ );
37
+ }
38
+
39
+ return (
40
+ <FloatingChat
41
+ chat={chat}
42
+ config={config}
43
+ onClose={onClose}
44
+ />
45
+ );
46
+ }
@@ -0,0 +1,92 @@
1
+
2
+ import React, {
3
+ createContext,
4
+ useContext,
5
+ useEffect,
6
+ useMemo,
7
+ useState,
8
+ } from "react";
9
+
10
+ import { NafzoApi } from "./services/api.js";
11
+ import { NafzoSession } from "./services/session.js";
12
+ import { validateConfig } from "./utils/helpers.js";
13
+
14
+ const NafzoContext = createContext(null);
15
+
16
+ export function NafzoProvider({ config, children }) {
17
+ const [ready, setReady] = useState(false);
18
+ const [error, setError] = useState(null);
19
+
20
+ const client = useMemo(() => {
21
+ validateConfig(config);
22
+
23
+ const normalizedConfig = {
24
+ ...config,
25
+ appIdentifier: config.appIdentifier.trim(),
26
+ };
27
+
28
+ return {
29
+ api: new NafzoApi(normalizedConfig),
30
+ session: new NafzoSession({
31
+ assistantId: normalizedConfig.assistantId,
32
+ userId: normalizedConfig.userId || null,
33
+ storage: normalizedConfig.storage,
34
+ }),
35
+ config: normalizedConfig,
36
+ };
37
+ }, [
38
+ config.assistantId,
39
+ config.userId,
40
+ config.baseUrl,
41
+ config.platform,
42
+ config.appIdentifier,
43
+ config.storage,
44
+ ]);
45
+
46
+ useEffect(() => {
47
+ let mounted = true;
48
+
49
+ setReady(false);
50
+ setError(null);
51
+
52
+ client.session
53
+ .initialize()
54
+ .then(() => {
55
+ if (mounted) setReady(true);
56
+ })
57
+ .catch((err) => {
58
+ if (mounted) setError(err);
59
+ });
60
+
61
+ return () => {
62
+ mounted = false;
63
+ };
64
+ }, [client]);
65
+
66
+ const value = useMemo(
67
+ () => ({
68
+ ...client,
69
+ ready,
70
+ error,
71
+ }),
72
+ [client, ready, error]
73
+ );
74
+
75
+ return React.createElement(
76
+ NafzoContext.Provider,
77
+ { value },
78
+ children
79
+ );
80
+ }
81
+
82
+ export function useNafzoContext() {
83
+ const context = useContext(NafzoContext);
84
+
85
+ if (!context) {
86
+ throw new Error(
87
+ "Nafzo: This component must be used inside NafzoProvider"
88
+ );
89
+ }
90
+
91
+ return context;
92
+ }
@@ -0,0 +1,97 @@
1
+ import React from "react";
2
+ import {
3
+ View,
4
+ Text,
5
+ Image,
6
+ TouchableOpacity,
7
+ StyleSheet,
8
+ } from "react-native";
9
+
10
+ export default function ChatHeader({
11
+ assistantName = "Nafzo Assistant",
12
+ avatarUrl,
13
+ primaryColor = "#2563EB",
14
+ textColor = "#FFFFFF",
15
+ onClose,
16
+ showClose = false,
17
+ }) {
18
+ return (
19
+ <View style={[styles.container, { backgroundColor: primaryColor }]}>
20
+ {avatarUrl ? (
21
+ <Image source={{ uri: avatarUrl }} style={styles.avatar} />
22
+ ) : (
23
+ <View style={styles.defaultAvatar}>
24
+ <Text style={styles.avatarText}>N</Text>
25
+ </View>
26
+ )}
27
+
28
+ <View style={styles.titleContainer}>
29
+ <Text style={[styles.title, { color: textColor }]}>
30
+ {assistantName}
31
+ </Text>
32
+ <Text style={[styles.subtitle, { color: textColor }]}>
33
+ Online
34
+ </Text>
35
+ </View>
36
+
37
+ {showClose && (
38
+ <TouchableOpacity
39
+ onPress={onClose}
40
+ style={styles.closeButton}
41
+ accessibilityLabel="Close chat"
42
+ >
43
+ <Text style={[styles.closeText, { color: textColor }]}>✕</Text>
44
+ </TouchableOpacity>
45
+ )}
46
+ </View>
47
+ );
48
+ }
49
+
50
+ const styles = StyleSheet.create({
51
+ container: {
52
+ minHeight: 64,
53
+ paddingHorizontal: 15,
54
+ flexDirection: "row",
55
+ alignItems: "center",
56
+ },
57
+ avatar: {
58
+ width: 38,
59
+ height: 38,
60
+ borderRadius: 19,
61
+ },
62
+ defaultAvatar: {
63
+ width: 38,
64
+ height: 38,
65
+ borderRadius: 19,
66
+ backgroundColor: "rgba(255,255,255,0.2)",
67
+ alignItems: "center",
68
+ justifyContent: "center",
69
+ },
70
+ avatarText: {
71
+ color: "#FFFFFF",
72
+ fontWeight: "700",
73
+ fontSize: 18,
74
+ },
75
+ titleContainer: {
76
+ flex: 1,
77
+ marginLeft: 11,
78
+ },
79
+ title: {
80
+ fontSize: 16,
81
+ fontWeight: "700",
82
+ },
83
+ subtitle: {
84
+ fontSize: 11,
85
+ opacity: 0.85,
86
+ marginTop: 2,
87
+ },
88
+ closeButton: {
89
+ width: 36,
90
+ height: 36,
91
+ alignItems: "center",
92
+ justifyContent: "center",
93
+ },
94
+ closeText: {
95
+ fontSize: 20,
96
+ },
97
+ });
@@ -0,0 +1,133 @@
1
+
2
+ import React, { useState } from "react";
3
+
4
+ import {
5
+ View,
6
+ TextInput,
7
+ TouchableOpacity,
8
+ Text,
9
+ StyleSheet,
10
+ ActivityIndicator,
11
+ } from "react-native";
12
+
13
+ export default function ChatInput({
14
+ onSend,
15
+ sending = false,
16
+ disabled = false,
17
+ placeholder = "Type your message...",
18
+ primaryColor = "#2563EB",
19
+ backgroundColor = "#FFFFFF",
20
+ textColor = "#1E293B",
21
+ }) {
22
+ const [message, setMessage] = useState("");
23
+ const [sendError, setSendError] = useState(null);
24
+
25
+ const handleSend = async () => {
26
+ const value = message.trim();
27
+
28
+ if (!value || sending || disabled) return;
29
+
30
+ setSendError(null);
31
+
32
+ try {
33
+ await onSend?.(value);
34
+ setMessage("");
35
+ } catch (error) {
36
+ setSendError(
37
+ error?.message || "Unable to send message"
38
+ );
39
+ }
40
+ };
41
+
42
+ return (
43
+ <View style={[styles.container, { backgroundColor }]}>
44
+ <View style={styles.inputContainer}>
45
+ <TextInput
46
+ style={[styles.input, { color: textColor }]}
47
+ value={message}
48
+ onChangeText={(value) => {
49
+ setMessage(value);
50
+ setSendError(null);
51
+ }}
52
+ placeholder={placeholder}
53
+ placeholderTextColor="#94A3B8"
54
+ multiline
55
+ maxLength={4000}
56
+ editable={!sending && !disabled}
57
+ />
58
+
59
+ <TouchableOpacity
60
+ style={[
61
+ styles.sendButton,
62
+ { backgroundColor: primaryColor },
63
+ (!message.trim() || sending || disabled) &&
64
+ styles.disabledButton,
65
+ ]}
66
+ onPress={handleSend}
67
+ disabled={!message.trim() || sending || disabled}
68
+ accessibilityLabel="Send message"
69
+ accessibilityRole="button"
70
+ >
71
+ {sending ? (
72
+ <ActivityIndicator
73
+ color="#FFFFFF"
74
+ size="small"
75
+ />
76
+ ) : (
77
+ <Text style={styles.sendText}>➤</Text>
78
+ )}
79
+ </TouchableOpacity>
80
+ </View>
81
+
82
+ {sendError ? (
83
+ <Text style={styles.errorText}>
84
+ {sendError}
85
+ </Text>
86
+ ) : null}
87
+ </View>
88
+ );
89
+ }
90
+
91
+ const styles = StyleSheet.create({
92
+ container: {
93
+ padding: 12,
94
+ borderTopWidth: 1,
95
+ borderTopColor: "#E2E8F0",
96
+ },
97
+ inputContainer: {
98
+ flexDirection: "row",
99
+ alignItems: "flex-end",
100
+ },
101
+ input: {
102
+ flex: 1,
103
+ minHeight: 44,
104
+ maxHeight: 110,
105
+ backgroundColor: "#F1F5F9",
106
+ borderRadius: 22,
107
+ paddingHorizontal: 16,
108
+ paddingVertical: 11,
109
+ fontSize: 15,
110
+ },
111
+ sendButton: {
112
+ width: 44,
113
+ height: 44,
114
+ borderRadius: 22,
115
+ alignItems: "center",
116
+ justifyContent: "center",
117
+ marginLeft: 8,
118
+ },
119
+ disabledButton: {
120
+ opacity: 0.5,
121
+ },
122
+ sendText: {
123
+ color: "#FFFFFF",
124
+ fontSize: 20,
125
+ fontWeight: "700",
126
+ },
127
+ errorText: {
128
+ color: "#DC2626",
129
+ fontSize: 12,
130
+ marginTop: 6,
131
+ marginLeft: 4,
132
+ },
133
+ });
@@ -0,0 +1,157 @@
1
+
2
+ import React, { useState } from "react";
3
+
4
+ import {
5
+ View,
6
+ Text,
7
+ TouchableOpacity,
8
+ StyleSheet,
9
+ KeyboardAvoidingView,
10
+ Platform,
11
+ } from "react-native";
12
+
13
+ import ChatHeader from "./ChatHeader.js";
14
+ import MessageList from "./MessageList.js";
15
+ import ChatInput from "./ChatInput.js";
16
+
17
+ export default function FloatingChat({
18
+ chat,
19
+ config = {},
20
+ onClose,
21
+ }) {
22
+ const [visible, setVisible] = useState(false);
23
+
24
+ const theme = config.theme || {};
25
+
26
+ const primaryColor =
27
+ theme.primaryColor || "#2563EB";
28
+
29
+ const position =
30
+ config.position || "bottom-right";
31
+
32
+ const isLeft = position === "bottom-left";
33
+
34
+ const colors = {
35
+ userBubble: theme.userBubbleColor || primaryColor,
36
+ assistantBubble:
37
+ theme.assistantBubbleColor || "#F1F5F9",
38
+ userText: theme.userTextColor || "#FFFFFF",
39
+ assistantText: theme.assistantTextColor || "#1E293B",
40
+ typingColor: theme.typingColor || "#64748B",
41
+ };
42
+
43
+ if (!visible) {
44
+ return (
45
+ <TouchableOpacity
46
+ style={[
47
+ styles.floatingButton,
48
+ { backgroundColor: primaryColor },
49
+ isLeft ? styles.leftPosition : styles.rightPosition,
50
+ ]}
51
+ onPress={() => setVisible(true)}
52
+ accessibilityLabel="Open chat"
53
+ accessibilityRole="button"
54
+ >
55
+ {React.isValidElement(config.floatingIcon) ? (
56
+ config.floatingIcon
57
+ ) : (
58
+ <Text style={styles.floatingButtonText}>
59
+ {config.floatingIcon || "✦"}
60
+ </Text>
61
+ )}
62
+ {/* <Text style={styles.floatingButtonText}>✦</Text> */}
63
+ </TouchableOpacity>
64
+ );
65
+ }
66
+
67
+ return (
68
+ <KeyboardAvoidingView
69
+ behavior={Platform.OS === "ios" ? "padding" : undefined}
70
+ style={[
71
+ styles.overlay,
72
+ isLeft ? styles.leftPosition : styles.rightPosition,
73
+ ]}
74
+ pointerEvents="box-none"
75
+ >
76
+ <View style={styles.window}>
77
+ <ChatHeader
78
+ assistantName={
79
+ config.assistantName || "Nafzo Assistant"
80
+ }
81
+ avatarUrl={config.avatarUrl}
82
+ primaryColor={primaryColor}
83
+ onClose={() => {
84
+ setVisible(false);
85
+ onClose?.();
86
+ }}
87
+ showClose
88
+ />
89
+
90
+ <View style={styles.messages}>
91
+ <MessageList
92
+ messages={chat.messages}
93
+ sending={chat.sending}
94
+ loading={chat.loading}
95
+ colors={colors}
96
+ emptyMessage={
97
+ config.welcomeMessage ||
98
+ "Hello! How can I help you today?"
99
+ }
100
+ />
101
+ </View>
102
+
103
+ <ChatInput
104
+ onSend={chat.sendMessage}
105
+ sending={chat.sending}
106
+ disabled={chat.loading}
107
+ primaryColor={primaryColor}
108
+ placeholder={
109
+ config.inputPlaceholder || "Type your message..."
110
+ }
111
+ />
112
+ </View>
113
+ </KeyboardAvoidingView>
114
+ );
115
+ }
116
+
117
+ const styles = StyleSheet.create({
118
+ floatingButton: {
119
+ position: "absolute",
120
+ bottom: 24,
121
+ width: 60,
122
+ height: 60,
123
+ borderRadius: 30,
124
+ alignItems: "center",
125
+ justifyContent: "center",
126
+ elevation: 8,
127
+ zIndex: 999,
128
+ },
129
+ rightPosition: {
130
+ right: 20,
131
+ },
132
+ leftPosition: {
133
+ left: 20,
134
+ },
135
+ floatingButtonText: {
136
+ color: "#FFFFFF",
137
+ fontSize: 30,
138
+ },
139
+ overlay: {
140
+ position: "absolute",
141
+ bottom: 20,
142
+ zIndex: 999,
143
+ width: 340,
144
+ maxWidth: "92%",
145
+ },
146
+ window: {
147
+ height: 500,
148
+ maxHeight: "85%",
149
+ backgroundColor: "#FFFFFF",
150
+ borderRadius: 16,
151
+ overflow: "hidden",
152
+ elevation: 12,
153
+ },
154
+ messages: {
155
+ flex: 1,
156
+ },
157
+ });
@@ -0,0 +1,83 @@
1
+
2
+ import React from "react";
3
+
4
+ import {
5
+ View,
6
+ StyleSheet,
7
+ KeyboardAvoidingView,
8
+ Platform,
9
+ } from "react-native";
10
+
11
+ import ChatHeader from "./ChatHeader.js";
12
+ import MessageList from "./MessageList.js";
13
+ import ChatInput from "./ChatInput.js";
14
+
15
+ export default function FullscreenChat({
16
+ chat,
17
+ config = {},
18
+ onClose,
19
+ }) {
20
+ const theme = config.theme || {};
21
+
22
+ const primaryColor =
23
+ theme.primaryColor || "#2563EB";
24
+
25
+ const colors = {
26
+ userBubble: theme.userBubbleColor || primaryColor,
27
+ assistantBubble:
28
+ theme.assistantBubbleColor || "#F1F5F9",
29
+ userText: theme.userTextColor || "#FFFFFF",
30
+ assistantText: theme.assistantTextColor || "#1E293B",
31
+ typingColor: theme.typingColor || "#64748B",
32
+ };
33
+
34
+ return (
35
+ <KeyboardAvoidingView
36
+ style={styles.container}
37
+ behavior={Platform.OS === "ios" ? "padding" : undefined}
38
+ >
39
+ <ChatHeader
40
+ assistantName={
41
+ config.assistantName || "Nafzo Assistant"
42
+ }
43
+ avatarUrl={config.avatarUrl}
44
+ primaryColor={primaryColor}
45
+ onClose={onClose}
46
+ showClose={Boolean(onClose)}
47
+ />
48
+
49
+ <View style={styles.messages}>
50
+ <MessageList
51
+ messages={chat.messages}
52
+ sending={chat.sending}
53
+ loading={chat.loading}
54
+ colors={colors}
55
+ emptyMessage={
56
+ config.welcomeMessage ||
57
+ "Hello! How can I help you today?"
58
+ }
59
+ />
60
+ </View>
61
+
62
+ <ChatInput
63
+ onSend={chat.sendMessage}
64
+ sending={chat.sending}
65
+ disabled={chat.loading}
66
+ primaryColor={primaryColor}
67
+ placeholder={
68
+ config.inputPlaceholder || "Type your message..."
69
+ }
70
+ />
71
+ </KeyboardAvoidingView>
72
+ );
73
+ }
74
+
75
+ const styles = StyleSheet.create({
76
+ container: {
77
+ flex: 1,
78
+ backgroundColor: "#FFFFFF",
79
+ },
80
+ messages: {
81
+ flex: 1,
82
+ },
83
+ });
@@ -0,0 +1,71 @@
1
+
2
+ import React from "react";
3
+ import {
4
+ View,
5
+ Text,
6
+ StyleSheet,
7
+ } from "react-native";
8
+
9
+ export default function MessageBubble({
10
+ message,
11
+ userBubbleColor = "#2563EB",
12
+ assistantBubbleColor = "#F1F5F9",
13
+ userTextColor = "#FFFFFF",
14
+ assistantTextColor = "#1E293B",
15
+ }) {
16
+ const isUser = message?.role === "user";
17
+ const content = message?.content || "";
18
+
19
+ return (
20
+ <View
21
+ style={[
22
+ styles.row,
23
+ isUser ? styles.userRow : styles.assistantRow,
24
+ ]}
25
+ >
26
+ <View
27
+ style={[
28
+ styles.bubble,
29
+ isUser
30
+ ? { backgroundColor: userBubbleColor }
31
+ : { backgroundColor: assistantBubbleColor },
32
+ ]}
33
+ >
34
+ <Text
35
+ style={[
36
+ styles.message,
37
+ {
38
+ color: isUser ? userTextColor : assistantTextColor,
39
+ },
40
+ ]}
41
+ >
42
+ {content}
43
+ </Text>
44
+ </View>
45
+ </View>
46
+ );
47
+ }
48
+
49
+ const styles = StyleSheet.create({
50
+ row: {
51
+ width: "100%",
52
+ marginVertical: 5,
53
+ paddingHorizontal: 12,
54
+ },
55
+ userRow: {
56
+ alignItems: "flex-end",
57
+ },
58
+ assistantRow: {
59
+ alignItems: "flex-start",
60
+ },
61
+ bubble: {
62
+ maxWidth: "85%",
63
+ paddingHorizontal: 14,
64
+ paddingVertical: 11,
65
+ borderRadius: 16,
66
+ },
67
+ message: {
68
+ fontSize: 15,
69
+ lineHeight: 22,
70
+ },
71
+ });