@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.esm.js
CHANGED
|
@@ -42,6 +42,9 @@ var ApiClient = class {
|
|
|
42
42
|
channelToken: this.channelToken
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
|
+
async getConversation(conversationId) {
|
|
46
|
+
return this.request("GET", `/conversations/${conversationId}`);
|
|
47
|
+
}
|
|
45
48
|
async getMessages(conversationId, params) {
|
|
46
49
|
const qs = new URLSearchParams();
|
|
47
50
|
if (params?.limit) qs.set("limit", String(params.limit));
|
|
@@ -303,7 +306,7 @@ function ChatBubble({
|
|
|
303
306
|
}
|
|
304
307
|
|
|
305
308
|
// src/ui/components/ChatWindow.tsx
|
|
306
|
-
import { useCallback as useCallback3, useEffect as useEffect3, useState as useState3 } from "preact/hooks";
|
|
309
|
+
import { useCallback as useCallback3, useEffect as useEffect3, useRef as useRef3, useState as useState3 } from "preact/hooks";
|
|
307
310
|
|
|
308
311
|
// src/ui/components/ConversationList.tsx
|
|
309
312
|
import { jsx as jsx3, jsxs as jsxs3 } from "preact/jsx-runtime";
|
|
@@ -709,6 +712,8 @@ function ChatWindow({
|
|
|
709
712
|
const [uploading, setUploading] = useState3(false);
|
|
710
713
|
const isOpen = conversation?.open !== false;
|
|
711
714
|
const allowViewHistory = channelInfo.config.allowViewHistory && isAuthenticated;
|
|
715
|
+
const startNewConversationRef = useRef3(async () => {
|
|
716
|
+
});
|
|
712
717
|
useEffect3(() => {
|
|
713
718
|
const init = async () => {
|
|
714
719
|
setLoading(true);
|
|
@@ -736,11 +741,21 @@ function ChatWindow({
|
|
|
736
741
|
const storedId = initialConversationId || storage.getConversationId();
|
|
737
742
|
if (storedId) {
|
|
738
743
|
try {
|
|
739
|
-
const
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
+
const conv = await apiClient.getConversation(storedId);
|
|
745
|
+
if (!conv.open) {
|
|
746
|
+
storage.clear();
|
|
747
|
+
if (needsPreChat()) {
|
|
748
|
+
setView("prechat");
|
|
749
|
+
} else {
|
|
750
|
+
await startNewConversation();
|
|
751
|
+
}
|
|
752
|
+
} else {
|
|
753
|
+
const msgs = await apiClient.getMessages(storedId, { limit: 50 });
|
|
754
|
+
setMessages(Array.isArray(msgs) ? msgs.reverse() : []);
|
|
755
|
+
setConversation(conv);
|
|
756
|
+
setView("chat");
|
|
757
|
+
connectRealtime(storedId);
|
|
758
|
+
}
|
|
744
759
|
} catch {
|
|
745
760
|
storage.clear();
|
|
746
761
|
if (needsPreChat()) {
|
|
@@ -795,11 +810,26 @@ function ChatWindow({
|
|
|
795
810
|
);
|
|
796
811
|
if (!conv.open) {
|
|
797
812
|
events.emit("conversation:closed", conv);
|
|
813
|
+
setTimeout(() => {
|
|
814
|
+
realtimeClient.unsubscribe();
|
|
815
|
+
storage.clear();
|
|
816
|
+
setConversation(null);
|
|
817
|
+
setMessages([]);
|
|
818
|
+
if (allowViewHistory) {
|
|
819
|
+
apiClient.getVisitorConversations().then(setConversations).catch(() => {
|
|
820
|
+
});
|
|
821
|
+
setView("conversations");
|
|
822
|
+
} else if (needsPreChat()) {
|
|
823
|
+
setView("prechat");
|
|
824
|
+
} else {
|
|
825
|
+
startNewConversationRef.current();
|
|
826
|
+
}
|
|
827
|
+
}, 2e3);
|
|
798
828
|
}
|
|
799
829
|
}
|
|
800
830
|
});
|
|
801
831
|
},
|
|
802
|
-
[realtimeClient, events]
|
|
832
|
+
[realtimeClient, events, storage, allowViewHistory, apiClient, needsPreChat]
|
|
803
833
|
);
|
|
804
834
|
const openConversation = useCallback3(
|
|
805
835
|
async (conv) => {
|
|
@@ -838,6 +868,7 @@ function ChatWindow({
|
|
|
838
868
|
setLoading(false);
|
|
839
869
|
}
|
|
840
870
|
}, [apiClient, visitor, storage, connectRealtime, events]);
|
|
871
|
+
startNewConversationRef.current = startNewConversation;
|
|
841
872
|
const handlePreChatSubmit = useCallback3(
|
|
842
873
|
async (data) => {
|
|
843
874
|
storage.setVisitor({ ...visitor, ...data });
|
|
@@ -907,11 +938,28 @@ function ChatWindow({
|
|
|
907
938
|
} catch (e) {
|
|
908
939
|
console.error("[BaseportalChat] Error sending message:", e);
|
|
909
940
|
setMessages((prev) => prev.filter((m) => m.id !== tempId));
|
|
910
|
-
|
|
941
|
+
const errMsg = e instanceof Error ? e.message : "";
|
|
942
|
+
if (errMsg.includes("Row not found") || errMsg.includes("404")) {
|
|
943
|
+
realtimeClient.unsubscribe();
|
|
944
|
+
storage.clear();
|
|
945
|
+
setConversation(null);
|
|
946
|
+
setMessages([]);
|
|
947
|
+
if (allowViewHistory) {
|
|
948
|
+
apiClient.getVisitorConversations().then(setConversations).catch(() => {
|
|
949
|
+
});
|
|
950
|
+
setView("conversations");
|
|
951
|
+
} else if (needsPreChat()) {
|
|
952
|
+
setView("prechat");
|
|
953
|
+
} else {
|
|
954
|
+
await startNewConversationRef.current();
|
|
955
|
+
}
|
|
956
|
+
} else {
|
|
957
|
+
setInputValue(content);
|
|
958
|
+
}
|
|
911
959
|
} finally {
|
|
912
960
|
setSending(false);
|
|
913
961
|
}
|
|
914
|
-
}, [inputValue, uploadedFileId, conversation, sending, apiClient, events]);
|
|
962
|
+
}, [inputValue, uploadedFileId, conversation, sending, apiClient, events, realtimeClient, storage, allowViewHistory, needsPreChat]);
|
|
915
963
|
const handleReopen = useCallback3(async () => {
|
|
916
964
|
if (!conversation) return;
|
|
917
965
|
try {
|