ss-support-widget 1.0.2 → 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/dist/chat-bot-widget.js +33 -33
- package/index.html +3 -3
- package/package.json +1 -1
- package/src/App.tsx +21 -9
- package/src/element.tsx +13 -7
- package/src/service.ts +14 -0
- package/src/session-storage.ts +10 -0
package/index.html
CHANGED
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
<body>
|
|
8
8
|
<script>
|
|
9
9
|
window.ChatBotConfig = {
|
|
10
|
-
clientId: "
|
|
10
|
+
clientId: "694802ab424bd274310d61d8",
|
|
11
11
|
apiBaseUrl: "https://ai-chatbots-api.azurewebsites.net/",
|
|
12
12
|
};
|
|
13
13
|
</script>
|
|
14
|
-
<script type="module" src="/src/element.tsx"></script>
|
|
15
|
-
|
|
14
|
+
<!-- <script type="module" src="/src/element.tsx"></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
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 || "",
|
|
@@ -81,14 +91,16 @@ export default function App({ config }: { config: Config }) {
|
|
|
81
91
|
}
|
|
82
92
|
|
|
83
93
|
return (
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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,7 +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 } from "./service";
|
|
8
|
+
import { getApiStatus, getHideChatForUrls } from "./service";
|
|
9
9
|
|
|
10
10
|
export type Config = {
|
|
11
11
|
clientId: string;
|
|
@@ -13,8 +13,12 @@ export type Config = {
|
|
|
13
13
|
threadId?: string;
|
|
14
14
|
token?: string;
|
|
15
15
|
userId?: string;
|
|
16
|
+
hideChatForUrls?: string[];
|
|
16
17
|
};
|
|
17
18
|
|
|
19
|
+
const cfg = (window as any).ChatBotConfig;
|
|
20
|
+
const chatBotComponentName = `chat-bot-${cfg.clientId}`;
|
|
21
|
+
|
|
18
22
|
class ChatBotElement extends HTMLElement {
|
|
19
23
|
private root?: Root;
|
|
20
24
|
private mountEl?: HTMLDivElement;
|
|
@@ -69,24 +73,26 @@ class ChatBotElement extends HTMLElement {
|
|
|
69
73
|
const token = this.getAttribute("token") || raw?.token || undefined;
|
|
70
74
|
const userId = this.getAttribute("user-id") || raw?.userId || undefined;
|
|
71
75
|
|
|
76
|
+
const hideChatForUrls = raw?.hideChatForUrls || []
|
|
77
|
+
|
|
72
78
|
if (!clientId || !apiBaseUrl) return null;
|
|
73
|
-
return { clientId, apiBaseUrl, threadId, token, userId };
|
|
79
|
+
return { clientId, apiBaseUrl, threadId, token, userId, hideChatForUrls };
|
|
74
80
|
}
|
|
75
81
|
}
|
|
76
82
|
|
|
77
|
-
if (!customElements.get(
|
|
78
|
-
customElements.define(
|
|
83
|
+
if (!customElements.get(chatBotComponentName)) {
|
|
84
|
+
customElements.define(chatBotComponentName, ChatBotElement);
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
(async function autoMount() {
|
|
82
|
-
const cfg = (window as any).ChatBotConfig;
|
|
83
88
|
if (!cfg) return;
|
|
84
|
-
if (document.querySelector(
|
|
89
|
+
if (document.querySelector(chatBotComponentName)) return;
|
|
85
90
|
|
|
86
91
|
var isHealty = await getApiStatus(cfg)
|
|
87
92
|
|
|
88
93
|
if(!isHealty) return;
|
|
89
94
|
|
|
90
|
-
|
|
95
|
+
cfg.hideChatForUrls = await getHideChatForUrls(cfg)
|
|
96
|
+
const el = document.createElement(chatBotComponentName);
|
|
91
97
|
document.body.appendChild(el);
|
|
92
98
|
})();
|
package/src/service.ts
CHANGED
|
@@ -36,3 +36,17 @@ export async function getApiStatus(config: Config): Promise<boolean> {
|
|
|
36
36
|
|
|
37
37
|
return res.ok;
|
|
38
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
|
+
}
|
package/src/session-storage.ts
CHANGED
|
@@ -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
|
+
// }
|