feeddo-react-native 0.0.0-stage → 0.1.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.
Files changed (225) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +401 -2
  3. package/android/build.gradle +39 -0
  4. package/android/src/main/AndroidManifest.xml +1 -0
  5. package/android/src/main/java/dev/feeddo/reactnative/FeeddoReactNativeModule.java +380 -0
  6. package/android/src/main/java/dev/feeddo/reactnative/FeeddoReactNativePackage.java +25 -0
  7. package/dist/Feeddo.d.ts +131 -0
  8. package/dist/Feeddo.d.ts.map +1 -0
  9. package/dist/Feeddo.js +176 -0
  10. package/dist/Feeddo.js.map +1 -0
  11. package/dist/FeeddoClient.d.ts +151 -0
  12. package/dist/FeeddoClient.d.ts.map +1 -0
  13. package/dist/FeeddoClient.js +690 -0
  14. package/dist/FeeddoClient.js.map +1 -0
  15. package/dist/FeeddoProvider.d.ts +23 -0
  16. package/dist/FeeddoProvider.d.ts.map +1 -0
  17. package/dist/FeeddoProvider.js +76 -0
  18. package/dist/FeeddoProvider.js.map +1 -0
  19. package/dist/config/FeeddoConfig.d.ts +10 -0
  20. package/dist/config/FeeddoConfig.d.ts.map +1 -0
  21. package/dist/config/FeeddoConfig.js +10 -0
  22. package/dist/config/FeeddoConfig.js.map +1 -0
  23. package/dist/index.d.ts +36 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +38 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/models/Conversation.d.ts +27 -0
  28. package/dist/models/Conversation.d.ts.map +1 -0
  29. package/dist/models/Conversation.js +24 -0
  30. package/dist/models/Conversation.js.map +1 -0
  31. package/dist/models/EndUser.d.ts +49 -0
  32. package/dist/models/EndUser.d.ts.map +1 -0
  33. package/dist/models/EndUser.js +2 -0
  34. package/dist/models/EndUser.js.map +1 -0
  35. package/dist/models/FeeddoNotification.d.ts +15 -0
  36. package/dist/models/FeeddoNotification.d.ts.map +1 -0
  37. package/dist/models/FeeddoNotification.js +15 -0
  38. package/dist/models/FeeddoNotification.js.map +1 -0
  39. package/dist/models/HomeData.d.ts +16 -0
  40. package/dist/models/HomeData.d.ts.map +1 -0
  41. package/dist/models/HomeData.js +19 -0
  42. package/dist/models/HomeData.js.map +1 -0
  43. package/dist/models/Message.d.ts +12 -0
  44. package/dist/models/Message.d.ts.map +1 -0
  45. package/dist/models/Message.js +25 -0
  46. package/dist/models/Message.js.map +1 -0
  47. package/dist/models/PushProvider.d.ts +14 -0
  48. package/dist/models/PushProvider.d.ts.map +1 -0
  49. package/dist/models/PushProvider.js +9 -0
  50. package/dist/models/PushProvider.js.map +1 -0
  51. package/dist/models/Task.d.ts +17 -0
  52. package/dist/models/Task.d.ts.map +1 -0
  53. package/dist/models/Task.js +22 -0
  54. package/dist/models/Task.js.map +1 -0
  55. package/dist/models/TaskComment.d.ts +11 -0
  56. package/dist/models/TaskComment.d.ts.map +1 -0
  57. package/dist/models/TaskComment.js +27 -0
  58. package/dist/models/TaskComment.js.map +1 -0
  59. package/dist/models/Ticket.d.ts +13 -0
  60. package/dist/models/Ticket.d.ts.map +1 -0
  61. package/dist/models/Ticket.js +14 -0
  62. package/dist/models/Ticket.js.map +1 -0
  63. package/dist/native/FeeddoNative.d.ts +58 -0
  64. package/dist/native/FeeddoNative.d.ts.map +1 -0
  65. package/dist/native/FeeddoNative.js +22 -0
  66. package/dist/native/FeeddoNative.js.map +1 -0
  67. package/dist/push/FeeddoPushNotificationApns.d.ts +73 -0
  68. package/dist/push/FeeddoPushNotificationApns.d.ts.map +1 -0
  69. package/dist/push/FeeddoPushNotificationApns.js +301 -0
  70. package/dist/push/FeeddoPushNotificationApns.js.map +1 -0
  71. package/dist/services/ApiService.d.ts +64 -0
  72. package/dist/services/ApiService.d.ts.map +1 -0
  73. package/dist/services/ApiService.js +406 -0
  74. package/dist/services/ApiService.js.map +1 -0
  75. package/dist/services/ConversationService.d.ts +45 -0
  76. package/dist/services/ConversationService.d.ts.map +1 -0
  77. package/dist/services/ConversationService.js +259 -0
  78. package/dist/services/ConversationService.js.map +1 -0
  79. package/dist/services/WebSocketService.d.ts +26 -0
  80. package/dist/services/WebSocketService.d.ts.map +1 -0
  81. package/dist/services/WebSocketService.js +157 -0
  82. package/dist/services/WebSocketService.js.map +1 -0
  83. package/dist/theme/FeeddoTheme.d.ts +31 -0
  84. package/dist/theme/FeeddoTheme.d.ts.map +1 -0
  85. package/dist/theme/FeeddoTheme.js +60 -0
  86. package/dist/theme/FeeddoTheme.js.map +1 -0
  87. package/dist/ui/FeeddoFeedbackCard.d.ts +15 -0
  88. package/dist/ui/FeeddoFeedbackCard.d.ts.map +1 -0
  89. package/dist/ui/FeeddoFeedbackCard.js +188 -0
  90. package/dist/ui/FeeddoFeedbackCard.js.map +1 -0
  91. package/dist/ui/FeeddoModal.d.ts +20 -0
  92. package/dist/ui/FeeddoModal.d.ts.map +1 -0
  93. package/dist/ui/FeeddoModal.js +174 -0
  94. package/dist/ui/FeeddoModal.js.map +1 -0
  95. package/dist/ui/components/AttachmentPreview.d.ts +10 -0
  96. package/dist/ui/components/AttachmentPreview.d.ts.map +1 -0
  97. package/dist/ui/components/AttachmentPreview.js +143 -0
  98. package/dist/ui/components/AttachmentPreview.js.map +1 -0
  99. package/dist/ui/components/ChatInputArea.d.ts +16 -0
  100. package/dist/ui/components/ChatInputArea.d.ts.map +1 -0
  101. package/dist/ui/components/ChatInputArea.js +85 -0
  102. package/dist/ui/components/ChatInputArea.js.map +1 -0
  103. package/dist/ui/components/FeeddoIcon.d.ts +10 -0
  104. package/dist/ui/components/FeeddoIcon.d.ts.map +1 -0
  105. package/dist/ui/components/FeeddoIcon.js +690 -0
  106. package/dist/ui/components/FeeddoIcon.js.map +1 -0
  107. package/dist/ui/components/FeeddoNotificationBadge.d.ts +14 -0
  108. package/dist/ui/components/FeeddoNotificationBadge.d.ts.map +1 -0
  109. package/dist/ui/components/FeeddoNotificationBadge.js +171 -0
  110. package/dist/ui/components/FeeddoNotificationBadge.js.map +1 -0
  111. package/dist/ui/components/MessageBubble.d.ts +13 -0
  112. package/dist/ui/components/MessageBubble.d.ts.map +1 -0
  113. package/dist/ui/components/MessageBubble.js +163 -0
  114. package/dist/ui/components/MessageBubble.js.map +1 -0
  115. package/dist/ui/components/NotificationsSheet.d.ts +10 -0
  116. package/dist/ui/components/NotificationsSheet.d.ts.map +1 -0
  117. package/dist/ui/components/NotificationsSheet.js +252 -0
  118. package/dist/ui/components/NotificationsSheet.js.map +1 -0
  119. package/dist/ui/components/RatingWidget.d.ts +11 -0
  120. package/dist/ui/components/RatingWidget.d.ts.map +1 -0
  121. package/dist/ui/components/RatingWidget.js +34 -0
  122. package/dist/ui/components/RatingWidget.js.map +1 -0
  123. package/dist/ui/components/TaskCard.d.ts +12 -0
  124. package/dist/ui/components/TaskCard.d.ts.map +1 -0
  125. package/dist/ui/components/TaskCard.js +210 -0
  126. package/dist/ui/components/TaskCard.js.map +1 -0
  127. package/dist/ui/components/TaskDetailsSheet.d.ts +15 -0
  128. package/dist/ui/components/TaskDetailsSheet.d.ts.map +1 -0
  129. package/dist/ui/components/TaskDetailsSheet.js +577 -0
  130. package/dist/ui/components/TaskDetailsSheet.js.map +1 -0
  131. package/dist/ui/components/TicketCard.d.ts +11 -0
  132. package/dist/ui/components/TicketCard.d.ts.map +1 -0
  133. package/dist/ui/components/TicketCard.js +100 -0
  134. package/dist/ui/components/TicketCard.js.map +1 -0
  135. package/dist/ui/components/TicketDetailsSheet.d.ts +14 -0
  136. package/dist/ui/components/TicketDetailsSheet.d.ts.map +1 -0
  137. package/dist/ui/components/TicketDetailsSheet.js +254 -0
  138. package/dist/ui/components/TicketDetailsSheet.js.map +1 -0
  139. package/dist/ui/components/TypingIndicator.d.ts +10 -0
  140. package/dist/ui/components/TypingIndicator.d.ts.map +1 -0
  141. package/dist/ui/components/TypingIndicator.js +77 -0
  142. package/dist/ui/components/TypingIndicator.js.map +1 -0
  143. package/dist/ui/screens/FeeddoChatScreen.d.ts +13 -0
  144. package/dist/ui/screens/FeeddoChatScreen.d.ts.map +1 -0
  145. package/dist/ui/screens/FeeddoChatScreen.js +681 -0
  146. package/dist/ui/screens/FeeddoChatScreen.js.map +1 -0
  147. package/dist/ui/screens/FeeddoHomeScreen.d.ts +15 -0
  148. package/dist/ui/screens/FeeddoHomeScreen.d.ts.map +1 -0
  149. package/dist/ui/screens/FeeddoHomeScreen.js +653 -0
  150. package/dist/ui/screens/FeeddoHomeScreen.js.map +1 -0
  151. package/dist/ui/screens/FeeddoMessagesScreen.d.ts +12 -0
  152. package/dist/ui/screens/FeeddoMessagesScreen.d.ts.map +1 -0
  153. package/dist/ui/screens/FeeddoMessagesScreen.js +326 -0
  154. package/dist/ui/screens/FeeddoMessagesScreen.js.map +1 -0
  155. package/dist/ui/screens/FeeddoTasksScreen.d.ts +13 -0
  156. package/dist/ui/screens/FeeddoTasksScreen.d.ts.map +1 -0
  157. package/dist/ui/screens/FeeddoTasksScreen.js +517 -0
  158. package/dist/ui/screens/FeeddoTasksScreen.js.map +1 -0
  159. package/dist/ui/screens/FeeddoTicketsScreen.d.ts +10 -0
  160. package/dist/ui/screens/FeeddoTicketsScreen.d.ts.map +1 -0
  161. package/dist/ui/screens/FeeddoTicketsScreen.js +112 -0
  162. package/dist/ui/screens/FeeddoTicketsScreen.js.map +1 -0
  163. package/dist/utils/DeviceInfoHelper.d.ts +29 -0
  164. package/dist/utils/DeviceInfoHelper.d.ts.map +1 -0
  165. package/dist/utils/DeviceInfoHelper.js +190 -0
  166. package/dist/utils/DeviceInfoHelper.js.map +1 -0
  167. package/dist/utils/MediaHelper.d.ts +20 -0
  168. package/dist/utils/MediaHelper.d.ts.map +1 -0
  169. package/dist/utils/MediaHelper.js +92 -0
  170. package/dist/utils/MediaHelper.js.map +1 -0
  171. package/dist/utils/NotificationSupport.d.ts +22 -0
  172. package/dist/utils/NotificationSupport.d.ts.map +1 -0
  173. package/dist/utils/NotificationSupport.js +211 -0
  174. package/dist/utils/NotificationSupport.js.map +1 -0
  175. package/dist/utils/StorageHelper.d.ts +65 -0
  176. package/dist/utils/StorageHelper.d.ts.map +1 -0
  177. package/dist/utils/StorageHelper.js +273 -0
  178. package/dist/utils/StorageHelper.js.map +1 -0
  179. package/feeddo-react-native.podspec +26 -0
  180. package/ios/FeeddoReactNativeModule.h +8 -0
  181. package/ios/FeeddoReactNativeModule.m +831 -0
  182. package/package.json +56 -4
  183. package/src/Feeddo.ts +247 -0
  184. package/src/FeeddoClient.ts +895 -0
  185. package/src/FeeddoProvider.tsx +139 -0
  186. package/src/config/FeeddoConfig.ts +9 -0
  187. package/src/index.ts +67 -0
  188. package/src/models/Conversation.ts +50 -0
  189. package/src/models/EndUser.ts +65 -0
  190. package/src/models/FeeddoNotification.ts +29 -0
  191. package/src/models/HomeData.ts +33 -0
  192. package/src/models/Message.ts +36 -0
  193. package/src/models/PushProvider.ts +15 -0
  194. package/src/models/Task.ts +40 -0
  195. package/src/models/TaskComment.ts +36 -0
  196. package/src/models/Ticket.ts +25 -0
  197. package/src/native/FeeddoNative.ts +80 -0
  198. package/src/push/FeeddoPushNotificationApns.ts +362 -0
  199. package/src/services/ApiService.ts +456 -0
  200. package/src/services/ConversationService.ts +305 -0
  201. package/src/services/WebSocketService.ts +185 -0
  202. package/src/theme/FeeddoTheme.ts +85 -0
  203. package/src/ui/FeeddoFeedbackCard.tsx +245 -0
  204. package/src/ui/FeeddoModal.tsx +303 -0
  205. package/src/ui/components/AttachmentPreview.tsx +192 -0
  206. package/src/ui/components/ChatInputArea.tsx +154 -0
  207. package/src/ui/components/FeeddoIcon.tsx +903 -0
  208. package/src/ui/components/FeeddoNotificationBadge.tsx +234 -0
  209. package/src/ui/components/MessageBubble.tsx +223 -0
  210. package/src/ui/components/NotificationsSheet.tsx +324 -0
  211. package/src/ui/components/RatingWidget.tsx +64 -0
  212. package/src/ui/components/TaskCard.tsx +259 -0
  213. package/src/ui/components/TaskDetailsSheet.tsx +747 -0
  214. package/src/ui/components/TicketCard.tsx +128 -0
  215. package/src/ui/components/TicketDetailsSheet.tsx +324 -0
  216. package/src/ui/components/TypingIndicator.tsx +104 -0
  217. package/src/ui/screens/FeeddoChatScreen.tsx +877 -0
  218. package/src/ui/screens/FeeddoHomeScreen.tsx +824 -0
  219. package/src/ui/screens/FeeddoMessagesScreen.tsx +414 -0
  220. package/src/ui/screens/FeeddoTasksScreen.tsx +671 -0
  221. package/src/ui/screens/FeeddoTicketsScreen.tsx +164 -0
  222. package/src/utils/DeviceInfoHelper.ts +209 -0
  223. package/src/utils/MediaHelper.ts +105 -0
  224. package/src/utils/NotificationSupport.ts +233 -0
  225. package/src/utils/StorageHelper.ts +305 -0
@@ -0,0 +1,305 @@
1
+ import { Conversation } from '../models/Conversation';
2
+ import { ApiService } from './ApiService';
3
+ import { WebSocketService } from './WebSocketService';
4
+
5
+ type Listener = () => void;
6
+ type UnreadCountListener = (count: number) => void;
7
+
8
+ export class ConversationService {
9
+ private apiService: ApiService;
10
+ private webSocketService: WebSocketService;
11
+
12
+ private _conversations: Conversation[] = [];
13
+ private _isLoading: boolean = false;
14
+ private _error: string | null = null;
15
+ private _activeConversationId: string | null = null;
16
+ private _unreadMessageCount: number = 0;
17
+ private _externalUnreadBaseline: number = 0;
18
+ private _unreadCounts: Map<string, number> = new Map();
19
+
20
+ private listeners: Set<Listener> = new Set();
21
+ private unreadCountListeners: Set<UnreadCountListener> = new Set();
22
+ private unsubscribeWs?: () => void;
23
+
24
+ public onNewMessage?: (conversation: Conversation) => void;
25
+
26
+ constructor(apiService: ApiService, webSocketService: WebSocketService) {
27
+ this.apiService = apiService;
28
+ this.webSocketService = webSocketService;
29
+ this.unsubscribeWs = this.webSocketService.subscribe(this.handleWebSocketMessage.bind(this));
30
+ }
31
+
32
+ public get conversations(): Conversation[] {
33
+ return [...this._conversations];
34
+ }
35
+
36
+ public get isLoading(): boolean {
37
+ return this._isLoading;
38
+ }
39
+
40
+ public get error(): string | null {
41
+ return this._error;
42
+ }
43
+
44
+ public get activeConversationId(): string | null {
45
+ return this._activeConversationId;
46
+ }
47
+
48
+ public get unreadMessageCount(): number {
49
+ return this._unreadMessageCount;
50
+ }
51
+
52
+ public addListener(listener: Listener): () => void {
53
+ this.listeners.add(listener);
54
+ return () => {
55
+ this.listeners.delete(listener);
56
+ };
57
+ }
58
+
59
+ public onUnreadCountChange(listener: UnreadCountListener): () => void {
60
+ this.unreadCountListeners.add(listener);
61
+ return () => {
62
+ this.unreadCountListeners.delete(listener);
63
+ };
64
+ }
65
+
66
+ public removeUnreadCountListener(listener: UnreadCountListener): void {
67
+ this.unreadCountListeners.delete(listener);
68
+ }
69
+
70
+ private notifyListeners(): void {
71
+ this.listeners.forEach((l) => {
72
+ try {
73
+ l();
74
+ } catch (_) {}
75
+ });
76
+ }
77
+
78
+ private notifyUnreadCountChange(): void {
79
+ this.unreadCountListeners.forEach((l) => {
80
+ try {
81
+ l(this._unreadMessageCount);
82
+ } catch (_) {}
83
+ });
84
+ }
85
+
86
+ public setActiveConversationId(id: string | null): void {
87
+ this._activeConversationId = id;
88
+ if (id) {
89
+ this.markAsRead(id);
90
+ }
91
+ }
92
+
93
+ public async loadConversations(userId: string): Promise<void> {
94
+ this._isLoading = true;
95
+ this._error = null;
96
+
97
+ try {
98
+ const convs = await this.apiService.getConversations(userId);
99
+ convs.sort((a, b) => b.lastMessageAt - a.lastMessageAt);
100
+ this._conversations = convs;
101
+
102
+ this._externalUnreadBaseline = 0;
103
+ this._unreadCounts.clear();
104
+ for (const c of convs) {
105
+ this._unreadCounts.set(c.id, c.unreadMessages);
106
+ }
107
+ this.updateTotalUnreadCount();
108
+ } catch (e: any) {
109
+ this._error = e.message || 'Failed to load conversations';
110
+ } finally {
111
+ this._isLoading = false;
112
+ this.notifyListeners();
113
+ }
114
+ }
115
+
116
+ public async createConversation(
117
+ userId: string,
118
+ agentName: string = 'support_agent'
119
+ ): Promise<Conversation> {
120
+ const conversation = await this.apiService.createConversation(userId, agentName);
121
+ this._conversations.unshift(conversation);
122
+ this.notifyListeners();
123
+ return conversation;
124
+ }
125
+
126
+ public markAsRead(conversationId: string): void {
127
+ this._unreadCounts.set(conversationId, 0);
128
+ this.updateTotalUnreadCount();
129
+
130
+ const index = this._conversations.findIndex((c) => c.id === conversationId);
131
+ if (index !== -1) {
132
+ this._conversations[index] = {
133
+ ...this._conversations[index],
134
+ unreadMessages: 0,
135
+ };
136
+ this.notifyListeners();
137
+ }
138
+ }
139
+
140
+ public sendReadReceipt(conversationId: string): void {
141
+ this.webSocketService.send({
142
+ type: 'read_receipt',
143
+ conversationId,
144
+ });
145
+ this.markAsRead(conversationId);
146
+ }
147
+
148
+ public async rateConversation(conversationId: string, rating: number): Promise<void> {
149
+ await this.apiService.rateConversation(conversationId, rating);
150
+
151
+ const index = this._conversations.findIndex((c) => c.id === conversationId);
152
+ if (index !== -1) {
153
+ this._conversations[index] = {
154
+ ...this._conversations[index],
155
+ userSatisfaction: rating,
156
+ };
157
+ this.notifyListeners();
158
+ }
159
+ }
160
+
161
+ private updateTotalUnreadCount(): void {
162
+ let sum = this._externalUnreadBaseline;
163
+ this._unreadCounts.forEach((count) => {
164
+ sum += count;
165
+ });
166
+ if (this._unreadMessageCount !== sum) {
167
+ this._unreadMessageCount = sum;
168
+ this.notifyUnreadCountChange();
169
+ }
170
+ }
171
+
172
+ public updateConversation(conversation: Conversation): void {
173
+ const index = this._conversations.findIndex((c) => c.id === conversation.id);
174
+ if (index !== -1) {
175
+ this._conversations[index] = conversation;
176
+ } else {
177
+ this._conversations.push(conversation);
178
+ this._conversations.sort((a, b) => b.lastMessageAt - a.lastMessageAt);
179
+ }
180
+
181
+ this._unreadCounts.set(conversation.id, conversation.unreadMessages);
182
+ this.updateTotalUnreadCount();
183
+ this.notifyListeners();
184
+ }
185
+
186
+ public incrementUnreadCount(conversationId: string): void {
187
+ const current = this._unreadCounts.get(conversationId) ?? 0;
188
+ const next = current + 1;
189
+ this._unreadCounts.set(conversationId, next);
190
+
191
+ const index = this._conversations.findIndex((c) => c.id === conversationId);
192
+ if (index !== -1) {
193
+ this._conversations[index] = {
194
+ ...this._conversations[index],
195
+ unreadMessages: next,
196
+ };
197
+ }
198
+ this.updateTotalUnreadCount();
199
+ this.notifyListeners();
200
+ }
201
+
202
+ public setTotalUnreadCount(count: number): void {
203
+ const normalized = Math.max(0, count);
204
+ if (normalized === 0) {
205
+ this._unreadCounts.forEach((_, id) => this._unreadCounts.set(id, 0));
206
+ this._externalUnreadBaseline = 0;
207
+ } else {
208
+ let mapSum = 0;
209
+ this._unreadCounts.forEach((c) => {
210
+ mapSum += c;
211
+ });
212
+ this._externalUnreadBaseline = Math.max(0, normalized - mapSum);
213
+ }
214
+
215
+ if (this._unreadMessageCount !== normalized) {
216
+ this._unreadMessageCount = normalized;
217
+ this.notifyUnreadCountChange();
218
+ this.notifyListeners();
219
+ }
220
+ }
221
+
222
+ private handleWebSocketMessage(data: Record<string, any>): void {
223
+ if (data.role === 'assistant' || data.role === 'human' || data.role === 'user') {
224
+ const conversationId = data.conversationId;
225
+ if (!conversationId) return;
226
+
227
+ const index = this._conversations.findIndex((c) => c.id === conversationId);
228
+ const content = data.content as string | undefined;
229
+ const createdAt = typeof data.createdAt === 'number' ? data.createdAt : Date.now();
230
+ const role = data.role as string | undefined;
231
+ const displayName = data.displayName as string | undefined;
232
+
233
+ // Clean preview of JSON code blocks
234
+ let preview = content;
235
+ if (content) {
236
+ preview = content.replace(/```json\s*(\{[\s\S]*?\})\s*```/g, '').trim();
237
+ }
238
+
239
+ if (index !== -1) {
240
+ let conversation = this._conversations[index];
241
+ let newDisplayName = conversation.displayName;
242
+ let unreadCount = conversation.unreadMessages;
243
+
244
+ if (role === 'assistant' || role === 'human') {
245
+ if (displayName) {
246
+ newDisplayName = displayName;
247
+ }
248
+ if (this._activeConversationId !== conversationId) {
249
+ unreadCount++;
250
+ this._unreadCounts.set(conversationId, (this._unreadCounts.get(conversationId) ?? 0) + 1);
251
+ }
252
+ }
253
+
254
+ conversation = {
255
+ ...conversation,
256
+ lastMessagePreview: preview,
257
+ lastMessageAt: createdAt,
258
+ displayName: newDisplayName,
259
+ unreadMessages: unreadCount,
260
+ };
261
+
262
+ this._conversations.splice(index, 1);
263
+ this._conversations.unshift(conversation);
264
+ this.updateTotalUnreadCount();
265
+ this.notifyListeners();
266
+
267
+ if (this._activeConversationId !== conversationId && (role === 'assistant' || role === 'human')) {
268
+ this.onNewMessage?.(conversation);
269
+ }
270
+ } else {
271
+ if (role === 'assistant' || role === 'human') {
272
+ const conversation: Conversation = {
273
+ id: conversationId,
274
+ appId: '',
275
+ status: 'active',
276
+ autoReply: false,
277
+ startedAt: createdAt,
278
+ lastMessageAt: createdAt,
279
+ displayName: displayName ?? 'Support',
280
+ lastMessagePreview: preview,
281
+ unreadMessages: 1,
282
+ tags: [],
283
+ customData: {},
284
+ hasTicket: false,
285
+ hasTask: false,
286
+ };
287
+
288
+ if (this._activeConversationId !== conversationId) {
289
+ this.onNewMessage?.(conversation);
290
+ this._unreadCounts.set(conversationId, (this._unreadCounts.get(conversationId) ?? 0) + 1);
291
+ this.updateTotalUnreadCount();
292
+ }
293
+ }
294
+ }
295
+ }
296
+ }
297
+
298
+ public dispose(): void {
299
+ if (this.unsubscribeWs) {
300
+ this.unsubscribeWs();
301
+ }
302
+ this.listeners.clear();
303
+ this.unreadCountListeners.clear();
304
+ }
305
+ }
@@ -0,0 +1,185 @@
1
+ import { StorageHelper } from '../utils/StorageHelper';
2
+
3
+ type MessageHandler = (data: Record<string, any>) => void;
4
+
5
+ export class WebSocketService {
6
+ private ws: WebSocket | null = null;
7
+ private readonly baseUrl: string;
8
+ private readonly apiKey: string;
9
+ private userId: string | null = null;
10
+ private currentConversationId: string | null = null;
11
+ private isDisposed: boolean = false;
12
+ private isConnected: boolean = false;
13
+ private retryCount: number = 0;
14
+ private static readonly MAX_RETRIES = 3;
15
+ private listeners: Set<MessageHandler> = new Set();
16
+ private reconnectTimer: any = null;
17
+ private pendingQueue: Array<Record<string, any>> = [];
18
+
19
+ constructor(baseUrl: string, apiKey: string) {
20
+ this.baseUrl = baseUrl;
21
+ this.apiKey = apiKey;
22
+ }
23
+
24
+ public setUserId(userId: string) {
25
+ this.userId = userId;
26
+ }
27
+
28
+ public getUserId(): string | null {
29
+ return this.userId;
30
+ }
31
+
32
+ public subscribe(handler: MessageHandler): () => void {
33
+ this.listeners.add(handler);
34
+ return () => {
35
+ this.listeners.delete(handler);
36
+ };
37
+ }
38
+
39
+ private notifyListeners(data: Record<string, any>) {
40
+ this.listeners.forEach((listener) => {
41
+ try {
42
+ listener(data);
43
+ } catch (err) {
44
+ console.warn('[Feeddo WS] Listener error:', err);
45
+ }
46
+ });
47
+ }
48
+
49
+ public async connect(userId: string, conversationId?: string, isRetry: boolean = false): Promise<void> {
50
+ this.isDisposed = false;
51
+ this.userId = userId;
52
+
53
+ if (!isRetry) {
54
+ this.retryCount = 0;
55
+ }
56
+
57
+ if (
58
+ this.ws &&
59
+ (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING)
60
+ ) {
61
+ if (this.currentConversationId === (conversationId ?? null)) {
62
+ return;
63
+ }
64
+ console.log(`[Feeddo WS] Switching conversation from ${this.currentConversationId} to ${conversationId}`);
65
+ this.ws.close();
66
+ this.ws = null;
67
+ this.isConnected = false;
68
+ }
69
+
70
+ this.currentConversationId = conversationId ?? null;
71
+
72
+ try {
73
+ const wsUrl = this.baseUrl
74
+ .replace(/^https:\/\//, 'wss://')
75
+ .replace(/^http:\/\//, 'ws://');
76
+
77
+ const queryParts: string[] = [
78
+ `apiKey=${encodeURIComponent(this.apiKey)}`,
79
+ `userId=${encodeURIComponent(userId)}`,
80
+ ];
81
+
82
+ if (conversationId) {
83
+ queryParts.push(`conversationId=${encodeURIComponent(conversationId)}`);
84
+ }
85
+
86
+ const fullUrl = `${wsUrl}/chat/ws?${queryParts.join('&')}`;
87
+ console.log('[Feeddo WS] Connecting to:', fullUrl);
88
+
89
+ if (this.ws) {
90
+ try {
91
+ this.ws.close();
92
+ } catch (_) {}
93
+ this.ws = null;
94
+ }
95
+
96
+ this.ws = new WebSocket(fullUrl);
97
+
98
+ this.ws.onopen = () => {
99
+ console.log('[Feeddo WS] Connected successfully!');
100
+ this.isConnected = true;
101
+ this.retryCount = 0;
102
+
103
+ // Flush queued messages
104
+ while (this.pendingQueue.length > 0) {
105
+ const msg = this.pendingQueue.shift();
106
+ if (msg && this.ws && this.ws.readyState === WebSocket.OPEN) {
107
+ console.log('[Feeddo WS] Sending queued message:', JSON.stringify(msg));
108
+ this.ws.send(JSON.stringify(msg));
109
+ }
110
+ }
111
+ };
112
+
113
+ this.ws.onmessage = (event) => {
114
+ this.retryCount = 0;
115
+ try {
116
+ console.log('[Feeddo WS] Received:', event.data);
117
+ const data = JSON.parse(event.data);
118
+ this.notifyListeners(data);
119
+ } catch (e) {
120
+ console.warn('[Feeddo WS] Error decoding message:', e);
121
+ }
122
+ };
123
+
124
+ this.ws.onerror = (e) => {
125
+ console.warn('[Feeddo WS] Error event:', e);
126
+ this.isConnected = false;
127
+ };
128
+
129
+ this.ws.onclose = (event) => {
130
+ console.log(`[Feeddo WS] Closed (code: ${event?.code}, reason: ${event?.reason})`);
131
+ this.isConnected = false;
132
+ };
133
+ } catch (e) {
134
+ this.isConnected = false;
135
+ console.warn('[Feeddo WS] Connect exception:', e);
136
+ }
137
+ }
138
+
139
+ public async send(message: Record<string, any>): Promise<void> {
140
+ if (this.isConnected && this.ws && this.ws.readyState === WebSocket.OPEN) {
141
+ console.log('[Feeddo WS] Sending:', JSON.stringify(message));
142
+ this.ws.send(JSON.stringify(message));
143
+ return;
144
+ }
145
+
146
+ // Socket not open - queue message
147
+ console.log('[Feeddo WS] Socket not open (state: ' + (this.ws ? this.ws.readyState : 'null') + '), queueing message');
148
+ this.pendingQueue.push(message);
149
+
150
+ // If socket is already in connecting state, it will flush in onopen
151
+ if (this.ws && this.ws.readyState === WebSocket.CONNECTING) {
152
+ return;
153
+ }
154
+
155
+ // Otherwise initiate connection
156
+ if (!this.userId) {
157
+ this.userId = await StorageHelper.getOrCreateLocalUserId();
158
+ }
159
+
160
+ if (this.userId) {
161
+ await this.connect(this.userId, this.currentConversationId ?? undefined);
162
+ } else {
163
+ console.warn('[Feeddo WS] Cannot connect - userId is still null after fallback');
164
+ }
165
+ }
166
+
167
+ public disconnect(): void {
168
+ this.isDisposed = true;
169
+ this.isConnected = false;
170
+ if (this.reconnectTimer) {
171
+ clearTimeout(this.reconnectTimer);
172
+ this.reconnectTimer = null;
173
+ }
174
+ if (this.ws) {
175
+ this.ws.close();
176
+ this.ws = null;
177
+ }
178
+ }
179
+
180
+ public dispose(): void {
181
+ this.disconnect();
182
+ this.pendingQueue = [];
183
+ this.listeners.clear();
184
+ }
185
+ }
@@ -0,0 +1,85 @@
1
+ export interface FeeddoColors {
2
+ background: string;
3
+ backgroundGradient?: string[];
4
+ backgroundGradientStops?: number[];
5
+ textPrimary: string;
6
+ textSecondary: string;
7
+ cardBackground: string;
8
+ cardText: string;
9
+ iconColor: string;
10
+ closeButtonColor: string;
11
+ primary: string;
12
+ surface: string;
13
+ success: string;
14
+ error: string;
15
+ border: string;
16
+ divider: string;
17
+ appBarBackground: string;
18
+ }
19
+
20
+ export class FeeddoTheme {
21
+ public colors: FeeddoColors;
22
+ public isDark: boolean;
23
+
24
+ constructor(options: { colors: Partial<FeeddoColors>; isDark?: boolean }) {
25
+ const isDark = options.isDark ?? true;
26
+ const baseColors = isDark ? FeeddoTheme.darkColors() : FeeddoTheme.lightColors();
27
+ this.isDark = isDark;
28
+ this.colors = {
29
+ ...baseColors,
30
+ ...options.colors,
31
+ };
32
+ }
33
+
34
+ public static darkColors(): FeeddoColors {
35
+ return {
36
+ background: '#09090B',
37
+ textPrimary: '#FAFAFA',
38
+ textSecondary: '#A1A1AA',
39
+ cardBackground: '#18181B',
40
+ cardText: '#FAFAFA',
41
+ iconColor: '#A1A1AA',
42
+ closeButtonColor: '#FAFAFA',
43
+ primary: '#FFFFFF',
44
+ surface: '#18181B',
45
+ success: '#22C55E',
46
+ error: '#EF4444',
47
+ border: '#27272A',
48
+ divider: '#1A1A1A',
49
+ appBarBackground: '#000000',
50
+ };
51
+ }
52
+
53
+ public static lightColors(): FeeddoColors {
54
+ return {
55
+ background: '#FBFBFB',
56
+ textPrimary: '#111827',
57
+ textSecondary: '#4B5563',
58
+ cardBackground: '#FFFFFF',
59
+ cardText: '#111827',
60
+ iconColor: '#4B5563',
61
+ closeButtonColor: '#111827',
62
+ primary: '#000000',
63
+ surface: '#FFFFFF',
64
+ success: '#10B981',
65
+ error: '#EF4444',
66
+ border: '#E5E7EB',
67
+ divider: '#E5E7EB',
68
+ appBarBackground: '#FFFFFF',
69
+ };
70
+ }
71
+
72
+ public static dark(customColors?: Partial<FeeddoColors>): FeeddoTheme {
73
+ return new FeeddoTheme({
74
+ isDark: true,
75
+ colors: customColors ?? {},
76
+ });
77
+ }
78
+
79
+ public static light(customColors?: Partial<FeeddoColors>): FeeddoTheme {
80
+ return new FeeddoTheme({
81
+ isDark: false,
82
+ colors: customColors ?? {},
83
+ });
84
+ }
85
+ }