@baseportal/chat-widget 0.1.0 → 0.1.2
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/dist/index.cjs.js +56 -8
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +57 -9
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +6 -6
- package/dist/index.iife.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -79,6 +79,9 @@ var ApiClient = class {
|
|
|
79
79
|
channelToken: this.channelToken
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
|
+
async getConversation(conversationId) {
|
|
83
|
+
return this.request("GET", `/conversations/${conversationId}`);
|
|
84
|
+
}
|
|
82
85
|
async getMessages(conversationId, params) {
|
|
83
86
|
const qs = new URLSearchParams();
|
|
84
87
|
if (params?.limit) qs.set("limit", String(params.limit));
|
|
@@ -746,6 +749,8 @@ function ChatWindow({
|
|
|
746
749
|
const [uploading, setUploading] = (0, import_hooks5.useState)(false);
|
|
747
750
|
const isOpen = conversation?.open !== false;
|
|
748
751
|
const allowViewHistory = channelInfo.config.allowViewHistory && isAuthenticated;
|
|
752
|
+
const startNewConversationRef = (0, import_hooks5.useRef)(async () => {
|
|
753
|
+
});
|
|
749
754
|
(0, import_hooks5.useEffect)(() => {
|
|
750
755
|
const init = async () => {
|
|
751
756
|
setLoading(true);
|
|
@@ -773,11 +778,21 @@ function ChatWindow({
|
|
|
773
778
|
const storedId = initialConversationId || storage.getConversationId();
|
|
774
779
|
if (storedId) {
|
|
775
780
|
try {
|
|
776
|
-
const
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
+
const conv = await apiClient.getConversation(storedId);
|
|
782
|
+
if (!conv.open) {
|
|
783
|
+
storage.clear();
|
|
784
|
+
if (needsPreChat()) {
|
|
785
|
+
setView("prechat");
|
|
786
|
+
} else {
|
|
787
|
+
await startNewConversation();
|
|
788
|
+
}
|
|
789
|
+
} else {
|
|
790
|
+
const msgs = await apiClient.getMessages(storedId, { limit: 50 });
|
|
791
|
+
setMessages(Array.isArray(msgs) ? msgs.reverse() : []);
|
|
792
|
+
setConversation(conv);
|
|
793
|
+
setView("chat");
|
|
794
|
+
connectRealtime(storedId);
|
|
795
|
+
}
|
|
781
796
|
} catch {
|
|
782
797
|
storage.clear();
|
|
783
798
|
if (needsPreChat()) {
|
|
@@ -832,11 +847,26 @@ function ChatWindow({
|
|
|
832
847
|
);
|
|
833
848
|
if (!conv.open) {
|
|
834
849
|
events.emit("conversation:closed", conv);
|
|
850
|
+
setTimeout(() => {
|
|
851
|
+
realtimeClient.unsubscribe();
|
|
852
|
+
storage.clear();
|
|
853
|
+
setConversation(null);
|
|
854
|
+
setMessages([]);
|
|
855
|
+
if (allowViewHistory) {
|
|
856
|
+
apiClient.getVisitorConversations().then(setConversations).catch(() => {
|
|
857
|
+
});
|
|
858
|
+
setView("conversations");
|
|
859
|
+
} else if (needsPreChat()) {
|
|
860
|
+
setView("prechat");
|
|
861
|
+
} else {
|
|
862
|
+
startNewConversationRef.current();
|
|
863
|
+
}
|
|
864
|
+
}, 2e3);
|
|
835
865
|
}
|
|
836
866
|
}
|
|
837
867
|
});
|
|
838
868
|
},
|
|
839
|
-
[realtimeClient, events]
|
|
869
|
+
[realtimeClient, events, storage, allowViewHistory, apiClient, needsPreChat]
|
|
840
870
|
);
|
|
841
871
|
const openConversation = (0, import_hooks5.useCallback)(
|
|
842
872
|
async (conv) => {
|
|
@@ -875,6 +905,7 @@ function ChatWindow({
|
|
|
875
905
|
setLoading(false);
|
|
876
906
|
}
|
|
877
907
|
}, [apiClient, visitor, storage, connectRealtime, events]);
|
|
908
|
+
startNewConversationRef.current = startNewConversation;
|
|
878
909
|
const handlePreChatSubmit = (0, import_hooks5.useCallback)(
|
|
879
910
|
async (data) => {
|
|
880
911
|
storage.setVisitor({ ...visitor, ...data });
|
|
@@ -944,11 +975,28 @@ function ChatWindow({
|
|
|
944
975
|
} catch (e) {
|
|
945
976
|
console.error("[BaseportalChat] Error sending message:", e);
|
|
946
977
|
setMessages((prev) => prev.filter((m) => m.id !== tempId));
|
|
947
|
-
|
|
978
|
+
const errMsg = e instanceof Error ? e.message : "";
|
|
979
|
+
if (errMsg.includes("Row not found") || errMsg.includes("404")) {
|
|
980
|
+
realtimeClient.unsubscribe();
|
|
981
|
+
storage.clear();
|
|
982
|
+
setConversation(null);
|
|
983
|
+
setMessages([]);
|
|
984
|
+
if (allowViewHistory) {
|
|
985
|
+
apiClient.getVisitorConversations().then(setConversations).catch(() => {
|
|
986
|
+
});
|
|
987
|
+
setView("conversations");
|
|
988
|
+
} else if (needsPreChat()) {
|
|
989
|
+
setView("prechat");
|
|
990
|
+
} else {
|
|
991
|
+
await startNewConversationRef.current();
|
|
992
|
+
}
|
|
993
|
+
} else {
|
|
994
|
+
setInputValue(content);
|
|
995
|
+
}
|
|
948
996
|
} finally {
|
|
949
997
|
setSending(false);
|
|
950
998
|
}
|
|
951
|
-
}, [inputValue, uploadedFileId, conversation, sending, apiClient, events]);
|
|
999
|
+
}, [inputValue, uploadedFileId, conversation, sending, apiClient, events, realtimeClient, storage, allowViewHistory, needsPreChat]);
|
|
952
1000
|
const handleReopen = (0, import_hooks5.useCallback)(async () => {
|
|
953
1001
|
if (!conversation) return;
|
|
954
1002
|
try {
|