@kilichat/react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +79 -2
- package/dist/index.cjs +969 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +273 -0
- package/dist/index.d.ts +273 -0
- package/dist/index.js +900 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +2 -0
- package/package.json +78 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,969 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
AppStateContext: () => AppStateContext,
|
|
35
|
+
AppStateProvider: () => state_default,
|
|
36
|
+
Button: () => Button,
|
|
37
|
+
ChatHome: () => ChatHome,
|
|
38
|
+
ChatThread: () => ChatThread,
|
|
39
|
+
ChatWidget: () => ChatWidget,
|
|
40
|
+
InitialsAvatar: () => InitialsAvatar,
|
|
41
|
+
KiliChat: () => KiliChat,
|
|
42
|
+
LocalStorageKeys: () => LocalStorageKeys,
|
|
43
|
+
SessionStorageKeys: () => SessionStorageKeys,
|
|
44
|
+
SocketContext: () => SocketContext,
|
|
45
|
+
SocketEvent: () => SocketEvent,
|
|
46
|
+
SocketProvider: () => SocketProvider,
|
|
47
|
+
Textarea: () => Textarea,
|
|
48
|
+
agentById: () => agentById,
|
|
49
|
+
agents: () => agents,
|
|
50
|
+
brand: () => brand,
|
|
51
|
+
buttonVariants: () => buttonVariants,
|
|
52
|
+
chatService: () => chat_service_default,
|
|
53
|
+
clockTime: () => clockTime,
|
|
54
|
+
cn: () => cn,
|
|
55
|
+
configureKili: () => configureKili,
|
|
56
|
+
coreService: () => core_service_default,
|
|
57
|
+
customer: () => customer,
|
|
58
|
+
dayLabel: () => dayLabel,
|
|
59
|
+
getInitialsFromFullName: () => getInitialsFromFullName,
|
|
60
|
+
getInitialsFromNames: () => getInitialsFromNames,
|
|
61
|
+
getKiliConfig: () => getKiliConfig,
|
|
62
|
+
http: () => http_default,
|
|
63
|
+
nextCannedReply: () => nextCannedReply,
|
|
64
|
+
relativeTime: () => relativeTime,
|
|
65
|
+
seedConversations: () => seedConversations,
|
|
66
|
+
useApp: () => useApp,
|
|
67
|
+
useIsMobile: () => useIsMobile
|
|
68
|
+
});
|
|
69
|
+
module.exports = __toCommonJS(index_exports);
|
|
70
|
+
|
|
71
|
+
// src/kili-chat.tsx
|
|
72
|
+
var import_react4 = require("react");
|
|
73
|
+
|
|
74
|
+
// src/components/widget/chat-widget.tsx
|
|
75
|
+
var import_react3 = require("react");
|
|
76
|
+
|
|
77
|
+
// src/components/widget/chat-home.tsx
|
|
78
|
+
var import_lucide_react = require("lucide-react");
|
|
79
|
+
|
|
80
|
+
// src/lib/utils.ts
|
|
81
|
+
var import_clsx = require("clsx");
|
|
82
|
+
var import_tailwind_merge = require("tailwind-merge");
|
|
83
|
+
function cn(...inputs) {
|
|
84
|
+
return (0, import_tailwind_merge.twMerge)((0, import_clsx.clsx)(inputs));
|
|
85
|
+
}
|
|
86
|
+
function getInitialsFromFullName(fullname) {
|
|
87
|
+
if (!fullname) return "CS";
|
|
88
|
+
const [fname, lname] = fullname.split(" ");
|
|
89
|
+
const firstInitial = fname?.substring(0, 1) ?? "";
|
|
90
|
+
const lastInitial = lname?.substring(0, 1) ?? "";
|
|
91
|
+
if (firstInitial === "" && lastInitial === "") return "CS";
|
|
92
|
+
return `${firstInitial.toUpperCase()}${lastInitial.toUpperCase()}`;
|
|
93
|
+
}
|
|
94
|
+
function getInitialsFromNames(fname, lname) {
|
|
95
|
+
const firstInitial = fname?.substring(0, 1) ?? "";
|
|
96
|
+
const lastInitial = lname?.substring(0, 1) ?? "";
|
|
97
|
+
if (firstInitial === "" && lastInitial === "") return "CS";
|
|
98
|
+
return `${firstInitial.toUpperCase()}${lastInitial.toUpperCase()}`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/components/widget/avatar.tsx
|
|
102
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
103
|
+
function InitialsAvatar({
|
|
104
|
+
initials,
|
|
105
|
+
size = "md",
|
|
106
|
+
tone = "brand",
|
|
107
|
+
className
|
|
108
|
+
}) {
|
|
109
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
110
|
+
"span",
|
|
111
|
+
{
|
|
112
|
+
className: cn(
|
|
113
|
+
"inline-flex shrink-0 items-center justify-center rounded-full font-heading font-semibold tracking-tight",
|
|
114
|
+
size === "sm" && "size-7 text-[11px]",
|
|
115
|
+
size === "md" && "size-9 text-xs",
|
|
116
|
+
size === "lg" && "size-12 text-sm",
|
|
117
|
+
tone === "brand" && "bg-primary/15 text-primary",
|
|
118
|
+
tone === "sage" && "bg-sage/20 text-forest",
|
|
119
|
+
tone === "muted" && "bg-muted text-muted-foreground",
|
|
120
|
+
className
|
|
121
|
+
),
|
|
122
|
+
children: initials
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/components/ui/button.tsx
|
|
128
|
+
var React = __toESM(require("react"), 1);
|
|
129
|
+
var import_react_slot = require("@radix-ui/react-slot");
|
|
130
|
+
var import_class_variance_authority = require("class-variance-authority");
|
|
131
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
132
|
+
var buttonVariants = (0, import_class_variance_authority.cva)(
|
|
133
|
+
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium cursor-pointer transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 disabled:cursor-not-allowed [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
|
|
134
|
+
{
|
|
135
|
+
variants: {
|
|
136
|
+
variant: {
|
|
137
|
+
default: "bg-primary text-primary-foreground shadow hover:bg-primary/90",
|
|
138
|
+
destructive: "bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
|
|
139
|
+
outline: "border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
|
|
140
|
+
secondary: "bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
|
|
141
|
+
ghost: "hover:bg-accent hover:text-accent-foreground",
|
|
142
|
+
link: "text-primary underline-offset-4 hover:underline"
|
|
143
|
+
},
|
|
144
|
+
size: {
|
|
145
|
+
default: "h-9 px-4 py-2",
|
|
146
|
+
sm: "h-8 rounded-md px-3 text-xs",
|
|
147
|
+
lg: "h-10 rounded-md px-8",
|
|
148
|
+
icon: "h-9 w-9"
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
defaultVariants: {
|
|
152
|
+
variant: "default",
|
|
153
|
+
size: "default"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
var Button = React.forwardRef(
|
|
158
|
+
({ className, variant, size, asChild = false, ...props }, ref) => {
|
|
159
|
+
const Comp = asChild ? import_react_slot.Slot : "button";
|
|
160
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Comp, { className: cn(buttonVariants({ variant, size, className })), ref, ...props });
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
Button.displayName = "Button";
|
|
164
|
+
|
|
165
|
+
// src/lib/widget-data.ts
|
|
166
|
+
var brand = {
|
|
167
|
+
name: "Kili",
|
|
168
|
+
greeting: "Hi there",
|
|
169
|
+
subline: "Ask us anything, or share your feedback. We usually reply in a few minutes.",
|
|
170
|
+
replyTime: "Typically replies in a few minutes"
|
|
171
|
+
};
|
|
172
|
+
var customer = {
|
|
173
|
+
name: "You",
|
|
174
|
+
initials: "YO"
|
|
175
|
+
};
|
|
176
|
+
var agents = [
|
|
177
|
+
{ id: "a-mara", name: "Mara Okafor", initials: "MO", role: "Customer Support" },
|
|
178
|
+
{ id: "a-devon", name: "Devon Reyes", initials: "DR", role: "Billing Specialist" },
|
|
179
|
+
{ id: "a-bot", name: "Kili Team", initials: "KT", role: "Support Team" }
|
|
180
|
+
];
|
|
181
|
+
var hoursAgo = (h) => new Date(Date.now() - h * 36e5).toISOString();
|
|
182
|
+
var daysAgo = (d, h = 0) => new Date(Date.now() - d * 864e5 - h * 36e5).toISOString();
|
|
183
|
+
var seedConversations = [
|
|
184
|
+
{
|
|
185
|
+
id: "c-1",
|
|
186
|
+
subject: "Trouble syncing my orders",
|
|
187
|
+
agentId: "a-mara",
|
|
188
|
+
messages: [
|
|
189
|
+
{
|
|
190
|
+
id: "m-1",
|
|
191
|
+
kind: "customer",
|
|
192
|
+
body: "Hey \u2014 my orders stopped syncing this morning. Nothing new has come through since about 8am.",
|
|
193
|
+
at: hoursAgo(5)
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
id: "m-2",
|
|
197
|
+
kind: "agent",
|
|
198
|
+
authorId: "a-mara",
|
|
199
|
+
body: "Thanks for flagging that! I can see the sync queue backed up on our side. I've re-queued your account now.",
|
|
200
|
+
at: hoursAgo(4.6)
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
id: "m-3",
|
|
204
|
+
kind: "customer",
|
|
205
|
+
body: "Great, I can see a few coming in now. How long until it catches up fully?",
|
|
206
|
+
at: hoursAgo(4.2)
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
id: "m-4",
|
|
210
|
+
kind: "agent",
|
|
211
|
+
authorId: "a-mara",
|
|
212
|
+
body: "Should be fully caught up within the hour. I'll keep an eye on it and let you know if anything stalls again.",
|
|
213
|
+
at: hoursAgo(4)
|
|
214
|
+
}
|
|
215
|
+
]
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
id: "c-2",
|
|
219
|
+
subject: "Invoice for August",
|
|
220
|
+
agentId: "a-devon",
|
|
221
|
+
messages: [
|
|
222
|
+
{
|
|
223
|
+
id: "m-5",
|
|
224
|
+
kind: "customer",
|
|
225
|
+
body: "Could you resend the August invoice? I can't find it in my inbox.",
|
|
226
|
+
at: daysAgo(3, 2)
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
id: "m-6",
|
|
230
|
+
kind: "agent",
|
|
231
|
+
authorId: "a-devon",
|
|
232
|
+
body: "Just sent it over to the billing email on file. Let me know if it doesn't land in the next few minutes.",
|
|
233
|
+
at: daysAgo(3, 1)
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
id: "m-7",
|
|
237
|
+
kind: "customer",
|
|
238
|
+
body: "Got it, thank you!",
|
|
239
|
+
at: daysAgo(3)
|
|
240
|
+
}
|
|
241
|
+
],
|
|
242
|
+
closed: true
|
|
243
|
+
}
|
|
244
|
+
];
|
|
245
|
+
var cannedReplies = [
|
|
246
|
+
"Thanks for reaching out! Let me take a look at that for you.",
|
|
247
|
+
"Good question \u2014 I'm checking with the team and will come right back to you.",
|
|
248
|
+
"I've made a note of this on your account. Anything else I can help with?",
|
|
249
|
+
"That should be sorted now. Give it a refresh and let me know how it looks."
|
|
250
|
+
];
|
|
251
|
+
var replyIndex = 0;
|
|
252
|
+
function nextCannedReply() {
|
|
253
|
+
const reply = cannedReplies[replyIndex % cannedReplies.length];
|
|
254
|
+
replyIndex += 1;
|
|
255
|
+
return reply;
|
|
256
|
+
}
|
|
257
|
+
function agentById(id) {
|
|
258
|
+
return agents.find((a) => a.id === id) ?? agents[2];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// src/lib/widget-format.ts
|
|
262
|
+
function clockTime(iso) {
|
|
263
|
+
if (!iso) return (/* @__PURE__ */ new Date()).toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
|
|
264
|
+
return new Date(iso).toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
|
|
265
|
+
}
|
|
266
|
+
function dayLabel(iso) {
|
|
267
|
+
if (!iso) return (/* @__PURE__ */ new Date()).toLocaleDateString([], { weekday: "long", month: "short", day: "numeric" });
|
|
268
|
+
const date = new Date(iso);
|
|
269
|
+
const today = /* @__PURE__ */ new Date();
|
|
270
|
+
const yesterday = new Date(Date.now() - 864e5);
|
|
271
|
+
const sameDay = (a, b) => a.toDateString() === b.toDateString();
|
|
272
|
+
if (sameDay(date, today)) return "Today";
|
|
273
|
+
if (sameDay(date, yesterday)) return "Yesterday";
|
|
274
|
+
return date.toLocaleDateString([], { weekday: "long", month: "short", day: "numeric" });
|
|
275
|
+
}
|
|
276
|
+
function relativeTime(iso) {
|
|
277
|
+
const diff = Date.now() - new Date(iso).getTime();
|
|
278
|
+
const minutes = Math.round(diff / 6e4);
|
|
279
|
+
if (minutes < 1) return "just now";
|
|
280
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
281
|
+
const hours = Math.round(minutes / 60);
|
|
282
|
+
if (hours < 24) return `${hours}h ago`;
|
|
283
|
+
const days = Math.round(hours / 24);
|
|
284
|
+
if (days < 7) return `${days}d ago`;
|
|
285
|
+
return new Date(iso).toLocaleDateString([], { month: "short", day: "numeric" });
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/components/widget/chat-home.tsx
|
|
289
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
290
|
+
function ChatHome({
|
|
291
|
+
chats,
|
|
292
|
+
onOpen,
|
|
293
|
+
onStartNew
|
|
294
|
+
}) {
|
|
295
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "flex min-h-0 flex-1 flex-col", children: [
|
|
296
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("header", { className: "bg-brand-gradient px-6 pt-10 pb-12 text-primary-foreground sm:px-10 sm:pt-14", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "mx-auto w-full max-w-2xl", children: [
|
|
297
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "font-heading text-sm font-semibold tracking-wide opacity-90", children: brand.name }),
|
|
298
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h1", { className: "mt-4 font-heading text-3xl font-semibold sm:text-4xl", children: brand.greeting }),
|
|
299
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "mt-2 max-w-md text-sm opacity-90 sm:text-base", children: brand.subline })
|
|
300
|
+
] }) }),
|
|
301
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "min-h-0 flex-1 overflow-y-auto scrollbar-thin px-4 pt-6 pb-10 sm:px-10", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "mx-auto w-full max-w-2xl space-y-3", children: [
|
|
302
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
303
|
+
"button",
|
|
304
|
+
{
|
|
305
|
+
type: "button",
|
|
306
|
+
onClick: onStartNew,
|
|
307
|
+
className: "flex w-full items-center gap-3 rounded-2xl border border-border bg-card p-4 text-left shadow-soft transition-colors hover:bg-accent/40",
|
|
308
|
+
children: [
|
|
309
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "grid size-10 shrink-0 place-items-center rounded-full bg-primary/15 text-primary", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_lucide_react.MessageSquarePlus, { className: "size-5" }) }),
|
|
310
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "min-w-0 flex-1", children: [
|
|
311
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "block font-heading text-sm font-semibold", children: "Start a conversation" }),
|
|
312
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "block text-xs text-muted-foreground", children: brand.replyTime })
|
|
313
|
+
] }),
|
|
314
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_lucide_react.ChevronRight, { className: "size-4 shrink-0 text-muted-foreground" })
|
|
315
|
+
]
|
|
316
|
+
}
|
|
317
|
+
),
|
|
318
|
+
chats.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
|
|
319
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("p", { className: "px-1 pt-4 text-xs font-medium tracking-wide text-muted-foreground uppercase", children: "Your conversations" }),
|
|
320
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("ul", { className: "space-y-2", children: chats.map((chat) => {
|
|
321
|
+
const last = chat.lastMessagePreview;
|
|
322
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
323
|
+
"button",
|
|
324
|
+
{
|
|
325
|
+
type: "button",
|
|
326
|
+
onClick: () => onOpen(chat),
|
|
327
|
+
className: "flex w-full items-start gap-3 rounded-2xl border border-border bg-card p-4 text-left transition-colors hover:bg-accent/40",
|
|
328
|
+
children: [
|
|
329
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(InitialsAvatar, { initials: getInitialsFromFullName(chat.assignedStaffFullName ?? ""), size: "md", tone: "sage" }),
|
|
330
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "min-w-0 flex-1", children: [
|
|
331
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "flex items-center justify-between gap-2", children: [
|
|
332
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "truncate font-heading text-sm font-semibold", children: chat.title ?? chat.summary ?? chat.lastMessagePreview?.substring(0, 30) ?? "Open conversation" }),
|
|
333
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shrink-0 text-[11px] text-muted-foreground", children: last ? relativeTime(chat.lastMessageSentAt ?? chat.updatedAt) : "" })
|
|
334
|
+
] }),
|
|
335
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mt-0.5 block truncate text-xs text-muted-foreground", children: last ? `${chat.lastMessageSenderAccountType === "staff" ? chat.assignedStaffFullName?.split(" ")[0] ?? "CS" : "You"}: ${last}` : "No messages yet" }),
|
|
336
|
+
chat.closedAt && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "mt-2 inline-flex rounded-full bg-muted px-2 py-0.5 text-[11px] text-muted-foreground", children: "Closed" })
|
|
337
|
+
] })
|
|
338
|
+
]
|
|
339
|
+
}
|
|
340
|
+
) }, chat.id);
|
|
341
|
+
}) })
|
|
342
|
+
] }),
|
|
343
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "pt-6 text-center", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Button, { variant: "ghost", size: "sm", className: "text-xs text-muted-foreground", onClick: () => {
|
|
344
|
+
}, children: "Can't find what you need? Message us" }) })
|
|
345
|
+
] }) })
|
|
346
|
+
] });
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// src/components/widget/chat-thread.tsx
|
|
350
|
+
var import_react2 = require("react");
|
|
351
|
+
var import_lucide_react2 = require("lucide-react");
|
|
352
|
+
|
|
353
|
+
// src/components/ui/textarea.tsx
|
|
354
|
+
var React2 = __toESM(require("react"), 1);
|
|
355
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
356
|
+
var Textarea = React2.forwardRef(
|
|
357
|
+
({ className, ...props }, ref) => {
|
|
358
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
359
|
+
"textarea",
|
|
360
|
+
{
|
|
361
|
+
className: cn(
|
|
362
|
+
"flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
|
363
|
+
className
|
|
364
|
+
),
|
|
365
|
+
ref,
|
|
366
|
+
...props
|
|
367
|
+
}
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
);
|
|
371
|
+
Textarea.displayName = "Textarea";
|
|
372
|
+
|
|
373
|
+
// src/lib/storage-keys.ts
|
|
374
|
+
var LocalStorageKeys = {
|
|
375
|
+
AccessTokenExp: "atexp",
|
|
376
|
+
JwtAccessToken: "at",
|
|
377
|
+
JwtRefreshToken: "rt",
|
|
378
|
+
User: "u"
|
|
379
|
+
};
|
|
380
|
+
var SessionStorageKeys = {
|
|
381
|
+
ActiveChat: "ac"
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
// src/context/state.tsx
|
|
385
|
+
var import_react = require("react");
|
|
386
|
+
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
387
|
+
var AppStateContext = (0, import_react.createContext)(null);
|
|
388
|
+
var AppStateProvider = ({ children }) => {
|
|
389
|
+
const [accessToken, setAccessToken] = (0, import_react.useState)(null);
|
|
390
|
+
const [chats, setChats] = (0, import_react.useState)([]);
|
|
391
|
+
const [messages, setMessages] = (0, import_react.useState)([]);
|
|
392
|
+
const [refreshToken, setRefreshToken] = (0, import_react.useState)(null);
|
|
393
|
+
const [user, setUser] = (0, import_react.useState)(null);
|
|
394
|
+
const appendMessage = (0, import_react.useCallback)((msg) => {
|
|
395
|
+
setMessages((prev) => {
|
|
396
|
+
const dedup = prev.filter((p) => String(p.id) !== String(msg.id));
|
|
397
|
+
return [...dedup, msg];
|
|
398
|
+
});
|
|
399
|
+
}, []);
|
|
400
|
+
const updateAccessToken = (0, import_react.useCallback)((at) => {
|
|
401
|
+
localStorage.setItem(LocalStorageKeys.JwtAccessToken, at);
|
|
402
|
+
setAccessToken(at);
|
|
403
|
+
}, []);
|
|
404
|
+
const updateChat = (0, import_react.useCallback)((chat) => {
|
|
405
|
+
setChats((prev) => prev.map((p) => String(p.id) === String(chat.id) ? chat : p));
|
|
406
|
+
}, []);
|
|
407
|
+
const updateChats = (0, import_react.useCallback)((chats2) => {
|
|
408
|
+
setChats(chats2);
|
|
409
|
+
}, []);
|
|
410
|
+
const updateMessages = (0, import_react.useCallback)((msgs) => {
|
|
411
|
+
setMessages(msgs);
|
|
412
|
+
}, []);
|
|
413
|
+
const updateRefreshToken = (0, import_react.useCallback)((rt) => {
|
|
414
|
+
localStorage.setItem(LocalStorageKeys.JwtRefreshToken, rt);
|
|
415
|
+
setRefreshToken(rt);
|
|
416
|
+
}, []);
|
|
417
|
+
const updateUser = (0, import_react.useCallback)((usr) => {
|
|
418
|
+
localStorage.setItem(LocalStorageKeys.User, JSON.stringify(usr));
|
|
419
|
+
setUser(usr);
|
|
420
|
+
}, []);
|
|
421
|
+
const appState = {
|
|
422
|
+
accessToken,
|
|
423
|
+
setAccessToken: updateAccessToken,
|
|
424
|
+
chats,
|
|
425
|
+
setChats: updateChats,
|
|
426
|
+
updateChat,
|
|
427
|
+
messages,
|
|
428
|
+
setMessages: updateMessages,
|
|
429
|
+
appendMessage,
|
|
430
|
+
refreshToken,
|
|
431
|
+
setRefreshToken: updateRefreshToken,
|
|
432
|
+
user,
|
|
433
|
+
setUser: updateUser
|
|
434
|
+
};
|
|
435
|
+
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(AppStateContext.Provider, { value: appState, children });
|
|
436
|
+
};
|
|
437
|
+
var useApp = () => {
|
|
438
|
+
const ctx = (0, import_react.useContext)(AppStateContext);
|
|
439
|
+
if (!ctx) throw new Error("useApp can only be called from inside AppStateProvider");
|
|
440
|
+
return ctx;
|
|
441
|
+
};
|
|
442
|
+
var state_default = AppStateProvider;
|
|
443
|
+
|
|
444
|
+
// src/lib/http.ts
|
|
445
|
+
var import_axios = __toESM(require("axios"), 1);
|
|
446
|
+
|
|
447
|
+
// src/lib/config.ts
|
|
448
|
+
var currentConfig = null;
|
|
449
|
+
function configureKili(config) {
|
|
450
|
+
currentConfig = config;
|
|
451
|
+
}
|
|
452
|
+
function getKiliConfig() {
|
|
453
|
+
if (!currentConfig) {
|
|
454
|
+
throw new Error(
|
|
455
|
+
"Kili SDK is not configured. Render <KiliChat config={...} /> or call configureKili() first."
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
return currentConfig;
|
|
459
|
+
}
|
|
460
|
+
function getSocketBaseUrl() {
|
|
461
|
+
const config = getKiliConfig();
|
|
462
|
+
return config.socketBaseUrl ?? config.apiBaseUrl;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// src/lib/http.ts
|
|
466
|
+
var http = import_axios.default.create();
|
|
467
|
+
http.interceptors.request.use((req) => {
|
|
468
|
+
const config = getKiliConfig();
|
|
469
|
+
req.baseURL = config.apiBaseUrl;
|
|
470
|
+
req.headers["Api-Key"] = config.apiKey;
|
|
471
|
+
req.headers["Api-Secret"] = config.apiSecret;
|
|
472
|
+
const token = localStorage.getItem(LocalStorageKeys.JwtAccessToken);
|
|
473
|
+
if (token) req.headers["Authorization"] = `Bearer ${token}`;
|
|
474
|
+
return req;
|
|
475
|
+
});
|
|
476
|
+
var http_default = http;
|
|
477
|
+
|
|
478
|
+
// src/services/chat.service.ts
|
|
479
|
+
var chatService = {
|
|
480
|
+
async getAllChats(userId) {
|
|
481
|
+
const res = await http_default.get(`/chats?customerId=${userId}`);
|
|
482
|
+
return res.data.data;
|
|
483
|
+
},
|
|
484
|
+
async getChatMessages(chatId) {
|
|
485
|
+
const res = await http_default.get(`/chats/${chatId}/messages`);
|
|
486
|
+
return res.data.data;
|
|
487
|
+
},
|
|
488
|
+
async startChat() {
|
|
489
|
+
const res = await http_default.post("/chats", { initiationChannel: "web_widget" });
|
|
490
|
+
return res.data.data;
|
|
491
|
+
},
|
|
492
|
+
async sendMessage() {
|
|
493
|
+
},
|
|
494
|
+
async markMessageAsRead() {
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
var chat_service_default = chatService;
|
|
498
|
+
|
|
499
|
+
// src/components/widget/chat-thread.tsx
|
|
500
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
501
|
+
function ChatThread({
|
|
502
|
+
chat: conversation,
|
|
503
|
+
draft,
|
|
504
|
+
typing,
|
|
505
|
+
onDraftChange,
|
|
506
|
+
onSend,
|
|
507
|
+
onBack,
|
|
508
|
+
onTextFieldBlur,
|
|
509
|
+
onTextfieldFocus
|
|
510
|
+
}) {
|
|
511
|
+
const bottomRef = (0, import_react2.useRef)(null);
|
|
512
|
+
const { messages, setMessages } = useApp();
|
|
513
|
+
const agentInitials = getInitialsFromFullName(conversation.assignedStaffFullName);
|
|
514
|
+
const customerInitials = getInitialsFromFullName(conversation.customerFullName);
|
|
515
|
+
const loadChatHistory = async () => {
|
|
516
|
+
const messages2 = await chat_service_default.getChatMessages(conversation.id);
|
|
517
|
+
setMessages(messages2.data);
|
|
518
|
+
};
|
|
519
|
+
(0, import_react2.useEffect)(() => {
|
|
520
|
+
bottomRef.current?.scrollIntoView({ behavior: "smooth", block: "end" });
|
|
521
|
+
}, [messages.length, typing]);
|
|
522
|
+
(0, import_react2.useEffect)(() => {
|
|
523
|
+
loadChatHistory();
|
|
524
|
+
}, []);
|
|
525
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex min-h-0 flex-1 flex-col bg-surface", children: [
|
|
526
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("header", { className: "border-b border-border bg-card px-4 py-3 sm:px-6", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "mx-auto flex w-full max-w-2xl items-center gap-3", children: [
|
|
527
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Button, { variant: "ghost", size: "icon", className: "size-8 shrink-0", onClick: onBack, "aria-label": "Back", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_lucide_react2.ArrowLeft, { className: "size-4" }) }),
|
|
528
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(InitialsAvatar, { initials: agentInitials, size: "md", tone: "sage" }),
|
|
529
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "min-w-0 flex-1", children: [
|
|
530
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "truncate font-heading text-sm font-semibold", children: conversation.assignedStaffFullName }),
|
|
531
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "truncate text-xs text-muted-foreground", children: typing ? `${conversation.assignedStaffFullName?.split(" ")[0]} is typing\u2026` : brand.replyTime })
|
|
532
|
+
] })
|
|
533
|
+
] }) }),
|
|
534
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "min-h-0 flex-1 overflow-y-auto scrollbar-thin px-4 py-6 sm:px-6", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "mx-auto w-full max-w-2xl space-y-4", children: [
|
|
535
|
+
!!messages.length && /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "rounded-2xl border border-dashed border-border bg-card/60 p-6 text-center", children: [
|
|
536
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(InitialsAvatar, { initials: getInitialsFromFullName(conversation.assignedStaffFullName), size: "lg", tone: "sage", className: "mx-auto" }),
|
|
537
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("p", { className: "mt-3 font-heading text-sm font-semibold", children: [
|
|
538
|
+
"Say hello to ",
|
|
539
|
+
conversation.assignedStaffFullName?.split(" ")[0]
|
|
540
|
+
] }),
|
|
541
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "mt-1 text-xs text-muted-foreground", children: "Describe what you need help with and we'll take it from there." })
|
|
542
|
+
] }),
|
|
543
|
+
messages.map((m, i) => {
|
|
544
|
+
const prev = messages[i - 1];
|
|
545
|
+
const showDay = !prev || dayLabel(prev.sentAt) !== dayLabel(m.sentAt);
|
|
546
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "space-y-4", children: [
|
|
547
|
+
showDay && /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex items-center gap-3", children: [
|
|
548
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "h-px flex-1 bg-border" }),
|
|
549
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "text-[11px] text-muted-foreground", children: dayLabel(m.sentAt) }),
|
|
550
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "h-px flex-1 bg-border" })
|
|
551
|
+
] }),
|
|
552
|
+
m.senderAccountType === "staff" ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex max-w-[80%] items-end gap-2", children: [
|
|
553
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(InitialsAvatar, { initials: getInitialsFromNames(m.senderFirstName, m.senderLastName), size: "sm", tone: "sage" }),
|
|
554
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "rounded-2xl rounded-bl-sm border border-border bg-card px-4 py-2.5", children: [
|
|
555
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "text-sm whitespace-pre-wrap", children: m.content }),
|
|
556
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("p", { className: "mt-1 text-[11px] text-muted-foreground", children: [
|
|
557
|
+
m.senderFirstName,
|
|
558
|
+
" \xB7 ",
|
|
559
|
+
clockTime(m.sentAt)
|
|
560
|
+
] })
|
|
561
|
+
] })
|
|
562
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "ml-auto flex max-w-[80%] flex-row-reverse items-end gap-2", children: [
|
|
563
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(InitialsAvatar, { initials: customerInitials, size: "sm", tone: "muted" }),
|
|
564
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "rounded-2xl rounded-br-sm bg-primary px-4 py-2.5 text-primary-foreground", children: [
|
|
565
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("p", { className: "text-sm whitespace-pre-wrap", children: m.content }),
|
|
566
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("p", { className: "mt-1 text-[11px] opacity-80", children: [
|
|
567
|
+
clockTime(m.sentAt),
|
|
568
|
+
" \xB7 Sent"
|
|
569
|
+
] })
|
|
570
|
+
] })
|
|
571
|
+
] })
|
|
572
|
+
] }, crypto.randomUUID());
|
|
573
|
+
}),
|
|
574
|
+
typing && /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex max-w-[80%] items-end gap-2", children: [
|
|
575
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(InitialsAvatar, { initials: agentInitials, size: "sm", tone: "sage" }),
|
|
576
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex items-center gap-1 rounded-2xl rounded-bl-sm border border-border bg-card px-4 py-3", children: [
|
|
577
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Dot, { delay: "0ms" }),
|
|
578
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Dot, { delay: "150ms" }),
|
|
579
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Dot, { delay: "300ms" })
|
|
580
|
+
] })
|
|
581
|
+
] }),
|
|
582
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { ref: bottomRef })
|
|
583
|
+
] }) }),
|
|
584
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { className: "border-t border-border bg-card px-4 py-3 sm:px-6", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "mx-auto w-full max-w-2xl", children: [
|
|
585
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "flex items-end gap-2", children: [
|
|
586
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
587
|
+
Textarea,
|
|
588
|
+
{
|
|
589
|
+
value: draft,
|
|
590
|
+
onChange: (e) => onDraftChange(e.target.value),
|
|
591
|
+
onKeyDown: (e) => {
|
|
592
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
593
|
+
e.preventDefault();
|
|
594
|
+
onSend();
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
onFocus: onTextfieldFocus,
|
|
598
|
+
onBlur: onTextFieldBlur,
|
|
599
|
+
placeholder: "Write a message\u2026",
|
|
600
|
+
"aria-label": "Write a message",
|
|
601
|
+
className: "max-h-40 min-h-[44px] flex-1 resize-none bg-background text-sm"
|
|
602
|
+
}
|
|
603
|
+
),
|
|
604
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Button, { size: "icon", className: "size-10 shrink-0", onClick: onSend, disabled: !draft.trim(), "aria-label": "Send", children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_lucide_react2.Send, { className: "size-4" }) })
|
|
605
|
+
] }),
|
|
606
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { className: "mt-2 flex items-center gap-2 text-muted-foreground", children: [
|
|
607
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_lucide_react2.Paperclip, { className: "size-4", "aria-hidden": true }),
|
|
608
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_lucide_react2.Smile, { className: "size-4", "aria-hidden": true }),
|
|
609
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { className: "ml-auto text-[11px]", children: "Enter to send \xB7 Shift + Enter for a new line" })
|
|
610
|
+
] })
|
|
611
|
+
] }) })
|
|
612
|
+
] });
|
|
613
|
+
}
|
|
614
|
+
function Dot({ delay }) {
|
|
615
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
616
|
+
"span",
|
|
617
|
+
{
|
|
618
|
+
className: "size-1.5 animate-bounce rounded-full bg-muted-foreground/60",
|
|
619
|
+
style: { animationDelay: delay }
|
|
620
|
+
}
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// src/lib/socket-events.ts
|
|
625
|
+
var SocketEvent = Object.freeze({
|
|
626
|
+
Connection: "connection",
|
|
627
|
+
Disconnect: "disconnect",
|
|
628
|
+
Error: "error",
|
|
629
|
+
AssignChat: "assign_chat",
|
|
630
|
+
ChatAction: "chat_action",
|
|
631
|
+
ChatAssigned: "chat_assigned",
|
|
632
|
+
ChatDeassigned: "chat_deassigned",
|
|
633
|
+
ChatStarted: "chat_started",
|
|
634
|
+
ChatUpdated: "chat_updated",
|
|
635
|
+
ChatWindowClosed: "chat_window_closed",
|
|
636
|
+
ChatWindowOpened: "chat_window_opened",
|
|
637
|
+
CloseChat: "close_chat",
|
|
638
|
+
InternalNote: "internal_note",
|
|
639
|
+
JoinAllUnclosedChatRooms: "join_all_unclosed_chat_rooms",
|
|
640
|
+
JoinChatRoom: "join_chat_room",
|
|
641
|
+
JoinedChatRoom: "joined_chat_room",
|
|
642
|
+
LeaveRoom: "leave_room",
|
|
643
|
+
MarkMsgAsRead: "mark_msg_as_read",
|
|
644
|
+
Message: "message",
|
|
645
|
+
MessageDelivered: "message_delivered",
|
|
646
|
+
NewMention: "new_mention",
|
|
647
|
+
PrioritizeChat: "prioritize_chat",
|
|
648
|
+
RefetchChats: "refetch_chats",
|
|
649
|
+
ReopenChat: "reopen_chat",
|
|
650
|
+
RefetchChatCounts: "refetch_chat_counts",
|
|
651
|
+
SnoozeChat: "snooze_chat",
|
|
652
|
+
StartChat: "start_chat"
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
// src/components/widget/chat-widget.tsx
|
|
656
|
+
var import_socket = require("socket.io-client");
|
|
657
|
+
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
658
|
+
function ChatWidget() {
|
|
659
|
+
const [draft, setDraft] = (0, import_react3.useState)("");
|
|
660
|
+
const [typing, setTyping] = (0, import_react3.useState)(false);
|
|
661
|
+
const timers = (0, import_react3.useRef)([]);
|
|
662
|
+
const state = useApp();
|
|
663
|
+
const [activeChat, setActiveChat] = (0, import_react3.useState)(null);
|
|
664
|
+
const socketRef = (0, import_react3.useRef)(null);
|
|
665
|
+
const appendMessage = (msg) => {
|
|
666
|
+
const chatJson = sessionStorage.getItem(SessionStorageKeys.ActiveChat);
|
|
667
|
+
if (!chatJson) return;
|
|
668
|
+
const chat = JSON.parse(chatJson);
|
|
669
|
+
if (msg.chatId == chat.id) {
|
|
670
|
+
state?.appendMessage(msg);
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
const handleChatAction = (action) => {
|
|
674
|
+
switch (action.action) {
|
|
675
|
+
case "typing.started":
|
|
676
|
+
setTyping(true);
|
|
677
|
+
break;
|
|
678
|
+
case "typing.stopped":
|
|
679
|
+
setTyping(false);
|
|
680
|
+
break;
|
|
681
|
+
case "uploading_file.started":
|
|
682
|
+
case "uploading_file.stopped":
|
|
683
|
+
default:
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
const handleChatStarted = async (chat) => {
|
|
688
|
+
const user = JSON.parse(localStorage.getItem(LocalStorageKeys.User));
|
|
689
|
+
if (chat.customerId != user.id) {
|
|
690
|
+
console.log("chat created by a different cstomer");
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
socketRef.current?.emit(SocketEvent.JoinChatRoom, chat.id);
|
|
694
|
+
const chats = await chat_service_default.getAllChats(user.id);
|
|
695
|
+
state?.setChats(chats.data);
|
|
696
|
+
persistActiveChat(chat);
|
|
697
|
+
};
|
|
698
|
+
const updateChat = (chat) => {
|
|
699
|
+
const acJson = sessionStorage.getItem(SessionStorageKeys.ActiveChat);
|
|
700
|
+
if (!acJson) return;
|
|
701
|
+
const ac = JSON.parse(acJson);
|
|
702
|
+
if (ac && ac.id === chat.id) {
|
|
703
|
+
setActiveChat(chat);
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
const persistActiveChat = (c) => {
|
|
707
|
+
setActiveChat(c);
|
|
708
|
+
if (!c) {
|
|
709
|
+
sessionStorage.removeItem(SessionStorageKeys.ActiveChat);
|
|
710
|
+
} else {
|
|
711
|
+
sessionStorage.setItem(SessionStorageKeys.ActiveChat, JSON.stringify(c));
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
(0, import_react3.useEffect)(() => {
|
|
715
|
+
const pending = timers.current;
|
|
716
|
+
return () => pending.forEach(clearTimeout);
|
|
717
|
+
}, []);
|
|
718
|
+
(0, import_react3.useEffect)(() => {
|
|
719
|
+
if (!state || !state.user || !state.accessToken) return;
|
|
720
|
+
if (socketRef.current) return;
|
|
721
|
+
const socket = (0, import_socket.io)(getSocketBaseUrl(), {
|
|
722
|
+
extraHeaders: {
|
|
723
|
+
"Api-Key": getKiliConfig().apiKey,
|
|
724
|
+
"Api-Secret": getKiliConfig().apiSecret,
|
|
725
|
+
"Authorization": `Bearer ${state.accessToken}`
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
socket.on("connect", () => {
|
|
729
|
+
console.log("socket connected");
|
|
730
|
+
socket.emit(SocketEvent.JoinAllUnclosedChatRooms);
|
|
731
|
+
socket.on(SocketEvent.ChatAction, handleChatAction);
|
|
732
|
+
socket.on(SocketEvent.ChatStarted, handleChatStarted);
|
|
733
|
+
socket.on(SocketEvent.ChatUpdated, updateChat);
|
|
734
|
+
socket.on(SocketEvent.MessageDelivered, appendMessage);
|
|
735
|
+
});
|
|
736
|
+
socket.on(SocketEvent.Disconnect, () => {
|
|
737
|
+
console.log("socket disconnected");
|
|
738
|
+
});
|
|
739
|
+
socket.on(SocketEvent.Error, (err) => {
|
|
740
|
+
console.log(socket.id, "has an error", err);
|
|
741
|
+
});
|
|
742
|
+
socketRef.current = socket;
|
|
743
|
+
}, [state]);
|
|
744
|
+
const sendMessage = (0, import_react3.useCallback)(() => {
|
|
745
|
+
if (!activeChat) return;
|
|
746
|
+
const user = state?.user ?? JSON.parse(localStorage.getItem(LocalStorageKeys.User));
|
|
747
|
+
const msg = {
|
|
748
|
+
chatId: Number(activeChat.id),
|
|
749
|
+
content: draft.trim(),
|
|
750
|
+
senderEmail: user.email,
|
|
751
|
+
senderId: user.id,
|
|
752
|
+
sentAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
753
|
+
senderAccountType: "CUSTOMER",
|
|
754
|
+
senderFirstName: user.firstName,
|
|
755
|
+
senderLastName: user.lastName,
|
|
756
|
+
type: "TEXT"
|
|
757
|
+
};
|
|
758
|
+
socketRef.current.emit(SocketEvent.ChatAction, {
|
|
759
|
+
action: "typing.stopped",
|
|
760
|
+
chatId: activeChat.id
|
|
761
|
+
});
|
|
762
|
+
socketRef.current.emit(SocketEvent.Message, msg);
|
|
763
|
+
setDraft("");
|
|
764
|
+
}, [activeChat, draft]);
|
|
765
|
+
const startChatOld = (0, import_react3.useCallback)(async () => {
|
|
766
|
+
const chat = await chat_service_default.startChat();
|
|
767
|
+
socketRef.current?.emit(SocketEvent.JoinChatRoom, { chatId: chat.id });
|
|
768
|
+
const user = JSON.parse(localStorage.getItem(LocalStorageKeys.User));
|
|
769
|
+
const chats = await chat_service_default.getAllChats(user.id);
|
|
770
|
+
state?.setChats(chats.data);
|
|
771
|
+
persistActiveChat(chat);
|
|
772
|
+
setDraft("");
|
|
773
|
+
}, []);
|
|
774
|
+
const startChat = (0, import_react3.useCallback)(() => {
|
|
775
|
+
socketRef?.current?.emit(SocketEvent.StartChat, { initiationChannel: "web_widget" });
|
|
776
|
+
setDraft("");
|
|
777
|
+
}, []);
|
|
778
|
+
return state && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "kili-root flex h-dvh w-full flex-col overflow-hidden bg-background", children: activeChat ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
779
|
+
ChatThread,
|
|
780
|
+
{
|
|
781
|
+
chat: activeChat,
|
|
782
|
+
draft,
|
|
783
|
+
typing,
|
|
784
|
+
onDraftChange: setDraft,
|
|
785
|
+
onSend: sendMessage,
|
|
786
|
+
onBack: () => {
|
|
787
|
+
persistActiveChat(null);
|
|
788
|
+
setTyping(false);
|
|
789
|
+
},
|
|
790
|
+
onTextFieldBlur: () => {
|
|
791
|
+
socketRef.current?.emit(SocketEvent.ChatAction, {
|
|
792
|
+
action: "typing.stopped",
|
|
793
|
+
chatId: activeChat.id
|
|
794
|
+
});
|
|
795
|
+
},
|
|
796
|
+
onTextfieldFocus: () => {
|
|
797
|
+
socketRef.current?.emit(SocketEvent.ChatAction, {
|
|
798
|
+
action: "typing.started",
|
|
799
|
+
chatId: activeChat.id
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
804
|
+
ChatHome,
|
|
805
|
+
{
|
|
806
|
+
chats: state.chats,
|
|
807
|
+
onOpen: (c) => {
|
|
808
|
+
persistActiveChat(c);
|
|
809
|
+
setDraft("");
|
|
810
|
+
},
|
|
811
|
+
onStartNew: startChat
|
|
812
|
+
}
|
|
813
|
+
) });
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// src/services/core.service.ts
|
|
817
|
+
var import_random_name = __toESM(require("@scaleway/random-name"), 1);
|
|
818
|
+
var coreService = {
|
|
819
|
+
async createCustomer() {
|
|
820
|
+
let user = localStorage.getItem(LocalStorageKeys.User);
|
|
821
|
+
if (user) return JSON.parse(user);
|
|
822
|
+
const [fname, lname] = (0, import_random_name.default)("", " ").split(" ");
|
|
823
|
+
const res = await http_default.post("/customers", {
|
|
824
|
+
city: "Denver",
|
|
825
|
+
country: "united states",
|
|
826
|
+
device: "iPhone 17 pro",
|
|
827
|
+
email: `${fname}.${lname}@kili.africa`,
|
|
828
|
+
firstName: fname,
|
|
829
|
+
id: crypto.randomUUID(),
|
|
830
|
+
lastName: lname,
|
|
831
|
+
phoneCountryCode: "1",
|
|
832
|
+
phoneNumberSuffix: "1234567",
|
|
833
|
+
state: "Colorado"
|
|
834
|
+
});
|
|
835
|
+
console.dir({ createdCustomer: res.data }, { depth: null });
|
|
836
|
+
localStorage.setItem(LocalStorageKeys.User, JSON.stringify(res.data.data));
|
|
837
|
+
return res.data.data;
|
|
838
|
+
},
|
|
839
|
+
async registerCustomer() {
|
|
840
|
+
const data = localStorage.getItem(LocalStorageKeys.User);
|
|
841
|
+
const user = JSON.parse(data);
|
|
842
|
+
const res = await http_default.put("/customers", {
|
|
843
|
+
id: user.userId,
|
|
844
|
+
email: user.email
|
|
845
|
+
});
|
|
846
|
+
localStorage.setItem(LocalStorageKeys.JwtAccessToken, res.data.data.accessToken);
|
|
847
|
+
localStorage.setItem(LocalStorageKeys.AccessTokenExp, res.data.data.expiresAt);
|
|
848
|
+
return res.data.data;
|
|
849
|
+
},
|
|
850
|
+
async setCustomerHmac() {
|
|
851
|
+
const data = localStorage.getItem(LocalStorageKeys.User);
|
|
852
|
+
const user = JSON.parse(data);
|
|
853
|
+
const hmac = crypto.randomUUID();
|
|
854
|
+
await http_default.patch(`/customers/${user.userId}/hmac`, { hmac });
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
var core_service_default = coreService;
|
|
858
|
+
|
|
859
|
+
// src/kili-chat.tsx
|
|
860
|
+
var import_jsx_runtime8 = require("react/jsx-runtime");
|
|
861
|
+
function KiliChat({ config }) {
|
|
862
|
+
configureKili(config);
|
|
863
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(state_default, { children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(KiliChatBootstrap, {}) });
|
|
864
|
+
}
|
|
865
|
+
function KiliChatBootstrap() {
|
|
866
|
+
const state = useApp();
|
|
867
|
+
const onMount = async () => {
|
|
868
|
+
const customer2 = await core_service_default.createCustomer();
|
|
869
|
+
await core_service_default.setCustomerHmac();
|
|
870
|
+
const auth = await core_service_default.registerCustomer();
|
|
871
|
+
state?.setUser(customer2);
|
|
872
|
+
state?.setAccessToken(auth.accessToken);
|
|
873
|
+
const chats = await chat_service_default.getAllChats(customer2.id);
|
|
874
|
+
state?.setChats(chats.data);
|
|
875
|
+
};
|
|
876
|
+
(0, import_react4.useEffect)(() => {
|
|
877
|
+
onMount();
|
|
878
|
+
}, []);
|
|
879
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ChatWidget, {});
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// src/context/socket.tsx
|
|
883
|
+
var import_socket2 = require("socket.io-client");
|
|
884
|
+
var import_react5 = __toESM(require("react"), 1);
|
|
885
|
+
var import_jsx_runtime9 = require("react/jsx-runtime");
|
|
886
|
+
var SocketContext = import_react5.default.createContext(null);
|
|
887
|
+
function SocketProvider({ children }) {
|
|
888
|
+
const [socket, setSocket] = import_react5.default.useState(null);
|
|
889
|
+
const state = (0, import_react5.useContext)(AppStateContext);
|
|
890
|
+
import_react5.default.useEffect(() => {
|
|
891
|
+
if (!state || !state.user || !state.accessToken) return;
|
|
892
|
+
if (socket) return;
|
|
893
|
+
const socketClient = (0, import_socket2.io)(getSocketBaseUrl(), {
|
|
894
|
+
extraHeaders: {
|
|
895
|
+
"Api-Key": getKiliConfig().apiKey,
|
|
896
|
+
"Api-Secret": getKiliConfig().apiSecret,
|
|
897
|
+
"Authorization": `Bearer ${state.accessToken}`
|
|
898
|
+
}
|
|
899
|
+
});
|
|
900
|
+
socketClient.on("connect", () => {
|
|
901
|
+
console.log(socketClient.id, "connected to the socket");
|
|
902
|
+
socketClient.emit(SocketEvent.JoinAllUnclosedChatRooms);
|
|
903
|
+
socketClient.on(SocketEvent.JoinedChatRoom, (roomId) => console.log("joined chat room", roomId));
|
|
904
|
+
});
|
|
905
|
+
socketClient.on(SocketEvent.Disconnect, () => {
|
|
906
|
+
console.log(socketClient.id, "has disconnected from the socket");
|
|
907
|
+
});
|
|
908
|
+
socketClient.on(SocketEvent.Error, (err) => {
|
|
909
|
+
console.log(socketClient.id, "has an error", err);
|
|
910
|
+
});
|
|
911
|
+
setSocket(socketClient);
|
|
912
|
+
}, [state]);
|
|
913
|
+
return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(SocketContext.Provider, { value: socket, children });
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
// src/hooks/use-mobile.tsx
|
|
917
|
+
var React4 = __toESM(require("react"), 1);
|
|
918
|
+
var MOBILE_BREAKPOINT = 768;
|
|
919
|
+
function useIsMobile() {
|
|
920
|
+
const [isMobile, setIsMobile] = React4.useState(void 0);
|
|
921
|
+
React4.useEffect(() => {
|
|
922
|
+
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
|
|
923
|
+
const onChange = () => {
|
|
924
|
+
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
|
|
925
|
+
};
|
|
926
|
+
mql.addEventListener("change", onChange);
|
|
927
|
+
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
|
|
928
|
+
return () => mql.removeEventListener("change", onChange);
|
|
929
|
+
}, []);
|
|
930
|
+
return !!isMobile;
|
|
931
|
+
}
|
|
932
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
933
|
+
0 && (module.exports = {
|
|
934
|
+
AppStateContext,
|
|
935
|
+
AppStateProvider,
|
|
936
|
+
Button,
|
|
937
|
+
ChatHome,
|
|
938
|
+
ChatThread,
|
|
939
|
+
ChatWidget,
|
|
940
|
+
InitialsAvatar,
|
|
941
|
+
KiliChat,
|
|
942
|
+
LocalStorageKeys,
|
|
943
|
+
SessionStorageKeys,
|
|
944
|
+
SocketContext,
|
|
945
|
+
SocketEvent,
|
|
946
|
+
SocketProvider,
|
|
947
|
+
Textarea,
|
|
948
|
+
agentById,
|
|
949
|
+
agents,
|
|
950
|
+
brand,
|
|
951
|
+
buttonVariants,
|
|
952
|
+
chatService,
|
|
953
|
+
clockTime,
|
|
954
|
+
cn,
|
|
955
|
+
configureKili,
|
|
956
|
+
coreService,
|
|
957
|
+
customer,
|
|
958
|
+
dayLabel,
|
|
959
|
+
getInitialsFromFullName,
|
|
960
|
+
getInitialsFromNames,
|
|
961
|
+
getKiliConfig,
|
|
962
|
+
http,
|
|
963
|
+
nextCannedReply,
|
|
964
|
+
relativeTime,
|
|
965
|
+
seedConversations,
|
|
966
|
+
useApp,
|
|
967
|
+
useIsMobile
|
|
968
|
+
});
|
|
969
|
+
//# sourceMappingURL=index.cjs.map
|