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.
- package/LICENSE +21 -0
- package/README.md +401 -2
- package/android/build.gradle +39 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/dev/feeddo/reactnative/FeeddoReactNativeModule.java +380 -0
- package/android/src/main/java/dev/feeddo/reactnative/FeeddoReactNativePackage.java +25 -0
- package/dist/Feeddo.d.ts +131 -0
- package/dist/Feeddo.d.ts.map +1 -0
- package/dist/Feeddo.js +176 -0
- package/dist/Feeddo.js.map +1 -0
- package/dist/FeeddoClient.d.ts +151 -0
- package/dist/FeeddoClient.d.ts.map +1 -0
- package/dist/FeeddoClient.js +690 -0
- package/dist/FeeddoClient.js.map +1 -0
- package/dist/FeeddoProvider.d.ts +23 -0
- package/dist/FeeddoProvider.d.ts.map +1 -0
- package/dist/FeeddoProvider.js +76 -0
- package/dist/FeeddoProvider.js.map +1 -0
- package/dist/config/FeeddoConfig.d.ts +10 -0
- package/dist/config/FeeddoConfig.d.ts.map +1 -0
- package/dist/config/FeeddoConfig.js +10 -0
- package/dist/config/FeeddoConfig.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/models/Conversation.d.ts +27 -0
- package/dist/models/Conversation.d.ts.map +1 -0
- package/dist/models/Conversation.js +24 -0
- package/dist/models/Conversation.js.map +1 -0
- package/dist/models/EndUser.d.ts +49 -0
- package/dist/models/EndUser.d.ts.map +1 -0
- package/dist/models/EndUser.js +2 -0
- package/dist/models/EndUser.js.map +1 -0
- package/dist/models/FeeddoNotification.d.ts +15 -0
- package/dist/models/FeeddoNotification.d.ts.map +1 -0
- package/dist/models/FeeddoNotification.js +15 -0
- package/dist/models/FeeddoNotification.js.map +1 -0
- package/dist/models/HomeData.d.ts +16 -0
- package/dist/models/HomeData.d.ts.map +1 -0
- package/dist/models/HomeData.js +19 -0
- package/dist/models/HomeData.js.map +1 -0
- package/dist/models/Message.d.ts +12 -0
- package/dist/models/Message.d.ts.map +1 -0
- package/dist/models/Message.js +25 -0
- package/dist/models/Message.js.map +1 -0
- package/dist/models/PushProvider.d.ts +14 -0
- package/dist/models/PushProvider.d.ts.map +1 -0
- package/dist/models/PushProvider.js +9 -0
- package/dist/models/PushProvider.js.map +1 -0
- package/dist/models/Task.d.ts +17 -0
- package/dist/models/Task.d.ts.map +1 -0
- package/dist/models/Task.js +22 -0
- package/dist/models/Task.js.map +1 -0
- package/dist/models/TaskComment.d.ts +11 -0
- package/dist/models/TaskComment.d.ts.map +1 -0
- package/dist/models/TaskComment.js +27 -0
- package/dist/models/TaskComment.js.map +1 -0
- package/dist/models/Ticket.d.ts +13 -0
- package/dist/models/Ticket.d.ts.map +1 -0
- package/dist/models/Ticket.js +14 -0
- package/dist/models/Ticket.js.map +1 -0
- package/dist/native/FeeddoNative.d.ts +58 -0
- package/dist/native/FeeddoNative.d.ts.map +1 -0
- package/dist/native/FeeddoNative.js +22 -0
- package/dist/native/FeeddoNative.js.map +1 -0
- package/dist/push/FeeddoPushNotificationApns.d.ts +73 -0
- package/dist/push/FeeddoPushNotificationApns.d.ts.map +1 -0
- package/dist/push/FeeddoPushNotificationApns.js +301 -0
- package/dist/push/FeeddoPushNotificationApns.js.map +1 -0
- package/dist/services/ApiService.d.ts +64 -0
- package/dist/services/ApiService.d.ts.map +1 -0
- package/dist/services/ApiService.js +406 -0
- package/dist/services/ApiService.js.map +1 -0
- package/dist/services/ConversationService.d.ts +45 -0
- package/dist/services/ConversationService.d.ts.map +1 -0
- package/dist/services/ConversationService.js +259 -0
- package/dist/services/ConversationService.js.map +1 -0
- package/dist/services/WebSocketService.d.ts +26 -0
- package/dist/services/WebSocketService.d.ts.map +1 -0
- package/dist/services/WebSocketService.js +157 -0
- package/dist/services/WebSocketService.js.map +1 -0
- package/dist/theme/FeeddoTheme.d.ts +31 -0
- package/dist/theme/FeeddoTheme.d.ts.map +1 -0
- package/dist/theme/FeeddoTheme.js +60 -0
- package/dist/theme/FeeddoTheme.js.map +1 -0
- package/dist/ui/FeeddoFeedbackCard.d.ts +15 -0
- package/dist/ui/FeeddoFeedbackCard.d.ts.map +1 -0
- package/dist/ui/FeeddoFeedbackCard.js +188 -0
- package/dist/ui/FeeddoFeedbackCard.js.map +1 -0
- package/dist/ui/FeeddoModal.d.ts +20 -0
- package/dist/ui/FeeddoModal.d.ts.map +1 -0
- package/dist/ui/FeeddoModal.js +174 -0
- package/dist/ui/FeeddoModal.js.map +1 -0
- package/dist/ui/components/AttachmentPreview.d.ts +10 -0
- package/dist/ui/components/AttachmentPreview.d.ts.map +1 -0
- package/dist/ui/components/AttachmentPreview.js +143 -0
- package/dist/ui/components/AttachmentPreview.js.map +1 -0
- package/dist/ui/components/ChatInputArea.d.ts +16 -0
- package/dist/ui/components/ChatInputArea.d.ts.map +1 -0
- package/dist/ui/components/ChatInputArea.js +85 -0
- package/dist/ui/components/ChatInputArea.js.map +1 -0
- package/dist/ui/components/FeeddoIcon.d.ts +10 -0
- package/dist/ui/components/FeeddoIcon.d.ts.map +1 -0
- package/dist/ui/components/FeeddoIcon.js +690 -0
- package/dist/ui/components/FeeddoIcon.js.map +1 -0
- package/dist/ui/components/FeeddoNotificationBadge.d.ts +14 -0
- package/dist/ui/components/FeeddoNotificationBadge.d.ts.map +1 -0
- package/dist/ui/components/FeeddoNotificationBadge.js +171 -0
- package/dist/ui/components/FeeddoNotificationBadge.js.map +1 -0
- package/dist/ui/components/MessageBubble.d.ts +13 -0
- package/dist/ui/components/MessageBubble.d.ts.map +1 -0
- package/dist/ui/components/MessageBubble.js +163 -0
- package/dist/ui/components/MessageBubble.js.map +1 -0
- package/dist/ui/components/NotificationsSheet.d.ts +10 -0
- package/dist/ui/components/NotificationsSheet.d.ts.map +1 -0
- package/dist/ui/components/NotificationsSheet.js +252 -0
- package/dist/ui/components/NotificationsSheet.js.map +1 -0
- package/dist/ui/components/RatingWidget.d.ts +11 -0
- package/dist/ui/components/RatingWidget.d.ts.map +1 -0
- package/dist/ui/components/RatingWidget.js +34 -0
- package/dist/ui/components/RatingWidget.js.map +1 -0
- package/dist/ui/components/TaskCard.d.ts +12 -0
- package/dist/ui/components/TaskCard.d.ts.map +1 -0
- package/dist/ui/components/TaskCard.js +210 -0
- package/dist/ui/components/TaskCard.js.map +1 -0
- package/dist/ui/components/TaskDetailsSheet.d.ts +15 -0
- package/dist/ui/components/TaskDetailsSheet.d.ts.map +1 -0
- package/dist/ui/components/TaskDetailsSheet.js +577 -0
- package/dist/ui/components/TaskDetailsSheet.js.map +1 -0
- package/dist/ui/components/TicketCard.d.ts +11 -0
- package/dist/ui/components/TicketCard.d.ts.map +1 -0
- package/dist/ui/components/TicketCard.js +100 -0
- package/dist/ui/components/TicketCard.js.map +1 -0
- package/dist/ui/components/TicketDetailsSheet.d.ts +14 -0
- package/dist/ui/components/TicketDetailsSheet.d.ts.map +1 -0
- package/dist/ui/components/TicketDetailsSheet.js +254 -0
- package/dist/ui/components/TicketDetailsSheet.js.map +1 -0
- package/dist/ui/components/TypingIndicator.d.ts +10 -0
- package/dist/ui/components/TypingIndicator.d.ts.map +1 -0
- package/dist/ui/components/TypingIndicator.js +77 -0
- package/dist/ui/components/TypingIndicator.js.map +1 -0
- package/dist/ui/screens/FeeddoChatScreen.d.ts +13 -0
- package/dist/ui/screens/FeeddoChatScreen.d.ts.map +1 -0
- package/dist/ui/screens/FeeddoChatScreen.js +681 -0
- package/dist/ui/screens/FeeddoChatScreen.js.map +1 -0
- package/dist/ui/screens/FeeddoHomeScreen.d.ts +15 -0
- package/dist/ui/screens/FeeddoHomeScreen.d.ts.map +1 -0
- package/dist/ui/screens/FeeddoHomeScreen.js +653 -0
- package/dist/ui/screens/FeeddoHomeScreen.js.map +1 -0
- package/dist/ui/screens/FeeddoMessagesScreen.d.ts +12 -0
- package/dist/ui/screens/FeeddoMessagesScreen.d.ts.map +1 -0
- package/dist/ui/screens/FeeddoMessagesScreen.js +326 -0
- package/dist/ui/screens/FeeddoMessagesScreen.js.map +1 -0
- package/dist/ui/screens/FeeddoTasksScreen.d.ts +13 -0
- package/dist/ui/screens/FeeddoTasksScreen.d.ts.map +1 -0
- package/dist/ui/screens/FeeddoTasksScreen.js +517 -0
- package/dist/ui/screens/FeeddoTasksScreen.js.map +1 -0
- package/dist/ui/screens/FeeddoTicketsScreen.d.ts +10 -0
- package/dist/ui/screens/FeeddoTicketsScreen.d.ts.map +1 -0
- package/dist/ui/screens/FeeddoTicketsScreen.js +112 -0
- package/dist/ui/screens/FeeddoTicketsScreen.js.map +1 -0
- package/dist/utils/DeviceInfoHelper.d.ts +29 -0
- package/dist/utils/DeviceInfoHelper.d.ts.map +1 -0
- package/dist/utils/DeviceInfoHelper.js +190 -0
- package/dist/utils/DeviceInfoHelper.js.map +1 -0
- package/dist/utils/MediaHelper.d.ts +20 -0
- package/dist/utils/MediaHelper.d.ts.map +1 -0
- package/dist/utils/MediaHelper.js +92 -0
- package/dist/utils/MediaHelper.js.map +1 -0
- package/dist/utils/NotificationSupport.d.ts +22 -0
- package/dist/utils/NotificationSupport.d.ts.map +1 -0
- package/dist/utils/NotificationSupport.js +211 -0
- package/dist/utils/NotificationSupport.js.map +1 -0
- package/dist/utils/StorageHelper.d.ts +65 -0
- package/dist/utils/StorageHelper.d.ts.map +1 -0
- package/dist/utils/StorageHelper.js +273 -0
- package/dist/utils/StorageHelper.js.map +1 -0
- package/feeddo-react-native.podspec +26 -0
- package/ios/FeeddoReactNativeModule.h +8 -0
- package/ios/FeeddoReactNativeModule.m +831 -0
- package/package.json +56 -4
- package/src/Feeddo.ts +247 -0
- package/src/FeeddoClient.ts +895 -0
- package/src/FeeddoProvider.tsx +139 -0
- package/src/config/FeeddoConfig.ts +9 -0
- package/src/index.ts +67 -0
- package/src/models/Conversation.ts +50 -0
- package/src/models/EndUser.ts +65 -0
- package/src/models/FeeddoNotification.ts +29 -0
- package/src/models/HomeData.ts +33 -0
- package/src/models/Message.ts +36 -0
- package/src/models/PushProvider.ts +15 -0
- package/src/models/Task.ts +40 -0
- package/src/models/TaskComment.ts +36 -0
- package/src/models/Ticket.ts +25 -0
- package/src/native/FeeddoNative.ts +80 -0
- package/src/push/FeeddoPushNotificationApns.ts +362 -0
- package/src/services/ApiService.ts +456 -0
- package/src/services/ConversationService.ts +305 -0
- package/src/services/WebSocketService.ts +185 -0
- package/src/theme/FeeddoTheme.ts +85 -0
- package/src/ui/FeeddoFeedbackCard.tsx +245 -0
- package/src/ui/FeeddoModal.tsx +303 -0
- package/src/ui/components/AttachmentPreview.tsx +192 -0
- package/src/ui/components/ChatInputArea.tsx +154 -0
- package/src/ui/components/FeeddoIcon.tsx +903 -0
- package/src/ui/components/FeeddoNotificationBadge.tsx +234 -0
- package/src/ui/components/MessageBubble.tsx +223 -0
- package/src/ui/components/NotificationsSheet.tsx +324 -0
- package/src/ui/components/RatingWidget.tsx +64 -0
- package/src/ui/components/TaskCard.tsx +259 -0
- package/src/ui/components/TaskDetailsSheet.tsx +747 -0
- package/src/ui/components/TicketCard.tsx +128 -0
- package/src/ui/components/TicketDetailsSheet.tsx +324 -0
- package/src/ui/components/TypingIndicator.tsx +104 -0
- package/src/ui/screens/FeeddoChatScreen.tsx +877 -0
- package/src/ui/screens/FeeddoHomeScreen.tsx +824 -0
- package/src/ui/screens/FeeddoMessagesScreen.tsx +414 -0
- package/src/ui/screens/FeeddoTasksScreen.tsx +671 -0
- package/src/ui/screens/FeeddoTicketsScreen.tsx +164 -0
- package/src/utils/DeviceInfoHelper.ts +209 -0
- package/src/utils/MediaHelper.ts +105 -0
- package/src/utils/NotificationSupport.ts +233 -0
- 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
|
+
}
|