@customerhero/react 2.1.1 → 2.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.
@@ -0,0 +1,2932 @@
1
+ // src/context.tsx
2
+ import {
3
+ createContext,
4
+ useContext,
5
+ useEffect,
6
+ useRef
7
+ } from "react";
8
+ import {
9
+ CustomerHeroChat
10
+ } from "@customerhero/js";
11
+ import { jsx } from "react/jsx-runtime";
12
+ var CustomerHeroContext = createContext(null);
13
+ function CustomerHeroProvider({
14
+ children,
15
+ disableAutoFetch,
16
+ ...config
17
+ }) {
18
+ const clientRef = useRef(null);
19
+ if (!clientRef.current) {
20
+ clientRef.current = new CustomerHeroChat(config);
21
+ }
22
+ useEffect(() => {
23
+ if (disableAutoFetch) return;
24
+ clientRef.current?.fetchConfig();
25
+ }, [disableAutoFetch]);
26
+ return /* @__PURE__ */ jsx(CustomerHeroContext.Provider, { value: clientRef.current, children });
27
+ }
28
+ function useCustomerHeroClient() {
29
+ const client = useContext(CustomerHeroContext);
30
+ if (!client) {
31
+ throw new Error(
32
+ "useCustomerHeroClient must be used within a <CustomerHeroProvider>"
33
+ );
34
+ }
35
+ return client;
36
+ }
37
+
38
+ // src/use-chat.ts
39
+ import { useCallback, useSyncExternalStore } from "react";
40
+ function useChat() {
41
+ const client = useCustomerHeroClient();
42
+ const state = useSyncExternalStore(
43
+ useCallback((cb) => client.subscribe(cb), [client]),
44
+ () => client.getState(),
45
+ () => client.getState()
46
+ );
47
+ return {
48
+ ...state,
49
+ t: client.t,
50
+ sendMessage: useCallback(
51
+ (message, options) => client.sendMessage(message, options),
52
+ [client]
53
+ ),
54
+ uploadAttachment: useCallback(
55
+ (blob, options) => client.uploadAttachment(blob, options),
56
+ [client]
57
+ ),
58
+ rateMessage: useCallback(
59
+ (messageId, rating) => client.rateMessage(messageId, rating),
60
+ [client]
61
+ ),
62
+ approveAction: useCallback(
63
+ (pendingId) => client.approveAction(pendingId),
64
+ [client]
65
+ ),
66
+ cancelAction: useCallback(
67
+ (pendingId) => client.cancelAction(pendingId),
68
+ [client]
69
+ ),
70
+ setLocale: useCallback((tag) => client.setLocale(tag), [client]),
71
+ toggle: useCallback(() => client.toggle(), [client]),
72
+ open: useCallback(() => client.open(), [client]),
73
+ close: useCallback(() => client.close(), [client]),
74
+ reset: useCallback(() => client.reset(), [client]),
75
+ identify: useCallback(
76
+ (payload) => client.identify(payload),
77
+ [client]
78
+ ),
79
+ setConsent: useCallback(
80
+ (consent) => client.setConsent(consent),
81
+ [client]
82
+ ),
83
+ setTraits: useCallback(
84
+ (traits) => client.setTraits(traits),
85
+ [client]
86
+ ),
87
+ submitPreChatForm: useCallback(
88
+ (submission) => client.submitPreChatForm(submission),
89
+ [client]
90
+ ),
91
+ cancelPreChatForm: useCallback(() => client.cancelPreChatForm(), [client]),
92
+ fireTrigger: useCallback(
93
+ (triggerId) => client.fireTrigger(triggerId),
94
+ [client]
95
+ ),
96
+ consumePendingPrefill: useCallback(
97
+ () => client.consumePendingPrefill(),
98
+ [client]
99
+ ),
100
+ dismissIncidentBanner: useCallback(
101
+ () => client.dismissIncidentBanner(),
102
+ [client]
103
+ )
104
+ };
105
+ }
106
+
107
+ // src/components/chat-bubble.tsx
108
+ import { useEffect as useEffect4, useState as useState3 } from "react";
109
+
110
+ // src/use-reduced-motion.ts
111
+ import { useEffect as useEffect2, useState } from "react";
112
+ function useReducedMotion() {
113
+ const [reduced, setReduced] = useState(() => {
114
+ if (typeof window === "undefined") return false;
115
+ return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
116
+ });
117
+ useEffect2(() => {
118
+ const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
119
+ const handler = (e) => setReduced(e.matches);
120
+ mq.addEventListener("change", handler);
121
+ return () => mq.removeEventListener("change", handler);
122
+ }, []);
123
+ return reduced;
124
+ }
125
+
126
+ // src/use-effective-theme.ts
127
+ import {
128
+ effectiveColors,
129
+ panelRadius,
130
+ resolveScheme,
131
+ sizePreset
132
+ } from "@customerhero/js";
133
+
134
+ // src/use-prefers-dark.ts
135
+ import { useEffect as useEffect3, useState as useState2 } from "react";
136
+ function usePrefersDark() {
137
+ const [prefersDark, setPrefersDark] = useState2(false);
138
+ useEffect3(() => {
139
+ if (typeof window === "undefined" || typeof window.matchMedia !== "function") {
140
+ return;
141
+ }
142
+ const mq = window.matchMedia("(prefers-color-scheme: dark)");
143
+ const update = () => setPrefersDark(mq.matches);
144
+ update();
145
+ if (typeof mq.addEventListener === "function") {
146
+ mq.addEventListener("change", update);
147
+ return () => mq.removeEventListener("change", update);
148
+ }
149
+ mq.addListener(update);
150
+ return () => mq.removeListener(update);
151
+ }, []);
152
+ return prefersDark;
153
+ }
154
+
155
+ // src/use-effective-theme.ts
156
+ function useEffectiveTheme() {
157
+ const { config } = useChat();
158
+ const prefersDark = usePrefersDark();
159
+ const scheme = resolveScheme(config.colorScheme, prefersDark);
160
+ const colors = effectiveColors(config, scheme);
161
+ const isDark = scheme === "dark";
162
+ return {
163
+ scheme,
164
+ primary: colors.primary,
165
+ background: colors.background,
166
+ text: colors.text,
167
+ bubbleBackground: isDark ? "rgba(255,255,255,0.08)" : "#f0f0f0",
168
+ divider: isDark ? "rgba(255,255,255,0.12)" : "#e0e0e0",
169
+ size: sizePreset(config.size),
170
+ radius: panelRadius(config.cornerStyle)
171
+ };
172
+ }
173
+
174
+ // src/components/chat-bubble.tsx
175
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
176
+ function ChatBubble({ embedded } = {}) {
177
+ const { toggle, config, t, isRtl } = useChat();
178
+ const reduced = useReducedMotion();
179
+ const theme = useEffectiveTheme();
180
+ const [mounted, setMounted] = useState3(false);
181
+ useEffect4(() => {
182
+ const id = requestAnimationFrame(() => setMounted(true));
183
+ return () => cancelAnimationFrame(id);
184
+ }, []);
185
+ const visible = mounted;
186
+ const effectivePosition = isRtl ? config.position === "bottom-right" ? "bottom-left" : "bottom-right" : config.position;
187
+ const colors = theme;
188
+ const preset = theme.size;
189
+ const hasLabel = !!config.launcher.label;
190
+ const width = hasLabel ? "auto" : preset.bubble;
191
+ const height = preset.bubble;
192
+ const borderRadius = hasLabel ? preset.bubble / 2 : "50%";
193
+ const style = {
194
+ position: embedded ? "absolute" : "fixed",
195
+ bottom: config.offset.bottom,
196
+ [effectivePosition === "bottom-left" ? "left" : "right"]: config.offset.side,
197
+ width,
198
+ height,
199
+ minWidth: preset.bubble,
200
+ paddingInline: hasLabel ? Math.round(preset.bubble * 0.35) : 0,
201
+ borderRadius,
202
+ background: colors.primary,
203
+ color: "#FFFFFF",
204
+ display: "flex",
205
+ alignItems: "center",
206
+ justifyContent: "center",
207
+ gap: hasLabel ? 8 : 0,
208
+ cursor: "pointer",
209
+ boxShadow: theme.scheme === "dark" ? "0 6px 20px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.06)" : "0 4px 20px rgba(0,0,0,0.15)",
210
+ zIndex: config.zIndex,
211
+ border: "none",
212
+ fontSize: preset.fontSize,
213
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
214
+ fontWeight: 500,
215
+ opacity: visible ? 1 : 0,
216
+ transform: visible ? "scale(1)" : "scale(0.6)",
217
+ transition: reduced ? "none" : "opacity 0.25s ease, transform 0.25s ease",
218
+ pointerEvents: visible ? "auto" : "none"
219
+ };
220
+ const iconSize = Math.round(preset.bubble * 0.43);
221
+ const dotSize = Math.max(10, Math.round(preset.bubble * 0.22));
222
+ const dotOffset = Math.round(preset.bubble * 0.08);
223
+ const dotStyle = {
224
+ position: "absolute",
225
+ top: dotOffset,
226
+ [effectivePosition === "bottom-left" ? "left" : "right"]: dotOffset,
227
+ width: dotSize,
228
+ height: dotSize,
229
+ borderRadius: "50%",
230
+ background: "#22c55e",
231
+ border: "2px solid rgba(255,255,255,0.9)",
232
+ pointerEvents: "none"
233
+ };
234
+ return /* @__PURE__ */ jsxs(
235
+ "button",
236
+ {
237
+ onClick: toggle,
238
+ style,
239
+ dir: isRtl ? "rtl" : "ltr",
240
+ "aria-label": t("open_chat"),
241
+ onMouseEnter: (e) => {
242
+ if (!reduced) e.currentTarget.style.transform = "scale(1.06)";
243
+ },
244
+ onMouseLeave: (e) => {
245
+ if (!reduced) e.currentTarget.style.transform = "scale(1)";
246
+ },
247
+ children: [
248
+ config.launcher.iconUrl ? /* @__PURE__ */ jsx2(
249
+ "img",
250
+ {
251
+ src: config.launcher.iconUrl,
252
+ alt: "",
253
+ width: iconSize,
254
+ height: iconSize,
255
+ style: { display: "block" }
256
+ }
257
+ ) : /* @__PURE__ */ jsx2(
258
+ "svg",
259
+ {
260
+ width: iconSize,
261
+ height: iconSize,
262
+ viewBox: "0 0 24 24",
263
+ fill: "none",
264
+ stroke: "currentColor",
265
+ strokeWidth: "2",
266
+ strokeLinecap: "round",
267
+ strokeLinejoin: "round",
268
+ children: /* @__PURE__ */ jsx2("path", { d: "M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" })
269
+ }
270
+ ),
271
+ hasLabel && /* @__PURE__ */ jsx2("span", { children: config.launcher.label }),
272
+ config.launcher.showOnlineDot && /* @__PURE__ */ jsx2("span", { style: dotStyle, "aria-hidden": true })
273
+ ]
274
+ }
275
+ );
276
+ }
277
+
278
+ // src/components/action-confirmation-card.tsx
279
+ import { useState as useState4 } from "react";
280
+ import { Fragment, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
281
+ function ActionConfirmationCard({
282
+ block,
283
+ primaryColor,
284
+ t,
285
+ onApprove,
286
+ onCancel
287
+ }) {
288
+ const [chosen, setChosen] = useState4(null);
289
+ const [showSpinner, setShowSpinner] = useState4(false);
290
+ const [error, setError] = useState4(null);
291
+ const [showSummary, setShowSummary] = useState4(false);
292
+ const handle = async (choice) => {
293
+ if (chosen) return;
294
+ setChosen(choice);
295
+ setError(null);
296
+ const spinnerTimer = window.setTimeout(() => setShowSpinner(true), 800);
297
+ try {
298
+ const fn = choice === "approve" ? onApprove : onCancel;
299
+ await fn(block.pendingToolCallId);
300
+ } catch (e) {
301
+ const msg = e instanceof Error ? e.message : t("action_failed");
302
+ setError(msg);
303
+ setChosen(null);
304
+ } finally {
305
+ window.clearTimeout(spinnerTimer);
306
+ setShowSpinner(false);
307
+ }
308
+ };
309
+ const cardStyle = {
310
+ marginTop: 6,
311
+ padding: 12,
312
+ borderRadius: 12,
313
+ border: "1px solid #e0e0e0",
314
+ background: "#fff",
315
+ display: "flex",
316
+ flexDirection: "column",
317
+ gap: 8,
318
+ fontSize: 13,
319
+ lineHeight: 1.4
320
+ };
321
+ const titleStyle = {
322
+ fontWeight: 600,
323
+ color: "#222"
324
+ };
325
+ const disclosureBtn = {
326
+ background: "none",
327
+ border: "none",
328
+ padding: 0,
329
+ color: primaryColor,
330
+ fontSize: 12,
331
+ cursor: "pointer",
332
+ textAlign: "left",
333
+ fontFamily: "inherit"
334
+ };
335
+ const summaryStyle = {
336
+ color: "#555",
337
+ background: "#fafafa",
338
+ padding: 8,
339
+ borderRadius: 8,
340
+ whiteSpace: "pre-wrap"
341
+ };
342
+ const buttonRow = {
343
+ display: "flex",
344
+ gap: 8,
345
+ marginTop: 4
346
+ };
347
+ const baseBtn = (variant) => ({
348
+ flex: 1,
349
+ padding: "8px 12px",
350
+ borderRadius: 8,
351
+ fontSize: 13,
352
+ fontWeight: 500,
353
+ cursor: chosen ? "default" : "pointer",
354
+ border: variant === "primary" ? `1px solid ${primaryColor}` : "1px solid #ddd",
355
+ background: variant === "primary" ? primaryColor : "#fff",
356
+ color: variant === "primary" ? "#fff" : "#333",
357
+ opacity: chosen && chosen !== (variant === "primary" ? "approve" : "cancel") ? 0.5 : 1,
358
+ transition: "opacity 0.15s",
359
+ display: "flex",
360
+ alignItems: "center",
361
+ justifyContent: "center",
362
+ gap: 6,
363
+ fontFamily: "inherit"
364
+ });
365
+ return /* @__PURE__ */ jsxs2("div", { style: cardStyle, role: "group", "aria-label": block.title, children: [
366
+ /* @__PURE__ */ jsx3("div", { style: titleStyle, children: block.title }),
367
+ block.summary && /* @__PURE__ */ jsxs2(Fragment, { children: [
368
+ /* @__PURE__ */ jsxs2(
369
+ "button",
370
+ {
371
+ type: "button",
372
+ style: disclosureBtn,
373
+ onClick: () => setShowSummary((s) => !s),
374
+ "aria-expanded": showSummary,
375
+ children: [
376
+ showSummary ? "\u25BE" : "\u25B8",
377
+ " ",
378
+ t("action_what_will_happen")
379
+ ]
380
+ }
381
+ ),
382
+ showSummary && /* @__PURE__ */ jsx3("div", { style: summaryStyle, children: block.summary })
383
+ ] }),
384
+ /* @__PURE__ */ jsxs2("div", { style: buttonRow, children: [
385
+ /* @__PURE__ */ jsx3(
386
+ "button",
387
+ {
388
+ type: "button",
389
+ style: baseBtn("ghost"),
390
+ disabled: chosen !== null,
391
+ onClick: () => handle("cancel"),
392
+ children: chosen === "cancel" && showSpinner ? /* @__PURE__ */ jsx3(Spinner, { color: "#333" }) : t("action_cancel")
393
+ }
394
+ ),
395
+ /* @__PURE__ */ jsx3(
396
+ "button",
397
+ {
398
+ type: "button",
399
+ style: baseBtn("primary"),
400
+ disabled: chosen !== null,
401
+ onClick: () => handle("approve"),
402
+ children: chosen === "approve" && showSpinner ? /* @__PURE__ */ jsx3(Spinner, { color: "#fff" }) : t("action_approve")
403
+ }
404
+ )
405
+ ] }),
406
+ error && /* @__PURE__ */ jsx3("div", { role: "alert", style: { color: "#b91c1c", fontSize: 12 }, children: error })
407
+ ] });
408
+ }
409
+ function Spinner({ color }) {
410
+ return /* @__PURE__ */ jsxs2(Fragment, { children: [
411
+ /* @__PURE__ */ jsx3("style", { children: `@keyframes ch-spin { to { transform: rotate(360deg); } }` }),
412
+ /* @__PURE__ */ jsx3(
413
+ "span",
414
+ {
415
+ "aria-hidden": "true",
416
+ style: {
417
+ width: 14,
418
+ height: 14,
419
+ borderRadius: "50%",
420
+ border: `2px solid ${color}`,
421
+ borderTopColor: "transparent",
422
+ animation: "ch-spin 0.8s linear infinite",
423
+ display: "inline-block"
424
+ }
425
+ }
426
+ )
427
+ ] });
428
+ }
429
+
430
+ // src/components/chat-window.tsx
431
+ import { useEffect as useEffect8, useState as useState9 } from "react";
432
+
433
+ // src/components/chat-header.tsx
434
+ import { useState as useState5, useEffect as useEffect5, useRef as useRef2 } from "react";
435
+ import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
436
+ function ChatHeader() {
437
+ const { config, close, reset, t } = useChat();
438
+ const reduced = useReducedMotion();
439
+ const theme = useEffectiveTheme();
440
+ const [menuOpen, setMenuOpen] = useState5(false);
441
+ const menuRef = useRef2(null);
442
+ useEffect5(() => {
443
+ if (!menuOpen) return;
444
+ const handleClick = (e) => {
445
+ if (menuRef.current && !menuRef.current.contains(e.target)) {
446
+ setMenuOpen(false);
447
+ }
448
+ };
449
+ document.addEventListener("mousedown", handleClick);
450
+ return () => document.removeEventListener("mousedown", handleClick);
451
+ }, [menuOpen]);
452
+ const headerStyle = {
453
+ background: theme.primary,
454
+ padding: 16,
455
+ display: "flex",
456
+ alignItems: "center",
457
+ gap: 12
458
+ };
459
+ const avatarStyle = {
460
+ width: 36,
461
+ height: 36,
462
+ borderRadius: "50%",
463
+ background: "rgba(255,255,255,0.2)",
464
+ display: "flex",
465
+ alignItems: "center",
466
+ justifyContent: "center",
467
+ flexShrink: 0
468
+ };
469
+ const titleStyle = {
470
+ fontSize: 14,
471
+ fontWeight: 600,
472
+ color: "white",
473
+ margin: 0
474
+ };
475
+ const subtitleStyle = {
476
+ fontSize: 11,
477
+ color: "rgba(255,255,255,0.7)",
478
+ margin: 0
479
+ };
480
+ const headerButtonStyle = {
481
+ background: "none",
482
+ border: "none",
483
+ color: "white",
484
+ cursor: "pointer",
485
+ opacity: 0.7,
486
+ padding: 4
487
+ };
488
+ const isDark = theme.scheme === "dark";
489
+ const menuStyle = {
490
+ position: "absolute",
491
+ top: "100%",
492
+ right: 0,
493
+ marginTop: 4,
494
+ background: theme.background,
495
+ border: `1px solid ${theme.divider}`,
496
+ borderRadius: 8,
497
+ boxShadow: isDark ? "0 4px 16px rgba(0,0,0,0.5)" : "0 4px 16px rgba(0,0,0,0.15)",
498
+ minWidth: 180,
499
+ overflow: "hidden",
500
+ zIndex: 10,
501
+ transformOrigin: "top right",
502
+ animation: reduced ? "none" : "ch-menu-in 0.15s ease"
503
+ };
504
+ const menuItemStyle = {
505
+ display: "flex",
506
+ alignItems: "center",
507
+ gap: 8,
508
+ width: "100%",
509
+ padding: "10px 14px",
510
+ border: "none",
511
+ background: "none",
512
+ cursor: "pointer",
513
+ fontSize: 13,
514
+ color: theme.text,
515
+ textAlign: "left",
516
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif"
517
+ };
518
+ return /* @__PURE__ */ jsxs3("div", { style: headerStyle, children: [
519
+ /* @__PURE__ */ jsx4("div", { style: avatarStyle, children: config.avatarUrl ? /* @__PURE__ */ jsx4(
520
+ "img",
521
+ {
522
+ src: config.avatarUrl,
523
+ alt: "",
524
+ style: { width: 36, height: 36, borderRadius: "50%" }
525
+ }
526
+ ) : /* @__PURE__ */ jsx4(
527
+ "svg",
528
+ {
529
+ width: "18",
530
+ height: "18",
531
+ viewBox: "0 0 24 24",
532
+ fill: "none",
533
+ stroke: "white",
534
+ strokeWidth: "2",
535
+ strokeLinecap: "round",
536
+ strokeLinejoin: "round",
537
+ children: /* @__PURE__ */ jsx4("path", { d: "M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" })
538
+ }
539
+ ) }),
540
+ /* @__PURE__ */ jsxs3("div", { style: { flex: 1 }, children: [
541
+ /* @__PURE__ */ jsx4("h3", { style: titleStyle, children: config.title }),
542
+ /* @__PURE__ */ jsx4("p", { style: subtitleStyle, children: t("online") })
543
+ ] }),
544
+ /* @__PURE__ */ jsxs3("div", { ref: menuRef, style: { position: "relative" }, children: [
545
+ /* @__PURE__ */ jsx4(
546
+ "button",
547
+ {
548
+ onClick: () => setMenuOpen(!menuOpen),
549
+ style: headerButtonStyle,
550
+ "aria-label": t("menu"),
551
+ onMouseEnter: (e) => {
552
+ e.currentTarget.style.opacity = "1";
553
+ },
554
+ onMouseLeave: (e) => {
555
+ e.currentTarget.style.opacity = "0.7";
556
+ },
557
+ children: /* @__PURE__ */ jsxs3(
558
+ "svg",
559
+ {
560
+ width: "18",
561
+ height: "18",
562
+ viewBox: "0 0 24 24",
563
+ fill: "none",
564
+ stroke: "currentColor",
565
+ strokeWidth: "2",
566
+ strokeLinecap: "round",
567
+ strokeLinejoin: "round",
568
+ children: [
569
+ /* @__PURE__ */ jsx4("circle", { cx: "12", cy: "5", r: "1" }),
570
+ /* @__PURE__ */ jsx4("circle", { cx: "12", cy: "12", r: "1" }),
571
+ /* @__PURE__ */ jsx4("circle", { cx: "12", cy: "19", r: "1" })
572
+ ]
573
+ }
574
+ )
575
+ }
576
+ ),
577
+ menuOpen && /* @__PURE__ */ jsxs3("div", { style: menuStyle, children: [
578
+ /* @__PURE__ */ jsx4("style", { children: `@keyframes ch-menu-in {
579
+ from { opacity: 0; transform: scale(0.9); }
580
+ to { opacity: 1; transform: scale(1); }
581
+ }` }),
582
+ /* @__PURE__ */ jsxs3(
583
+ "button",
584
+ {
585
+ style: menuItemStyle,
586
+ onClick: () => {
587
+ setMenuOpen(false);
588
+ reset();
589
+ },
590
+ onMouseEnter: (e) => {
591
+ e.currentTarget.style.background = theme.bubbleBackground;
592
+ },
593
+ onMouseLeave: (e) => {
594
+ e.currentTarget.style.background = "none";
595
+ },
596
+ children: [
597
+ /* @__PURE__ */ jsxs3(
598
+ "svg",
599
+ {
600
+ width: "14",
601
+ height: "14",
602
+ viewBox: "0 0 24 24",
603
+ fill: "none",
604
+ stroke: "currentColor",
605
+ strokeWidth: "2",
606
+ strokeLinecap: "round",
607
+ strokeLinejoin: "round",
608
+ children: [
609
+ /* @__PURE__ */ jsx4("path", { d: "M12 20h9" }),
610
+ /* @__PURE__ */ jsx4("path", { d: "M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z" })
611
+ ]
612
+ }
613
+ ),
614
+ t("new_conversation")
615
+ ]
616
+ }
617
+ )
618
+ ] })
619
+ ] }),
620
+ /* @__PURE__ */ jsx4(
621
+ "button",
622
+ {
623
+ onClick: close,
624
+ style: headerButtonStyle,
625
+ "aria-label": t("close_chat"),
626
+ onMouseEnter: (e) => {
627
+ e.currentTarget.style.opacity = "1";
628
+ },
629
+ onMouseLeave: (e) => {
630
+ e.currentTarget.style.opacity = "0.7";
631
+ },
632
+ children: /* @__PURE__ */ jsxs3(
633
+ "svg",
634
+ {
635
+ width: "20",
636
+ height: "20",
637
+ viewBox: "0 0 24 24",
638
+ fill: "none",
639
+ stroke: "currentColor",
640
+ strokeWidth: "2",
641
+ children: [
642
+ /* @__PURE__ */ jsx4("line", { x1: "18", y1: "6", x2: "6", y2: "18" }),
643
+ /* @__PURE__ */ jsx4("line", { x1: "6", y1: "6", x2: "18", y2: "18" })
644
+ ]
645
+ }
646
+ )
647
+ }
648
+ )
649
+ ] });
650
+ }
651
+
652
+ // src/components/chat-messages.tsx
653
+ import { useEffect as useEffect6, useRef as useRef3, useState as useState6 } from "react";
654
+
655
+ // src/markdown/render.tsx
656
+ import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
657
+ function parseBlocks(source) {
658
+ const lines = source.replace(/\r\n?/g, "\n").split("\n");
659
+ const blocks = [];
660
+ let i = 0;
661
+ while (i < lines.length) {
662
+ const line = lines[i];
663
+ if (line.trim() === "") {
664
+ i++;
665
+ continue;
666
+ }
667
+ const fenceMatch = line.match(/^```(.*)$/);
668
+ if (fenceMatch) {
669
+ const lang = fenceMatch[1].trim() || void 0;
670
+ const body2 = [];
671
+ i++;
672
+ while (i < lines.length && !/^```\s*$/.test(lines[i])) {
673
+ body2.push(lines[i]);
674
+ i++;
675
+ }
676
+ if (i < lines.length) i++;
677
+ blocks.push({ kind: "code", lines: body2, lang });
678
+ continue;
679
+ }
680
+ const headingMatch = line.match(/^(#{1,6})\s+(.*?)\s*#*\s*$/);
681
+ if (headingMatch) {
682
+ blocks.push({
683
+ kind: "heading",
684
+ level: headingMatch[1].length,
685
+ lines: [headingMatch[2]]
686
+ });
687
+ i++;
688
+ continue;
689
+ }
690
+ if (/^(\s*)[-*+]\s+/.test(line)) {
691
+ const body2 = [];
692
+ while (i < lines.length && /^(\s*)[-*+]\s+/.test(lines[i])) {
693
+ body2.push(lines[i].replace(/^(\s*)[-*+]\s+/, ""));
694
+ i++;
695
+ }
696
+ blocks.push({ kind: "ul", lines: body2 });
697
+ continue;
698
+ }
699
+ if (/^(\s*)\d+\.\s+/.test(line)) {
700
+ const body2 = [];
701
+ while (i < lines.length && /^(\s*)\d+\.\s+/.test(lines[i])) {
702
+ body2.push(lines[i].replace(/^(\s*)\d+\.\s+/, ""));
703
+ i++;
704
+ }
705
+ blocks.push({ kind: "ol", lines: body2 });
706
+ continue;
707
+ }
708
+ if (/^>\s?/.test(line)) {
709
+ const body2 = [];
710
+ while (i < lines.length && /^>\s?/.test(lines[i])) {
711
+ body2.push(lines[i].replace(/^>\s?/, ""));
712
+ i++;
713
+ }
714
+ blocks.push({ kind: "blockquote", lines: body2 });
715
+ continue;
716
+ }
717
+ const body = [line];
718
+ i++;
719
+ while (i < lines.length) {
720
+ const next = lines[i];
721
+ if (next.trim() === "") break;
722
+ if (/^```/.test(next) || /^#{1,6}\s/.test(next) || /^(\s*)[-*+]\s+/.test(next) || /^(\s*)\d+\.\s+/.test(next) || /^>\s?/.test(next)) {
723
+ break;
724
+ }
725
+ body.push(next);
726
+ i++;
727
+ }
728
+ blocks.push({ kind: "paragraph", lines: body });
729
+ }
730
+ return blocks;
731
+ }
732
+ function findClosing(text, from, end) {
733
+ let i = from;
734
+ while (i <= text.length - end.length) {
735
+ const c = text[i];
736
+ if (c === "\\") {
737
+ i += 2;
738
+ continue;
739
+ }
740
+ if (text.startsWith(end, i)) return i;
741
+ i++;
742
+ }
743
+ return -1;
744
+ }
745
+ function renderInlineText(text, ctx) {
746
+ const out = [];
747
+ let buffer = "";
748
+ let i = 0;
749
+ const flushBuffer = () => {
750
+ if (buffer.length > 0) {
751
+ out.push(buffer);
752
+ buffer = "";
753
+ }
754
+ };
755
+ while (i < text.length) {
756
+ const c = text[i];
757
+ if (c === "\\" && i + 1 < text.length) {
758
+ buffer += text[i + 1];
759
+ i += 2;
760
+ continue;
761
+ }
762
+ if (c === " " && text[i + 1] === " " && text[i + 2] === "\n") {
763
+ flushBuffer();
764
+ out.push(/* @__PURE__ */ jsx5("br", {}, ctx.nextKey()));
765
+ i += 3;
766
+ continue;
767
+ }
768
+ if (c === "\n") {
769
+ buffer += " ";
770
+ i++;
771
+ continue;
772
+ }
773
+ if (c === "`") {
774
+ const close = findClosing(text, i + 1, "`");
775
+ if (close !== -1) {
776
+ flushBuffer();
777
+ out.push(
778
+ /* @__PURE__ */ jsx5(
779
+ "code",
780
+ {
781
+ style: {
782
+ fontFamily: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
783
+ fontSize: "0.92em",
784
+ background: "rgba(0,0,0,0.06)",
785
+ padding: "1px 4px",
786
+ borderRadius: 3
787
+ },
788
+ children: text.slice(i + 1, close)
789
+ },
790
+ ctx.nextKey()
791
+ )
792
+ );
793
+ i = close + 1;
794
+ continue;
795
+ }
796
+ }
797
+ if ((text.startsWith("**", i) || text.startsWith("__", i)) && i + 2 < text.length) {
798
+ const marker = text.slice(i, i + 2);
799
+ const close = findClosing(text, i + 2, marker);
800
+ if (close !== -1 && close > i + 2) {
801
+ flushBuffer();
802
+ const inner = renderInlineText(text.slice(i + 2, close), ctx);
803
+ out.push(/* @__PURE__ */ jsx5("strong", { children: inner }, ctx.nextKey()));
804
+ i = close + 2;
805
+ continue;
806
+ }
807
+ }
808
+ if (c === "*" || c === "_") {
809
+ const prev = i === 0 ? " " : text[i - 1];
810
+ const isWordBoundaryBefore = c === "*" || !/\w/.test(prev);
811
+ if (isWordBoundaryBefore) {
812
+ const close = findClosing(text, i + 1, c);
813
+ const prevOfClose = close === -1 ? " " : close + 1 < text.length ? text[close + 1] : " ";
814
+ const isWordBoundaryAfter = c === "*" || !/\w/.test(prevOfClose);
815
+ if (close !== -1 && close > i + 1 && text[close + 1] !== c && isWordBoundaryAfter) {
816
+ flushBuffer();
817
+ const inner = renderInlineText(text.slice(i + 1, close), ctx);
818
+ out.push(/* @__PURE__ */ jsx5("em", { children: inner }, ctx.nextKey()));
819
+ i = close + 1;
820
+ continue;
821
+ }
822
+ }
823
+ }
824
+ if (c === "[") {
825
+ const closeLabel = findClosing(text, i + 1, "]");
826
+ if (closeLabel !== -1 && text[closeLabel + 1] === "(") {
827
+ const closeUrl = findClosing(text, closeLabel + 2, ")");
828
+ if (closeUrl !== -1) {
829
+ const label = text.slice(i + 1, closeLabel);
830
+ const url = text.slice(closeLabel + 2, closeUrl).trim();
831
+ if (isSafeUrl(url)) {
832
+ flushBuffer();
833
+ out.push(
834
+ /* @__PURE__ */ jsx5(
835
+ "a",
836
+ {
837
+ href: url,
838
+ target: "_blank",
839
+ rel: "noopener noreferrer",
840
+ style: { color: ctx.linkColor, textDecoration: "underline" },
841
+ children: renderInlineText(label, ctx)
842
+ },
843
+ ctx.nextKey()
844
+ )
845
+ );
846
+ i = closeUrl + 1;
847
+ continue;
848
+ }
849
+ }
850
+ }
851
+ if (closeLabel !== -1 && ctx.sources && ctx.sources.length > 0) {
852
+ const body = text.slice(i + 1, closeLabel).trim();
853
+ const parts = body.split(/\s*,\s*/).filter((s) => s.length > 0);
854
+ const indices = parts.map((p) => Number(p));
855
+ const allValid = parts.length > 0 && indices.every(
856
+ (n) => Number.isInteger(n) && n >= 1 && n <= (ctx.sources?.length ?? 0)
857
+ );
858
+ if (allValid) {
859
+ flushBuffer();
860
+ out.push(
861
+ /* @__PURE__ */ jsx5("sup", { style: { whiteSpace: "nowrap" }, children: indices.map((n, idx) => {
862
+ const src = ctx.sources[n - 1];
863
+ const content = /* @__PURE__ */ jsxs4(
864
+ "span",
865
+ {
866
+ style: {
867
+ fontSize: "0.75em",
868
+ color: ctx.linkColor,
869
+ cursor: src.url ? "pointer" : "default"
870
+ },
871
+ title: src.heading ? `${src.title} \u2014 ${src.heading}` : src.title,
872
+ children: [
873
+ "[",
874
+ n,
875
+ "]"
876
+ ]
877
+ }
878
+ );
879
+ const separator = idx > 0 ? " " : null;
880
+ return /* @__PURE__ */ jsxs4("span", { children: [
881
+ separator,
882
+ src.url ? /* @__PURE__ */ jsx5(
883
+ "a",
884
+ {
885
+ href: src.url,
886
+ target: "_blank",
887
+ rel: "noopener noreferrer",
888
+ style: {
889
+ color: ctx.linkColor,
890
+ textDecoration: "none"
891
+ },
892
+ children: content
893
+ }
894
+ ) : content
895
+ ] }, `${ctx.keyRoot}-cit-${idx}`);
896
+ }) }, ctx.nextKey())
897
+ );
898
+ i = closeLabel + 1;
899
+ continue;
900
+ }
901
+ }
902
+ }
903
+ buffer += c;
904
+ i++;
905
+ }
906
+ flushBuffer();
907
+ return out;
908
+ }
909
+ function isSafeUrl(url) {
910
+ if (/^https?:\/\//i.test(url)) return true;
911
+ if (/^mailto:/i.test(url)) return true;
912
+ if (/^tel:/i.test(url)) return true;
913
+ if (/^\/[^/]/.test(url) || /^#/.test(url)) return true;
914
+ return false;
915
+ }
916
+ function renderMarkdown(source, opts = {}) {
917
+ const blocks = parseBlocks(source);
918
+ const linkColor = opts.linkColor ?? "#6C3CE1";
919
+ let keyCounter = 0;
920
+ const ctx = {
921
+ sources: opts.sources,
922
+ linkColor,
923
+ keyRoot: "md",
924
+ nextKey: () => `md-${keyCounter++}`
925
+ };
926
+ const renderInline = (line) => renderInlineText(line, ctx);
927
+ return /* @__PURE__ */ jsx5(Fragment2, { children: blocks.map((block, idx) => {
928
+ const key = `b-${idx}`;
929
+ switch (block.kind) {
930
+ case "heading": {
931
+ const level = block.level ?? 1;
932
+ const style = {
933
+ margin: "8px 0 4px",
934
+ fontWeight: 600,
935
+ fontSize: level <= 2 ? "1.15em" : level === 3 ? "1.05em" : "1em"
936
+ };
937
+ const children = renderInline(block.lines[0] ?? "");
938
+ if (level === 1)
939
+ return /* @__PURE__ */ jsx5("h1", { style, children }, key);
940
+ if (level === 2)
941
+ return /* @__PURE__ */ jsx5("h2", { style, children }, key);
942
+ if (level === 3)
943
+ return /* @__PURE__ */ jsx5("h3", { style, children }, key);
944
+ if (level === 4)
945
+ return /* @__PURE__ */ jsx5("h4", { style, children }, key);
946
+ if (level === 5)
947
+ return /* @__PURE__ */ jsx5("h5", { style, children }, key);
948
+ return /* @__PURE__ */ jsx5("h6", { style, children }, key);
949
+ }
950
+ case "paragraph":
951
+ return /* @__PURE__ */ jsx5("p", { style: { margin: "0 0 8px", lineHeight: 1.5 }, children: renderInline(block.lines.join("\n")) }, key);
952
+ case "ul":
953
+ return /* @__PURE__ */ jsx5(
954
+ "ul",
955
+ {
956
+ style: {
957
+ margin: "0 0 8px",
958
+ paddingLeft: 20,
959
+ lineHeight: 1.5,
960
+ listStyleType: "disc",
961
+ listStylePosition: "outside"
962
+ },
963
+ children: block.lines.map((l, i) => /* @__PURE__ */ jsx5("li", { style: { display: "list-item" }, children: renderInline(l) }, i))
964
+ },
965
+ key
966
+ );
967
+ case "ol":
968
+ return /* @__PURE__ */ jsx5(
969
+ "ol",
970
+ {
971
+ style: {
972
+ margin: "0 0 8px",
973
+ paddingLeft: 22,
974
+ lineHeight: 1.5,
975
+ listStyleType: "decimal",
976
+ listStylePosition: "outside"
977
+ },
978
+ children: block.lines.map((l, i) => /* @__PURE__ */ jsx5("li", { style: { display: "list-item" }, children: renderInline(l) }, i))
979
+ },
980
+ key
981
+ );
982
+ case "code":
983
+ return /* @__PURE__ */ jsx5(
984
+ "pre",
985
+ {
986
+ style: {
987
+ margin: "0 0 8px",
988
+ padding: "10px 12px",
989
+ background: "rgba(0,0,0,0.06)",
990
+ borderRadius: 6,
991
+ overflowX: "auto",
992
+ fontFamily: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
993
+ fontSize: "0.88em",
994
+ lineHeight: 1.45
995
+ },
996
+ children: /* @__PURE__ */ jsx5("code", { children: block.lines.join("\n") })
997
+ },
998
+ key
999
+ );
1000
+ case "blockquote":
1001
+ return /* @__PURE__ */ jsx5(
1002
+ "blockquote",
1003
+ {
1004
+ style: {
1005
+ margin: "0 0 8px",
1006
+ padding: "4px 0 4px 10px",
1007
+ borderLeft: "3px solid rgba(0,0,0,0.15)",
1008
+ color: "rgba(0,0,0,0.75)",
1009
+ fontStyle: "italic"
1010
+ },
1011
+ children: renderInline(block.lines.join("\n"))
1012
+ },
1013
+ key
1014
+ );
1015
+ }
1016
+ }) });
1017
+ }
1018
+
1019
+ // src/components/chat-messages.tsx
1020
+ import { Fragment as Fragment3, jsx as jsx6, jsxs as jsxs5 } from "react/jsx-runtime";
1021
+ function MessageStatusPill({
1022
+ status,
1023
+ t
1024
+ }) {
1025
+ const failed = status === "failed";
1026
+ const containerStyle = {
1027
+ display: "flex",
1028
+ alignItems: "center",
1029
+ justifyContent: "flex-end",
1030
+ gap: 4,
1031
+ marginTop: 2,
1032
+ fontSize: 11,
1033
+ color: failed ? "#b91c1c" : "#888"
1034
+ };
1035
+ const labelKey = status === "sending" ? "status_sending" : status === "sent" ? "status_sent" : "status_failed";
1036
+ return /* @__PURE__ */ jsxs5("div", { style: containerStyle, "aria-live": "polite", children: [
1037
+ status === "sending" && /* @__PURE__ */ jsx6(ClockIcon, {}),
1038
+ status === "sent" && /* @__PURE__ */ jsx6(CheckIcon, {}),
1039
+ status === "failed" && /* @__PURE__ */ jsx6(ExclamationIcon, {}),
1040
+ /* @__PURE__ */ jsx6("span", { children: t(labelKey) })
1041
+ ] });
1042
+ }
1043
+ function ClockIcon() {
1044
+ return /* @__PURE__ */ jsxs5(
1045
+ "svg",
1046
+ {
1047
+ width: "11",
1048
+ height: "11",
1049
+ viewBox: "0 0 24 24",
1050
+ fill: "none",
1051
+ stroke: "currentColor",
1052
+ strokeWidth: "2",
1053
+ strokeLinecap: "round",
1054
+ strokeLinejoin: "round",
1055
+ "aria-hidden": "true",
1056
+ children: [
1057
+ /* @__PURE__ */ jsx6("circle", { cx: "12", cy: "12", r: "10" }),
1058
+ /* @__PURE__ */ jsx6("polyline", { points: "12 6 12 12 16 14" })
1059
+ ]
1060
+ }
1061
+ );
1062
+ }
1063
+ function CheckIcon() {
1064
+ return /* @__PURE__ */ jsx6(
1065
+ "svg",
1066
+ {
1067
+ width: "11",
1068
+ height: "11",
1069
+ viewBox: "0 0 24 24",
1070
+ fill: "none",
1071
+ stroke: "currentColor",
1072
+ strokeWidth: "2.5",
1073
+ strokeLinecap: "round",
1074
+ strokeLinejoin: "round",
1075
+ "aria-hidden": "true",
1076
+ children: /* @__PURE__ */ jsx6("polyline", { points: "20 6 9 17 4 12" })
1077
+ }
1078
+ );
1079
+ }
1080
+ function ExclamationIcon() {
1081
+ return /* @__PURE__ */ jsxs5(
1082
+ "svg",
1083
+ {
1084
+ width: "11",
1085
+ height: "11",
1086
+ viewBox: "0 0 24 24",
1087
+ fill: "none",
1088
+ stroke: "currentColor",
1089
+ strokeWidth: "2",
1090
+ strokeLinecap: "round",
1091
+ strokeLinejoin: "round",
1092
+ "aria-hidden": "true",
1093
+ children: [
1094
+ /* @__PURE__ */ jsx6("circle", { cx: "12", cy: "12", r: "10" }),
1095
+ /* @__PURE__ */ jsx6("line", { x1: "12", y1: "8", x2: "12", y2: "12" }),
1096
+ /* @__PURE__ */ jsx6("line", { x1: "12", y1: "16", x2: "12.01", y2: "16" })
1097
+ ]
1098
+ }
1099
+ );
1100
+ }
1101
+ function MessageRatingButtons({
1102
+ messageId,
1103
+ onRate,
1104
+ primaryColor,
1105
+ t,
1106
+ reduced
1107
+ }) {
1108
+ const [rated, setRated] = useState6(null);
1109
+ const handleRate = (rating) => {
1110
+ setRated(rating);
1111
+ onRate(messageId, rating);
1112
+ };
1113
+ const buttonStyle = (isRated) => ({
1114
+ background: isRated ? `${primaryColor}11` : "none",
1115
+ border: `1px solid ${isRated ? primaryColor : "#ddd"}`,
1116
+ borderRadius: 4,
1117
+ padding: "4px 6px",
1118
+ cursor: rated ? "default" : "pointer",
1119
+ color: isRated ? primaryColor : "#888",
1120
+ display: "flex",
1121
+ alignItems: "center",
1122
+ justifyContent: "center",
1123
+ transition: reduced ? "color 0.15s" : "all 0.15s",
1124
+ transform: isRated && !reduced ? "scale(1.15)" : "scale(1)"
1125
+ });
1126
+ return /* @__PURE__ */ jsxs5("div", { style: { display: "flex", gap: 4, marginTop: 4 }, children: [
1127
+ /* @__PURE__ */ jsx6(
1128
+ "button",
1129
+ {
1130
+ onClick: () => handleRate("positive"),
1131
+ disabled: rated !== null,
1132
+ style: buttonStyle(rated === "positive"),
1133
+ title: t("helpful"),
1134
+ "aria-label": t("helpful"),
1135
+ children: /* @__PURE__ */ jsxs5(
1136
+ "svg",
1137
+ {
1138
+ width: "14",
1139
+ height: "14",
1140
+ viewBox: "0 0 24 24",
1141
+ fill: "none",
1142
+ stroke: "currentColor",
1143
+ strokeWidth: "2",
1144
+ strokeLinecap: "round",
1145
+ strokeLinejoin: "round",
1146
+ children: [
1147
+ /* @__PURE__ */ jsx6("path", { d: "M7 10v12" }),
1148
+ /* @__PURE__ */ jsx6("path", { d: "M15 5.88 14 10h5.83a2 2 0 0 1 1.92 2.56l-2.33 8A2 2 0 0 1 17.5 22H4a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h2.76a2 2 0 0 0 1.79-1.11L12 2h0a3.13 3.13 0 0 1 3 3.88Z" })
1149
+ ]
1150
+ }
1151
+ )
1152
+ }
1153
+ ),
1154
+ /* @__PURE__ */ jsx6(
1155
+ "button",
1156
+ {
1157
+ onClick: () => handleRate("negative"),
1158
+ disabled: rated !== null,
1159
+ style: buttonStyle(rated === "negative"),
1160
+ title: t("not_helpful"),
1161
+ "aria-label": t("not_helpful"),
1162
+ children: /* @__PURE__ */ jsxs5(
1163
+ "svg",
1164
+ {
1165
+ width: "14",
1166
+ height: "14",
1167
+ viewBox: "0 0 24 24",
1168
+ fill: "none",
1169
+ stroke: "currentColor",
1170
+ strokeWidth: "2",
1171
+ strokeLinecap: "round",
1172
+ strokeLinejoin: "round",
1173
+ children: [
1174
+ /* @__PURE__ */ jsx6("path", { d: "M17 14V2" }),
1175
+ /* @__PURE__ */ jsx6("path", { d: "M9 18.12 10 14H4.17a2 2 0 0 1-1.92-2.56l2.33-8A2 2 0 0 1 6.5 2H20a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-2.76a2 2 0 0 0-1.79 1.11L12 22h0a3.13 3.13 0 0 1-3-3.88Z" })
1176
+ ]
1177
+ }
1178
+ )
1179
+ }
1180
+ )
1181
+ ] });
1182
+ }
1183
+ function AnimatedMessage({
1184
+ children,
1185
+ isUser,
1186
+ animate,
1187
+ reduced
1188
+ }) {
1189
+ const [visible, setVisible] = useState6(!animate);
1190
+ useEffect6(() => {
1191
+ if (!animate || reduced) {
1192
+ setVisible(true);
1193
+ return;
1194
+ }
1195
+ const id = requestAnimationFrame(() => setVisible(true));
1196
+ return () => cancelAnimationFrame(id);
1197
+ }, [animate, reduced]);
1198
+ const style = {
1199
+ alignSelf: isUser ? "flex-end" : "flex-start",
1200
+ maxWidth: "80%",
1201
+ opacity: visible ? 1 : 0,
1202
+ transform: visible ? "translateX(0)" : `translateX(${isUser ? "12px" : "-12px"})`,
1203
+ transition: animate && !reduced ? "opacity 0.2s ease, transform 0.2s ease" : "none"
1204
+ };
1205
+ return /* @__PURE__ */ jsx6("div", { style, children });
1206
+ }
1207
+ function ChipRow({
1208
+ options,
1209
+ onSelect,
1210
+ primaryColor,
1211
+ reduced
1212
+ }) {
1213
+ const chip = {
1214
+ background: "none",
1215
+ border: "1px solid #e0e0e0",
1216
+ borderRadius: 20,
1217
+ padding: "6px 12px",
1218
+ fontSize: 13,
1219
+ color: "#333",
1220
+ cursor: "pointer",
1221
+ textAlign: "left",
1222
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
1223
+ transition: reduced ? "none" : "border-color 0.15s, background 0.15s"
1224
+ };
1225
+ return /* @__PURE__ */ jsx6(
1226
+ "div",
1227
+ {
1228
+ style: {
1229
+ display: "flex",
1230
+ flexWrap: "wrap",
1231
+ gap: 6,
1232
+ marginTop: 6
1233
+ },
1234
+ children: options.map((text) => /* @__PURE__ */ jsx6(
1235
+ "button",
1236
+ {
1237
+ style: chip,
1238
+ onClick: () => onSelect(text),
1239
+ onMouseEnter: (e) => {
1240
+ e.currentTarget.style.borderColor = primaryColor;
1241
+ e.currentTarget.style.background = `${primaryColor}08`;
1242
+ },
1243
+ onMouseLeave: (e) => {
1244
+ e.currentTarget.style.borderColor = "#e0e0e0";
1245
+ e.currentTarget.style.background = "none";
1246
+ },
1247
+ children: text
1248
+ },
1249
+ text
1250
+ ))
1251
+ }
1252
+ );
1253
+ }
1254
+ function BlockRenderer({
1255
+ block,
1256
+ onSend,
1257
+ onApproveAction,
1258
+ onCancelAction,
1259
+ primaryColor,
1260
+ reduced,
1261
+ t
1262
+ }) {
1263
+ switch (block.type) {
1264
+ case "quick_replies":
1265
+ return /* @__PURE__ */ jsx6(
1266
+ ChipRow,
1267
+ {
1268
+ options: block.options,
1269
+ onSelect: onSend,
1270
+ primaryColor,
1271
+ reduced
1272
+ }
1273
+ );
1274
+ case "action_confirmation":
1275
+ return /* @__PURE__ */ jsx6(
1276
+ ActionConfirmationCard,
1277
+ {
1278
+ block,
1279
+ primaryColor,
1280
+ t,
1281
+ onApprove: onApproveAction,
1282
+ onCancel: onCancelAction
1283
+ }
1284
+ );
1285
+ }
1286
+ }
1287
+ function StreamingCursor({ reduced }) {
1288
+ return /* @__PURE__ */ jsxs5(Fragment3, { children: [
1289
+ /* @__PURE__ */ jsx6("style", { children: `@keyframes ch-caret-blink {
1290
+ 0%, 50% { opacity: 1; }
1291
+ 50.01%, 100% { opacity: 0; }
1292
+ }` }),
1293
+ /* @__PURE__ */ jsx6(
1294
+ "span",
1295
+ {
1296
+ "aria-hidden": "true",
1297
+ style: {
1298
+ display: "inline-block",
1299
+ width: 2,
1300
+ height: "1em",
1301
+ marginLeft: 2,
1302
+ verticalAlign: "text-bottom",
1303
+ background: "#777",
1304
+ animation: reduced ? "none" : "ch-caret-blink 1s step-start infinite"
1305
+ }
1306
+ }
1307
+ )
1308
+ ] });
1309
+ }
1310
+ function Message({
1311
+ message,
1312
+ config,
1313
+ onRate,
1314
+ onSend,
1315
+ onApproveAction,
1316
+ onCancelAction,
1317
+ hasConversation,
1318
+ t,
1319
+ animate,
1320
+ reduced,
1321
+ showSuggestions
1322
+ }) {
1323
+ const isUser = message.role === "user";
1324
+ const bubbleStyle = {
1325
+ padding: "10px 14px",
1326
+ borderRadius: 16,
1327
+ fontSize: 14,
1328
+ lineHeight: 1.5,
1329
+ wordBreak: "break-word",
1330
+ ...isUser ? {
1331
+ background: config.primaryColor,
1332
+ color: "white",
1333
+ borderBottomRightRadius: 4
1334
+ } : {
1335
+ background: config.bubbleBackground,
1336
+ color: config.textColor,
1337
+ borderBottomLeftRadius: 4
1338
+ }
1339
+ };
1340
+ const linkColor = isUser ? "#ffffff" : config.primaryColor;
1341
+ return /* @__PURE__ */ jsxs5(AnimatedMessage, { isUser, animate, reduced, children: [
1342
+ /* @__PURE__ */ jsx6(
1343
+ "div",
1344
+ {
1345
+ style: bubbleStyle,
1346
+ "data-streaming-bubble": !isUser && message.streaming ? "true" : void 0,
1347
+ children: isUser ? message.content : /* @__PURE__ */ jsxs5(Fragment3, { children: [
1348
+ renderMarkdown(message.content, {
1349
+ sources: message.sources,
1350
+ linkColor
1351
+ }),
1352
+ message.streaming && /* @__PURE__ */ jsx6(StreamingCursor, { reduced })
1353
+ ] })
1354
+ }
1355
+ ),
1356
+ isUser && message.status && /* @__PURE__ */ jsx6(MessageStatusPill, { status: message.status, t }),
1357
+ !isUser && message.blocks?.map((block, i) => /* @__PURE__ */ jsx6(
1358
+ BlockRenderer,
1359
+ {
1360
+ block,
1361
+ onSend,
1362
+ onApproveAction,
1363
+ onCancelAction,
1364
+ primaryColor: config.primaryColor,
1365
+ reduced,
1366
+ t
1367
+ },
1368
+ i
1369
+ )),
1370
+ !isUser && showSuggestions && message.suggestions?.length ? /* @__PURE__ */ jsx6(
1371
+ ChipRow,
1372
+ {
1373
+ options: message.suggestions,
1374
+ onSelect: onSend,
1375
+ primaryColor: config.primaryColor,
1376
+ reduced
1377
+ }
1378
+ ) : null,
1379
+ message.role === "bot" && message.id && hasConversation && !message.streaming && !message.blocks?.some((b) => b.type === "action_confirmation") && /* @__PURE__ */ jsx6(
1380
+ MessageRatingButtons,
1381
+ {
1382
+ messageId: message.id,
1383
+ onRate,
1384
+ primaryColor: config.primaryColor,
1385
+ t,
1386
+ reduced
1387
+ }
1388
+ )
1389
+ ] });
1390
+ }
1391
+ function TypingDots({ reduced }) {
1392
+ const dotStyle = (delay) => ({
1393
+ width: 6,
1394
+ height: 6,
1395
+ borderRadius: "50%",
1396
+ background: "#999",
1397
+ animation: reduced ? "none" : `ch-dot-pulse 1.2s ease-in-out ${delay}s infinite`
1398
+ });
1399
+ return /* @__PURE__ */ jsxs5(Fragment3, { children: [
1400
+ /* @__PURE__ */ jsx6("style", { children: `@keyframes ch-dot-pulse {
1401
+ 0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
1402
+ 40% { opacity: 1; transform: scale(1); }
1403
+ }` }),
1404
+ /* @__PURE__ */ jsxs5(
1405
+ "div",
1406
+ {
1407
+ style: {
1408
+ alignSelf: "flex-start",
1409
+ padding: "12px 16px",
1410
+ borderRadius: 16,
1411
+ borderBottomLeftRadius: 4,
1412
+ background: "#f0f0f0",
1413
+ display: "flex",
1414
+ gap: 4,
1415
+ alignItems: "center"
1416
+ },
1417
+ children: [
1418
+ /* @__PURE__ */ jsx6("div", { style: dotStyle(0) }),
1419
+ /* @__PURE__ */ jsx6("div", { style: dotStyle(0.2) }),
1420
+ /* @__PURE__ */ jsx6("div", { style: dotStyle(0.4) })
1421
+ ]
1422
+ }
1423
+ )
1424
+ ] });
1425
+ }
1426
+ function ChatMessages() {
1427
+ const {
1428
+ messages,
1429
+ isLoading,
1430
+ error,
1431
+ conversationId,
1432
+ rateMessage,
1433
+ sendMessage,
1434
+ approveAction,
1435
+ cancelAction,
1436
+ t
1437
+ } = useChat();
1438
+ const theme = useEffectiveTheme();
1439
+ const themedConfig = {
1440
+ primaryColor: theme.primary,
1441
+ textColor: theme.text,
1442
+ bubbleBackground: theme.bubbleBackground
1443
+ };
1444
+ const reduced = useReducedMotion();
1445
+ const containerRef = useRef3(null);
1446
+ const messagesEndRef = useRef3(null);
1447
+ const isFirstRender = useRef3(true);
1448
+ const prevMessageCount = useRef3(0);
1449
+ const stickRef = useRef3(true);
1450
+ const suppressScrollRef = useRef3(false);
1451
+ const autoScrollTo = (top) => {
1452
+ const el = containerRef.current;
1453
+ if (!el) return;
1454
+ suppressScrollRef.current = true;
1455
+ el.scrollTop = top;
1456
+ requestAnimationFrame(() => {
1457
+ suppressScrollRef.current = false;
1458
+ });
1459
+ };
1460
+ const handleScroll = () => {
1461
+ if (suppressScrollRef.current) return;
1462
+ const el = containerRef.current;
1463
+ if (!el) return;
1464
+ const distFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
1465
+ stickRef.current = distFromBottom < 60;
1466
+ };
1467
+ useEffect6(() => {
1468
+ const el = containerRef.current;
1469
+ if (!el) return;
1470
+ const lastMsg2 = messages[messages.length - 1];
1471
+ if (messages.length > prevMessageCount.current && lastMsg2?.role === "user") {
1472
+ stickRef.current = true;
1473
+ }
1474
+ if (!stickRef.current && !isFirstRender.current) {
1475
+ return;
1476
+ }
1477
+ const streamingBubble = el.querySelector(
1478
+ "[data-streaming-bubble='true']"
1479
+ );
1480
+ let target = el.scrollHeight - el.clientHeight;
1481
+ if (streamingBubble && streamingBubble.offsetHeight > el.clientHeight - 24) {
1482
+ const containerTop = el.getBoundingClientRect().top;
1483
+ const bubbleTop = streamingBubble.getBoundingClientRect().top;
1484
+ const TOP_GAP = 16;
1485
+ target = el.scrollTop + (bubbleTop - containerTop) - TOP_GAP;
1486
+ stickRef.current = false;
1487
+ }
1488
+ autoScrollTo(target);
1489
+ isFirstRender.current = false;
1490
+ }, [messages, isLoading, reduced]);
1491
+ const newStartIndex = isFirstRender.current ? messages.length : prevMessageCount.current;
1492
+ useEffect6(() => {
1493
+ prevMessageCount.current = messages.length;
1494
+ }, [messages.length]);
1495
+ let lastBotIndex = -1;
1496
+ for (let i = messages.length - 1; i >= 0; i--) {
1497
+ if (messages[i].role === "bot") {
1498
+ lastBotIndex = i;
1499
+ break;
1500
+ }
1501
+ }
1502
+ const containerStyle = {
1503
+ flex: 1,
1504
+ overflowY: "auto",
1505
+ padding: 16,
1506
+ display: "flex",
1507
+ flexDirection: "column",
1508
+ gap: 12
1509
+ };
1510
+ const lastMsg = messages[messages.length - 1];
1511
+ const waitingForFirstToken = isLoading && (lastMsg?.role !== "bot" || lastMsg.streaming !== true);
1512
+ return /* @__PURE__ */ jsxs5("div", { ref: containerRef, style: containerStyle, onScroll: handleScroll, children: [
1513
+ messages.map((msg, i) => /* @__PURE__ */ jsx6(
1514
+ Message,
1515
+ {
1516
+ message: msg,
1517
+ config: themedConfig,
1518
+ onRate: rateMessage,
1519
+ onSend: sendMessage,
1520
+ onApproveAction: approveAction,
1521
+ onCancelAction: cancelAction,
1522
+ hasConversation: conversationId !== null,
1523
+ t,
1524
+ animate: i >= newStartIndex,
1525
+ reduced,
1526
+ showSuggestions: i === lastBotIndex && !isLoading && msg.streaming !== true
1527
+ },
1528
+ i
1529
+ )),
1530
+ waitingForFirstToken && /* @__PURE__ */ jsx6(TypingDots, { reduced }),
1531
+ error && /* @__PURE__ */ jsx6(
1532
+ "div",
1533
+ {
1534
+ role: "alert",
1535
+ style: {
1536
+ alignSelf: "flex-start",
1537
+ padding: "10px 14px",
1538
+ borderRadius: 16,
1539
+ borderBottomLeftRadius: 4,
1540
+ fontSize: 13,
1541
+ background: "#fee2e2",
1542
+ color: "#b91c1c"
1543
+ },
1544
+ children: error
1545
+ }
1546
+ ),
1547
+ /* @__PURE__ */ jsx6("div", { ref: messagesEndRef })
1548
+ ] });
1549
+ }
1550
+
1551
+ // src/components/chat-suggestions.tsx
1552
+ import { jsx as jsx7 } from "react/jsx-runtime";
1553
+ function ChatSuggestions() {
1554
+ const { messages, isLoading, config, sendMessage } = useChat();
1555
+ const reduced = useReducedMotion();
1556
+ const theme = useEffectiveTheme();
1557
+ const hasUserMessage = messages.some((m) => m.role === "user");
1558
+ if (config.suggestedMessages.length === 0 || hasUserMessage || isLoading) {
1559
+ return null;
1560
+ }
1561
+ const isDark = theme.scheme === "dark";
1562
+ const idleBorder = isDark ? "rgba(255,255,255,0.18)" : "#e0e0e0";
1563
+ const containerStyle = {
1564
+ padding: "8px 16px",
1565
+ display: "flex",
1566
+ flexWrap: "wrap",
1567
+ gap: 6,
1568
+ justifyContent: "flex-end"
1569
+ };
1570
+ const chipStyle = {
1571
+ background: "none",
1572
+ border: `1px solid ${idleBorder}`,
1573
+ borderRadius: 20,
1574
+ padding: "7px 14px",
1575
+ fontSize: 13,
1576
+ color: theme.text,
1577
+ cursor: "pointer",
1578
+ textAlign: "left",
1579
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
1580
+ transition: reduced ? "none" : "border-color 0.15s, background 0.15s"
1581
+ };
1582
+ return /* @__PURE__ */ jsx7("div", { style: containerStyle, children: config.suggestedMessages.map((text) => /* @__PURE__ */ jsx7(
1583
+ "button",
1584
+ {
1585
+ style: chipStyle,
1586
+ onClick: () => sendMessage(text),
1587
+ onMouseEnter: (e) => {
1588
+ e.currentTarget.style.borderColor = theme.primary;
1589
+ e.currentTarget.style.background = `${theme.primary}14`;
1590
+ },
1591
+ onMouseLeave: (e) => {
1592
+ e.currentTarget.style.borderColor = idleBorder;
1593
+ e.currentTarget.style.background = "none";
1594
+ },
1595
+ children: text
1596
+ },
1597
+ text
1598
+ )) });
1599
+ }
1600
+
1601
+ // src/components/chat-input.tsx
1602
+ import {
1603
+ useEffect as useEffect7,
1604
+ useLayoutEffect,
1605
+ useRef as useRef4,
1606
+ useState as useState7
1607
+ } from "react";
1608
+ import {
1609
+ ScreenshotCancelled,
1610
+ canCaptureScreenshot,
1611
+ captureScreenshot
1612
+ } from "@customerhero/js";
1613
+ import { Fragment as Fragment4, jsx as jsx8, jsxs as jsxs6 } from "react/jsx-runtime";
1614
+ var MAX_ATTACHMENTS = 3;
1615
+ var ALLOWED_MIME_TYPES = [
1616
+ "image/png",
1617
+ "image/jpeg",
1618
+ "image/webp",
1619
+ "application/pdf"
1620
+ ];
1621
+ var ACCEPT_ATTR = ALLOWED_MIME_TYPES.join(",");
1622
+ function isImageMime(mime) {
1623
+ return mime.startsWith("image/");
1624
+ }
1625
+ function ChatInput() {
1626
+ const {
1627
+ sendMessage,
1628
+ uploadAttachment,
1629
+ isLoading,
1630
+ readOnly,
1631
+ config,
1632
+ t,
1633
+ consumePendingPrefill,
1634
+ pendingPrefill
1635
+ } = useChat();
1636
+ const theme = useEffectiveTheme();
1637
+ const isDark = theme.scheme === "dark";
1638
+ const reduced = useReducedMotion();
1639
+ const [value, setValue] = useState7("");
1640
+ const [attachments, setAttachments] = useState7([]);
1641
+ const [captureSupported, setCaptureSupported] = useState7(false);
1642
+ const [menuOpen, setMenuOpen] = useState7(false);
1643
+ const [dragActive, setDragActive] = useState7(false);
1644
+ const [transientError, setTransientError] = useState7(null);
1645
+ const fileInputRef = useRef4(null);
1646
+ const textInputRef = useRef4(null);
1647
+ const menuRef = useRef4(null);
1648
+ const menuButtonRef = useRef4(null);
1649
+ const dragCounterRef = useRef4(0);
1650
+ useEffect7(() => {
1651
+ setCaptureSupported(canCaptureScreenshot());
1652
+ }, []);
1653
+ useEffect7(() => {
1654
+ const id = requestAnimationFrame(() => textInputRef.current?.focus());
1655
+ return () => cancelAnimationFrame(id);
1656
+ }, []);
1657
+ useLayoutEffect(() => {
1658
+ const el = textInputRef.current;
1659
+ if (!el) return;
1660
+ el.style.height = "auto";
1661
+ el.style.height = `${el.scrollHeight}px`;
1662
+ }, [value]);
1663
+ useEffect7(() => {
1664
+ if (pendingPrefill === null) return;
1665
+ const text = consumePendingPrefill();
1666
+ if (text !== null) setValue(text);
1667
+ }, [pendingPrefill, consumePendingPrefill]);
1668
+ useEffect7(() => {
1669
+ return () => {
1670
+ for (const a of attachments) URL.revokeObjectURL(a.previewUrl);
1671
+ };
1672
+ }, []);
1673
+ useEffect7(() => {
1674
+ if (!menuOpen) return;
1675
+ const onClick = (e) => {
1676
+ const target = e.target;
1677
+ if (menuRef.current?.contains(target) || menuButtonRef.current?.contains(target)) {
1678
+ return;
1679
+ }
1680
+ setMenuOpen(false);
1681
+ };
1682
+ const onKey = (e) => {
1683
+ if (e.key === "Escape") setMenuOpen(false);
1684
+ };
1685
+ document.addEventListener("mousedown", onClick);
1686
+ document.addEventListener("keydown", onKey);
1687
+ return () => {
1688
+ document.removeEventListener("mousedown", onClick);
1689
+ document.removeEventListener("keydown", onKey);
1690
+ };
1691
+ }, [menuOpen]);
1692
+ useEffect7(() => {
1693
+ if (!transientError) return;
1694
+ const id = window.setTimeout(() => setTransientError(null), 4e3);
1695
+ return () => window.clearTimeout(id);
1696
+ }, [transientError]);
1697
+ const updateAttachment = (id, patch) => {
1698
+ setAttachments(
1699
+ (current) => current.map(
1700
+ (a) => a.id === id ? { ...a, ...patch } : a
1701
+ )
1702
+ );
1703
+ };
1704
+ const startUpload = async (blob, filename) => {
1705
+ if (attachments.length >= MAX_ATTACHMENTS) return;
1706
+ const id = `att_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
1707
+ const previewUrl = URL.createObjectURL(blob);
1708
+ setAttachments((current) => [
1709
+ ...current,
1710
+ { id, status: "uploading", previewUrl, blob, filename }
1711
+ ]);
1712
+ try {
1713
+ const { attachmentToken } = await uploadAttachment(blob, { filename });
1714
+ updateAttachment(id, {
1715
+ status: "ready",
1716
+ token: attachmentToken
1717
+ });
1718
+ } catch {
1719
+ updateAttachment(id, { status: "error" });
1720
+ }
1721
+ };
1722
+ const ingestFiles = (files) => {
1723
+ const remainingSlots = Math.max(0, MAX_ATTACHMENTS - attachments.length);
1724
+ if (remainingSlots === 0) return 0;
1725
+ let queued = 0;
1726
+ let rejectedAny = false;
1727
+ for (const f of Array.from(files)) {
1728
+ if (queued >= remainingSlots) break;
1729
+ const type = f.type;
1730
+ if (!ALLOWED_MIME_TYPES.includes(
1731
+ type
1732
+ )) {
1733
+ rejectedAny = true;
1734
+ continue;
1735
+ }
1736
+ const filename = typeof f.name === "string" && f.name.length > 0 ? f.name : void 0;
1737
+ void startUpload(f, filename);
1738
+ queued += 1;
1739
+ }
1740
+ if (rejectedAny) {
1741
+ setTransientError(t("attachment_unsupported_type"));
1742
+ }
1743
+ return queued;
1744
+ };
1745
+ const handleCapture = async () => {
1746
+ setMenuOpen(false);
1747
+ try {
1748
+ const blob = await captureScreenshot();
1749
+ await startUpload(blob);
1750
+ } catch (e) {
1751
+ if (e instanceof ScreenshotCancelled) return;
1752
+ }
1753
+ };
1754
+ const handlePickFile = () => {
1755
+ setMenuOpen(false);
1756
+ fileInputRef.current?.click();
1757
+ };
1758
+ const handleFileInputChange = (e) => {
1759
+ const files = e.target.files;
1760
+ if (files && files.length > 0) {
1761
+ ingestFiles(files);
1762
+ }
1763
+ e.target.value = "";
1764
+ };
1765
+ const handleRemove = (id) => {
1766
+ setAttachments((current) => {
1767
+ const target = current.find((a) => a.id === id);
1768
+ if (target) URL.revokeObjectURL(target.previewUrl);
1769
+ return current.filter((a) => a.id !== id);
1770
+ });
1771
+ };
1772
+ const handlePaste = (e) => {
1773
+ const items = e.clipboardData?.items;
1774
+ if (!items || items.length === 0) return;
1775
+ const blobs = [];
1776
+ for (const item of Array.from(items)) {
1777
+ if (item.kind !== "file") continue;
1778
+ const blob = item.getAsFile();
1779
+ if (blob) blobs.push(blob);
1780
+ }
1781
+ if (blobs.length === 0) return;
1782
+ e.preventDefault();
1783
+ ingestFiles(blobs);
1784
+ };
1785
+ const handleDragEnter = (e) => {
1786
+ if (!hasFiles(e)) return;
1787
+ e.preventDefault();
1788
+ dragCounterRef.current += 1;
1789
+ setDragActive(true);
1790
+ };
1791
+ const handleDragOver = (e) => {
1792
+ if (!hasFiles(e)) return;
1793
+ e.preventDefault();
1794
+ };
1795
+ const handleDragLeave = (e) => {
1796
+ if (!hasFiles(e)) return;
1797
+ e.preventDefault();
1798
+ dragCounterRef.current = Math.max(0, dragCounterRef.current - 1);
1799
+ if (dragCounterRef.current === 0) setDragActive(false);
1800
+ };
1801
+ const handleDrop = (e) => {
1802
+ if (!hasFiles(e)) return;
1803
+ e.preventDefault();
1804
+ dragCounterRef.current = 0;
1805
+ setDragActive(false);
1806
+ const files = e.dataTransfer?.files;
1807
+ if (files && files.length > 0) {
1808
+ ingestFiles(files);
1809
+ }
1810
+ };
1811
+ const readyTokens = attachments.filter(
1812
+ (a) => a.status === "ready"
1813
+ ).map((a) => a.token);
1814
+ const handleSend = () => {
1815
+ if (!value.trim() || isLoading) return;
1816
+ sendMessage(
1817
+ value,
1818
+ readyTokens.length > 0 ? { attachmentTokens: readyTokens } : void 0
1819
+ );
1820
+ for (const a of attachments) URL.revokeObjectURL(a.previewUrl);
1821
+ setAttachments([]);
1822
+ setValue("");
1823
+ };
1824
+ const handleKeyDown = (e) => {
1825
+ if (e.key === "Enter" && !e.shiftKey) {
1826
+ e.preventDefault();
1827
+ handleSend();
1828
+ }
1829
+ };
1830
+ const containerStyle = {
1831
+ position: "relative",
1832
+ padding: "12px 16px",
1833
+ borderTop: `1px solid ${theme.divider}`,
1834
+ display: "flex",
1835
+ flexDirection: "column",
1836
+ gap: 8
1837
+ };
1838
+ const rowStyle = {
1839
+ display: "flex",
1840
+ // Anchor the icon buttons to the bottom of the row so a multi-line
1841
+ // textarea grows upward without dragging them along.
1842
+ alignItems: "flex-end",
1843
+ gap: 8
1844
+ };
1845
+ const TEXTAREA_MAX_HEIGHT = 140;
1846
+ const inputStyle = {
1847
+ flex: 1,
1848
+ border: `1px solid ${theme.divider}`,
1849
+ borderRadius: 18,
1850
+ padding: "10px 16px",
1851
+ fontSize: 14,
1852
+ lineHeight: 1.4,
1853
+ outline: "none",
1854
+ background: isDark ? "rgba(255,255,255,0.06)" : "#fafafa",
1855
+ color: theme.text,
1856
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
1857
+ resize: "none",
1858
+ overflowY: "auto",
1859
+ maxHeight: TEXTAREA_MAX_HEIGHT,
1860
+ boxSizing: "border-box"
1861
+ };
1862
+ const sendButtonStyle = {
1863
+ width: 36,
1864
+ height: 36,
1865
+ borderRadius: "50%",
1866
+ background: theme.primary,
1867
+ border: "none",
1868
+ color: "white",
1869
+ cursor: isLoading ? "not-allowed" : "pointer",
1870
+ display: "flex",
1871
+ alignItems: "center",
1872
+ justifyContent: "center",
1873
+ opacity: isLoading ? 0.5 : 1,
1874
+ transition: reduced ? "opacity 0.2s" : "opacity 0.2s, transform 0.15s",
1875
+ flexShrink: 0,
1876
+ transform: "scale(1)"
1877
+ };
1878
+ const iconButtonStyle = (disabled) => ({
1879
+ width: 36,
1880
+ height: 36,
1881
+ borderRadius: "50%",
1882
+ background: "transparent",
1883
+ border: "none",
1884
+ color: disabled ? "#ccc" : "#666",
1885
+ cursor: disabled ? "not-allowed" : "pointer",
1886
+ display: "flex",
1887
+ alignItems: "center",
1888
+ justifyContent: "center",
1889
+ flexShrink: 0,
1890
+ padding: 0
1891
+ });
1892
+ const menuStyle = {
1893
+ position: "absolute",
1894
+ bottom: "calc(100% + 4px)",
1895
+ left: 0,
1896
+ background: theme.background,
1897
+ border: `1px solid ${theme.divider}`,
1898
+ borderRadius: 8,
1899
+ boxShadow: isDark ? "0 4px 16px rgba(0,0,0,0.5)" : "0 4px 16px rgba(0,0,0,0.12)",
1900
+ padding: 4,
1901
+ minWidth: 180,
1902
+ zIndex: 10,
1903
+ display: "flex",
1904
+ flexDirection: "column"
1905
+ };
1906
+ const menuItemStyle = {
1907
+ display: "flex",
1908
+ alignItems: "center",
1909
+ gap: 10,
1910
+ padding: "8px 12px",
1911
+ background: "transparent",
1912
+ border: "none",
1913
+ borderRadius: 4,
1914
+ cursor: "pointer",
1915
+ fontSize: 14,
1916
+ color: theme.text,
1917
+ textAlign: "left",
1918
+ whiteSpace: "nowrap",
1919
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif"
1920
+ };
1921
+ const dropOverlayStyle = {
1922
+ position: "absolute",
1923
+ inset: 0,
1924
+ background: isDark ? "rgba(15,23,42,0.92)" : "rgba(255,255,255,0.92)",
1925
+ border: `2px dashed ${theme.primary}`,
1926
+ borderRadius: 4,
1927
+ display: "flex",
1928
+ alignItems: "center",
1929
+ justifyContent: "center",
1930
+ color: theme.primary,
1931
+ fontSize: 14,
1932
+ fontWeight: 500,
1933
+ pointerEvents: "none",
1934
+ zIndex: 11
1935
+ };
1936
+ const errorPillStyle = {
1937
+ alignSelf: "flex-start",
1938
+ background: "#fef2f2",
1939
+ color: "#b91c1c",
1940
+ border: "1px solid #fecaca",
1941
+ borderRadius: 12,
1942
+ padding: "4px 10px",
1943
+ fontSize: 12
1944
+ };
1945
+ const attachDisabled = attachments.length >= MAX_ATTACHMENTS || isLoading || readOnly;
1946
+ return /* @__PURE__ */ jsxs6(
1947
+ "div",
1948
+ {
1949
+ style: containerStyle,
1950
+ onDragEnter: handleDragEnter,
1951
+ onDragOver: handleDragOver,
1952
+ onDragLeave: handleDragLeave,
1953
+ onDrop: handleDrop,
1954
+ children: [
1955
+ dragActive && /* @__PURE__ */ jsx8("div", { style: dropOverlayStyle, "aria-hidden": "true", children: t("drop_files_here") }),
1956
+ transientError && /* @__PURE__ */ jsx8("div", { role: "alert", style: errorPillStyle, children: transientError }),
1957
+ attachments.length > 0 && /* @__PURE__ */ jsx8(
1958
+ "div",
1959
+ {
1960
+ style: { display: "flex", gap: 8, flexWrap: "wrap" },
1961
+ "aria-label": "Attachments",
1962
+ children: attachments.map((a) => /* @__PURE__ */ jsx8(
1963
+ Thumbnail,
1964
+ {
1965
+ attachment: a,
1966
+ onRemove: () => handleRemove(a.id),
1967
+ t
1968
+ },
1969
+ a.id
1970
+ ))
1971
+ }
1972
+ ),
1973
+ /* @__PURE__ */ jsxs6("div", { style: rowStyle, children: [
1974
+ config.allowAttachments !== false && /* @__PURE__ */ jsxs6("div", { style: { position: "relative" }, children: [
1975
+ /* @__PURE__ */ jsx8(
1976
+ "button",
1977
+ {
1978
+ ref: menuButtonRef,
1979
+ type: "button",
1980
+ onClick: () => setMenuOpen((o) => !o),
1981
+ disabled: attachDisabled,
1982
+ style: iconButtonStyle(attachDisabled),
1983
+ "aria-label": t("attach_menu_open"),
1984
+ "aria-haspopup": "menu",
1985
+ "aria-expanded": menuOpen,
1986
+ title: t("attach_menu_open"),
1987
+ children: /* @__PURE__ */ jsx8(PaperclipIcon, {})
1988
+ }
1989
+ ),
1990
+ menuOpen && /* @__PURE__ */ jsxs6("div", { ref: menuRef, role: "menu", style: menuStyle, children: [
1991
+ /* @__PURE__ */ jsxs6(
1992
+ "button",
1993
+ {
1994
+ type: "button",
1995
+ role: "menuitem",
1996
+ onClick: handlePickFile,
1997
+ style: menuItemStyle,
1998
+ onMouseEnter: (e) => {
1999
+ e.currentTarget.style.background = theme.bubbleBackground;
2000
+ },
2001
+ onMouseLeave: (e) => {
2002
+ e.currentTarget.style.background = "transparent";
2003
+ },
2004
+ children: [
2005
+ /* @__PURE__ */ jsx8(ImageIcon, {}),
2006
+ t("attach_photo")
2007
+ ]
2008
+ }
2009
+ ),
2010
+ captureSupported && /* @__PURE__ */ jsxs6(
2011
+ "button",
2012
+ {
2013
+ type: "button",
2014
+ role: "menuitem",
2015
+ onClick: handleCapture,
2016
+ style: menuItemStyle,
2017
+ onMouseEnter: (e) => {
2018
+ e.currentTarget.style.background = theme.bubbleBackground;
2019
+ },
2020
+ onMouseLeave: (e) => {
2021
+ e.currentTarget.style.background = "transparent";
2022
+ },
2023
+ children: [
2024
+ /* @__PURE__ */ jsx8(CameraIcon, {}),
2025
+ t("screenshot_capture")
2026
+ ]
2027
+ }
2028
+ )
2029
+ ] })
2030
+ ] }),
2031
+ /* @__PURE__ */ jsx8(
2032
+ "input",
2033
+ {
2034
+ ref: fileInputRef,
2035
+ type: "file",
2036
+ accept: ACCEPT_ATTR,
2037
+ multiple: true,
2038
+ onChange: handleFileInputChange,
2039
+ style: { display: "none" },
2040
+ "aria-hidden": "true",
2041
+ tabIndex: -1
2042
+ }
2043
+ ),
2044
+ /* @__PURE__ */ jsx8(
2045
+ "textarea",
2046
+ {
2047
+ ref: textInputRef,
2048
+ rows: 1,
2049
+ value,
2050
+ onChange: (e) => setValue(e.target.value),
2051
+ onKeyDown: handleKeyDown,
2052
+ onPaste: handlePaste,
2053
+ placeholder: config.placeholderText,
2054
+ style: inputStyle,
2055
+ disabled: isLoading || readOnly
2056
+ }
2057
+ ),
2058
+ /* @__PURE__ */ jsx8(
2059
+ "button",
2060
+ {
2061
+ onClick: handleSend,
2062
+ disabled: isLoading || readOnly || !value.trim(),
2063
+ style: sendButtonStyle,
2064
+ "aria-label": t("send_message"),
2065
+ onMouseEnter: (e) => {
2066
+ if (!reduced && !isLoading)
2067
+ e.currentTarget.style.transform = "scale(1.1)";
2068
+ },
2069
+ onMouseLeave: (e) => {
2070
+ if (!reduced) e.currentTarget.style.transform = "scale(1)";
2071
+ },
2072
+ children: /* @__PURE__ */ jsxs6(
2073
+ "svg",
2074
+ {
2075
+ width: "16",
2076
+ height: "16",
2077
+ viewBox: "0 0 24 24",
2078
+ fill: "none",
2079
+ stroke: "currentColor",
2080
+ strokeWidth: "2",
2081
+ strokeLinecap: "round",
2082
+ strokeLinejoin: "round",
2083
+ children: [
2084
+ /* @__PURE__ */ jsx8("line", { x1: "22", y1: "2", x2: "11", y2: "13" }),
2085
+ /* @__PURE__ */ jsx8("polygon", { points: "22 2 15 22 11 13 2 9 22 2" })
2086
+ ]
2087
+ }
2088
+ )
2089
+ }
2090
+ )
2091
+ ] })
2092
+ ]
2093
+ }
2094
+ );
2095
+ }
2096
+ function hasFiles(e) {
2097
+ const types = e.dataTransfer?.types;
2098
+ if (!types) return false;
2099
+ for (let i = 0; i < types.length; i++) {
2100
+ if (types[i] === "Files") return true;
2101
+ }
2102
+ return false;
2103
+ }
2104
+ function Thumbnail({
2105
+ attachment,
2106
+ onRemove,
2107
+ t
2108
+ }) {
2109
+ const { status, previewUrl, blob, filename } = attachment;
2110
+ const isImage = isImageMime(blob.type);
2111
+ const wrap = {
2112
+ position: "relative",
2113
+ width: isImage ? 56 : 160,
2114
+ height: 56,
2115
+ borderRadius: 8,
2116
+ overflow: "hidden",
2117
+ border: status === "error" ? "2px solid #b91c1c" : "1px solid #e0e0e0",
2118
+ background: "#f5f5f5",
2119
+ display: "flex",
2120
+ alignItems: "center",
2121
+ justifyContent: isImage ? "stretch" : "flex-start",
2122
+ gap: isImage ? 0 : 8,
2123
+ padding: isImage ? 0 : "0 8px"
2124
+ };
2125
+ const img = {
2126
+ width: "100%",
2127
+ height: "100%",
2128
+ objectFit: "cover"
2129
+ };
2130
+ const removeBtn = {
2131
+ position: "absolute",
2132
+ top: 2,
2133
+ right: 2,
2134
+ width: 18,
2135
+ height: 18,
2136
+ borderRadius: "50%",
2137
+ border: "none",
2138
+ background: "rgba(0,0,0,0.6)",
2139
+ color: "white",
2140
+ fontSize: 11,
2141
+ lineHeight: "18px",
2142
+ padding: 0,
2143
+ cursor: "pointer",
2144
+ textAlign: "center"
2145
+ };
2146
+ const overlay = {
2147
+ position: "absolute",
2148
+ inset: 0,
2149
+ background: "rgba(255,255,255,0.7)",
2150
+ display: "flex",
2151
+ alignItems: "center",
2152
+ justifyContent: "center"
2153
+ };
2154
+ const docName = {
2155
+ fontSize: 12,
2156
+ fontWeight: 500,
2157
+ color: "#333",
2158
+ overflow: "hidden",
2159
+ textOverflow: "ellipsis",
2160
+ whiteSpace: "nowrap",
2161
+ maxWidth: 110,
2162
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif"
2163
+ };
2164
+ const docMeta = {
2165
+ fontSize: 11,
2166
+ color: "#888",
2167
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif"
2168
+ };
2169
+ const displayName = filename ?? (isImage ? "" : "Document");
2170
+ const sizeLabel = formatBytes(blob.size);
2171
+ return /* @__PURE__ */ jsxs6("div", { style: wrap, children: [
2172
+ isImage ? /* @__PURE__ */ jsx8("img", { src: previewUrl, alt: "", style: img }) : /* @__PURE__ */ jsxs6(Fragment4, { children: [
2173
+ /* @__PURE__ */ jsx8(DocIcon, {}),
2174
+ /* @__PURE__ */ jsxs6(
2175
+ "div",
2176
+ {
2177
+ style: {
2178
+ display: "flex",
2179
+ flexDirection: "column",
2180
+ minWidth: 0,
2181
+ flex: 1
2182
+ },
2183
+ children: [
2184
+ /* @__PURE__ */ jsx8("span", { style: docName, title: displayName, children: displayName }),
2185
+ sizeLabel && /* @__PURE__ */ jsx8("span", { style: docMeta, children: sizeLabel })
2186
+ ]
2187
+ }
2188
+ )
2189
+ ] }),
2190
+ status === "uploading" && /* @__PURE__ */ jsx8("div", { style: overlay, "aria-label": "Uploading", children: /* @__PURE__ */ jsx8(Spinner2, {}) }),
2191
+ status === "error" && /* @__PURE__ */ jsx8(
2192
+ "div",
2193
+ {
2194
+ style: { ...overlay, background: "rgba(255,255,255,0.85)" },
2195
+ "aria-label": t("status_failed"),
2196
+ title: t("status_failed"),
2197
+ children: /* @__PURE__ */ jsx8("span", { style: { color: "#b91c1c", fontSize: 11, fontWeight: 600 }, children: "!" })
2198
+ }
2199
+ ),
2200
+ /* @__PURE__ */ jsx8(
2201
+ "button",
2202
+ {
2203
+ type: "button",
2204
+ style: removeBtn,
2205
+ onClick: onRemove,
2206
+ "aria-label": t("attachment_remove"),
2207
+ children: "\xD7"
2208
+ }
2209
+ )
2210
+ ] });
2211
+ }
2212
+ function PaperclipIcon() {
2213
+ return /* @__PURE__ */ jsx8(
2214
+ "svg",
2215
+ {
2216
+ width: "20",
2217
+ height: "20",
2218
+ viewBox: "0 0 24 24",
2219
+ fill: "none",
2220
+ stroke: "currentColor",
2221
+ strokeWidth: "2",
2222
+ strokeLinecap: "round",
2223
+ strokeLinejoin: "round",
2224
+ "aria-hidden": "true",
2225
+ children: /* @__PURE__ */ jsx8("path", { d: "M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48" })
2226
+ }
2227
+ );
2228
+ }
2229
+ function ImageIcon() {
2230
+ return /* @__PURE__ */ jsxs6(
2231
+ "svg",
2232
+ {
2233
+ width: "18",
2234
+ height: "18",
2235
+ viewBox: "0 0 24 24",
2236
+ fill: "none",
2237
+ stroke: "currentColor",
2238
+ strokeWidth: "2",
2239
+ strokeLinecap: "round",
2240
+ strokeLinejoin: "round",
2241
+ "aria-hidden": "true",
2242
+ children: [
2243
+ /* @__PURE__ */ jsx8("rect", { x: "3", y: "3", width: "18", height: "18", rx: "2", ry: "2" }),
2244
+ /* @__PURE__ */ jsx8("circle", { cx: "8.5", cy: "8.5", r: "1.5" }),
2245
+ /* @__PURE__ */ jsx8("polyline", { points: "21 15 16 10 5 21" })
2246
+ ]
2247
+ }
2248
+ );
2249
+ }
2250
+ function DocIcon() {
2251
+ return /* @__PURE__ */ jsxs6(
2252
+ "svg",
2253
+ {
2254
+ width: "20",
2255
+ height: "20",
2256
+ viewBox: "0 0 24 24",
2257
+ fill: "none",
2258
+ stroke: "currentColor",
2259
+ strokeWidth: "2",
2260
+ strokeLinecap: "round",
2261
+ strokeLinejoin: "round",
2262
+ "aria-hidden": "true",
2263
+ style: { color: "#666", flexShrink: 0 },
2264
+ children: [
2265
+ /* @__PURE__ */ jsx8("path", { d: "M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" }),
2266
+ /* @__PURE__ */ jsx8("polyline", { points: "14 2 14 8 20 8" }),
2267
+ /* @__PURE__ */ jsx8("line", { x1: "16", y1: "13", x2: "8", y2: "13" }),
2268
+ /* @__PURE__ */ jsx8("line", { x1: "16", y1: "17", x2: "8", y2: "17" }),
2269
+ /* @__PURE__ */ jsx8("polyline", { points: "10 9 9 9 8 9" })
2270
+ ]
2271
+ }
2272
+ );
2273
+ }
2274
+ function formatBytes(bytes) {
2275
+ if (!Number.isFinite(bytes) || bytes <= 0) return "";
2276
+ if (bytes < 1024) return `${bytes} B`;
2277
+ if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
2278
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
2279
+ }
2280
+ function CameraIcon() {
2281
+ return /* @__PURE__ */ jsxs6(
2282
+ "svg",
2283
+ {
2284
+ width: "18",
2285
+ height: "18",
2286
+ viewBox: "0 0 24 24",
2287
+ fill: "none",
2288
+ stroke: "currentColor",
2289
+ strokeWidth: "2",
2290
+ strokeLinecap: "round",
2291
+ strokeLinejoin: "round",
2292
+ "aria-hidden": "true",
2293
+ children: [
2294
+ /* @__PURE__ */ jsx8("path", { d: "M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z" }),
2295
+ /* @__PURE__ */ jsx8("circle", { cx: "12", cy: "13", r: "4" })
2296
+ ]
2297
+ }
2298
+ );
2299
+ }
2300
+ function Spinner2() {
2301
+ return /* @__PURE__ */ jsxs6(Fragment4, { children: [
2302
+ /* @__PURE__ */ jsx8("style", { children: `@keyframes ch-att-spin { to { transform: rotate(360deg); } }` }),
2303
+ /* @__PURE__ */ jsx8(
2304
+ "span",
2305
+ {
2306
+ "aria-hidden": "true",
2307
+ style: {
2308
+ width: 16,
2309
+ height: 16,
2310
+ borderRadius: "50%",
2311
+ border: "2px solid #888",
2312
+ borderTopColor: "transparent",
2313
+ animation: "ch-att-spin 0.8s linear infinite",
2314
+ display: "inline-block"
2315
+ }
2316
+ }
2317
+ )
2318
+ ] });
2319
+ }
2320
+
2321
+ // src/components/incident-banner.tsx
2322
+ import { useState as useState8 } from "react";
2323
+ import { jsx as jsx9, jsxs as jsxs7 } from "react/jsx-runtime";
2324
+ var PALETTE = {
2325
+ info: {
2326
+ bg: "#EFF6FF",
2327
+ fg: "#1E3A8A",
2328
+ fgMuted: "#3B5BA9",
2329
+ accent: "#2563EB",
2330
+ iconBg: "#DBEAFE",
2331
+ iconFg: "#2563EB"
2332
+ },
2333
+ warning: {
2334
+ bg: "#FFFBEB",
2335
+ fg: "#78350F",
2336
+ fgMuted: "#92541A",
2337
+ accent: "#B45309",
2338
+ iconBg: "#FEF3C7",
2339
+ iconFg: "#B45309"
2340
+ },
2341
+ outage: {
2342
+ bg: "#FEF2F2",
2343
+ fg: "#991B1B",
2344
+ fgMuted: "#B23A3A",
2345
+ accent: "#DC2626",
2346
+ iconBg: "#FEE2E2",
2347
+ iconFg: "#DC2626"
2348
+ }
2349
+ };
2350
+ function SeverityIcon({
2351
+ severity,
2352
+ color
2353
+ }) {
2354
+ const props = {
2355
+ width: 14,
2356
+ height: 14,
2357
+ viewBox: "0 0 24 24",
2358
+ fill: "none",
2359
+ stroke: color,
2360
+ strokeWidth: 2.25,
2361
+ strokeLinecap: "round",
2362
+ strokeLinejoin: "round",
2363
+ "aria-hidden": true
2364
+ };
2365
+ if (severity === "outage") {
2366
+ return /* @__PURE__ */ jsxs7("svg", { ...props, children: [
2367
+ /* @__PURE__ */ jsx9("circle", { cx: "12", cy: "12", r: "10" }),
2368
+ /* @__PURE__ */ jsx9("line", { x1: "12", y1: "8", x2: "12", y2: "12" }),
2369
+ /* @__PURE__ */ jsx9("line", { x1: "12", y1: "16", x2: "12.01", y2: "16" })
2370
+ ] });
2371
+ }
2372
+ if (severity === "warning") {
2373
+ return /* @__PURE__ */ jsxs7("svg", { ...props, children: [
2374
+ /* @__PURE__ */ jsx9("path", { d: "M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" }),
2375
+ /* @__PURE__ */ jsx9("line", { x1: "12", y1: "9", x2: "12", y2: "13" }),
2376
+ /* @__PURE__ */ jsx9("line", { x1: "12", y1: "17", x2: "12.01", y2: "17" })
2377
+ ] });
2378
+ }
2379
+ return /* @__PURE__ */ jsxs7("svg", { ...props, children: [
2380
+ /* @__PURE__ */ jsx9("circle", { cx: "12", cy: "12", r: "10" }),
2381
+ /* @__PURE__ */ jsx9("line", { x1: "12", y1: "16", x2: "12", y2: "12" }),
2382
+ /* @__PURE__ */ jsx9("line", { x1: "12", y1: "8", x2: "12.01", y2: "8" })
2383
+ ] });
2384
+ }
2385
+ function IncidentBanner() {
2386
+ const { incidentBanner, incidentBannerDismissed, dismissIncidentBanner, t } = useChat();
2387
+ const [linkHover, setLinkHover] = useState8(false);
2388
+ const [closeHover, setCloseHover] = useState8(false);
2389
+ if (!incidentBanner || incidentBannerDismissed) return null;
2390
+ const palette = PALETTE[incidentBanner.severity];
2391
+ const wrap = {
2392
+ background: palette.bg,
2393
+ color: palette.fg,
2394
+ padding: "12px 14px 12px 12px",
2395
+ display: "flex",
2396
+ gap: 10,
2397
+ alignItems: "flex-start",
2398
+ fontSize: 13,
2399
+ lineHeight: 1.45,
2400
+ boxShadow: "inset 0 -1px 0 rgba(0,0,0,0.04)"
2401
+ };
2402
+ const iconBadge = {
2403
+ width: 22,
2404
+ height: 22,
2405
+ borderRadius: "50%",
2406
+ background: palette.iconBg,
2407
+ display: "flex",
2408
+ alignItems: "center",
2409
+ justifyContent: "center",
2410
+ flexShrink: 0,
2411
+ marginTop: 1
2412
+ };
2413
+ const titleStyle = {
2414
+ margin: 0,
2415
+ fontSize: 13,
2416
+ fontWeight: 600,
2417
+ letterSpacing: "-0.005em"
2418
+ };
2419
+ const bodyStyle = {
2420
+ margin: "3px 0 0",
2421
+ fontSize: 12.5,
2422
+ color: palette.fgMuted
2423
+ };
2424
+ const footerRowStyle = {
2425
+ display: "flex",
2426
+ flexWrap: "wrap",
2427
+ alignItems: "center",
2428
+ gap: 10,
2429
+ marginTop: 8
2430
+ };
2431
+ const etaStyle = {
2432
+ display: "inline-block",
2433
+ padding: "2px 8px",
2434
+ borderRadius: 4,
2435
+ fontSize: 11,
2436
+ fontWeight: 500,
2437
+ color: palette.accent,
2438
+ background: palette.iconBg,
2439
+ lineHeight: 1.4
2440
+ };
2441
+ const linkStyle = {
2442
+ display: "inline-flex",
2443
+ alignItems: "center",
2444
+ gap: 4,
2445
+ color: palette.accent,
2446
+ textDecoration: linkHover ? "underline" : "none",
2447
+ textUnderlineOffset: 3,
2448
+ fontSize: 12.5,
2449
+ fontWeight: 600
2450
+ };
2451
+ const closeButtonStyle = {
2452
+ background: closeHover ? "rgba(0,0,0,0.06)" : "transparent",
2453
+ border: "none",
2454
+ color: palette.fgMuted,
2455
+ cursor: "pointer",
2456
+ padding: 4,
2457
+ borderRadius: 6,
2458
+ flexShrink: 0,
2459
+ lineHeight: 0,
2460
+ marginTop: -2,
2461
+ marginRight: -2,
2462
+ transition: "background-color 0.12s ease"
2463
+ };
2464
+ const role = incidentBanner.severity === "outage" ? "alert" : "status";
2465
+ return /* @__PURE__ */ jsxs7("div", { role, style: wrap, children: [
2466
+ /* @__PURE__ */ jsx9("div", { style: iconBadge, children: /* @__PURE__ */ jsx9(
2467
+ SeverityIcon,
2468
+ {
2469
+ severity: incidentBanner.severity,
2470
+ color: palette.iconFg
2471
+ }
2472
+ ) }),
2473
+ /* @__PURE__ */ jsxs7("div", { style: { flex: 1, minWidth: 0 }, children: [
2474
+ /* @__PURE__ */ jsx9("p", { style: titleStyle, children: incidentBanner.title }),
2475
+ incidentBanner.body ? /* @__PURE__ */ jsx9("p", { style: bodyStyle, children: incidentBanner.body }) : null,
2476
+ incidentBanner.eta || incidentBanner.link ? /* @__PURE__ */ jsxs7("div", { style: footerRowStyle, children: [
2477
+ incidentBanner.eta ? /* @__PURE__ */ jsx9("span", { style: etaStyle, children: incidentBanner.eta }) : null,
2478
+ incidentBanner.link ? /* @__PURE__ */ jsxs7(
2479
+ "a",
2480
+ {
2481
+ href: incidentBanner.link.url,
2482
+ target: "_blank",
2483
+ rel: "noopener noreferrer",
2484
+ style: linkStyle,
2485
+ onMouseEnter: () => setLinkHover(true),
2486
+ onMouseLeave: () => setLinkHover(false),
2487
+ children: [
2488
+ incidentBanner.link.label ?? t("incident_default_link_label"),
2489
+ /* @__PURE__ */ jsxs7(
2490
+ "svg",
2491
+ {
2492
+ width: "12",
2493
+ height: "12",
2494
+ viewBox: "0 0 24 24",
2495
+ fill: "none",
2496
+ stroke: "currentColor",
2497
+ strokeWidth: "2.5",
2498
+ strokeLinecap: "round",
2499
+ strokeLinejoin: "round",
2500
+ "aria-hidden": "true",
2501
+ children: [
2502
+ /* @__PURE__ */ jsx9("line", { x1: "5", y1: "12", x2: "19", y2: "12" }),
2503
+ /* @__PURE__ */ jsx9("polyline", { points: "12 5 19 12 12 19" })
2504
+ ]
2505
+ }
2506
+ )
2507
+ ]
2508
+ }
2509
+ ) : null
2510
+ ] }) : null
2511
+ ] }),
2512
+ /* @__PURE__ */ jsx9(
2513
+ "button",
2514
+ {
2515
+ type: "button",
2516
+ onClick: dismissIncidentBanner,
2517
+ "aria-label": t("incident_dismiss"),
2518
+ style: closeButtonStyle,
2519
+ onMouseEnter: () => setCloseHover(true),
2520
+ onMouseLeave: () => setCloseHover(false),
2521
+ children: /* @__PURE__ */ jsxs7(
2522
+ "svg",
2523
+ {
2524
+ width: "14",
2525
+ height: "14",
2526
+ viewBox: "0 0 24 24",
2527
+ fill: "none",
2528
+ stroke: "currentColor",
2529
+ strokeWidth: "2",
2530
+ strokeLinecap: "round",
2531
+ strokeLinejoin: "round",
2532
+ "aria-hidden": "true",
2533
+ children: [
2534
+ /* @__PURE__ */ jsx9("line", { x1: "18", y1: "6", x2: "6", y2: "18" }),
2535
+ /* @__PURE__ */ jsx9("line", { x1: "6", y1: "6", x2: "18", y2: "18" })
2536
+ ]
2537
+ }
2538
+ )
2539
+ }
2540
+ )
2541
+ ] });
2542
+ }
2543
+
2544
+ // src/components/chat-window.tsx
2545
+ import { Fragment as Fragment5, jsx as jsx10, jsxs as jsxs8 } from "react/jsx-runtime";
2546
+ function ConfigError({ message, title }) {
2547
+ const errorStyle = {
2548
+ flex: 1,
2549
+ display: "flex",
2550
+ flexDirection: "column",
2551
+ alignItems: "center",
2552
+ justifyContent: "center",
2553
+ padding: 24,
2554
+ textAlign: "center",
2555
+ gap: 8
2556
+ };
2557
+ return /* @__PURE__ */ jsxs8("div", { style: errorStyle, children: [
2558
+ /* @__PURE__ */ jsxs8(
2559
+ "svg",
2560
+ {
2561
+ width: "32",
2562
+ height: "32",
2563
+ viewBox: "0 0 24 24",
2564
+ fill: "none",
2565
+ stroke: "#999",
2566
+ strokeWidth: "2",
2567
+ strokeLinecap: "round",
2568
+ strokeLinejoin: "round",
2569
+ children: [
2570
+ /* @__PURE__ */ jsx10("circle", { cx: "12", cy: "12", r: "10" }),
2571
+ /* @__PURE__ */ jsx10("line", { x1: "12", y1: "8", x2: "12", y2: "12" }),
2572
+ /* @__PURE__ */ jsx10("line", { x1: "12", y1: "16", x2: "12.01", y2: "16" })
2573
+ ]
2574
+ }
2575
+ ),
2576
+ /* @__PURE__ */ jsx10("p", { style: { fontSize: 14, fontWeight: 500, color: "#333", margin: 0 }, children: title }),
2577
+ /* @__PURE__ */ jsx10("p", { style: { fontSize: 12, color: "#999", margin: 0 }, children: message })
2578
+ ] });
2579
+ }
2580
+ function fieldKey(field) {
2581
+ if (field.kind === "name" || field.kind === "email" || field.kind === "phone") {
2582
+ return field.kind;
2583
+ }
2584
+ return field.key;
2585
+ }
2586
+ function fieldLabel(field) {
2587
+ if (field.kind === "name") return field.label ?? "Name";
2588
+ if (field.kind === "email") return field.label ?? "Email";
2589
+ if (field.kind === "phone") return field.label ?? "Phone";
2590
+ return field.label;
2591
+ }
2592
+ function validateField(field, value) {
2593
+ const required = "required" in field ? !!field.required : false;
2594
+ if (required && (value === void 0 || value === "" || value === false)) {
2595
+ return "Required";
2596
+ }
2597
+ if (field.kind === "email" && typeof value === "string" && value !== "") {
2598
+ if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(value)) return "Invalid email";
2599
+ }
2600
+ return null;
2601
+ }
2602
+ function PreChatFormView() {
2603
+ const { preChatForm, submitPreChatForm, cancelPreChatForm, config, t } = useChat();
2604
+ const [values, setValues] = useState9({});
2605
+ const [errors, setErrors] = useState9({});
2606
+ const [submitting, setSubmitting] = useState9(false);
2607
+ if (!preChatForm) return null;
2608
+ function setValue(key, value) {
2609
+ setValues((prev) => ({ ...prev, [key]: value }));
2610
+ setErrors((prev) => ({ ...prev, [key]: null }));
2611
+ }
2612
+ async function handleSubmit(e) {
2613
+ e.preventDefault();
2614
+ if (!preChatForm) return;
2615
+ const nextErrors = {};
2616
+ let hasError = false;
2617
+ for (const f of preChatForm.fields) {
2618
+ const k = fieldKey(f);
2619
+ const err = validateField(f, values[k]);
2620
+ nextErrors[k] = err;
2621
+ if (err) hasError = true;
2622
+ }
2623
+ setErrors(nextErrors);
2624
+ if (hasError) return;
2625
+ const submission = {};
2626
+ const properties = {};
2627
+ for (const f of preChatForm.fields) {
2628
+ const k = fieldKey(f);
2629
+ const v = values[k];
2630
+ if (v === void 0 || v === "") continue;
2631
+ if (f.kind === "name" && typeof v === "string") submission.name = v;
2632
+ else if (f.kind === "email" && typeof v === "string")
2633
+ submission.email = v;
2634
+ else if (f.kind === "phone" && typeof v === "string")
2635
+ submission.phone = v;
2636
+ else if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
2637
+ properties[k] = v;
2638
+ }
2639
+ }
2640
+ if (Object.keys(properties).length > 0) submission.properties = properties;
2641
+ setSubmitting(true);
2642
+ try {
2643
+ await submitPreChatForm(submission);
2644
+ } finally {
2645
+ setSubmitting(false);
2646
+ }
2647
+ }
2648
+ const containerStyle = {
2649
+ flex: 1,
2650
+ overflowY: "auto",
2651
+ padding: 20,
2652
+ display: "flex",
2653
+ flexDirection: "column",
2654
+ gap: 12,
2655
+ background: config.backgroundColor,
2656
+ color: config.textColor
2657
+ };
2658
+ const labelStyle = {
2659
+ fontSize: 13,
2660
+ fontWeight: 500,
2661
+ display: "flex",
2662
+ flexDirection: "column",
2663
+ gap: 4
2664
+ };
2665
+ const inputStyle = {
2666
+ width: "100%",
2667
+ border: "1px solid #d4d4d8",
2668
+ borderRadius: 8,
2669
+ padding: "8px 10px",
2670
+ fontSize: 14,
2671
+ background: "white",
2672
+ color: "#111",
2673
+ boxSizing: "border-box"
2674
+ };
2675
+ const errorStyle = { color: "#dc2626", fontSize: 12 };
2676
+ const buttonRowStyle = {
2677
+ display: "flex",
2678
+ gap: 8,
2679
+ marginTop: 8
2680
+ };
2681
+ const submitStyle = {
2682
+ flex: 1,
2683
+ background: config.primaryColor,
2684
+ color: "white",
2685
+ border: "none",
2686
+ borderRadius: 8,
2687
+ padding: "10px 14px",
2688
+ fontSize: 14,
2689
+ fontWeight: 600,
2690
+ cursor: submitting ? "not-allowed" : "pointer",
2691
+ opacity: submitting ? 0.7 : 1
2692
+ };
2693
+ const cancelStyle = {
2694
+ background: "transparent",
2695
+ color: config.textColor,
2696
+ border: "1px solid #d4d4d8",
2697
+ borderRadius: 8,
2698
+ padding: "10px 14px",
2699
+ fontSize: 14,
2700
+ cursor: "pointer"
2701
+ };
2702
+ return /* @__PURE__ */ jsxs8(
2703
+ "form",
2704
+ {
2705
+ style: containerStyle,
2706
+ onSubmit: handleSubmit,
2707
+ "data-customerhero-prechat-form": true,
2708
+ children: [
2709
+ preChatForm.title && /* @__PURE__ */ jsx10("h3", { style: { fontSize: 16, fontWeight: 600, margin: 0 }, children: preChatForm.title }),
2710
+ preChatForm.description && /* @__PURE__ */ jsx10("p", { style: { fontSize: 13, margin: 0, opacity: 0.8 }, children: preChatForm.description }),
2711
+ preChatForm.fields.map((field) => {
2712
+ const k = fieldKey(field);
2713
+ const v = values[k];
2714
+ const err = errors[k];
2715
+ const required = "required" in field ? !!field.required : false;
2716
+ const label = fieldLabel(field);
2717
+ if (field.kind === "textarea") {
2718
+ return /* @__PURE__ */ jsxs8("label", { style: labelStyle, children: [
2719
+ /* @__PURE__ */ jsxs8("span", { children: [
2720
+ label,
2721
+ required && " *"
2722
+ ] }),
2723
+ /* @__PURE__ */ jsx10(
2724
+ "textarea",
2725
+ {
2726
+ style: { ...inputStyle, minHeight: 80, resize: "vertical" },
2727
+ value: v ?? "",
2728
+ maxLength: field.maxLength,
2729
+ onChange: (e) => setValue(k, e.target.value)
2730
+ }
2731
+ ),
2732
+ err && /* @__PURE__ */ jsx10("span", { style: errorStyle, children: err })
2733
+ ] }, k);
2734
+ }
2735
+ if (field.kind === "select") {
2736
+ return /* @__PURE__ */ jsxs8("label", { style: labelStyle, children: [
2737
+ /* @__PURE__ */ jsxs8("span", { children: [
2738
+ label,
2739
+ required && " *"
2740
+ ] }),
2741
+ /* @__PURE__ */ jsxs8(
2742
+ "select",
2743
+ {
2744
+ style: inputStyle,
2745
+ value: v ?? "",
2746
+ onChange: (e) => setValue(k, e.target.value),
2747
+ children: [
2748
+ /* @__PURE__ */ jsx10("option", { value: "", children: "\u2014" }),
2749
+ field.options.map((opt) => /* @__PURE__ */ jsx10("option", { value: opt.value, children: opt.label }, opt.value))
2750
+ ]
2751
+ }
2752
+ ),
2753
+ err && /* @__PURE__ */ jsx10("span", { style: errorStyle, children: err })
2754
+ ] }, k);
2755
+ }
2756
+ if (field.kind === "consent") {
2757
+ return /* @__PURE__ */ jsxs8(
2758
+ "label",
2759
+ {
2760
+ style: {
2761
+ ...labelStyle,
2762
+ flexDirection: "row",
2763
+ alignItems: "flex-start",
2764
+ gap: 8
2765
+ },
2766
+ children: [
2767
+ /* @__PURE__ */ jsx10(
2768
+ "input",
2769
+ {
2770
+ type: "checkbox",
2771
+ checked: v === true,
2772
+ onChange: (e) => setValue(k, e.target.checked),
2773
+ style: { marginTop: 3 }
2774
+ }
2775
+ ),
2776
+ /* @__PURE__ */ jsxs8("span", { style: { fontSize: 13, fontWeight: 400 }, children: [
2777
+ field.label,
2778
+ field.url && /* @__PURE__ */ jsxs8(Fragment5, { children: [
2779
+ " ",
2780
+ /* @__PURE__ */ jsx10(
2781
+ "a",
2782
+ {
2783
+ href: field.url,
2784
+ target: "_blank",
2785
+ rel: "noopener noreferrer",
2786
+ style: { color: config.primaryColor },
2787
+ children: "\u2197"
2788
+ }
2789
+ )
2790
+ ] })
2791
+ ] }),
2792
+ err && /* @__PURE__ */ jsx10("span", { style: errorStyle, children: err })
2793
+ ]
2794
+ },
2795
+ k
2796
+ );
2797
+ }
2798
+ const inputType = field.kind === "email" ? "email" : field.kind === "phone" ? "tel" : "text";
2799
+ const maxLength = field.kind === "text" ? field.maxLength : void 0;
2800
+ return /* @__PURE__ */ jsxs8("label", { style: labelStyle, children: [
2801
+ /* @__PURE__ */ jsxs8("span", { children: [
2802
+ label,
2803
+ required && " *"
2804
+ ] }),
2805
+ /* @__PURE__ */ jsx10(
2806
+ "input",
2807
+ {
2808
+ type: inputType,
2809
+ style: inputStyle,
2810
+ value: v ?? "",
2811
+ maxLength,
2812
+ onChange: (e) => setValue(k, e.target.value)
2813
+ }
2814
+ ),
2815
+ err && /* @__PURE__ */ jsx10("span", { style: errorStyle, children: err })
2816
+ ] }, k);
2817
+ }),
2818
+ /* @__PURE__ */ jsxs8("div", { style: buttonRowStyle, children: [
2819
+ /* @__PURE__ */ jsx10(
2820
+ "button",
2821
+ {
2822
+ type: "button",
2823
+ onClick: cancelPreChatForm,
2824
+ style: cancelStyle,
2825
+ disabled: submitting,
2826
+ children: t("action_cancel")
2827
+ }
2828
+ ),
2829
+ /* @__PURE__ */ jsx10("button", { type: "submit", style: submitStyle, disabled: submitting, children: preChatForm.submitLabel })
2830
+ ] })
2831
+ ]
2832
+ }
2833
+ );
2834
+ }
2835
+ function ChatWindow({ embedded } = {}) {
2836
+ const { isOpen, config, configError, t, isRtl, preChatFormVisible } = useChat();
2837
+ const reduced = useReducedMotion();
2838
+ const theme = useEffectiveTheme();
2839
+ const [visible, setVisible] = useState9(false);
2840
+ const [shouldRender, setShouldRender] = useState9(false);
2841
+ useEffect8(() => {
2842
+ if (isOpen) {
2843
+ setShouldRender(true);
2844
+ requestAnimationFrame(() => {
2845
+ requestAnimationFrame(() => setVisible(true));
2846
+ });
2847
+ } else {
2848
+ setVisible(false);
2849
+ if (reduced) {
2850
+ setShouldRender(false);
2851
+ } else {
2852
+ const timer = setTimeout(() => setShouldRender(false), 250);
2853
+ return () => clearTimeout(timer);
2854
+ }
2855
+ }
2856
+ }, [isOpen, reduced]);
2857
+ if (!shouldRender) return null;
2858
+ const effectivePosition = isRtl ? config.position === "bottom-right" ? "bottom-left" : "bottom-right" : config.position;
2859
+ const colors = theme;
2860
+ const preset = theme.size;
2861
+ const radius = theme.radius;
2862
+ const panelBottom = config.offset.bottom + preset.bubble + 14;
2863
+ const style = {
2864
+ position: embedded ? "absolute" : "fixed",
2865
+ bottom: panelBottom,
2866
+ [effectivePosition === "bottom-left" ? "left" : "right"]: config.offset.side,
2867
+ width: preset.width,
2868
+ maxWidth: embedded ? "calc(100% - 16px)" : "calc(100vw - 40px)",
2869
+ height: preset.height,
2870
+ maxHeight: embedded ? `calc(100% - ${panelBottom + 16}px)` : `calc(100vh - ${panelBottom + 30}px)`,
2871
+ borderRadius: radius,
2872
+ overflow: "hidden",
2873
+ display: "flex",
2874
+ flexDirection: "column",
2875
+ // In dark mode the default 0.15 opacity black shadow is invisible
2876
+ // against a dark page; switch to a stronger shadow plus a subtle 1px
2877
+ // light outline so the panel still reads as a separated surface.
2878
+ boxShadow: theme.scheme === "dark" ? "0 12px 40px rgba(0,0,0,0.55), 0 0 0 1px rgba(255,255,255,0.06)" : "0 8px 40px rgba(0,0,0,0.15)",
2879
+ zIndex: config.zIndex,
2880
+ background: colors.background,
2881
+ color: colors.text,
2882
+ fontSize: preset.fontSize,
2883
+ fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif",
2884
+ opacity: visible ? 1 : 0,
2885
+ transform: visible ? "translateY(0) scale(1)" : "translateY(16px) scale(0.97)",
2886
+ transition: reduced ? "none" : "opacity 0.25s ease, transform 0.25s ease"
2887
+ };
2888
+ const isDark = theme.scheme === "dark";
2889
+ const poweredStyle = {
2890
+ textAlign: "center",
2891
+ padding: 6,
2892
+ fontSize: 10,
2893
+ color: isDark ? "rgba(255,255,255,0.45)" : "#aaa"
2894
+ };
2895
+ const linkStyle = {
2896
+ color: isDark ? "rgba(255,255,255,0.6)" : "#888",
2897
+ textDecoration: "underline",
2898
+ textUnderlineOffset: 2
2899
+ };
2900
+ return /* @__PURE__ */ jsxs8("div", { style, dir: isRtl ? "rtl" : "ltr", children: [
2901
+ /* @__PURE__ */ jsx10(ChatHeader, {}),
2902
+ /* @__PURE__ */ jsx10(IncidentBanner, {}),
2903
+ configError ? /* @__PURE__ */ jsx10(ConfigError, { title: t("unable_to_load"), message: configError }) : preChatFormVisible ? /* @__PURE__ */ jsx10(PreChatFormView, {}) : /* @__PURE__ */ jsxs8(Fragment5, { children: [
2904
+ /* @__PURE__ */ jsx10(ChatMessages, {}),
2905
+ /* @__PURE__ */ jsx10(ChatSuggestions, {}),
2906
+ /* @__PURE__ */ jsx10(ChatInput, {})
2907
+ ] }),
2908
+ /* @__PURE__ */ jsxs8("div", { style: poweredStyle, children: [
2909
+ t("powered_by"),
2910
+ " ",
2911
+ /* @__PURE__ */ jsx10(
2912
+ "a",
2913
+ {
2914
+ href: "https://customerhero.app",
2915
+ target: "_blank",
2916
+ rel: "noopener noreferrer",
2917
+ style: linkStyle,
2918
+ children: "CustomerHero"
2919
+ }
2920
+ )
2921
+ ] })
2922
+ ] });
2923
+ }
2924
+
2925
+ export {
2926
+ CustomerHeroProvider,
2927
+ useCustomerHeroClient,
2928
+ useChat,
2929
+ ChatBubble,
2930
+ ActionConfirmationCard,
2931
+ ChatWindow
2932
+ };