@baseportal/chat-widget 0.1.1 → 0.1.3

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.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));
@@ -162,6 +165,7 @@ var pt = {
162
165
  placeholder: "Digite uma mensagem...",
163
166
  closed: "Esta conversa foi encerrada.",
164
167
  reopen: "Reabrir conversa",
168
+ newConversation: "Nova conversa",
165
169
  attachFile: "Anexar arquivo",
166
170
  uploading: "Enviando...",
167
171
  fileTooLarge: "Arquivo muito grande (m\xE1x. 25MB)",
@@ -193,6 +197,7 @@ var en = {
193
197
  placeholder: "Type a message...",
194
198
  closed: "This conversation has been closed.",
195
199
  reopen: "Reopen conversation",
200
+ newConversation: "New conversation",
196
201
  attachFile: "Attach file",
197
202
  uploading: "Uploading...",
198
203
  fileTooLarge: "File too large (max 25MB)",
@@ -224,6 +229,7 @@ var es = {
224
229
  placeholder: "Escribe un mensaje...",
225
230
  closed: "Esta conversaci\xF3n ha sido cerrada.",
226
231
  reopen: "Reabrir conversaci\xF3n",
232
+ newConversation: "Nueva conversaci\xF3n",
227
233
  attachFile: "Adjuntar archivo",
228
234
  uploading: "Subiendo...",
229
235
  fileTooLarge: "Archivo demasiado grande (m\xE1x. 25MB)",
@@ -738,11 +744,14 @@ function ChatWindow({
738
744
  const storedId = initialConversationId || storage.getConversationId();
739
745
  if (storedId) {
740
746
  try {
747
+ const conv = await apiClient.getConversation(storedId);
741
748
  const msgs = await apiClient.getMessages(storedId, { limit: 50 });
742
749
  setMessages(Array.isArray(msgs) ? msgs.reverse() : []);
743
- setConversation({ id: storedId, open: true });
750
+ setConversation(conv);
744
751
  setView("chat");
745
- connectRealtime(storedId);
752
+ if (conv.open) {
753
+ connectRealtime(storedId);
754
+ }
746
755
  } catch {
747
756
  storage.clear();
748
757
  if (needsPreChat()) {
@@ -797,21 +806,6 @@ function ChatWindow({
797
806
  );
798
807
  if (!conv.open) {
799
808
  events.emit("conversation:closed", conv);
800
- setTimeout(() => {
801
- realtimeClient.unsubscribe();
802
- storage.clear();
803
- setConversation(null);
804
- setMessages([]);
805
- if (allowViewHistory) {
806
- apiClient.getVisitorConversations().then(setConversations).catch(() => {
807
- });
808
- setView("conversations");
809
- } else if (needsPreChat()) {
810
- setView("prechat");
811
- } else {
812
- startNewConversationRef.current();
813
- }
814
- }, 2e3);
815
809
  }
816
810
  }
817
811
  });
@@ -947,6 +941,21 @@ function ChatWindow({
947
941
  setSending(false);
948
942
  }
949
943
  }, [inputValue, uploadedFileId, conversation, sending, apiClient, events, realtimeClient, storage, allowViewHistory, needsPreChat]);
944
+ const handleNewConversation = useCallback3(async () => {
945
+ realtimeClient.unsubscribe();
946
+ storage.clear();
947
+ setConversation(null);
948
+ setMessages([]);
949
+ if (allowViewHistory) {
950
+ apiClient.getVisitorConversations().then(setConversations).catch(() => {
951
+ });
952
+ setView("conversations");
953
+ } else if (needsPreChat()) {
954
+ setView("prechat");
955
+ } else {
956
+ await startNewConversationRef.current();
957
+ }
958
+ }, [realtimeClient, storage, allowViewHistory, apiClient, needsPreChat]);
950
959
  const handleReopen = useCallback3(async () => {
951
960
  if (!conversation) return;
952
961
  try {
@@ -1018,7 +1027,7 @@ function ChatWindow({
1018
1027
  }
1019
1028
  ) : /* @__PURE__ */ jsxs9("div", { class: "bp-closed-banner", children: [
1020
1029
  /* @__PURE__ */ jsx9("span", { class: "bp-closed-banner__text", children: t.chat.closed }),
1021
- channelInfo.config.allowReopenConversation && /* @__PURE__ */ jsx9("button", { class: "bp-closed-banner__reopen", onClick: handleReopen, children: t.chat.reopen })
1030
+ channelInfo.config.allowReopenConversation ? /* @__PURE__ */ jsx9("button", { class: "bp-closed-banner__reopen", onClick: handleReopen, children: t.chat.reopen }) : /* @__PURE__ */ jsx9("button", { class: "bp-closed-banner__reopen", onClick: handleNewConversation, children: t.chat.newConversation })
1022
1031
  ] }),
1023
1032
  channelInfo.config.privacyPolicyUrl && /* @__PURE__ */ jsx9("div", { class: "bp-privacy-footer", children: /* @__PURE__ */ jsx9(
1024
1033
  "a",