ss-support-widget 1.0.1 → 1.0.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/index.html CHANGED
@@ -7,11 +7,11 @@
7
7
  <body>
8
8
  <script>
9
9
  window.ChatBotConfig = {
10
- clientId: "6944264ec246421953fae9f7",
11
- apiBaseUrl: "http://apiservice.local",
10
+ clientId: "694802ab424bd274310d61d8",
11
+ apiBaseUrl: "https://ai-chatbots-api.azurewebsites.net/",
12
12
  };
13
13
  </script>
14
14
  <!-- <script type="module" src="/src/element.tsx"></script> -->
15
- <script src="https://cdn.jsdelivr.net/npm/ss-support-widget@1.0.0/dist/chat-bot-widget.js"></script>
15
+ <script src="https://cdn.jsdelivr.net/npm/ss-support-widget@1.0.2/dist/chat-bot-widget.js"></script>
16
16
  </body>
17
17
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ss-support-widget",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Chatbot widget for customer support",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/App.tsx CHANGED
@@ -17,6 +17,7 @@ export default function App({ config }: { config: Config }) {
17
17
  const [threadId, setThreadId] = useState(getThreadId() || config.threadId || "");
18
18
  const [msgs, setMsgs] = useState<Msg[]>([]);
19
19
  const [sending, setSending] = useState(false);
20
+ const [hideChat, setHideChat] = useState(false);
20
21
 
21
22
  const anchorSx = useMemo(
22
23
  () => ({
@@ -29,6 +30,15 @@ export default function App({ config }: { config: Config }) {
29
30
  []
30
31
  );
31
32
 
33
+ useEffect(() => {
34
+ const path = window.location.pathname;
35
+ if (config.hideChatForUrls?.some(u => path.startsWith(u))) {
36
+ setHideChat(true);
37
+ } else {
38
+ setHideChat(false);
39
+ }
40
+ });
41
+
32
42
  useEffect(() => {
33
43
  getHistoryMessages(config,
34
44
  getThreadId() || config.threadId || "",
@@ -62,7 +72,7 @@ export default function App({ config }: { config: Config }) {
62
72
 
63
73
  try {
64
74
  await streamChat({
65
- url: config.apiBaseUrl + "/chat-support",
75
+ url: config.apiBaseUrl + "api/chat-support",
66
76
  token: config.token,
67
77
  clientId: config.clientId,
68
78
  threadId: threadId,
@@ -81,14 +91,16 @@ export default function App({ config }: { config: Config }) {
81
91
  }
82
92
 
83
93
  return (
84
- <ChatWidget
85
- anchorSx={anchorSx}
86
- open={open}
87
- setOpen={setOpen}
88
- msgs={msgs}
89
- input={input}
90
- setInput={setInput}
91
- send={send}
92
- sending={sending} />
94
+ hideChat
95
+ ? <></>
96
+ : <ChatWidget
97
+ anchorSx={anchorSx}
98
+ open={open}
99
+ setOpen={setOpen}
100
+ msgs={msgs}
101
+ input={input}
102
+ setInput={setInput}
103
+ send={send}
104
+ sending={sending} />
93
105
  );
94
106
  }
package/src/element.tsx CHANGED
@@ -5,6 +5,7 @@ import { CacheProvider } from "@emotion/react";
5
5
  import { CssBaseline, ThemeProvider, createTheme } from "@mui/material";
6
6
  import App from "./App";
7
7
  import { ensureViewportMeta } from "./hooks";
8
+ import { getApiStatus, getHideChatForUrls } from "./service";
8
9
 
9
10
  export type Config = {
10
11
  clientId: string;
@@ -12,8 +13,12 @@ export type Config = {
12
13
  threadId?: string;
13
14
  token?: string;
14
15
  userId?: string;
16
+ hideChatForUrls?: string[];
15
17
  };
16
18
 
19
+ const cfg = (window as any).ChatBotConfig;
20
+ const chatBotComponentName = `chat-bot-${cfg.clientId}`;
21
+
17
22
  class ChatBotElement extends HTMLElement {
18
23
  private root?: Root;
19
24
  private mountEl?: HTMLDivElement;
@@ -68,20 +73,26 @@ class ChatBotElement extends HTMLElement {
68
73
  const token = this.getAttribute("token") || raw?.token || undefined;
69
74
  const userId = this.getAttribute("user-id") || raw?.userId || undefined;
70
75
 
76
+ const hideChatForUrls = raw?.hideChatForUrls || []
77
+
71
78
  if (!clientId || !apiBaseUrl) return null;
72
- return { clientId, apiBaseUrl, threadId, token, userId };
79
+ return { clientId, apiBaseUrl, threadId, token, userId, hideChatForUrls };
73
80
  }
74
81
  }
75
82
 
76
- if (!customElements.get("chat-bot")) {
77
- customElements.define("chat-bot", ChatBotElement);
83
+ if (!customElements.get(chatBotComponentName)) {
84
+ customElements.define(chatBotComponentName, ChatBotElement);
78
85
  }
79
86
 
80
- (function autoMount() {
81
- const cfg = (window as any).ChatBotConfig;
87
+ (async function autoMount() {
82
88
  if (!cfg) return;
83
- if (document.querySelector("chat-bot")) return;
89
+ if (document.querySelector(chatBotComponentName)) return;
90
+
91
+ var isHealty = await getApiStatus(cfg)
92
+
93
+ if(!isHealty) return;
84
94
 
85
- const el = document.createElement("chat-bot");
95
+ cfg.hideChatForUrls = await getHideChatForUrls(cfg)
96
+ const el = document.createElement(chatBotComponentName);
86
97
  document.body.appendChild(el);
87
98
  })();
package/src/service.ts CHANGED
@@ -2,7 +2,7 @@ import { Config } from "./element";
2
2
  import { MsgDelta } from "./MsgDelta";
3
3
 
4
4
  export async function getHistoryMessages(config: Config, threadId: string, onSucces: (delta: MsgDelta[]) => void): Promise<void> {
5
- fetch(config.apiBaseUrl + "/chat-support-history", {
5
+ fetch(config.apiBaseUrl + "api/chat-support-history", {
6
6
  method: "GET",
7
7
  headers: {
8
8
  "Content-Type": "application/json",
@@ -22,5 +22,31 @@ export async function getHistoryMessages(config: Config, threadId: string, onSuc
22
22
  .catch(err => {
23
23
  console.error(err);
24
24
  });
25
+ }
26
+
27
+ export async function getApiStatus(config: Config): Promise<boolean> {
25
28
 
29
+ const res = await fetch(config.apiBaseUrl + "api/health", {
30
+ method: "GET",
31
+ headers: {
32
+ "Content-Type": "application/json",
33
+ ...(config.clientId ? { "X-Client-Id": config.clientId } : {}),
34
+ },
35
+ });
36
+
37
+ return res.ok;
26
38
  }
39
+
40
+
41
+ export async function getHideChatForUrls(config: Config): Promise<string[]> {
42
+
43
+ const res = await fetch(config.apiBaseUrl + "api/hide-chat-for", {
44
+ method: "GET",
45
+ headers: {
46
+ "Content-Type": "application/json",
47
+ ...(config.clientId ? { "X-Client-Id": config.clientId } : {}),
48
+ },
49
+ });
50
+
51
+ return res.body ? await res.json() as string[] : [];
52
+ }
@@ -5,3 +5,13 @@ export function saveThreadId(threadId: string) {
5
5
  export function getThreadId() : string | null {
6
6
  return sessionStorage.getItem("threadId");
7
7
  }
8
+
9
+
10
+ // export function saveUrlPaths(urls: string[]) {
11
+ // sessionStorage.setItem("urlPaths", JSON.stringify(urls));
12
+ // }
13
+
14
+ // export function getUrlPaths() : string[] | [] {
15
+ // const item = sessionStorage.getItem("urlPaths");
16
+ // return item ? JSON.parse(item) : [];
17
+ // }