simple-support-chat 0.1.1 → 0.3.0
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/README.md +302 -3
- package/dist/client/index.cjs +194 -25
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +91 -6
- package/dist/client/index.d.ts +91 -6
- package/dist/client/index.js +195 -27
- package/dist/client/index.js.map +1 -1
- package/dist/server/index.cjs +2176 -9
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +189 -1
- package/dist/server/index.d.ts +189 -1
- package/dist/server/index.js +2166 -10
- package/dist/server/index.js.map +1 -1
- package/package.json +87 -85
package/dist/client/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState,
|
|
1
|
+
import { useState, useRef, useCallback, useEffect, useMemo } from 'react';
|
|
2
2
|
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
3
3
|
|
|
4
4
|
// src/client/ChatBubble.tsx
|
|
@@ -48,13 +48,18 @@ function collectAnonymousContext() {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
// src/client/useChatEngine.ts
|
|
51
|
+
var POLL_INTERVAL = 4e3;
|
|
51
52
|
function useChatEngine({
|
|
52
53
|
apiUrl,
|
|
53
|
-
user
|
|
54
|
+
user,
|
|
55
|
+
repliesUrl,
|
|
56
|
+
isOpen = true
|
|
54
57
|
}) {
|
|
55
58
|
const [messages, setMessages] = useState([]);
|
|
56
59
|
const [input, setInput] = useState("");
|
|
57
60
|
const [sending, setSending] = useState(false);
|
|
61
|
+
const lastReplyTimestampRef = useRef(null);
|
|
62
|
+
const knownReplyIdsRef = useRef(/* @__PURE__ */ new Set());
|
|
58
63
|
const sendMessage = useCallback(async () => {
|
|
59
64
|
const text = input.trim();
|
|
60
65
|
if (!text || sending) return;
|
|
@@ -113,6 +118,44 @@ function useChatEngine({
|
|
|
113
118
|
},
|
|
114
119
|
[sendMessage]
|
|
115
120
|
);
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
if (!repliesUrl || !isOpen) return;
|
|
123
|
+
const sessionId = user?.id ?? getSessionId();
|
|
124
|
+
const fetchReplies = async () => {
|
|
125
|
+
try {
|
|
126
|
+
const params = new URLSearchParams({ sessionId });
|
|
127
|
+
if (lastReplyTimestampRef.current) {
|
|
128
|
+
params.set("since", lastReplyTimestampRef.current);
|
|
129
|
+
}
|
|
130
|
+
const response = await fetch(`${repliesUrl}?${params.toString()}`);
|
|
131
|
+
if (!response.ok) return;
|
|
132
|
+
const data = await response.json();
|
|
133
|
+
if (data.replies.length === 0) return;
|
|
134
|
+
const newReplies = data.replies.filter(
|
|
135
|
+
(r) => !knownReplyIdsRef.current.has(r.id)
|
|
136
|
+
);
|
|
137
|
+
if (newReplies.length === 0) return;
|
|
138
|
+
for (const r of newReplies) {
|
|
139
|
+
knownReplyIdsRef.current.add(r.id);
|
|
140
|
+
}
|
|
141
|
+
const latestTimestamp = newReplies.reduce((latest, r) => {
|
|
142
|
+
return r.timestamp > latest ? r.timestamp : latest;
|
|
143
|
+
}, lastReplyTimestampRef.current ?? "");
|
|
144
|
+
lastReplyTimestampRef.current = latestTimestamp;
|
|
145
|
+
const replyMessages = newReplies.map((r) => ({
|
|
146
|
+
id: r.id,
|
|
147
|
+
text: r.text,
|
|
148
|
+
sender: "received",
|
|
149
|
+
timestamp: new Date(r.timestamp).getTime()
|
|
150
|
+
}));
|
|
151
|
+
setMessages((prev) => [...prev, ...replyMessages]);
|
|
152
|
+
} catch {
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
void fetchReplies();
|
|
156
|
+
const intervalId = setInterval(() => void fetchReplies(), POLL_INTERVAL);
|
|
157
|
+
return () => clearInterval(intervalId);
|
|
158
|
+
}, [repliesUrl, isOpen, user]);
|
|
116
159
|
return { messages, input, setInput, sending, sendMessage, handleKeyDown };
|
|
117
160
|
}
|
|
118
161
|
function useColorScheme() {
|
|
@@ -172,6 +215,10 @@ function injectKeyframes() {
|
|
|
172
215
|
from { opacity: 1; transform: translateY(0) scale(1); }
|
|
173
216
|
to { opacity: 0; transform: translateY(12px) scale(0.96); }
|
|
174
217
|
}
|
|
218
|
+
@keyframes sc-badge-scale-in {
|
|
219
|
+
from { transform: scale(0); }
|
|
220
|
+
to { transform: scale(1); }
|
|
221
|
+
}
|
|
175
222
|
`;
|
|
176
223
|
document.head.appendChild(style);
|
|
177
224
|
}
|
|
@@ -182,12 +229,31 @@ function ChatBubble({
|
|
|
182
229
|
title = "Support",
|
|
183
230
|
placeholder = "Type a message...",
|
|
184
231
|
show = true,
|
|
185
|
-
user
|
|
232
|
+
user,
|
|
233
|
+
repliesUrl,
|
|
234
|
+
isOpen: isOpenProp,
|
|
235
|
+
onOpenChange,
|
|
236
|
+
badgeColor = "#eab308",
|
|
237
|
+
unreadCount = 0
|
|
186
238
|
}) {
|
|
187
|
-
const
|
|
239
|
+
const isControlled = isOpenProp !== void 0;
|
|
240
|
+
const [isOpenInternal, setIsOpenInternal] = useState(false);
|
|
241
|
+
const isOpen = isControlled ? isOpenProp : isOpenInternal;
|
|
242
|
+
const setIsOpen = useCallback(
|
|
243
|
+
(valueOrUpdater) => {
|
|
244
|
+
const newValue = typeof valueOrUpdater === "function" ? valueOrUpdater(isOpen) : valueOrUpdater;
|
|
245
|
+
if (isControlled) {
|
|
246
|
+
onOpenChange?.(newValue);
|
|
247
|
+
} else {
|
|
248
|
+
setIsOpenInternal(newValue);
|
|
249
|
+
onOpenChange?.(newValue);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
[isControlled, isOpen, onOpenChange]
|
|
253
|
+
);
|
|
188
254
|
const colorScheme = useColorScheme();
|
|
189
255
|
const theme = useMemo(() => getThemeTokens(colorScheme), [colorScheme]);
|
|
190
|
-
const { messages, input, setInput, sending, sendMessage, handleKeyDown } = useChatEngine({ apiUrl, user });
|
|
256
|
+
const { messages, input, setInput, sending, sendMessage, handleKeyDown } = useChatEngine({ apiUrl, user, repliesUrl, isOpen });
|
|
191
257
|
const [panelState, setPanelState] = useState(
|
|
192
258
|
"closed"
|
|
193
259
|
);
|
|
@@ -246,8 +312,8 @@ function ChatBubble({
|
|
|
246
312
|
};
|
|
247
313
|
document.addEventListener("keydown", handleTrapKeyDown);
|
|
248
314
|
return () => document.removeEventListener("keydown", handleTrapKeyDown);
|
|
249
|
-
}, [panelState]);
|
|
250
|
-
const toggle = useCallback(() => setIsOpen((o) => !o), []);
|
|
315
|
+
}, [panelState, setIsOpen]);
|
|
316
|
+
const toggle = useCallback(() => setIsOpen((o) => !o), [setIsOpen]);
|
|
251
317
|
const positionStyles = {
|
|
252
318
|
position: "fixed",
|
|
253
319
|
zIndex: 99999,
|
|
@@ -280,7 +346,7 @@ function ChatBubble({
|
|
|
280
346
|
}
|
|
281
347
|
}
|
|
282
348
|
` }),
|
|
283
|
-
show && /* @__PURE__ */
|
|
349
|
+
show && /* @__PURE__ */ jsxs(
|
|
284
350
|
"button",
|
|
285
351
|
{
|
|
286
352
|
onClick: toggle,
|
|
@@ -299,7 +365,8 @@ function ChatBubble({
|
|
|
299
365
|
alignItems: "center",
|
|
300
366
|
justifyContent: "center",
|
|
301
367
|
boxShadow: "0 4px 12px rgba(0,0,0,0.15)",
|
|
302
|
-
transition: "transform 0.2s ease, box-shadow 0.2s ease"
|
|
368
|
+
transition: "transform 0.2s ease, box-shadow 0.2s ease",
|
|
369
|
+
overflow: "visible"
|
|
303
370
|
},
|
|
304
371
|
onMouseEnter: (e) => {
|
|
305
372
|
e.currentTarget.style.transform = "scale(1.1)";
|
|
@@ -309,21 +376,50 @@ function ChatBubble({
|
|
|
309
376
|
e.currentTarget.style.transform = "scale(1)";
|
|
310
377
|
e.currentTarget.style.boxShadow = "0 4px 12px rgba(0,0,0,0.15)";
|
|
311
378
|
},
|
|
312
|
-
children:
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
379
|
+
children: [
|
|
380
|
+
/* @__PURE__ */ jsx(
|
|
381
|
+
"svg",
|
|
382
|
+
{
|
|
383
|
+
width: "24",
|
|
384
|
+
height: "24",
|
|
385
|
+
viewBox: "0 0 24 24",
|
|
386
|
+
fill: "none",
|
|
387
|
+
stroke: "white",
|
|
388
|
+
strokeWidth: "2",
|
|
389
|
+
strokeLinecap: "round",
|
|
390
|
+
strokeLinejoin: "round",
|
|
391
|
+
"aria-hidden": "true",
|
|
392
|
+
children: isOpen ? /* @__PURE__ */ jsx("path", { d: "M18 6L6 18M6 6l12 12" }) : /* @__PURE__ */ jsx("path", { d: "M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" })
|
|
393
|
+
}
|
|
394
|
+
),
|
|
395
|
+
!isOpen && unreadCount > 0 && /* @__PURE__ */ jsx(
|
|
396
|
+
"span",
|
|
397
|
+
{
|
|
398
|
+
"data-testid": "unread-badge",
|
|
399
|
+
"aria-label": `${unreadCount > 9 ? "9+" : unreadCount} unread messages`,
|
|
400
|
+
style: {
|
|
401
|
+
position: "absolute",
|
|
402
|
+
top: "-4px",
|
|
403
|
+
right: "-4px",
|
|
404
|
+
minWidth: "20px",
|
|
405
|
+
height: "20px",
|
|
406
|
+
borderRadius: "10px",
|
|
407
|
+
backgroundColor: badgeColor,
|
|
408
|
+
color: "#fff",
|
|
409
|
+
fontSize: "11px",
|
|
410
|
+
fontWeight: 700,
|
|
411
|
+
display: "flex",
|
|
412
|
+
alignItems: "center",
|
|
413
|
+
justifyContent: "center",
|
|
414
|
+
padding: "0 5px",
|
|
415
|
+
lineHeight: 1,
|
|
416
|
+
boxShadow: "0 2px 4px rgba(0,0,0,0.2)",
|
|
417
|
+
animation: "sc-badge-scale-in 0.2s ease-out forwards"
|
|
418
|
+
},
|
|
419
|
+
children: unreadCount > 9 ? "9+" : unreadCount
|
|
420
|
+
}
|
|
421
|
+
)
|
|
422
|
+
]
|
|
327
423
|
}
|
|
328
424
|
),
|
|
329
425
|
showPanel && /* @__PURE__ */ jsxs(
|
|
@@ -412,6 +508,7 @@ function ChatBubble({
|
|
|
412
508
|
messages.map((msg) => /* @__PURE__ */ jsx(
|
|
413
509
|
"div",
|
|
414
510
|
{
|
|
511
|
+
"data-sender": msg.sender,
|
|
415
512
|
style: {
|
|
416
513
|
alignSelf: msg.sender === "user" ? "flex-end" : "flex-start",
|
|
417
514
|
maxWidth: "80%",
|
|
@@ -525,9 +622,10 @@ function SupportChatModal({
|
|
|
525
622
|
color = "#2563eb",
|
|
526
623
|
title = "Contact Us",
|
|
527
624
|
placeholder = "Type a message...",
|
|
528
|
-
user
|
|
625
|
+
user,
|
|
626
|
+
repliesUrl
|
|
529
627
|
}) {
|
|
530
|
-
const { messages, input, setInput, sending, sendMessage, handleKeyDown } = useChatEngine({ apiUrl, user });
|
|
628
|
+
const { messages, input, setInput, sending, sendMessage, handleKeyDown } = useChatEngine({ apiUrl, user, repliesUrl, isOpen });
|
|
531
629
|
const colorScheme = useColorScheme();
|
|
532
630
|
const theme = useMemo(() => getThemeTokens(colorScheme), [colorScheme]);
|
|
533
631
|
const modalRef = useRef(null);
|
|
@@ -719,6 +817,7 @@ function SupportChatModal({
|
|
|
719
817
|
messages.map((msg) => /* @__PURE__ */ jsx(
|
|
720
818
|
"div",
|
|
721
819
|
{
|
|
820
|
+
"data-sender": msg.sender,
|
|
722
821
|
style: {
|
|
723
822
|
alignSelf: msg.sender === "user" ? "flex-end" : "flex-start",
|
|
724
823
|
maxWidth: "80%",
|
|
@@ -809,7 +908,76 @@ function useSupportChat() {
|
|
|
809
908
|
const toggle = useCallback(() => setIsOpen((prev) => !prev), []);
|
|
810
909
|
return { open, close, toggle, isOpen };
|
|
811
910
|
}
|
|
911
|
+
var POLL_INTERVAL2 = 4e3;
|
|
912
|
+
function useUnreadCount({
|
|
913
|
+
repliesUrl,
|
|
914
|
+
user,
|
|
915
|
+
isOpen
|
|
916
|
+
}) {
|
|
917
|
+
const [pendingReplies, setPendingReplies] = useState([]);
|
|
918
|
+
const lastReplyTimestampRef = useRef(null);
|
|
919
|
+
const knownReplyIdsRef = useRef(/* @__PURE__ */ new Set());
|
|
920
|
+
const prevIsOpenRef = useRef(isOpen);
|
|
921
|
+
useEffect(() => {
|
|
922
|
+
if (isOpen && !prevIsOpenRef.current) {
|
|
923
|
+
setPendingReplies([]);
|
|
924
|
+
knownReplyIdsRef.current.clear();
|
|
925
|
+
lastReplyTimestampRef.current = null;
|
|
926
|
+
}
|
|
927
|
+
prevIsOpenRef.current = isOpen;
|
|
928
|
+
}, [isOpen]);
|
|
929
|
+
const markAsRead = useCallback(() => {
|
|
930
|
+
setPendingReplies([]);
|
|
931
|
+
knownReplyIdsRef.current.clear();
|
|
932
|
+
lastReplyTimestampRef.current = null;
|
|
933
|
+
}, []);
|
|
934
|
+
useEffect(() => {
|
|
935
|
+
if (!repliesUrl || isOpen) return;
|
|
936
|
+
const sessionId = user?.id ?? getSessionId();
|
|
937
|
+
const fetchReplies = async () => {
|
|
938
|
+
try {
|
|
939
|
+
const params = new URLSearchParams({ sessionId });
|
|
940
|
+
if (lastReplyTimestampRef.current) {
|
|
941
|
+
params.set("since", lastReplyTimestampRef.current);
|
|
942
|
+
}
|
|
943
|
+
const response = await fetch(`${repliesUrl}?${params.toString()}`);
|
|
944
|
+
if (!response.ok) return;
|
|
945
|
+
const data = await response.json();
|
|
946
|
+
if (data.replies.length === 0) return;
|
|
947
|
+
const newReplies = data.replies.filter(
|
|
948
|
+
(r) => !knownReplyIdsRef.current.has(r.id)
|
|
949
|
+
);
|
|
950
|
+
if (newReplies.length === 0) return;
|
|
951
|
+
for (const r of newReplies) {
|
|
952
|
+
knownReplyIdsRef.current.add(r.id);
|
|
953
|
+
}
|
|
954
|
+
const latestTimestamp = newReplies.reduce((latest, r) => {
|
|
955
|
+
return r.timestamp > latest ? r.timestamp : latest;
|
|
956
|
+
}, lastReplyTimestampRef.current ?? "");
|
|
957
|
+
lastReplyTimestampRef.current = latestTimestamp;
|
|
958
|
+
const replyMessages = newReplies.map((r) => ({
|
|
959
|
+
id: r.id,
|
|
960
|
+
text: r.text,
|
|
961
|
+
sender: "received",
|
|
962
|
+
timestamp: new Date(r.timestamp).getTime()
|
|
963
|
+
}));
|
|
964
|
+
setPendingReplies((prev) => [...prev, ...replyMessages]);
|
|
965
|
+
} catch {
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
void fetchReplies();
|
|
969
|
+
const intervalId = setInterval(() => void fetchReplies(), POLL_INTERVAL2);
|
|
970
|
+
return () => clearInterval(intervalId);
|
|
971
|
+
}, [repliesUrl, isOpen, user]);
|
|
972
|
+
const unreadCount = pendingReplies.length;
|
|
973
|
+
return {
|
|
974
|
+
unreadCount,
|
|
975
|
+
hasUnread: unreadCount > 0,
|
|
976
|
+
pendingReplies,
|
|
977
|
+
markAsRead
|
|
978
|
+
};
|
|
979
|
+
}
|
|
812
980
|
|
|
813
|
-
export { ChatBubble, SupportChatModal, collectAnonymousContext, getSessionId, useChatEngine, useSupportChat };
|
|
981
|
+
export { ChatBubble, SupportChatModal, collectAnonymousContext, getSessionId, useChatEngine, useSupportChat, useUnreadCount };
|
|
814
982
|
//# sourceMappingURL=index.js.map
|
|
815
983
|
//# sourceMappingURL=index.js.map
|
package/dist/client/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/client/context.ts","../../src/client/useChatEngine.ts","../../src/client/useColorScheme.ts","../../src/client/ChatBubble.tsx","../../src/client/SupportChatModal.tsx","../../src/client/useSupportChat.ts"],"names":["useState","useEffect","useCallback","useMemo","useRef","jsxs","Fragment","jsx"],"mappings":";;;;;;AAEA,IAAM,WAAA,GAAc,yBAAA;AAGpB,SAAS,iBAAA,GAA4B;AACnC,EAAA,MAAM,KAAA,GACJ,gEAAA;AACF,EAAA,IAAI,EA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ACF;AAAA;AACF,GAAA,EACF,CAAA;AAEJ;ACpWO,SAAS,cAAA,GAAmC;AACjD,EAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,CAAA,GAAIP,SAAS,KAAK,CAAA;AAE1C,EAAA,MAAM,OAAOE,WAAAA,CAAY,MAAM,UAAU,IAAI,CAAA,EAAG,EAAE,CAAA;AAClD,EAAA,MAAM,QAAQA,WAAAA,CAAY,MAAM,UAAU,KAAK,CAAA,EAAG,EAAE,CAAA;AACpD,EAAA,MAAM,MAAA,GAASA,WAAAA,CAAY,MAAM,SAAA,CAAU,CAAC,SAAS,CAAC,IAAI,CAAA,EAAG,EAAE,CAAA;AAE/D,EAAA,OAAO,EAAE,IAAA,EAAM,KAAA,EAAO,MAAA,EAAQ,MAAA,EAAO;AACvC","file":"index.js","sourcesContent":["import type { AnonymousContext } from \"./types\";\r\n\r\nconst SESSION_KEY = \"support-chat-session-id\";\r\n\r\n/** Generate a random session ID */\r\nfunction generateSessionId(): string {\r\n const chars =\r\n \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\";\r\n let id = \"\";\r\n for (let i = 0; i < 16; i++) {\r\n id += chars.charAt(Math.floor(Math.random() * chars.length));\r\n }\r\n return id;\r\n}\r\n\r\n/** Get or create a persistent session ID from localStorage */\r\nexport function getSessionId(): string {\r\n if (typeof window === \"undefined\") return generateSessionId();\r\n\r\n try {\r\n const existing = localStorage.getItem(SESSION_KEY);\r\n if (existing) return existing;\r\n\r\n const newId = generateSessionId();\r\n localStorage.setItem(SESSION_KEY, newId);\r\n return newId;\r\n } catch {\r\n // localStorage may be blocked (private browsing, etc.)\r\n return generateSessionId();\r\n }\r\n}\r\n\r\n/** Collect anonymous browser context */\r\nexport function collectAnonymousContext(): AnonymousContext {\r\n const sessionId = getSessionId();\r\n\r\n if (typeof window === \"undefined\") {\r\n return {\r\n pageUrl: \"\",\r\n referrer: \"\",\r\n userAgent: \"\",\r\n screenSize: \"\",\r\n timezone: \"\",\r\n sessionId,\r\n };\r\n }\r\n\r\n return {\r\n pageUrl: window.location.href,\r\n referrer: document.referrer,\r\n userAgent: navigator.userAgent,\r\n screenSize: `${window.screen.width}x${window.screen.height}`,\r\n timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,\r\n sessionId,\r\n };\r\n}\r\n","import { useState, useCallback } from \"react\";\r\nimport type { ChatMessage, ChatUser } from \"./types\";\r\nimport { collectAnonymousContext, getSessionId } from \"./context\";\r\n\r\n/** Options for initializing the chat engine */\r\nexport interface ChatEngineOptions {\r\n /** URL of the support chat API endpoint */\r\n apiUrl: string;\r\n /** Authenticated user info (optional) */\r\n user?: ChatUser;\r\n}\r\n\r\n/** Return value from useChatEngine */\r\nexport interface ChatEngineState {\r\n /** All chat messages */\r\n messages: ChatMessage[];\r\n /** Current input value */\r\n input: string;\r\n /** Update the input value */\r\n setInput: (value: string) => void;\r\n /** Whether a message is currently being sent */\r\n sending: boolean;\r\n /** Send the current input as a message */\r\n sendMessage: () => Promise<void>;\r\n /** Handle keydown on the input (Enter to send) */\r\n handleKeyDown: (e: React.KeyboardEvent) => void;\r\n}\r\n\r\n/**\r\n * Shared chat engine hook used by both ChatBubble and SupportChatModal.\r\n * Manages message state, input, and API communication.\r\n */\r\nexport function useChatEngine({\r\n apiUrl,\r\n user,\r\n}: ChatEngineOptions): ChatEngineState {\r\n const [messages, setMessages] = useState<ChatMessage[]>([]);\r\n const [input, setInput] = useState(\"\");\r\n const [sending, setSending] = useState(false);\r\n\r\n const sendMessage = useCallback(async () => {\r\n const text = input.trim();\r\n if (!text || sending) return;\r\n\r\n const msg: ChatMessage = {\r\n id: `msg-${Date.now()}`,\r\n text,\r\n sender: \"user\",\r\n timestamp: Date.now(),\r\n };\r\n\r\n setMessages((prev) => [...prev, msg]);\r\n setInput(\"\");\r\n setSending(true);\r\n\r\n try {\r\n const context = collectAnonymousContext();\r\n const response = await fetch(apiUrl, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n message: text,\r\n user: user ?? undefined,\r\n sessionId: user?.id ?? getSessionId(),\r\n context,\r\n }),\r\n });\r\n\r\n if (!response.ok) {\r\n setMessages((prev) => [\r\n ...prev,\r\n {\r\n id: `err-${Date.now()}`,\r\n text: \"Failed to send message. Please try again.\",\r\n sender: \"system\",\r\n timestamp: Date.now(),\r\n },\r\n ]);\r\n }\r\n } catch {\r\n setMessages((prev) => [\r\n ...prev,\r\n {\r\n id: `err-${Date.now()}`,\r\n text: \"Connection error. Please try again.\",\r\n sender: \"system\",\r\n timestamp: Date.now(),\r\n },\r\n ]);\r\n } finally {\r\n setSending(false);\r\n }\r\n }, [input, sending, apiUrl, user]);\r\n\r\n const handleKeyDown = useCallback(\r\n (e: React.KeyboardEvent) => {\r\n if (e.key === \"Enter\" && !e.shiftKey) {\r\n e.preventDefault();\r\n void sendMessage();\r\n }\r\n },\r\n [sendMessage],\r\n );\r\n\r\n return { messages, input, setInput, sending, sendMessage, handleKeyDown };\r\n}\r\n","import { useState, useEffect } from \"react\";\r\n\r\nexport type ColorScheme = \"light\" | \"dark\";\r\n\r\n/**\r\n * Detects the user's system color scheme preference via `prefers-color-scheme`.\r\n * Returns \"dark\" or \"light\". Updates reactively if the user changes their system setting.\r\n */\r\nexport function useColorScheme(): ColorScheme {\r\n const [scheme, setScheme] = useState<ColorScheme>(() => {\r\n if (typeof window === \"undefined\") return \"light\";\r\n return window.matchMedia(\"(prefers-color-scheme: dark)\").matches\r\n ? \"dark\"\r\n : \"light\";\r\n });\r\n\r\n useEffect(() => {\r\n if (typeof window === \"undefined\") return;\r\n const mq = window.matchMedia(\"(prefers-color-scheme: dark)\");\r\n const handler = (e: MediaQueryListEvent) => {\r\n setScheme(e.matches ? \"dark\" : \"light\");\r\n };\r\n mq.addEventListener(\"change\", handler);\r\n return () => mq.removeEventListener(\"change\", handler);\r\n }, []);\r\n\r\n return scheme;\r\n}\r\n\r\n/** Resolved color tokens for light and dark modes. */\r\nexport interface ThemeTokens {\r\n panelBg: string;\r\n panelBorder: string;\r\n inputBg: string;\r\n inputBorder: string;\r\n inputText: string;\r\n inputPlaceholder: string;\r\n receivedBg: string;\r\n receivedText: string;\r\n emptyText: string;\r\n inputAreaBorder: string;\r\n}\r\n\r\nconst lightTokens: ThemeTokens = {\r\n panelBg: \"#ffffff\",\r\n panelBorder: \"#e5e7eb\",\r\n inputBg: \"#ffffff\",\r\n inputBorder: \"#d1d5db\",\r\n inputText: \"#1f2937\",\r\n inputPlaceholder: \"#9ca3af\",\r\n receivedBg: \"#f3f4f6\",\r\n receivedText: \"#1f2937\",\r\n emptyText: \"#9ca3af\",\r\n inputAreaBorder: \"#e5e7eb\",\r\n};\r\n\r\nconst darkTokens: ThemeTokens = {\r\n panelBg: \"#1f2937\",\r\n panelBorder: \"#374151\",\r\n inputBg: \"#111827\",\r\n inputBorder: \"#4b5563\",\r\n inputText: \"#f9fafb\",\r\n inputPlaceholder: \"#9ca3af\",\r\n receivedBg: \"#374151\",\r\n receivedText: \"#f3f4f6\",\r\n emptyText: \"#6b7280\",\r\n inputAreaBorder: \"#374151\",\r\n};\r\n\r\nexport function getThemeTokens(scheme: ColorScheme): ThemeTokens {\r\n return scheme === \"dark\" ? darkTokens : lightTokens;\r\n}\r\n","import { useState, useCallback, useRef, useEffect, useMemo } from \"react\";\r\nimport type { ChatBubbleProps } from \"./types\";\r\nimport { useChatEngine } from \"./useChatEngine\";\r\nimport { useColorScheme, getThemeTokens } from \"./useColorScheme\";\r\n\r\n/**\r\n * Inject a <style> tag for keyframe animations.\r\n * Called once on first mount. Uses a data-attribute to avoid duplicates.\r\n */\r\nfunction injectKeyframes(): void {\r\n if (typeof document === \"undefined\") return;\r\n if (document.querySelector(\"[data-support-chat-keyframes]\")) return;\r\n\r\n const style = document.createElement(\"style\");\r\n style.setAttribute(\"data-support-chat-keyframes\", \"\");\r\n style.textContent = `\r\n @keyframes sc-slide-in {\r\n from { opacity: 0; transform: translateY(12px) scale(0.96); }\r\n to { opacity: 1; transform: translateY(0) scale(1); }\r\n }\r\n @keyframes sc-slide-out {\r\n from { opacity: 1; transform: translateY(0) scale(1); }\r\n to { opacity: 0; transform: translateY(12px) scale(0.96); }\r\n }\r\n `;\r\n document.head.appendChild(style);\r\n}\r\n\r\n/**\r\n * ChatBubble -- floating support chat widget.\r\n *\r\n * Renders a circular button that opens an inline chat panel.\r\n * Messages are sent via POST to the configured `apiUrl`.\r\n *\r\n * @example\r\n * ```tsx\r\n * <ChatBubble apiUrl=\"/api/support\" />\r\n * ```\r\n */\r\nexport function ChatBubble({\r\n apiUrl,\r\n position = \"bottom-right\",\r\n color = \"#2563eb\",\r\n title = \"Support\",\r\n placeholder = \"Type a message...\",\r\n show = true,\r\n user,\r\n}: ChatBubbleProps) {\r\n const [isOpen, setIsOpen] = useState(false);\r\n\r\n // Detect system color scheme\r\n const colorScheme = useColorScheme();\r\n const theme = useMemo(() => getThemeTokens(colorScheme), [colorScheme]);\r\n\r\n // Use shared chat engine for message logic\r\n const { messages, input, setInput, sending, sendMessage, handleKeyDown } =\r\n useChatEngine({ apiUrl, user });\r\n\r\n // Animation state: \"open\" | \"closing\" | \"closed\"\r\n const [panelState, setPanelState] = useState<\"open\" | \"closing\" | \"closed\">(\r\n \"closed\",\r\n );\r\n\r\n const panelRef = useRef<HTMLDivElement>(null);\r\n const messagesEndRef = useRef<HTMLDivElement>(null);\r\n const inputRef = useRef<HTMLInputElement>(null);\r\n\r\n // Inject keyframe animations on first render\r\n useEffect(() => {\r\n injectKeyframes();\r\n }, []);\r\n\r\n // Sync isOpen -> panelState\r\n useEffect(() => {\r\n if (isOpen) {\r\n setPanelState(\"open\");\r\n } else if (panelState === \"open\") {\r\n // Start closing animation\r\n setPanelState(\"closing\");\r\n }\r\n }, [isOpen]);\r\n\r\n // When closing animation ends, set to fully closed\r\n const handleAnimationEnd = useCallback(() => {\r\n if (panelState === \"closing\") {\r\n setPanelState(\"closed\");\r\n }\r\n }, [panelState]);\r\n\r\n // Focus input when panel opens\r\n useEffect(() => {\r\n if (panelState === \"open\" && inputRef.current) {\r\n inputRef.current.focus();\r\n }\r\n }, [panelState]);\r\n\r\n // Auto-scroll to latest message\r\n useEffect(() => {\r\n messagesEndRef.current?.scrollIntoView({ behavior: \"smooth\" });\r\n }, [messages]);\r\n\r\n // Focus trap: keep Tab cycling inside the dialog when open\r\n useEffect(() => {\r\n if (panelState !== \"open\" || !panelRef.current) return;\r\n\r\n const panel = panelRef.current;\r\n\r\n const handleTrapKeyDown = (e: KeyboardEvent) => {\r\n if (e.key === \"Escape\") {\r\n setIsOpen(false);\r\n return;\r\n }\r\n\r\n if (e.key !== \"Tab\") return;\r\n\r\n const focusable = panel.querySelectorAll<HTMLElement>(\r\n 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])',\r\n );\r\n if (focusable.length === 0) return;\r\n\r\n const first = focusable[0]!;\r\n const last = focusable[focusable.length - 1]!;\r\n\r\n if (e.shiftKey) {\r\n if (document.activeElement === first) {\r\n e.preventDefault();\r\n last.focus();\r\n }\r\n } else {\r\n if (document.activeElement === last) {\r\n e.preventDefault();\r\n first.focus();\r\n }\r\n }\r\n };\r\n\r\n document.addEventListener(\"keydown\", handleTrapKeyDown);\r\n return () => document.removeEventListener(\"keydown\", handleTrapKeyDown);\r\n }, [panelState]);\r\n\r\n const toggle = useCallback(() => setIsOpen((o) => !o), []);\r\n\r\n // Position styles for the bubble button\r\n const positionStyles: React.CSSProperties = {\r\n position: \"fixed\",\r\n zIndex: 99999,\r\n ...(position.includes(\"bottom\") ? { bottom: \"20px\" } : { top: \"20px\" }),\r\n ...(position.includes(\"right\") ? { right: \"20px\" } : { left: \"20px\" }),\r\n };\r\n\r\n // Panel position & responsive sizing\r\n // On mobile (<480px viewport), expand to near-full screen\r\n const panelPositionStyles: React.CSSProperties = {\r\n position: \"fixed\",\r\n zIndex: 99999,\r\n width: \"380px\",\r\n maxWidth: \"calc(100vw - 40px)\",\r\n height: \"500px\",\r\n maxHeight: \"calc(100vh - 120px)\",\r\n ...(position.includes(\"bottom\") ? { bottom: \"80px\" } : { top: \"80px\" }),\r\n ...(position.includes(\"right\") ? { right: \"20px\" } : { left: \"20px\" }),\r\n };\r\n\r\n // Check if panel is visible (open or animating out)\r\n const showPanel = panelState === \"open\" || panelState === \"closing\";\r\n\r\n return (\r\n <>\r\n {/* Responsive overrides injected as inline <style> */}\r\n {showPanel && (\r\n <style>{`\r\n @media (max-width: 479px) {\r\n [data-support-chat-panel] {\r\n width: calc(100vw - 16px) !important;\r\n height: calc(100vh - 100px) !important;\r\n max-width: none !important;\r\n max-height: none !important;\r\n left: 8px !important;\r\n right: 8px !important;\r\n bottom: 72px !important;\r\n border-radius: 12px !important;\r\n }\r\n }\r\n `}</style>\r\n )}\r\n\r\n {/* Floating bubble button */}\r\n {show && (\r\n <button\r\n onClick={toggle}\r\n aria-label={isOpen ? \"Close support chat\" : \"Open support chat\"}\r\n aria-expanded={isOpen}\r\n aria-haspopup=\"dialog\"\r\n style={{\r\n ...positionStyles,\r\n width: \"56px\",\r\n height: \"56px\",\r\n borderRadius: \"50%\",\r\n backgroundColor: color,\r\n border: \"none\",\r\n cursor: \"pointer\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"center\",\r\n boxShadow: \"0 4px 12px rgba(0,0,0,0.15)\",\r\n transition: \"transform 0.2s ease, box-shadow 0.2s ease\",\r\n }}\r\n onMouseEnter={(e) => {\r\n e.currentTarget.style.transform = \"scale(1.1)\";\r\n e.currentTarget.style.boxShadow = \"0 6px 20px rgba(0,0,0,0.2)\";\r\n }}\r\n onMouseLeave={(e) => {\r\n e.currentTarget.style.transform = \"scale(1)\";\r\n e.currentTarget.style.boxShadow = \"0 4px 12px rgba(0,0,0,0.15)\";\r\n }}\r\n >\r\n <svg\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"white\"\r\n strokeWidth=\"2\"\r\n strokeLinecap=\"round\"\r\n strokeLinejoin=\"round\"\r\n aria-hidden=\"true\"\r\n >\r\n {isOpen ? (\r\n <path d=\"M18 6L6 18M6 6l12 12\" />\r\n ) : (\r\n <path d=\"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z\" />\r\n )}\r\n </svg>\r\n </button>\r\n )}\r\n\r\n {/* Chat panel */}\r\n {showPanel && (\r\n <div\r\n ref={panelRef}\r\n role=\"dialog\"\r\n aria-label=\"Support chat\"\r\n aria-modal=\"true\"\r\n data-support-chat-panel=\"\"\r\n onAnimationEnd={handleAnimationEnd}\r\n style={{\r\n ...panelPositionStyles,\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n borderRadius: \"12px\",\r\n overflow: \"hidden\",\r\n boxShadow: \"0 8px 30px rgba(0,0,0,0.12)\",\r\n fontFamily:\r\n '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif',\r\n fontSize: \"14px\",\r\n backgroundColor: theme.panelBg,\r\n border: `1px solid ${theme.panelBorder}`,\r\n animation:\r\n panelState === \"open\"\r\n ? \"sc-slide-in 0.25s ease-out forwards\"\r\n : \"sc-slide-out 0.2s ease-in forwards\",\r\n }}\r\n >\r\n {/* Header */}\r\n <div\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n padding: \"16px\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"space-between\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <span style={{ fontWeight: 600, fontSize: \"16px\" }}>{title}</span>\r\n <button\r\n onClick={() => setIsOpen(false)}\r\n aria-label=\"Close chat\"\r\n style={{\r\n background: \"none\",\r\n border: \"none\",\r\n color: \"#fff\",\r\n cursor: \"pointer\",\r\n padding: \"4px\",\r\n lineHeight: 1,\r\n fontSize: \"18px\",\r\n }}\r\n >\r\n ✕\r\n </button>\r\n </div>\r\n\r\n {/* Messages */}\r\n <div\r\n role=\"log\"\r\n aria-live=\"polite\"\r\n aria-label=\"Chat messages\"\r\n style={{\r\n flex: 1,\r\n overflowY: \"auto\",\r\n padding: \"16px\",\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n gap: \"8px\",\r\n }}\r\n >\r\n {messages.length === 0 && (\r\n <div\r\n style={{\r\n color: theme.emptyText,\r\n textAlign: \"center\",\r\n marginTop: \"40px\",\r\n }}\r\n >\r\n Send us a message and we will get back to you!\r\n </div>\r\n )}\r\n {messages.map((msg) => (\r\n <div\r\n key={msg.id}\r\n style={{\r\n alignSelf:\r\n msg.sender === \"user\" ? \"flex-end\" : \"flex-start\",\r\n maxWidth: \"80%\",\r\n padding: \"10px 14px\",\r\n borderRadius:\r\n msg.sender === \"user\"\r\n ? \"16px 16px 4px 16px\"\r\n : \"16px 16px 16px 4px\",\r\n backgroundColor:\r\n msg.sender === \"user\" ? color : theme.receivedBg,\r\n color: msg.sender === \"user\" ? \"#fff\" : theme.receivedText,\r\n wordBreak: \"break-word\",\r\n }}\r\n >\r\n {msg.text}\r\n </div>\r\n ))}\r\n <div ref={messagesEndRef} />\r\n </div>\r\n\r\n {/* Input */}\r\n <div\r\n style={{\r\n borderTop: `1px solid ${theme.inputAreaBorder}`,\r\n padding: \"12px\",\r\n display: \"flex\",\r\n gap: \"8px\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <input\r\n ref={inputRef}\r\n type=\"text\"\r\n value={input}\r\n onChange={(e) => setInput(e.target.value)}\r\n onKeyDown={handleKeyDown}\r\n placeholder={placeholder}\r\n aria-label=\"Type your message\"\r\n style={{\r\n flex: 1,\r\n border: `1px solid ${theme.inputBorder}`,\r\n borderRadius: \"8px\",\r\n padding: \"10px 12px\",\r\n fontSize: \"14px\",\r\n outline: \"none\",\r\n fontFamily: \"inherit\",\r\n backgroundColor: theme.inputBg,\r\n color: theme.inputText,\r\n }}\r\n />\r\n <button\r\n onClick={() => void sendMessage()}\r\n disabled={sending || !input.trim()}\r\n aria-label=\"Send message\"\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n border: \"none\",\r\n borderRadius: \"8px\",\r\n padding: \"10px 16px\",\r\n cursor:\r\n sending || !input.trim() ? \"not-allowed\" : \"pointer\",\r\n opacity: sending || !input.trim() ? 0.5 : 1,\r\n fontWeight: 600,\r\n fontSize: \"14px\",\r\n fontFamily: \"inherit\",\r\n transition: \"opacity 0.2s ease\",\r\n }}\r\n >\r\n Send\r\n </button>\r\n </div>\r\n </div>\r\n )}\r\n </>\r\n );\r\n}\r\n","import { useRef, useEffect, useCallback, useMemo } from \"react\";\r\nimport type { SupportChatModalProps } from \"./types\";\r\nimport { useChatEngine } from \"./useChatEngine\";\r\nimport { useColorScheme, getThemeTokens } from \"./useColorScheme\";\r\n\r\n/**\r\n * Inject a <style> tag for modal keyframe animations.\r\n * Called once on first mount. Uses a data-attribute to avoid duplicates.\r\n */\r\nfunction injectModalKeyframes(): void {\r\n if (typeof document === \"undefined\") return;\r\n if (document.querySelector(\"[data-support-chat-modal-keyframes]\")) return;\r\n\r\n const style = document.createElement(\"style\");\r\n style.setAttribute(\"data-support-chat-modal-keyframes\", \"\");\r\n style.textContent = `\r\n @keyframes sc-modal-backdrop-in {\r\n from { opacity: 0; }\r\n to { opacity: 1; }\r\n }\r\n @keyframes sc-modal-backdrop-out {\r\n from { opacity: 1; }\r\n to { opacity: 0; }\r\n }\r\n @keyframes sc-modal-slide-in {\r\n from { opacity: 0; transform: translateY(20px) scale(0.96); }\r\n to { opacity: 1; transform: translateY(0) scale(1); }\r\n }\r\n @keyframes sc-modal-slide-out {\r\n from { opacity: 1; transform: translateY(0) scale(1); }\r\n to { opacity: 0; transform: translateY(20px) scale(0.96); }\r\n }\r\n `;\r\n document.head.appendChild(style);\r\n}\r\n\r\n/**\r\n * SupportChatModal -- modal-based support chat.\r\n *\r\n * Renders a centered modal dialog for support chat, designed to be\r\n * triggered from custom UI elements (e.g., \"Contact Us\" links).\r\n *\r\n * Desktop: centered modal, max-width ~500px, with backdrop overlay.\r\n * Mobile: full-screen modal.\r\n *\r\n * Use with the `useSupportChat()` hook:\r\n *\r\n * @example\r\n * ```tsx\r\n * import { SupportChatModal, useSupportChat } from 'support-chat';\r\n *\r\n * function App() {\r\n * const { open, close, isOpen } = useSupportChat();\r\n * return (\r\n * <>\r\n * <button onClick={open}>Contact Us</button>\r\n * <SupportChatModal\r\n * apiUrl=\"/api/support\"\r\n * isOpen={isOpen}\r\n * onClose={close}\r\n * />\r\n * </>\r\n * );\r\n * }\r\n * ```\r\n */\r\nexport function SupportChatModal({\r\n apiUrl,\r\n isOpen,\r\n onClose,\r\n color = \"#2563eb\",\r\n title = \"Contact Us\",\r\n placeholder = \"Type a message...\",\r\n user,\r\n}: SupportChatModalProps) {\r\n const { messages, input, setInput, sending, sendMessage, handleKeyDown } =\r\n useChatEngine({ apiUrl, user });\r\n\r\n const colorScheme = useColorScheme();\r\n const theme = useMemo(() => getThemeTokens(colorScheme), [colorScheme]);\r\n\r\n const modalRef = useRef<HTMLDivElement>(null);\r\n const inputRef = useRef<HTMLInputElement>(null);\r\n const messagesEndRef = useRef<HTMLDivElement>(null);\r\n const closingRef = useRef(false);\r\n const backdropRef = useRef<HTMLDivElement>(null);\r\n\r\n // Inject modal keyframes on first render\r\n useEffect(() => {\r\n injectModalKeyframes();\r\n }, []);\r\n\r\n // Focus input when modal opens\r\n useEffect(() => {\r\n if (isOpen && inputRef.current) {\r\n // Small delay to let the modal render before focusing\r\n const timer = setTimeout(() => inputRef.current?.focus(), 50);\r\n return () => clearTimeout(timer);\r\n }\r\n }, [isOpen]);\r\n\r\n // Auto-scroll to latest message\r\n useEffect(() => {\r\n messagesEndRef.current?.scrollIntoView({ behavior: \"smooth\" });\r\n }, [messages]);\r\n\r\n // Prevent body scroll when modal is open\r\n useEffect(() => {\r\n if (isOpen) {\r\n const prev = document.body.style.overflow;\r\n document.body.style.overflow = \"hidden\";\r\n return () => {\r\n document.body.style.overflow = prev;\r\n };\r\n }\r\n }, [isOpen]);\r\n\r\n // Focus trap and Escape key handling\r\n useEffect(() => {\r\n if (!isOpen || !modalRef.current) return;\r\n\r\n const modal = modalRef.current;\r\n\r\n const handleTrapKeyDown = (e: KeyboardEvent) => {\r\n if (e.key === \"Escape\") {\r\n onClose();\r\n return;\r\n }\r\n\r\n if (e.key !== \"Tab\") return;\r\n\r\n const focusable = modal.querySelectorAll<HTMLElement>(\r\n 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])',\r\n );\r\n if (focusable.length === 0) return;\r\n\r\n const first = focusable[0]!;\r\n const last = focusable[focusable.length - 1]!;\r\n\r\n if (e.shiftKey) {\r\n if (document.activeElement === first) {\r\n e.preventDefault();\r\n last.focus();\r\n }\r\n } else {\r\n if (document.activeElement === last) {\r\n e.preventDefault();\r\n first.focus();\r\n }\r\n }\r\n };\r\n\r\n document.addEventListener(\"keydown\", handleTrapKeyDown);\r\n return () => document.removeEventListener(\"keydown\", handleTrapKeyDown);\r\n }, [isOpen, onClose]);\r\n\r\n // Handle close with animation\r\n const handleClose = useCallback(() => {\r\n closingRef.current = true;\r\n onClose();\r\n }, [onClose]);\r\n\r\n // Handle backdrop click\r\n const handleBackdropClick = useCallback(\r\n (e: React.MouseEvent) => {\r\n if (e.target === e.currentTarget) {\r\n handleClose();\r\n }\r\n },\r\n [handleClose],\r\n );\r\n\r\n if (!isOpen) return null;\r\n\r\n return (\r\n <>\r\n {/* Responsive style for full-screen on mobile */}\r\n <style>{`\r\n @media (max-width: 479px) {\r\n [data-support-chat-modal] {\r\n width: 100vw !important;\r\n height: 100vh !important;\r\n max-width: none !important;\r\n max-height: none !important;\r\n border-radius: 0 !important;\r\n margin: 0 !important;\r\n }\r\n [data-support-chat-modal-backdrop] {\r\n align-items: stretch !important;\r\n justify-content: stretch !important;\r\n }\r\n }\r\n `}</style>\r\n\r\n {/* Backdrop overlay */}\r\n <div\r\n ref={backdropRef}\r\n data-support-chat-modal-backdrop=\"\"\r\n onClick={handleBackdropClick}\r\n style={{\r\n position: \"fixed\",\r\n inset: 0,\r\n zIndex: 100000,\r\n backgroundColor: \"rgba(0, 0, 0, 0.5)\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"center\",\r\n fontFamily:\r\n '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif',\r\n fontSize: \"14px\",\r\n animation: \"sc-modal-backdrop-in 0.2s ease-out forwards\",\r\n }}\r\n >\r\n {/* Modal dialog */}\r\n <div\r\n ref={modalRef}\r\n role=\"dialog\"\r\n aria-label=\"Support chat\"\r\n aria-modal=\"true\"\r\n data-support-chat-modal=\"\"\r\n style={{\r\n width: \"500px\",\r\n maxWidth: \"calc(100vw - 32px)\",\r\n height: \"600px\",\r\n maxHeight: \"calc(100vh - 64px)\",\r\n backgroundColor: theme.panelBg,\r\n borderRadius: \"16px\",\r\n overflow: \"hidden\",\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n boxShadow: \"0 20px 60px rgba(0, 0, 0, 0.2)\",\r\n animation: \"sc-modal-slide-in 0.3s ease-out forwards\",\r\n }}\r\n >\r\n {/* Header */}\r\n <div\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n padding: \"20px 24px\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"space-between\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <span style={{ fontWeight: 600, fontSize: \"18px\" }}>{title}</span>\r\n <button\r\n onClick={handleClose}\r\n aria-label=\"Close chat\"\r\n style={{\r\n background: \"none\",\r\n border: \"none\",\r\n color: \"#fff\",\r\n cursor: \"pointer\",\r\n padding: \"4px\",\r\n lineHeight: 1,\r\n fontSize: \"20px\",\r\n }}\r\n >\r\n ✕\r\n </button>\r\n </div>\r\n\r\n {/* Messages */}\r\n <div\r\n role=\"log\"\r\n aria-live=\"polite\"\r\n aria-label=\"Chat messages\"\r\n style={{\r\n flex: 1,\r\n overflowY: \"auto\",\r\n padding: \"20px\",\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n gap: \"8px\",\r\n }}\r\n >\r\n {messages.length === 0 && (\r\n <div\r\n style={{\r\n color: theme.emptyText,\r\n textAlign: \"center\",\r\n marginTop: \"60px\",\r\n }}\r\n >\r\n Send us a message and we will get back to you!\r\n </div>\r\n )}\r\n {messages.map((msg) => (\r\n <div\r\n key={msg.id}\r\n style={{\r\n alignSelf:\r\n msg.sender === \"user\" ? \"flex-end\" : \"flex-start\",\r\n maxWidth: \"80%\",\r\n padding: \"10px 14px\",\r\n borderRadius:\r\n msg.sender === \"user\"\r\n ? \"16px 16px 4px 16px\"\r\n : \"16px 16px 16px 4px\",\r\n backgroundColor:\r\n msg.sender === \"user\" ? color : theme.receivedBg,\r\n color: msg.sender === \"user\" ? \"#fff\" : theme.receivedText,\r\n wordBreak: \"break-word\",\r\n }}\r\n >\r\n {msg.text}\r\n </div>\r\n ))}\r\n <div ref={messagesEndRef} />\r\n </div>\r\n\r\n {/* Input */}\r\n <div\r\n style={{\r\n borderTop: `1px solid ${theme.inputAreaBorder}`,\r\n padding: \"16px 20px\",\r\n display: \"flex\",\r\n gap: \"8px\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <input\r\n ref={inputRef}\r\n type=\"text\"\r\n value={input}\r\n onChange={(e) => setInput(e.target.value)}\r\n onKeyDown={handleKeyDown}\r\n placeholder={placeholder}\r\n aria-label=\"Type your message\"\r\n style={{\r\n flex: 1,\r\n border: `1px solid ${theme.inputBorder}`,\r\n borderRadius: \"8px\",\r\n padding: \"12px 14px\",\r\n fontSize: \"14px\",\r\n outline: \"none\",\r\n fontFamily: \"inherit\",\r\n backgroundColor: theme.inputBg,\r\n color: theme.inputText,\r\n }}\r\n />\r\n <button\r\n onClick={() => void sendMessage()}\r\n disabled={sending || !input.trim()}\r\n aria-label=\"Send message\"\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n border: \"none\",\r\n borderRadius: \"8px\",\r\n padding: \"12px 20px\",\r\n cursor:\r\n sending || !input.trim() ? \"not-allowed\" : \"pointer\",\r\n opacity: sending || !input.trim() ? 0.5 : 1,\r\n fontWeight: 600,\r\n fontSize: \"14px\",\r\n fontFamily: \"inherit\",\r\n transition: \"opacity 0.2s ease\",\r\n }}\r\n >\r\n Send\r\n </button>\r\n </div>\r\n </div>\r\n </div>\r\n </>\r\n );\r\n}\r\n","import { useState, useCallback } from \"react\";\r\nimport type { SupportChatState } from \"./types\";\r\n\r\n/**\r\n * Hook that returns controls for opening/closing the support chat.\r\n * Use with `<SupportChatModal />` for modal-triggered chat.\r\n *\r\n * @example\r\n * ```tsx\r\n * const { open, close, toggle, isOpen } = useSupportChat();\r\n * return <button onClick={open}>Contact Us</button>;\r\n * ```\r\n */\r\nexport function useSupportChat(): SupportChatState {\r\n const [isOpen, setIsOpen] = useState(false);\r\n\r\n const open = useCallback(() => setIsOpen(true), []);\r\n const close = useCallback(() => setIsOpen(false), []);\r\n const toggle = useCallback(() => setIsOpen((prev) => !prev), []);\r\n\r\n return { open, close, toggle, isOpen };\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/client/context.ts","../../src/client/useChatEngine.ts","../../src/client/useColorScheme.ts","../../src/client/ChatBubble.tsx","../../src/client/SupportChatModal.tsx","../../src/client/useSupportChat.ts","../../src/client/useUnreadCount.ts"],"names":["useState","useEffect","useCallback","useRef","useMemo","jsxs","Fragment","jsx","POLL_INTERVAL"],"mappings":";;;;;;AAEA,IAAM,WAAA,GAAc,yBAAA;AAGpB,SAAS,iBAAA,GAA4B;AACnC,EAAA,MAAM,KAAA,GACJ,gEAAA;AACF,EAAA,IAAI,EAAA,GAAK,EAAA;AACT,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,EAAA,EAAI,CAAA,EAAA,EAAK;AAC3B,IAAA,EAAA,IAAM,KAAA,CAAM,OAAO,IAAA,CAAK,KAAA,CAAM,KA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,KAAM;AACvD,UAAA,OAAO,CAAA,CAAE,SAAA,GAAY,MAAA,GAAS,CAAA,CAAE,SAAA,GAAY,MAAA;AAAA,QAC9C,CAAA,EAAG,qBAAA,CAAsB,OAAA,IAAW,EAAE,CAAA;AACtC,QAAA,qBAAA,CAAsB,OAAA,GAAU,eAAA;AAGhC,QAAA,MAAM,aAAA,GAA+B,UAAA,CAAW,GAAA,CAAI,CAAC,CAAA,MAAO;AAAA,UAC1D,IAAI,CAAA,CAAE,EAAA;AAAA,UACN,MAAM,CAAA,CAAE,IAAA;AAAA,UACR,MAAA,EAAQ,UAAA;AAAA,UACR,WAAW,IAAI,IAAA,CAAK,CAAA,CAAE,SAAS,EAAE,OAAA;AAAQ,SAC3C,CAAE,CAAA;AAEF,QAAA,iBAAA,CAAkB,CAAC,IAAA,KAAS,CAAC,GAAG,IAAA,EAAM,GAAG,aAAa,CAAC,CAAA;AAAA,MACzD,CAAA,CAAA,MAAQ;AAAA,MAER;AAAA,IACF,CAAA;AAGA,IAAA,KAAK,YAAA,EAAa;AAClB,IAAA,MAAM,aAAa,WAAA,CAAY,MAAM,KAAK,YAAA,IAAgBO,cAAa,CAAA;AAEvE,IAAA,OAAO,MAAM,cAAc,UAAU,CAAA;AAAA,EACvC,CAAA,EAAG,CAAC,UAAA,EAAY,MAAA,EAAQ,IAAI,CAAC,CAAA;AAE7B,EAAA,MAAM,cAAc,cAAA,CAAe,MAAA;AAEnC,EAAA,OAAO;AAAA,IACL,WAAA;AAAA,IACA,WAAW,WAAA,GAAc,CAAA;AAAA,IACzB,cAAA;AAAA,IACA;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import type { AnonymousContext } from \"./types\";\r\n\r\nconst SESSION_KEY = \"support-chat-session-id\";\r\n\r\n/** Generate a random session ID */\r\nfunction generateSessionId(): string {\r\n const chars =\r\n \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\";\r\n let id = \"\";\r\n for (let i = 0; i < 16; i++) {\r\n id += chars.charAt(Math.floor(Math.random() * chars.length));\r\n }\r\n return id;\r\n}\r\n\r\n/** Get or create a persistent session ID from localStorage */\r\nexport function getSessionId(): string {\r\n if (typeof window === \"undefined\") return generateSessionId();\r\n\r\n try {\r\n const existing = localStorage.getItem(SESSION_KEY);\r\n if (existing) return existing;\r\n\r\n const newId = generateSessionId();\r\n localStorage.setItem(SESSION_KEY, newId);\r\n return newId;\r\n } catch {\r\n // localStorage may be blocked (private browsing, etc.)\r\n return generateSessionId();\r\n }\r\n}\r\n\r\n/** Collect anonymous browser context */\r\nexport function collectAnonymousContext(): AnonymousContext {\r\n const sessionId = getSessionId();\r\n\r\n if (typeof window === \"undefined\") {\r\n return {\r\n pageUrl: \"\",\r\n referrer: \"\",\r\n userAgent: \"\",\r\n screenSize: \"\",\r\n timezone: \"\",\r\n sessionId,\r\n };\r\n }\r\n\r\n return {\r\n pageUrl: window.location.href,\r\n referrer: document.referrer,\r\n userAgent: navigator.userAgent,\r\n screenSize: `${window.screen.width}x${window.screen.height}`,\r\n timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,\r\n sessionId,\r\n };\r\n}\r\n","import { useState, useCallback, useEffect, useRef } from \"react\";\r\nimport type { ChatMessage, ChatUser } from \"./types\";\r\nimport { collectAnonymousContext, getSessionId } from \"./context\";\r\n\r\n/** Options for initializing the chat engine */\r\nexport interface ChatEngineOptions {\r\n /** URL of the support chat API endpoint */\r\n apiUrl: string;\r\n /** Authenticated user info (optional) */\r\n user?: ChatUser;\r\n /** URL to poll for team replies. When set, polling starts while chat is active. */\r\n repliesUrl?: string;\r\n /** Whether the chat panel is currently visible (controls polling lifecycle). Defaults to true. */\r\n isOpen?: boolean;\r\n}\r\n\r\n/** Return value from useChatEngine */\r\nexport interface ChatEngineState {\r\n /** All chat messages */\r\n messages: ChatMessage[];\r\n /** Current input value */\r\n input: string;\r\n /** Update the input value */\r\n setInput: (value: string) => void;\r\n /** Whether a message is currently being sent */\r\n sending: boolean;\r\n /** Send the current input as a message */\r\n sendMessage: () => Promise<void>;\r\n /** Handle keydown on the input (Enter to send) */\r\n handleKeyDown: (e: React.KeyboardEvent) => void;\r\n}\r\n\r\n/** Shape of the replies endpoint response */\r\ninterface RepliesApiResponse {\r\n replies: Array<{\r\n id: string;\r\n text: string;\r\n sender: string;\r\n timestamp: string;\r\n threadTs: string;\r\n }>;\r\n lastChecked: string;\r\n}\r\n\r\n/** Polling interval in milliseconds */\r\nconst POLL_INTERVAL = 4000;\r\n\r\n/**\r\n * Shared chat engine hook used by both ChatBubble and SupportChatModal.\r\n * Manages message state, input, API communication, and reply polling.\r\n */\r\nexport function useChatEngine({\r\n apiUrl,\r\n user,\r\n repliesUrl,\r\n isOpen = true,\r\n}: ChatEngineOptions): ChatEngineState {\r\n const [messages, setMessages] = useState<ChatMessage[]>([]);\r\n const [input, setInput] = useState(\"\");\r\n const [sending, setSending] = useState(false);\r\n\r\n // Track the latest reply timestamp for incremental polling (since parameter)\r\n const lastReplyTimestampRef = useRef<string | null>(null);\r\n // Track known reply IDs for deduplication\r\n const knownReplyIdsRef = useRef<Set<string>>(new Set());\r\n\r\n const sendMessage = useCallback(async () => {\r\n const text = input.trim();\r\n if (!text || sending) return;\r\n\r\n const msg: ChatMessage = {\r\n id: `msg-${Date.now()}`,\r\n text,\r\n sender: \"user\",\r\n timestamp: Date.now(),\r\n };\r\n\r\n setMessages((prev) => [...prev, msg]);\r\n setInput(\"\");\r\n setSending(true);\r\n\r\n try {\r\n const context = collectAnonymousContext();\r\n const response = await fetch(apiUrl, {\r\n method: \"POST\",\r\n headers: { \"Content-Type\": \"application/json\" },\r\n body: JSON.stringify({\r\n message: text,\r\n user: user ?? undefined,\r\n sessionId: user?.id ?? getSessionId(),\r\n context,\r\n }),\r\n });\r\n\r\n if (!response.ok) {\r\n setMessages((prev) => [\r\n ...prev,\r\n {\r\n id: `err-${Date.now()}`,\r\n text: \"Failed to send message. Please try again.\",\r\n sender: \"system\",\r\n timestamp: Date.now(),\r\n },\r\n ]);\r\n }\r\n } catch {\r\n setMessages((prev) => [\r\n ...prev,\r\n {\r\n id: `err-${Date.now()}`,\r\n text: \"Connection error. Please try again.\",\r\n sender: \"system\",\r\n timestamp: Date.now(),\r\n },\r\n ]);\r\n } finally {\r\n setSending(false);\r\n }\r\n }, [input, sending, apiUrl, user]);\r\n\r\n const handleKeyDown = useCallback(\r\n (e: React.KeyboardEvent) => {\r\n if (e.key === \"Enter\" && !e.shiftKey) {\r\n e.preventDefault();\r\n void sendMessage();\r\n }\r\n },\r\n [sendMessage],\r\n );\r\n\r\n // Reply polling effect\r\n useEffect(() => {\r\n if (!repliesUrl || !isOpen) return;\r\n\r\n const sessionId = user?.id ?? getSessionId();\r\n\r\n const fetchReplies = async () => {\r\n try {\r\n const params = new URLSearchParams({ sessionId });\r\n if (lastReplyTimestampRef.current) {\r\n params.set(\"since\", lastReplyTimestampRef.current);\r\n }\r\n\r\n const response = await fetch(`${repliesUrl}?${params.toString()}`);\r\n if (!response.ok) return;\r\n\r\n const data: RepliesApiResponse = await response.json();\r\n\r\n if (data.replies.length === 0) return;\r\n\r\n // Filter out already-known replies (deduplication)\r\n const newReplies = data.replies.filter(\r\n (r) => !knownReplyIdsRef.current.has(r.id),\r\n );\r\n\r\n if (newReplies.length === 0) return;\r\n\r\n // Track new reply IDs\r\n for (const r of newReplies) {\r\n knownReplyIdsRef.current.add(r.id);\r\n }\r\n\r\n // Update the since cursor to the latest reply timestamp\r\n const latestTimestamp = newReplies.reduce((latest, r) => {\r\n return r.timestamp > latest ? r.timestamp : latest;\r\n }, lastReplyTimestampRef.current ?? \"\");\r\n lastReplyTimestampRef.current = latestTimestamp;\r\n\r\n // Convert server replies to ChatMessage format and append\r\n const replyMessages: ChatMessage[] = newReplies.map((r) => ({\r\n id: r.id,\r\n text: r.text,\r\n sender: \"received\" as const,\r\n timestamp: new Date(r.timestamp).getTime(),\r\n }));\r\n\r\n setMessages((prev) => [...prev, ...replyMessages]);\r\n } catch {\r\n // Silently ignore polling errors — don't spam the user with error messages\r\n }\r\n };\r\n\r\n // Fetch immediately, then set up interval\r\n void fetchReplies();\r\n const intervalId = setInterval(() => void fetchReplies(), POLL_INTERVAL);\r\n\r\n return () => clearInterval(intervalId);\r\n }, [repliesUrl, isOpen, user]);\r\n\r\n return { messages, input, setInput, sending, sendMessage, handleKeyDown };\r\n}\r\n","import { useState, useEffect } from \"react\";\r\n\r\nexport type ColorScheme = \"light\" | \"dark\";\r\n\r\n/**\r\n * Detects the user's system color scheme preference via `prefers-color-scheme`.\r\n * Returns \"dark\" or \"light\". Updates reactively if the user changes their system setting.\r\n */\r\nexport function useColorScheme(): ColorScheme {\r\n const [scheme, setScheme] = useState<ColorScheme>(() => {\r\n if (typeof window === \"undefined\") return \"light\";\r\n return window.matchMedia(\"(prefers-color-scheme: dark)\").matches\r\n ? \"dark\"\r\n : \"light\";\r\n });\r\n\r\n useEffect(() => {\r\n if (typeof window === \"undefined\") return;\r\n const mq = window.matchMedia(\"(prefers-color-scheme: dark)\");\r\n const handler = (e: MediaQueryListEvent) => {\r\n setScheme(e.matches ? \"dark\" : \"light\");\r\n };\r\n mq.addEventListener(\"change\", handler);\r\n return () => mq.removeEventListener(\"change\", handler);\r\n }, []);\r\n\r\n return scheme;\r\n}\r\n\r\n/** Resolved color tokens for light and dark modes. */\r\nexport interface ThemeTokens {\r\n panelBg: string;\r\n panelBorder: string;\r\n inputBg: string;\r\n inputBorder: string;\r\n inputText: string;\r\n inputPlaceholder: string;\r\n receivedBg: string;\r\n receivedText: string;\r\n emptyText: string;\r\n inputAreaBorder: string;\r\n}\r\n\r\nconst lightTokens: ThemeTokens = {\r\n panelBg: \"#ffffff\",\r\n panelBorder: \"#e5e7eb\",\r\n inputBg: \"#ffffff\",\r\n inputBorder: \"#d1d5db\",\r\n inputText: \"#1f2937\",\r\n inputPlaceholder: \"#9ca3af\",\r\n receivedBg: \"#f3f4f6\",\r\n receivedText: \"#1f2937\",\r\n emptyText: \"#9ca3af\",\r\n inputAreaBorder: \"#e5e7eb\",\r\n};\r\n\r\nconst darkTokens: ThemeTokens = {\r\n panelBg: \"#1f2937\",\r\n panelBorder: \"#374151\",\r\n inputBg: \"#111827\",\r\n inputBorder: \"#4b5563\",\r\n inputText: \"#f9fafb\",\r\n inputPlaceholder: \"#9ca3af\",\r\n receivedBg: \"#374151\",\r\n receivedText: \"#f3f4f6\",\r\n emptyText: \"#6b7280\",\r\n inputAreaBorder: \"#374151\",\r\n};\r\n\r\nexport function getThemeTokens(scheme: ColorScheme): ThemeTokens {\r\n return scheme === \"dark\" ? darkTokens : lightTokens;\r\n}\r\n","import { useState, useCallback, useRef, useEffect, useMemo } from \"react\";\r\nimport type { ChatBubbleProps } from \"./types\";\r\nimport { useChatEngine } from \"./useChatEngine\";\r\nimport { useColorScheme, getThemeTokens } from \"./useColorScheme\";\r\n\r\n/**\r\n * Inject a <style> tag for keyframe animations.\r\n * Called once on first mount. Uses a data-attribute to avoid duplicates.\r\n */\r\nfunction injectKeyframes(): void {\r\n if (typeof document === \"undefined\") return;\r\n if (document.querySelector(\"[data-support-chat-keyframes]\")) return;\r\n\r\n const style = document.createElement(\"style\");\r\n style.setAttribute(\"data-support-chat-keyframes\", \"\");\r\n style.textContent = `\r\n @keyframes sc-slide-in {\r\n from { opacity: 0; transform: translateY(12px) scale(0.96); }\r\n to { opacity: 1; transform: translateY(0) scale(1); }\r\n }\r\n @keyframes sc-slide-out {\r\n from { opacity: 1; transform: translateY(0) scale(1); }\r\n to { opacity: 0; transform: translateY(12px) scale(0.96); }\r\n }\r\n @keyframes sc-badge-scale-in {\r\n from { transform: scale(0); }\r\n to { transform: scale(1); }\r\n }\r\n `;\r\n document.head.appendChild(style);\r\n}\r\n\r\n/**\r\n * ChatBubble -- floating support chat widget.\r\n *\r\n * Renders a circular button that opens an inline chat panel.\r\n * Messages are sent via POST to the configured `apiUrl`.\r\n *\r\n * @example\r\n * ```tsx\r\n * <ChatBubble apiUrl=\"/api/support\" />\r\n * ```\r\n */\r\nexport function ChatBubble({\r\n apiUrl,\r\n position = \"bottom-right\",\r\n color = \"#2563eb\",\r\n title = \"Support\",\r\n placeholder = \"Type a message...\",\r\n show = true,\r\n user,\r\n repliesUrl,\r\n isOpen: isOpenProp,\r\n onOpenChange,\r\n badgeColor = \"#eab308\",\r\n unreadCount = 0,\r\n}: ChatBubbleProps) {\r\n // Determine if we're in controlled mode\r\n const isControlled = isOpenProp !== undefined;\r\n\r\n // Internal state for uncontrolled mode\r\n const [isOpenInternal, setIsOpenInternal] = useState(false);\r\n\r\n // Resolved open state: controlled prop takes precedence\r\n const isOpen = isControlled ? isOpenProp : isOpenInternal;\r\n\r\n // Unified state setter that handles both modes\r\n const setIsOpen = useCallback(\r\n (valueOrUpdater: boolean | ((prev: boolean) => boolean)) => {\r\n const newValue =\r\n typeof valueOrUpdater === \"function\"\r\n ? valueOrUpdater(isOpen)\r\n : valueOrUpdater;\r\n\r\n if (isControlled) {\r\n // In controlled mode, only notify the parent -- don't set internal state\r\n onOpenChange?.(newValue);\r\n } else {\r\n // In uncontrolled mode, update internal state and notify if callback provided\r\n setIsOpenInternal(newValue);\r\n onOpenChange?.(newValue);\r\n }\r\n },\r\n [isControlled, isOpen, onOpenChange],\r\n );\r\n\r\n // Detect system color scheme\r\n const colorScheme = useColorScheme();\r\n const theme = useMemo(() => getThemeTokens(colorScheme), [colorScheme]);\r\n\r\n // Use shared chat engine for message logic\r\n const { messages, input, setInput, sending, sendMessage, handleKeyDown } =\r\n useChatEngine({ apiUrl, user, repliesUrl, isOpen });\r\n\r\n // Animation state: \"open\" | \"closing\" | \"closed\"\r\n const [panelState, setPanelState] = useState<\"open\" | \"closing\" | \"closed\">(\r\n \"closed\",\r\n );\r\n\r\n const panelRef = useRef<HTMLDivElement>(null);\r\n const messagesEndRef = useRef<HTMLDivElement>(null);\r\n const inputRef = useRef<HTMLInputElement>(null);\r\n\r\n // Inject keyframe animations on first render\r\n useEffect(() => {\r\n injectKeyframes();\r\n }, []);\r\n\r\n // Sync isOpen -> panelState\r\n useEffect(() => {\r\n if (isOpen) {\r\n setPanelState(\"open\");\r\n } else if (panelState === \"open\") {\r\n // Start closing animation\r\n setPanelState(\"closing\");\r\n }\r\n }, [isOpen]);\r\n\r\n // When closing animation ends, set to fully closed\r\n const handleAnimationEnd = useCallback(() => {\r\n if (panelState === \"closing\") {\r\n setPanelState(\"closed\");\r\n }\r\n }, [panelState]);\r\n\r\n // Focus input when panel opens\r\n useEffect(() => {\r\n if (panelState === \"open\" && inputRef.current) {\r\n inputRef.current.focus();\r\n }\r\n }, [panelState]);\r\n\r\n // Auto-scroll to latest message\r\n useEffect(() => {\r\n messagesEndRef.current?.scrollIntoView({ behavior: \"smooth\" });\r\n }, [messages]);\r\n\r\n // Focus trap: keep Tab cycling inside the dialog when open\r\n useEffect(() => {\r\n if (panelState !== \"open\" || !panelRef.current) return;\r\n\r\n const panel = panelRef.current;\r\n\r\n const handleTrapKeyDown = (e: KeyboardEvent) => {\r\n if (e.key === \"Escape\") {\r\n setIsOpen(false);\r\n return;\r\n }\r\n\r\n if (e.key !== \"Tab\") return;\r\n\r\n const focusable = panel.querySelectorAll<HTMLElement>(\r\n 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])',\r\n );\r\n if (focusable.length === 0) return;\r\n\r\n const first = focusable[0]!;\r\n const last = focusable[focusable.length - 1]!;\r\n\r\n if (e.shiftKey) {\r\n if (document.activeElement === first) {\r\n e.preventDefault();\r\n last.focus();\r\n }\r\n } else {\r\n if (document.activeElement === last) {\r\n e.preventDefault();\r\n first.focus();\r\n }\r\n }\r\n };\r\n\r\n document.addEventListener(\"keydown\", handleTrapKeyDown);\r\n return () => document.removeEventListener(\"keydown\", handleTrapKeyDown);\r\n }, [panelState, setIsOpen]);\r\n\r\n const toggle = useCallback(() => setIsOpen((o) => !o), [setIsOpen]);\r\n\r\n // Position styles for the bubble button\r\n const positionStyles: React.CSSProperties = {\r\n position: \"fixed\",\r\n zIndex: 99999,\r\n ...(position.includes(\"bottom\") ? { bottom: \"20px\" } : { top: \"20px\" }),\r\n ...(position.includes(\"right\") ? { right: \"20px\" } : { left: \"20px\" }),\r\n };\r\n\r\n // Panel position & responsive sizing\r\n // On mobile (<480px viewport), expand to near-full screen\r\n const panelPositionStyles: React.CSSProperties = {\r\n position: \"fixed\",\r\n zIndex: 99999,\r\n width: \"380px\",\r\n maxWidth: \"calc(100vw - 40px)\",\r\n height: \"500px\",\r\n maxHeight: \"calc(100vh - 120px)\",\r\n ...(position.includes(\"bottom\") ? { bottom: \"80px\" } : { top: \"80px\" }),\r\n ...(position.includes(\"right\") ? { right: \"20px\" } : { left: \"20px\" }),\r\n };\r\n\r\n // Check if panel is visible (open or animating out)\r\n const showPanel = panelState === \"open\" || panelState === \"closing\";\r\n\r\n return (\r\n <>\r\n {/* Responsive overrides injected as inline <style> */}\r\n {showPanel && (\r\n <style>{`\r\n @media (max-width: 479px) {\r\n [data-support-chat-panel] {\r\n width: calc(100vw - 16px) !important;\r\n height: calc(100vh - 100px) !important;\r\n max-width: none !important;\r\n max-height: none !important;\r\n left: 8px !important;\r\n right: 8px !important;\r\n bottom: 72px !important;\r\n border-radius: 12px !important;\r\n }\r\n }\r\n `}</style>\r\n )}\r\n\r\n {/* Floating bubble button */}\r\n {show && (\r\n <button\r\n onClick={toggle}\r\n aria-label={isOpen ? \"Close support chat\" : \"Open support chat\"}\r\n aria-expanded={isOpen}\r\n aria-haspopup=\"dialog\"\r\n style={{\r\n ...positionStyles,\r\n width: \"56px\",\r\n height: \"56px\",\r\n borderRadius: \"50%\",\r\n backgroundColor: color,\r\n border: \"none\",\r\n cursor: \"pointer\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"center\",\r\n boxShadow: \"0 4px 12px rgba(0,0,0,0.15)\",\r\n transition: \"transform 0.2s ease, box-shadow 0.2s ease\",\r\n overflow: \"visible\",\r\n }}\r\n onMouseEnter={(e) => {\r\n e.currentTarget.style.transform = \"scale(1.1)\";\r\n e.currentTarget.style.boxShadow = \"0 6px 20px rgba(0,0,0,0.2)\";\r\n }}\r\n onMouseLeave={(e) => {\r\n e.currentTarget.style.transform = \"scale(1)\";\r\n e.currentTarget.style.boxShadow = \"0 4px 12px rgba(0,0,0,0.15)\";\r\n }}\r\n >\r\n <svg\r\n width=\"24\"\r\n height=\"24\"\r\n viewBox=\"0 0 24 24\"\r\n fill=\"none\"\r\n stroke=\"white\"\r\n strokeWidth=\"2\"\r\n strokeLinecap=\"round\"\r\n strokeLinejoin=\"round\"\r\n aria-hidden=\"true\"\r\n >\r\n {isOpen ? (\r\n <path d=\"M18 6L6 18M6 6l12 12\" />\r\n ) : (\r\n <path d=\"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z\" />\r\n )}\r\n </svg>\r\n {/* Unread badge */}\r\n {!isOpen && unreadCount > 0 && (\r\n <span\r\n data-testid=\"unread-badge\"\r\n aria-label={`${unreadCount > 9 ? \"9+\" : unreadCount} unread messages`}\r\n style={{\r\n position: \"absolute\",\r\n top: \"-4px\",\r\n right: \"-4px\",\r\n minWidth: \"20px\",\r\n height: \"20px\",\r\n borderRadius: \"10px\",\r\n backgroundColor: badgeColor,\r\n color: \"#fff\",\r\n fontSize: \"11px\",\r\n fontWeight: 700,\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"center\",\r\n padding: \"0 5px\",\r\n lineHeight: 1,\r\n boxShadow: \"0 2px 4px rgba(0,0,0,0.2)\",\r\n animation: \"sc-badge-scale-in 0.2s ease-out forwards\",\r\n }}\r\n >\r\n {unreadCount > 9 ? \"9+\" : unreadCount}\r\n </span>\r\n )}\r\n </button>\r\n )}\r\n\r\n {/* Chat panel */}\r\n {showPanel && (\r\n <div\r\n ref={panelRef}\r\n role=\"dialog\"\r\n aria-label=\"Support chat\"\r\n aria-modal=\"true\"\r\n data-support-chat-panel=\"\"\r\n onAnimationEnd={handleAnimationEnd}\r\n style={{\r\n ...panelPositionStyles,\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n borderRadius: \"12px\",\r\n overflow: \"hidden\",\r\n boxShadow: \"0 8px 30px rgba(0,0,0,0.12)\",\r\n fontFamily:\r\n '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif',\r\n fontSize: \"14px\",\r\n backgroundColor: theme.panelBg,\r\n border: `1px solid ${theme.panelBorder}`,\r\n animation:\r\n panelState === \"open\"\r\n ? \"sc-slide-in 0.25s ease-out forwards\"\r\n : \"sc-slide-out 0.2s ease-in forwards\",\r\n }}\r\n >\r\n {/* Header */}\r\n <div\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n padding: \"16px\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"space-between\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <span style={{ fontWeight: 600, fontSize: \"16px\" }}>{title}</span>\r\n <button\r\n onClick={() => setIsOpen(false)}\r\n aria-label=\"Close chat\"\r\n style={{\r\n background: \"none\",\r\n border: \"none\",\r\n color: \"#fff\",\r\n cursor: \"pointer\",\r\n padding: \"4px\",\r\n lineHeight: 1,\r\n fontSize: \"18px\",\r\n }}\r\n >\r\n ✕\r\n </button>\r\n </div>\r\n\r\n {/* Messages */}\r\n <div\r\n role=\"log\"\r\n aria-live=\"polite\"\r\n aria-label=\"Chat messages\"\r\n style={{\r\n flex: 1,\r\n overflowY: \"auto\",\r\n padding: \"16px\",\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n gap: \"8px\",\r\n }}\r\n >\r\n {messages.length === 0 && (\r\n <div\r\n style={{\r\n color: theme.emptyText,\r\n textAlign: \"center\",\r\n marginTop: \"40px\",\r\n }}\r\n >\r\n Send us a message and we will get back to you!\r\n </div>\r\n )}\r\n {messages.map((msg) => (\r\n <div\r\n key={msg.id}\r\n data-sender={msg.sender}\r\n style={{\r\n alignSelf:\r\n msg.sender === \"user\" ? \"flex-end\" : \"flex-start\",\r\n maxWidth: \"80%\",\r\n padding: \"10px 14px\",\r\n borderRadius:\r\n msg.sender === \"user\"\r\n ? \"16px 16px 4px 16px\"\r\n : \"16px 16px 16px 4px\",\r\n backgroundColor:\r\n msg.sender === \"user\" ? color : theme.receivedBg,\r\n color: msg.sender === \"user\" ? \"#fff\" : theme.receivedText,\r\n wordBreak: \"break-word\",\r\n }}\r\n >\r\n {msg.text}\r\n </div>\r\n ))}\r\n <div ref={messagesEndRef} />\r\n </div>\r\n\r\n {/* Input */}\r\n <div\r\n style={{\r\n borderTop: `1px solid ${theme.inputAreaBorder}`,\r\n padding: \"12px\",\r\n display: \"flex\",\r\n gap: \"8px\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <input\r\n ref={inputRef}\r\n type=\"text\"\r\n value={input}\r\n onChange={(e) => setInput(e.target.value)}\r\n onKeyDown={handleKeyDown}\r\n placeholder={placeholder}\r\n aria-label=\"Type your message\"\r\n style={{\r\n flex: 1,\r\n border: `1px solid ${theme.inputBorder}`,\r\n borderRadius: \"8px\",\r\n padding: \"10px 12px\",\r\n fontSize: \"14px\",\r\n outline: \"none\",\r\n fontFamily: \"inherit\",\r\n backgroundColor: theme.inputBg,\r\n color: theme.inputText,\r\n }}\r\n />\r\n <button\r\n onClick={() => void sendMessage()}\r\n disabled={sending || !input.trim()}\r\n aria-label=\"Send message\"\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n border: \"none\",\r\n borderRadius: \"8px\",\r\n padding: \"10px 16px\",\r\n cursor:\r\n sending || !input.trim() ? \"not-allowed\" : \"pointer\",\r\n opacity: sending || !input.trim() ? 0.5 : 1,\r\n fontWeight: 600,\r\n fontSize: \"14px\",\r\n fontFamily: \"inherit\",\r\n transition: \"opacity 0.2s ease\",\r\n }}\r\n >\r\n Send\r\n </button>\r\n </div>\r\n </div>\r\n )}\r\n </>\r\n );\r\n}\r\n","import { useRef, useEffect, useCallback, useMemo } from \"react\";\r\nimport type { SupportChatModalProps } from \"./types\";\r\nimport { useChatEngine } from \"./useChatEngine\";\r\nimport { useColorScheme, getThemeTokens } from \"./useColorScheme\";\r\n\r\n/**\r\n * Inject a <style> tag for modal keyframe animations.\r\n * Called once on first mount. Uses a data-attribute to avoid duplicates.\r\n */\r\nfunction injectModalKeyframes(): void {\r\n if (typeof document === \"undefined\") return;\r\n if (document.querySelector(\"[data-support-chat-modal-keyframes]\")) return;\r\n\r\n const style = document.createElement(\"style\");\r\n style.setAttribute(\"data-support-chat-modal-keyframes\", \"\");\r\n style.textContent = `\r\n @keyframes sc-modal-backdrop-in {\r\n from { opacity: 0; }\r\n to { opacity: 1; }\r\n }\r\n @keyframes sc-modal-backdrop-out {\r\n from { opacity: 1; }\r\n to { opacity: 0; }\r\n }\r\n @keyframes sc-modal-slide-in {\r\n from { opacity: 0; transform: translateY(20px) scale(0.96); }\r\n to { opacity: 1; transform: translateY(0) scale(1); }\r\n }\r\n @keyframes sc-modal-slide-out {\r\n from { opacity: 1; transform: translateY(0) scale(1); }\r\n to { opacity: 0; transform: translateY(20px) scale(0.96); }\r\n }\r\n `;\r\n document.head.appendChild(style);\r\n}\r\n\r\n/**\r\n * SupportChatModal -- modal-based support chat.\r\n *\r\n * Renders a centered modal dialog for support chat, designed to be\r\n * triggered from custom UI elements (e.g., \"Contact Us\" links).\r\n *\r\n * Desktop: centered modal, max-width ~500px, with backdrop overlay.\r\n * Mobile: full-screen modal.\r\n *\r\n * Use with the `useSupportChat()` hook:\r\n *\r\n * @example\r\n * ```tsx\r\n * import { SupportChatModal, useSupportChat } from 'support-chat';\r\n *\r\n * function App() {\r\n * const { open, close, isOpen } = useSupportChat();\r\n * return (\r\n * <>\r\n * <button onClick={open}>Contact Us</button>\r\n * <SupportChatModal\r\n * apiUrl=\"/api/support\"\r\n * isOpen={isOpen}\r\n * onClose={close}\r\n * />\r\n * </>\r\n * );\r\n * }\r\n * ```\r\n */\r\nexport function SupportChatModal({\r\n apiUrl,\r\n isOpen,\r\n onClose,\r\n color = \"#2563eb\",\r\n title = \"Contact Us\",\r\n placeholder = \"Type a message...\",\r\n user,\r\n repliesUrl,\r\n}: SupportChatModalProps) {\r\n const { messages, input, setInput, sending, sendMessage, handleKeyDown } =\r\n useChatEngine({ apiUrl, user, repliesUrl, isOpen });\r\n\r\n const colorScheme = useColorScheme();\r\n const theme = useMemo(() => getThemeTokens(colorScheme), [colorScheme]);\r\n\r\n const modalRef = useRef<HTMLDivElement>(null);\r\n const inputRef = useRef<HTMLInputElement>(null);\r\n const messagesEndRef = useRef<HTMLDivElement>(null);\r\n const closingRef = useRef(false);\r\n const backdropRef = useRef<HTMLDivElement>(null);\r\n\r\n // Inject modal keyframes on first render\r\n useEffect(() => {\r\n injectModalKeyframes();\r\n }, []);\r\n\r\n // Focus input when modal opens\r\n useEffect(() => {\r\n if (isOpen && inputRef.current) {\r\n // Small delay to let the modal render before focusing\r\n const timer = setTimeout(() => inputRef.current?.focus(), 50);\r\n return () => clearTimeout(timer);\r\n }\r\n }, [isOpen]);\r\n\r\n // Auto-scroll to latest message\r\n useEffect(() => {\r\n messagesEndRef.current?.scrollIntoView({ behavior: \"smooth\" });\r\n }, [messages]);\r\n\r\n // Prevent body scroll when modal is open\r\n useEffect(() => {\r\n if (isOpen) {\r\n const prev = document.body.style.overflow;\r\n document.body.style.overflow = \"hidden\";\r\n return () => {\r\n document.body.style.overflow = prev;\r\n };\r\n }\r\n }, [isOpen]);\r\n\r\n // Focus trap and Escape key handling\r\n useEffect(() => {\r\n if (!isOpen || !modalRef.current) return;\r\n\r\n const modal = modalRef.current;\r\n\r\n const handleTrapKeyDown = (e: KeyboardEvent) => {\r\n if (e.key === \"Escape\") {\r\n onClose();\r\n return;\r\n }\r\n\r\n if (e.key !== \"Tab\") return;\r\n\r\n const focusable = modal.querySelectorAll<HTMLElement>(\r\n 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])',\r\n );\r\n if (focusable.length === 0) return;\r\n\r\n const first = focusable[0]!;\r\n const last = focusable[focusable.length - 1]!;\r\n\r\n if (e.shiftKey) {\r\n if (document.activeElement === first) {\r\n e.preventDefault();\r\n last.focus();\r\n }\r\n } else {\r\n if (document.activeElement === last) {\r\n e.preventDefault();\r\n first.focus();\r\n }\r\n }\r\n };\r\n\r\n document.addEventListener(\"keydown\", handleTrapKeyDown);\r\n return () => document.removeEventListener(\"keydown\", handleTrapKeyDown);\r\n }, [isOpen, onClose]);\r\n\r\n // Handle close with animation\r\n const handleClose = useCallback(() => {\r\n closingRef.current = true;\r\n onClose();\r\n }, [onClose]);\r\n\r\n // Handle backdrop click\r\n const handleBackdropClick = useCallback(\r\n (e: React.MouseEvent) => {\r\n if (e.target === e.currentTarget) {\r\n handleClose();\r\n }\r\n },\r\n [handleClose],\r\n );\r\n\r\n if (!isOpen) return null;\r\n\r\n return (\r\n <>\r\n {/* Responsive style for full-screen on mobile */}\r\n <style>{`\r\n @media (max-width: 479px) {\r\n [data-support-chat-modal] {\r\n width: 100vw !important;\r\n height: 100vh !important;\r\n max-width: none !important;\r\n max-height: none !important;\r\n border-radius: 0 !important;\r\n margin: 0 !important;\r\n }\r\n [data-support-chat-modal-backdrop] {\r\n align-items: stretch !important;\r\n justify-content: stretch !important;\r\n }\r\n }\r\n `}</style>\r\n\r\n {/* Backdrop overlay */}\r\n <div\r\n ref={backdropRef}\r\n data-support-chat-modal-backdrop=\"\"\r\n onClick={handleBackdropClick}\r\n style={{\r\n position: \"fixed\",\r\n inset: 0,\r\n zIndex: 100000,\r\n backgroundColor: \"rgba(0, 0, 0, 0.5)\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"center\",\r\n fontFamily:\r\n '-apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, sans-serif',\r\n fontSize: \"14px\",\r\n animation: \"sc-modal-backdrop-in 0.2s ease-out forwards\",\r\n }}\r\n >\r\n {/* Modal dialog */}\r\n <div\r\n ref={modalRef}\r\n role=\"dialog\"\r\n aria-label=\"Support chat\"\r\n aria-modal=\"true\"\r\n data-support-chat-modal=\"\"\r\n style={{\r\n width: \"500px\",\r\n maxWidth: \"calc(100vw - 32px)\",\r\n height: \"600px\",\r\n maxHeight: \"calc(100vh - 64px)\",\r\n backgroundColor: theme.panelBg,\r\n borderRadius: \"16px\",\r\n overflow: \"hidden\",\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n boxShadow: \"0 20px 60px rgba(0, 0, 0, 0.2)\",\r\n animation: \"sc-modal-slide-in 0.3s ease-out forwards\",\r\n }}\r\n >\r\n {/* Header */}\r\n <div\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n padding: \"20px 24px\",\r\n display: \"flex\",\r\n alignItems: \"center\",\r\n justifyContent: \"space-between\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <span style={{ fontWeight: 600, fontSize: \"18px\" }}>{title}</span>\r\n <button\r\n onClick={handleClose}\r\n aria-label=\"Close chat\"\r\n style={{\r\n background: \"none\",\r\n border: \"none\",\r\n color: \"#fff\",\r\n cursor: \"pointer\",\r\n padding: \"4px\",\r\n lineHeight: 1,\r\n fontSize: \"20px\",\r\n }}\r\n >\r\n ✕\r\n </button>\r\n </div>\r\n\r\n {/* Messages */}\r\n <div\r\n role=\"log\"\r\n aria-live=\"polite\"\r\n aria-label=\"Chat messages\"\r\n style={{\r\n flex: 1,\r\n overflowY: \"auto\",\r\n padding: \"20px\",\r\n display: \"flex\",\r\n flexDirection: \"column\",\r\n gap: \"8px\",\r\n }}\r\n >\r\n {messages.length === 0 && (\r\n <div\r\n style={{\r\n color: theme.emptyText,\r\n textAlign: \"center\",\r\n marginTop: \"60px\",\r\n }}\r\n >\r\n Send us a message and we will get back to you!\r\n </div>\r\n )}\r\n {messages.map((msg) => (\r\n <div\r\n key={msg.id}\r\n data-sender={msg.sender}\r\n style={{\r\n alignSelf:\r\n msg.sender === \"user\" ? \"flex-end\" : \"flex-start\",\r\n maxWidth: \"80%\",\r\n padding: \"10px 14px\",\r\n borderRadius:\r\n msg.sender === \"user\"\r\n ? \"16px 16px 4px 16px\"\r\n : \"16px 16px 16px 4px\",\r\n backgroundColor:\r\n msg.sender === \"user\" ? color : theme.receivedBg,\r\n color: msg.sender === \"user\" ? \"#fff\" : theme.receivedText,\r\n wordBreak: \"break-word\",\r\n }}\r\n >\r\n {msg.text}\r\n </div>\r\n ))}\r\n <div ref={messagesEndRef} />\r\n </div>\r\n\r\n {/* Input */}\r\n <div\r\n style={{\r\n borderTop: `1px solid ${theme.inputAreaBorder}`,\r\n padding: \"16px 20px\",\r\n display: \"flex\",\r\n gap: \"8px\",\r\n flexShrink: 0,\r\n }}\r\n >\r\n <input\r\n ref={inputRef}\r\n type=\"text\"\r\n value={input}\r\n onChange={(e) => setInput(e.target.value)}\r\n onKeyDown={handleKeyDown}\r\n placeholder={placeholder}\r\n aria-label=\"Type your message\"\r\n style={{\r\n flex: 1,\r\n border: `1px solid ${theme.inputBorder}`,\r\n borderRadius: \"8px\",\r\n padding: \"12px 14px\",\r\n fontSize: \"14px\",\r\n outline: \"none\",\r\n fontFamily: \"inherit\",\r\n backgroundColor: theme.inputBg,\r\n color: theme.inputText,\r\n }}\r\n />\r\n <button\r\n onClick={() => void sendMessage()}\r\n disabled={sending || !input.trim()}\r\n aria-label=\"Send message\"\r\n style={{\r\n backgroundColor: color,\r\n color: \"#fff\",\r\n border: \"none\",\r\n borderRadius: \"8px\",\r\n padding: \"12px 20px\",\r\n cursor:\r\n sending || !input.trim() ? \"not-allowed\" : \"pointer\",\r\n opacity: sending || !input.trim() ? 0.5 : 1,\r\n fontWeight: 600,\r\n fontSize: \"14px\",\r\n fontFamily: \"inherit\",\r\n transition: \"opacity 0.2s ease\",\r\n }}\r\n >\r\n Send\r\n </button>\r\n </div>\r\n </div>\r\n </div>\r\n </>\r\n );\r\n}\r\n","import { useState, useCallback } from \"react\";\r\nimport type { SupportChatState } from \"./types\";\r\n\r\n/**\r\n * Hook that returns controls for opening/closing the support chat.\r\n * Use with `<SupportChatModal />` for modal-triggered chat.\r\n *\r\n * @example\r\n * ```tsx\r\n * const { open, close, toggle, isOpen } = useSupportChat();\r\n * return <button onClick={open}>Contact Us</button>;\r\n * ```\r\n */\r\nexport function useSupportChat(): SupportChatState {\r\n const [isOpen, setIsOpen] = useState(false);\r\n\r\n const open = useCallback(() => setIsOpen(true), []);\r\n const close = useCallback(() => setIsOpen(false), []);\r\n const toggle = useCallback(() => setIsOpen((prev) => !prev), []);\r\n\r\n return { open, close, toggle, isOpen };\r\n}\r\n","import { useState, useCallback, useEffect, useRef } from \"react\";\r\nimport type { ChatMessage, ChatUser } from \"./types\";\r\nimport { getSessionId } from \"./context\";\r\n\r\n/** Options for the useUnreadCount hook */\r\nexport interface UnreadCountOptions {\r\n /** URL to poll for team replies (e.g., '/api/support/replies') */\r\n repliesUrl: string;\r\n /** Authenticated user info (optional). When provided, user.id is used as sessionId. */\r\n user?: ChatUser;\r\n /** Whether the chat panel is currently open. Polling pauses when true. */\r\n isOpen: boolean;\r\n}\r\n\r\n/** Return value from useUnreadCount */\r\nexport interface UnreadCountState {\r\n /** Number of unread reply messages */\r\n unreadCount: number;\r\n /** Convenience boolean: true when unreadCount > 0 */\r\n hasUnread: boolean;\r\n /** Pre-fetched reply messages ready to display instantly when chat opens */\r\n pendingReplies: ChatMessage[];\r\n /** Reset unreadCount to 0 and clear pendingReplies */\r\n markAsRead: () => void;\r\n}\r\n\r\n/** Shape of the replies endpoint response */\r\ninterface RepliesApiResponse {\r\n replies: Array<{\r\n id: string;\r\n text: string;\r\n sender: string;\r\n timestamp: string;\r\n threadTs: string;\r\n }>;\r\n lastChecked: string;\r\n}\r\n\r\n/** Background polling interval in milliseconds */\r\nconst POLL_INTERVAL = 4000;\r\n\r\n/**\r\n * Hook that tracks unread reply count in the background via polling.\r\n *\r\n * Polls the replies endpoint every 4 seconds when `isOpen` is false.\r\n * Stops background polling when `isOpen` is true (useChatEngine handles it at that point).\r\n * Pre-fetches reply messages so they appear instantly when the chat opens.\r\n *\r\n * Can be used independently of ChatBubble -- any component can call useUnreadCount\r\n * to build custom unread UX.\r\n *\r\n * @example\r\n * ```tsx\r\n * const { unreadCount, hasUnread, pendingReplies, markAsRead } = useUnreadCount({\r\n * repliesUrl: '/api/support/replies',\r\n * user: currentUser,\r\n * isOpen: false,\r\n * });\r\n *\r\n * return (\r\n * <button onClick={openChat}>\r\n * Support {hasUnread && <span className=\"badge\">{unreadCount}</span>}\r\n * </button>\r\n * );\r\n * ```\r\n */\r\nexport function useUnreadCount({\r\n repliesUrl,\r\n user,\r\n isOpen,\r\n}: UnreadCountOptions): UnreadCountState {\r\n const [pendingReplies, setPendingReplies] = useState<ChatMessage[]>([]);\r\n\r\n // Track the latest reply timestamp for incremental polling (since parameter)\r\n const lastReplyTimestampRef = useRef<string | null>(null);\r\n // Track known reply IDs for deduplication\r\n const knownReplyIdsRef = useRef<Set<string>>(new Set());\r\n // Track previous isOpen value to detect transitions\r\n const prevIsOpenRef = useRef<boolean>(isOpen);\r\n\r\n // Auto-reset when isOpen transitions from false to true\r\n useEffect(() => {\r\n if (isOpen && !prevIsOpenRef.current) {\r\n // Transitioning from closed to open: reset unread state\r\n setPendingReplies([]);\r\n knownReplyIdsRef.current.clear();\r\n lastReplyTimestampRef.current = null;\r\n }\r\n prevIsOpenRef.current = isOpen;\r\n }, [isOpen]);\r\n\r\n const markAsRead = useCallback(() => {\r\n setPendingReplies([]);\r\n knownReplyIdsRef.current.clear();\r\n lastReplyTimestampRef.current = null;\r\n }, []);\r\n\r\n // Background polling effect -- only runs when chat is CLOSED\r\n useEffect(() => {\r\n if (!repliesUrl || isOpen) return;\r\n\r\n const sessionId = user?.id ?? getSessionId();\r\n\r\n const fetchReplies = async () => {\r\n try {\r\n const params = new URLSearchParams({ sessionId });\r\n if (lastReplyTimestampRef.current) {\r\n params.set(\"since\", lastReplyTimestampRef.current);\r\n }\r\n\r\n const response = await fetch(`${repliesUrl}?${params.toString()}`);\r\n if (!response.ok) return;\r\n\r\n const data: RepliesApiResponse = await response.json();\r\n\r\n if (data.replies.length === 0) return;\r\n\r\n // Filter out already-known replies (deduplication)\r\n const newReplies = data.replies.filter(\r\n (r) => !knownReplyIdsRef.current.has(r.id),\r\n );\r\n\r\n if (newReplies.length === 0) return;\r\n\r\n // Track new reply IDs\r\n for (const r of newReplies) {\r\n knownReplyIdsRef.current.add(r.id);\r\n }\r\n\r\n // Update the since cursor to the latest reply timestamp\r\n const latestTimestamp = newReplies.reduce((latest, r) => {\r\n return r.timestamp > latest ? r.timestamp : latest;\r\n }, lastReplyTimestampRef.current ?? \"\");\r\n lastReplyTimestampRef.current = latestTimestamp;\r\n\r\n // Convert server replies to ChatMessage format and append\r\n const replyMessages: ChatMessage[] = newReplies.map((r) => ({\r\n id: r.id,\r\n text: r.text,\r\n sender: \"received\" as const,\r\n timestamp: new Date(r.timestamp).getTime(),\r\n }));\r\n\r\n setPendingReplies((prev) => [...prev, ...replyMessages]);\r\n } catch {\r\n // Silently ignore polling errors\r\n }\r\n };\r\n\r\n // Fetch immediately, then set up interval\r\n void fetchReplies();\r\n const intervalId = setInterval(() => void fetchReplies(), POLL_INTERVAL);\r\n\r\n return () => clearInterval(intervalId);\r\n }, [repliesUrl, isOpen, user]);\r\n\r\n const unreadCount = pendingReplies.length;\r\n\r\n return {\r\n unreadCount,\r\n hasUnread: unreadCount > 0,\r\n pendingReplies,\r\n markAsRead,\r\n };\r\n}\r\n"]}
|