|
| 1 | +import { createRoot } from "react-dom/client"; |
1 | 2 | import Sprite from "app/components/SpriteIcon/Sprite"; |
2 | 3 | import PublicChat from "app/components/publicChat/PublicChat"; |
3 | | -import ReactDOM from "react-dom"; |
4 | 4 |
|
5 | | -const renderComponent = () => { |
| 5 | +function getOrCreateUserId(): string { |
| 6 | + let userId = localStorage.getItem("chat-user-id"); |
| 7 | + if (!userId) { |
| 8 | + userId = Date.now().toString(); |
| 9 | + localStorage.setItem("chat-user-id", userId); |
| 10 | + } |
| 11 | + return userId; |
| 12 | +} |
| 13 | + |
| 14 | +function renderChat() { |
6 | 15 | const container = document.getElementById("support-ai-chat-place"); |
7 | | - if (container) { |
8 | | - const shopName = container?.getAttribute("data-shopName"); |
9 | | - const position = container.getAttribute("data-position"); |
10 | | - const localChatId = localStorage.getItem("supportAiChatId"); |
| 16 | + if (!container) return; |
11 | 17 |
|
12 | | - let userId = localStorage.getItem("chat-user-id"); |
13 | | - if (!userId) { |
14 | | - userId = Date.now().toString(); |
15 | | - localStorage.setItem("chat-user-id", userId); |
16 | | - } |
| 18 | + const shopName = container.getAttribute("data-shopName") ?? ""; |
| 19 | + const position = container.getAttribute("data-position") ?? "right"; |
| 20 | + const localChatId = localStorage.getItem("supportAiChatId"); |
| 21 | + const userId = getOrCreateUserId(); |
17 | 22 |
|
18 | | - ReactDOM.render( |
19 | | - <> |
20 | | - <div |
21 | | - style={{ |
22 | | - width: 0, |
23 | | - height: 0, |
24 | | - overflow: "hidden", |
25 | | - visibility: "hidden", |
26 | | - }} |
27 | | - > |
28 | | - <Sprite /> |
29 | | - </div> |
30 | | - <PublicChat |
31 | | - shopName={shopName} |
32 | | - userId={userId} |
33 | | - position={position || "right"} |
34 | | - chatId={ |
35 | | - localChatId && localChatId !== "undefined" ? localChatId : null |
36 | | - } |
37 | | - /> |
38 | | - </>, |
39 | | - container, |
40 | | - ); |
41 | | - } |
42 | | -}; |
| 23 | + const chatId = |
| 24 | + localChatId && localChatId !== "undefined" ? localChatId : null; |
| 25 | + |
| 26 | + const root = createRoot(container); |
| 27 | + root.render( |
| 28 | + <> |
| 29 | + <div |
| 30 | + style={{ |
| 31 | + width: 0, |
| 32 | + height: 0, |
| 33 | + overflow: "hidden", |
| 34 | + visibility: "hidden", |
| 35 | + }} |
| 36 | + > |
| 37 | + <Sprite /> |
| 38 | + </div> |
| 39 | + <PublicChat |
| 40 | + shopName={shopName} |
| 41 | + userId={userId} |
| 42 | + position={position} |
| 43 | + chatId={chatId} |
| 44 | + /> |
| 45 | + </>, |
| 46 | + ); |
| 47 | +} |
43 | 48 |
|
44 | 49 | if (document.readyState === "loading") { |
45 | | - document.addEventListener("DOMContentLoaded", renderComponent); |
| 50 | + document.addEventListener("DOMContentLoaded", renderChat); |
46 | 51 | } else { |
47 | | - renderComponent(); |
| 52 | + renderChat(); |
48 | 53 | } |
0 commit comments