@consilioweb/payload-support 0.6.1 → 0.6.2

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.
Files changed (70) hide show
  1. package/dist/views/BillingView/client.cjs +209 -0
  2. package/dist/views/BillingView/client.js +204 -0
  3. package/dist/views/BillingView/index.cjs +34 -0
  4. package/dist/views/BillingView/index.js +29 -0
  5. package/dist/views/ChatView/client.cjs +253 -0
  6. package/dist/views/ChatView/client.js +252 -0
  7. package/dist/views/ChatView/index.cjs +34 -0
  8. package/dist/views/ChatView/index.js +29 -0
  9. package/dist/views/CrmView/client.cjs +233 -0
  10. package/dist/views/CrmView/client.js +232 -0
  11. package/dist/views/CrmView/index.cjs +34 -0
  12. package/dist/views/CrmView/index.js +29 -0
  13. package/dist/views/EmailTrackingView/client.cjs +159 -0
  14. package/dist/views/EmailTrackingView/client.js +154 -0
  15. package/dist/views/EmailTrackingView/index.cjs +34 -0
  16. package/dist/views/EmailTrackingView/index.js +29 -0
  17. package/dist/views/ImportConversationView/client.cjs +205 -0
  18. package/dist/views/ImportConversationView/client.js +204 -0
  19. package/dist/views/ImportConversationView/index.cjs +34 -0
  20. package/dist/views/ImportConversationView/index.js +29 -0
  21. package/dist/views/LogsView/client.cjs +149 -0
  22. package/dist/views/LogsView/client.js +148 -0
  23. package/dist/views/LogsView/index.cjs +32 -0
  24. package/dist/views/LogsView/index.js +27 -0
  25. package/dist/views/NewTicketView/client.cjs +229 -0
  26. package/dist/views/NewTicketView/client.js +224 -0
  27. package/dist/views/NewTicketView/index.cjs +32 -0
  28. package/dist/views/NewTicketView/index.js +27 -0
  29. package/dist/views/PendingEmailsView/client.cjs +173 -0
  30. package/dist/views/PendingEmailsView/client.js +172 -0
  31. package/dist/views/PendingEmailsView/index.cjs +34 -0
  32. package/dist/views/PendingEmailsView/index.js +29 -0
  33. package/dist/views/SupportDashboardView/client.cjs +301 -0
  34. package/dist/views/SupportDashboardView/client.js +296 -0
  35. package/dist/views/SupportDashboardView/index.cjs +34 -0
  36. package/dist/views/SupportDashboardView/index.js +29 -0
  37. package/dist/views/TicketDetailView/client.cjs +850 -0
  38. package/dist/views/TicketDetailView/client.js +844 -0
  39. package/dist/views/TicketDetailView/index.cjs +34 -0
  40. package/dist/views/TicketDetailView/index.js +29 -0
  41. package/dist/views/TicketInboxView/client.cjs +299 -0
  42. package/dist/views/TicketInboxView/client.js +294 -0
  43. package/dist/views/TicketInboxView/index.cjs +32 -0
  44. package/dist/views/TicketInboxView/index.js +27 -0
  45. package/dist/{views.css → views/TicketingSettingsView/TicketingSettings.module.scss} +138 -66
  46. package/dist/views/TicketingSettingsView/client.cjs +733 -0
  47. package/dist/views/TicketingSettingsView/client.js +728 -0
  48. package/dist/views/TicketingSettingsView/index.cjs +34 -0
  49. package/dist/views/TicketingSettingsView/index.js +29 -0
  50. package/dist/views/TimeDashboardView/client.cjs +169 -0
  51. package/dist/views/TimeDashboardView/client.js +164 -0
  52. package/dist/views/TimeDashboardView/index.cjs +34 -0
  53. package/dist/views/TimeDashboardView/index.js +29 -0
  54. package/dist/views/shared/AdminViewHeader.cjs +68 -0
  55. package/dist/views/shared/AdminViewHeader.js +67 -0
  56. package/dist/views/shared/ErrorBoundary.cjs +55 -0
  57. package/dist/views/shared/ErrorBoundary.js +50 -0
  58. package/dist/views/shared/Skeleton.cjs +77 -0
  59. package/dist/views/shared/Skeleton.js +72 -0
  60. package/dist/views/shared/adminTokens.cjs +34 -0
  61. package/dist/views/shared/adminTokens.js +31 -0
  62. package/dist/views/shared/config.cjs +44 -0
  63. package/dist/views/shared/config.js +40 -0
  64. package/dist/views/shared/index.cjs +58 -0
  65. package/dist/views/shared/index.js +5 -0
  66. package/dist/views.cjs +77 -6175
  67. package/dist/views.d.cts +13 -30
  68. package/dist/views.d.ts +13 -30
  69. package/dist/views.js +13 -6165
  70. package/package.json +1 -1
@@ -0,0 +1,253 @@
1
+ 'use strict';
2
+
3
+ var jsxRuntime = require('react/jsx-runtime');
4
+ var react = require('react');
5
+ var useTranslation = require('../../components/TicketConversation/hooks/useTranslation');
6
+
7
+ const ChatViewClient = () => {
8
+ const { t } = useTranslation.useTranslation();
9
+ const [sessions, setSessions] = react.useState({ active: [], closed: [] });
10
+ const [selectedSession, setSelectedSession] = react.useState(null);
11
+ const [messages, setMessages] = react.useState([]);
12
+ const [input, setInput] = react.useState("");
13
+ const [sending, setSending] = react.useState(false);
14
+ const [showClosed, setShowClosed] = react.useState(false);
15
+ const [loading, setLoading] = react.useState(true);
16
+ const [cannedResponses, setCannedResponses] = react.useState([]);
17
+ const messagesEndRef = react.useRef(null);
18
+ const lastFetchRef = react.useRef(null);
19
+ const [sessionExpired, setSessionExpired] = react.useState(false);
20
+ const sessionsESRef = react.useRef(null);
21
+ const messagesESRef = react.useRef(null);
22
+ const fetchSessions = react.useCallback(async () => {
23
+ try {
24
+ const res = await fetch("/api/support/admin-chat");
25
+ if (res.status === 401 || res.status === 403) {
26
+ setSessionExpired(true);
27
+ return;
28
+ }
29
+ if (res.ok) {
30
+ const data = await res.json();
31
+ setSessions({ active: data.active || [], closed: data.closed || [] });
32
+ }
33
+ } catch {
34
+ }
35
+ setLoading(false);
36
+ }, []);
37
+ react.useEffect(() => {
38
+ if (sessionExpired) return;
39
+ fetchSessions();
40
+ if (typeof EventSource !== "undefined") {
41
+ const es = new EventSource("/api/support/admin-chat-stream");
42
+ sessionsESRef.current = es;
43
+ es.onmessage = (event) => {
44
+ try {
45
+ const parsed = JSON.parse(event.data);
46
+ if (parsed.type === "sessions" && parsed.data) {
47
+ setSessions({ active: parsed.data.active || [], closed: parsed.data.closed || [] });
48
+ setLoading(false);
49
+ }
50
+ } catch {
51
+ }
52
+ };
53
+ es.onerror = () => {
54
+ es.close();
55
+ sessionsESRef.current = null;
56
+ const iv2 = setInterval(fetchSessions, 5e3);
57
+ return () => clearInterval(iv2);
58
+ };
59
+ return () => {
60
+ es.close();
61
+ sessionsESRef.current = null;
62
+ };
63
+ }
64
+ const iv = setInterval(fetchSessions, 5e3);
65
+ return () => clearInterval(iv);
66
+ }, [fetchSessions, sessionExpired]);
67
+ react.useEffect(() => {
68
+ fetch("/api/canned-responses?sort=sortOrder&limit=50&depth=0", { credentials: "include" }).then((res) => res.ok ? res.json() : null).then((data) => {
69
+ if (data?.docs) setCannedResponses(data.docs);
70
+ }).catch(() => {
71
+ });
72
+ }, []);
73
+ react.useEffect(() => {
74
+ if (!selectedSession) return;
75
+ const fetchMessages = async () => {
76
+ try {
77
+ const after = lastFetchRef.current || "";
78
+ const url = `/api/support/admin-chat?session=${selectedSession}${after ? `&after=${after}` : ""}`;
79
+ const res = await fetch(url);
80
+ if (res.ok) {
81
+ const data = await res.json();
82
+ if (!lastFetchRef.current) {
83
+ setMessages(data.messages || []);
84
+ } else if (data.messages?.length > 0) {
85
+ setMessages((prev) => {
86
+ const ids = new Set(prev.map((m) => m.id));
87
+ const newMsgs = data.messages.filter((m) => !ids.has(m.id));
88
+ return newMsgs.length > 0 ? [...prev, ...newMsgs] : prev;
89
+ });
90
+ }
91
+ if (data.messages?.length > 0) {
92
+ lastFetchRef.current = data.messages[data.messages.length - 1].createdAt;
93
+ }
94
+ }
95
+ } catch {
96
+ }
97
+ };
98
+ lastFetchRef.current = null;
99
+ fetchMessages();
100
+ if (typeof EventSource !== "undefined") {
101
+ const es = new EventSource(`/api/support/admin-chat-stream?session=${selectedSession}`);
102
+ messagesESRef.current = es;
103
+ es.onmessage = (event) => {
104
+ try {
105
+ const parsed = JSON.parse(event.data);
106
+ if (parsed.type === "messages" && parsed.data?.length > 0) {
107
+ setMessages((prev) => {
108
+ const ids = new Set(prev.map((m) => m.id));
109
+ const newMsgs = parsed.data.filter((m) => !ids.has(m.id));
110
+ return newMsgs.length > 0 ? [...prev, ...newMsgs] : prev;
111
+ });
112
+ }
113
+ } catch {
114
+ }
115
+ };
116
+ es.onerror = () => {
117
+ es.close();
118
+ messagesESRef.current = null;
119
+ const iv2 = setInterval(fetchMessages, 3e3);
120
+ fetchMessages._fallbackIv = iv2;
121
+ };
122
+ return () => {
123
+ es.close();
124
+ messagesESRef.current = null;
125
+ if (fetchMessages._fallbackIv) clearInterval(fetchMessages._fallbackIv);
126
+ };
127
+ }
128
+ const iv = setInterval(fetchMessages, 3e3);
129
+ return () => clearInterval(iv);
130
+ }, [selectedSession]);
131
+ react.useEffect(() => {
132
+ messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
133
+ }, [messages]);
134
+ const sendMessage = async (e) => {
135
+ e.preventDefault();
136
+ if (!input.trim() || !selectedSession || sending) return;
137
+ setSending(true);
138
+ try {
139
+ const res = await fetch("/api/support/admin-chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "send", session: selectedSession, message: input.trim() }) });
140
+ if (res.ok) {
141
+ const data = await res.json();
142
+ setMessages((prev) => [...prev, data.message]);
143
+ lastFetchRef.current = data.message.createdAt;
144
+ setInput("");
145
+ }
146
+ } catch {
147
+ }
148
+ setSending(false);
149
+ };
150
+ const closeSession = async () => {
151
+ if (!selectedSession) return;
152
+ try {
153
+ await fetch("/api/support/admin-chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "close", session: selectedSession }) });
154
+ setSelectedSession(null);
155
+ fetchSessions();
156
+ } catch {
157
+ }
158
+ };
159
+ const getClientName = (client) => {
160
+ if (typeof client === "number") return `Client #${client}`;
161
+ const parts = [client.firstName, client.lastName].filter(Boolean);
162
+ if (parts.length > 0) return parts.join(" ");
163
+ return client.email || `Client #${client.id}`;
164
+ };
165
+ const displayedSessions = showClosed ? sessions.closed : sessions.active;
166
+ const S = {
167
+ page: { padding: "20px 30px", maxWidth: 1200, margin: "0 auto" },
168
+ container: { display: "grid", gridTemplateColumns: "320px 1fr", gap: 16, minHeight: "calc(100vh - 300px)" },
169
+ sidebar: { borderRight: "1px solid var(--theme-elevation-200)" },
170
+ sessionItem: { display: "block", width: "100%", padding: "10px 14px", border: "none", background: "none", cursor: "pointer", textAlign: "left", borderBottom: "1px solid var(--theme-elevation-100)", fontSize: 13 },
171
+ sessionActive: { background: "var(--theme-elevation-50)" },
172
+ chatPanel: { display: "flex", flexDirection: "column" },
173
+ messagesArea: { flex: 1, overflowY: "auto", padding: "12px 0" },
174
+ bubble: { maxWidth: "70%", padding: "8px 12px", borderRadius: 10, marginBottom: 8, fontSize: 14 },
175
+ bubbleAgent: { background: "#dbeafe", color: "#1e3a5f", marginLeft: "auto" },
176
+ bubbleClient: { background: "var(--theme-elevation-100)", color: "var(--theme-text)" },
177
+ bubbleSystem: { margin: "4px auto", padding: "4px 12px", fontSize: 11, color: "#6b7280", textAlign: "center" },
178
+ composer: { borderTop: "1px solid var(--theme-elevation-200)", padding: "8px 0" },
179
+ composerInput: { flex: 1, padding: "8px 12px", borderRadius: 8, border: "1px solid var(--theme-elevation-200)", fontSize: 13, background: "var(--theme-elevation-0)", color: "var(--theme-text)" },
180
+ sendBtn: { padding: "8px 16px", borderRadius: 8, background: "#2563eb", color: "#fff", border: "none", fontWeight: 600, cursor: "pointer", fontSize: 13 },
181
+ tabsRow: { display: "flex", gap: 4, padding: "8px 14px", borderBottom: "1px solid var(--theme-elevation-200)" },
182
+ tab: { padding: "4px 10px", borderRadius: 6, border: "none", background: "none", cursor: "pointer", fontSize: 12, color: "var(--theme-elevation-500)" },
183
+ tabActive: { background: "var(--theme-elevation-100)", fontWeight: 700, color: "var(--theme-text)" }
184
+ };
185
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: S.page, children: [
186
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { marginBottom: 16 }, children: [
187
+ /* @__PURE__ */ jsxRuntime.jsx("h1", { style: { fontSize: 22, fontWeight: 700, margin: 0, color: "var(--theme-text)" }, children: t("chat.title") }),
188
+ /* @__PURE__ */ jsxRuntime.jsx("p", { style: { color: "var(--theme-elevation-500)", fontSize: 13, margin: "4px 0 0" }, children: sessions.active.length !== 1 ? t("chat.sessionCountPlural", { count: String(sessions.active.length) }) : t("chat.sessionCount", { count: String(sessions.active.length) }) })
189
+ ] }),
190
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: S.container, children: [
191
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: S.sidebar, children: [
192
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: S.tabsRow, children: [
193
+ /* @__PURE__ */ jsxRuntime.jsxs("button", { onClick: () => setShowClosed(false), style: { ...S.tab, ...!showClosed ? S.tabActive : {} }, children: [
194
+ t("chat.tabs.active"),
195
+ " (",
196
+ sessions.active.length,
197
+ ")"
198
+ ] }),
199
+ /* @__PURE__ */ jsxRuntime.jsxs("button", { onClick: () => setShowClosed(true), style: { ...S.tab, ...showClosed ? S.tabActive : {} }, children: [
200
+ t("chat.tabs.closed"),
201
+ " (",
202
+ sessions.closed.length,
203
+ ")"
204
+ ] })
205
+ ] }),
206
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: loading ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: { padding: 20, textAlign: "center", color: "#94a3b8" }, children: t("common.loading") }) : displayedSessions.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: { padding: 20, textAlign: "center", color: "#94a3b8" }, children: showClosed ? t("chat.noSessionClosed") : t("chat.noSessionActive") }) : displayedSessions.map((s2) => /* @__PURE__ */ jsxRuntime.jsxs("button", { onClick: () => setSelectedSession(s2.session), style: { ...S.sessionItem, ...selectedSession === s2.session ? S.sessionActive : {} }, children: [
207
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", justifyContent: "space-between" }, children: [
208
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontWeight: 600 }, children: getClientName(s2.client) }),
209
+ s2.unreadCount > 0 && /* @__PURE__ */ jsxRuntime.jsx("span", { style: { padding: "1px 6px", borderRadius: 10, background: "#dc2626", color: "#fff", fontSize: 10, fontWeight: 700 }, children: s2.unreadCount })
210
+ ] }),
211
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 12, color: "var(--theme-elevation-500)", marginTop: 2, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, children: s2.lastMessage }),
212
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { fontSize: 11, color: "var(--theme-elevation-400)", marginTop: 2 }, children: [
213
+ new Date(s2.lastMessageAt).toLocaleString("fr-FR", { day: "2-digit", month: "2-digit", hour: "2-digit", minute: "2-digit" }),
214
+ " -- ",
215
+ s2.messageCount,
216
+ " ",
217
+ t("chat.msg")
218
+ ] })
219
+ ] }, s2.session)) })
220
+ ] }),
221
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: S.chatPanel, children: !selectedSession ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: { flex: 1, display: "flex", alignItems: "center", justifyContent: "center", color: "#94a3b8", fontSize: 14 }, children: t("chat.selectSession") }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
222
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", justifyContent: "space-between", alignItems: "center", padding: "8px 14px", borderBottom: "1px solid var(--theme-elevation-200)" }, children: [
223
+ /* @__PURE__ */ jsxRuntime.jsx("span", { style: { fontFamily: "monospace", fontSize: 12, color: "var(--theme-elevation-500)" }, children: selectedSession }),
224
+ /* @__PURE__ */ jsxRuntime.jsx("button", { onClick: closeSession, style: { padding: "4px 12px", borderRadius: 6, border: "1px solid #dc2626", background: "none", color: "#dc2626", fontSize: 12, cursor: "pointer" }, children: t("chat.closeChat") })
225
+ ] }),
226
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: S.messagesArea, children: [
227
+ messages.map((msg) => /* @__PURE__ */ jsxRuntime.jsx("div", { style: { display: "flex", flexDirection: msg.senderType === "agent" ? "row-reverse" : "row", padding: "2px 14px" }, children: msg.senderType === "system" ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: S.bubbleSystem, children: msg.message }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { ...S.bubble, ...msg.senderType === "agent" ? S.bubbleAgent : S.bubbleClient }, children: [
228
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 11, fontWeight: 600, marginBottom: 2 }, children: msg.senderType === "agent" ? t("chat.you") : t("chat.clientLabel") }),
229
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: msg.message }),
230
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 10, color: msg.senderType === "agent" ? "#1e40af" : "var(--theme-elevation-400)", marginTop: 2 }, children: new Date(msg.createdAt).toLocaleTimeString("fr-FR", { hour: "2-digit", minute: "2-digit" }) })
231
+ ] }) }, msg.id)),
232
+ /* @__PURE__ */ jsxRuntime.jsx("div", { ref: messagesEndRef })
233
+ ] }),
234
+ /* @__PURE__ */ jsxRuntime.jsxs("form", { onSubmit: sendMessage, style: S.composer, children: [
235
+ cannedResponses.length > 0 && /* @__PURE__ */ jsxRuntime.jsxs("select", { onChange: (e) => {
236
+ const cr = cannedResponses.find((c) => String(c.id) === e.target.value);
237
+ if (cr) setInput(cr.body);
238
+ e.target.value = "";
239
+ }, style: { padding: "4px 8px", borderRadius: 6, border: "1px solid var(--theme-elevation-200)", fontSize: 11, marginBottom: 6, color: "var(--theme-text)", background: "var(--theme-elevation-0)" }, children: [
240
+ /* @__PURE__ */ jsxRuntime.jsx("option", { value: "", children: t("chat.quickReply") }),
241
+ cannedResponses.map((cr) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: String(cr.id), children: cr.title }, cr.id))
242
+ ] }),
243
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 8 }, children: [
244
+ /* @__PURE__ */ jsxRuntime.jsx("input", { type: "text", value: input, onChange: (e) => setInput(e.target.value), placeholder: t("chat.inputPlaceholder"), maxLength: 2e3, style: S.composerInput, autoFocus: true }),
245
+ /* @__PURE__ */ jsxRuntime.jsx("button", { type: "submit", disabled: !input.trim() || sending, style: S.sendBtn, children: t("chat.sendButton") })
246
+ ] })
247
+ ] })
248
+ ] }) })
249
+ ] })
250
+ ] });
251
+ };
252
+
253
+ exports.ChatViewClient = ChatViewClient;
@@ -0,0 +1,252 @@
1
+ "use client";
2
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
3
+ import { useState, useRef, useCallback, useEffect } from 'react';
4
+ import { useTranslation } from '../../components/TicketConversation/hooks/useTranslation';
5
+
6
+ const ChatViewClient = () => {
7
+ const { t } = useTranslation();
8
+ const [sessions, setSessions] = useState({ active: [], closed: [] });
9
+ const [selectedSession, setSelectedSession] = useState(null);
10
+ const [messages, setMessages] = useState([]);
11
+ const [input, setInput] = useState("");
12
+ const [sending, setSending] = useState(false);
13
+ const [showClosed, setShowClosed] = useState(false);
14
+ const [loading, setLoading] = useState(true);
15
+ const [cannedResponses, setCannedResponses] = useState([]);
16
+ const messagesEndRef = useRef(null);
17
+ const lastFetchRef = useRef(null);
18
+ const [sessionExpired, setSessionExpired] = useState(false);
19
+ const sessionsESRef = useRef(null);
20
+ const messagesESRef = useRef(null);
21
+ const fetchSessions = useCallback(async () => {
22
+ try {
23
+ const res = await fetch("/api/support/admin-chat");
24
+ if (res.status === 401 || res.status === 403) {
25
+ setSessionExpired(true);
26
+ return;
27
+ }
28
+ if (res.ok) {
29
+ const data = await res.json();
30
+ setSessions({ active: data.active || [], closed: data.closed || [] });
31
+ }
32
+ } catch {
33
+ }
34
+ setLoading(false);
35
+ }, []);
36
+ useEffect(() => {
37
+ if (sessionExpired) return;
38
+ fetchSessions();
39
+ if (typeof EventSource !== "undefined") {
40
+ const es = new EventSource("/api/support/admin-chat-stream");
41
+ sessionsESRef.current = es;
42
+ es.onmessage = (event) => {
43
+ try {
44
+ const parsed = JSON.parse(event.data);
45
+ if (parsed.type === "sessions" && parsed.data) {
46
+ setSessions({ active: parsed.data.active || [], closed: parsed.data.closed || [] });
47
+ setLoading(false);
48
+ }
49
+ } catch {
50
+ }
51
+ };
52
+ es.onerror = () => {
53
+ es.close();
54
+ sessionsESRef.current = null;
55
+ const iv2 = setInterval(fetchSessions, 5e3);
56
+ return () => clearInterval(iv2);
57
+ };
58
+ return () => {
59
+ es.close();
60
+ sessionsESRef.current = null;
61
+ };
62
+ }
63
+ const iv = setInterval(fetchSessions, 5e3);
64
+ return () => clearInterval(iv);
65
+ }, [fetchSessions, sessionExpired]);
66
+ useEffect(() => {
67
+ fetch("/api/canned-responses?sort=sortOrder&limit=50&depth=0", { credentials: "include" }).then((res) => res.ok ? res.json() : null).then((data) => {
68
+ if (data?.docs) setCannedResponses(data.docs);
69
+ }).catch(() => {
70
+ });
71
+ }, []);
72
+ useEffect(() => {
73
+ if (!selectedSession) return;
74
+ const fetchMessages = async () => {
75
+ try {
76
+ const after = lastFetchRef.current || "";
77
+ const url = `/api/support/admin-chat?session=${selectedSession}${after ? `&after=${after}` : ""}`;
78
+ const res = await fetch(url);
79
+ if (res.ok) {
80
+ const data = await res.json();
81
+ if (!lastFetchRef.current) {
82
+ setMessages(data.messages || []);
83
+ } else if (data.messages?.length > 0) {
84
+ setMessages((prev) => {
85
+ const ids = new Set(prev.map((m) => m.id));
86
+ const newMsgs = data.messages.filter((m) => !ids.has(m.id));
87
+ return newMsgs.length > 0 ? [...prev, ...newMsgs] : prev;
88
+ });
89
+ }
90
+ if (data.messages?.length > 0) {
91
+ lastFetchRef.current = data.messages[data.messages.length - 1].createdAt;
92
+ }
93
+ }
94
+ } catch {
95
+ }
96
+ };
97
+ lastFetchRef.current = null;
98
+ fetchMessages();
99
+ if (typeof EventSource !== "undefined") {
100
+ const es = new EventSource(`/api/support/admin-chat-stream?session=${selectedSession}`);
101
+ messagesESRef.current = es;
102
+ es.onmessage = (event) => {
103
+ try {
104
+ const parsed = JSON.parse(event.data);
105
+ if (parsed.type === "messages" && parsed.data?.length > 0) {
106
+ setMessages((prev) => {
107
+ const ids = new Set(prev.map((m) => m.id));
108
+ const newMsgs = parsed.data.filter((m) => !ids.has(m.id));
109
+ return newMsgs.length > 0 ? [...prev, ...newMsgs] : prev;
110
+ });
111
+ }
112
+ } catch {
113
+ }
114
+ };
115
+ es.onerror = () => {
116
+ es.close();
117
+ messagesESRef.current = null;
118
+ const iv2 = setInterval(fetchMessages, 3e3);
119
+ fetchMessages._fallbackIv = iv2;
120
+ };
121
+ return () => {
122
+ es.close();
123
+ messagesESRef.current = null;
124
+ if (fetchMessages._fallbackIv) clearInterval(fetchMessages._fallbackIv);
125
+ };
126
+ }
127
+ const iv = setInterval(fetchMessages, 3e3);
128
+ return () => clearInterval(iv);
129
+ }, [selectedSession]);
130
+ useEffect(() => {
131
+ messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
132
+ }, [messages]);
133
+ const sendMessage = async (e) => {
134
+ e.preventDefault();
135
+ if (!input.trim() || !selectedSession || sending) return;
136
+ setSending(true);
137
+ try {
138
+ const res = await fetch("/api/support/admin-chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "send", session: selectedSession, message: input.trim() }) });
139
+ if (res.ok) {
140
+ const data = await res.json();
141
+ setMessages((prev) => [...prev, data.message]);
142
+ lastFetchRef.current = data.message.createdAt;
143
+ setInput("");
144
+ }
145
+ } catch {
146
+ }
147
+ setSending(false);
148
+ };
149
+ const closeSession = async () => {
150
+ if (!selectedSession) return;
151
+ try {
152
+ await fetch("/api/support/admin-chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "close", session: selectedSession }) });
153
+ setSelectedSession(null);
154
+ fetchSessions();
155
+ } catch {
156
+ }
157
+ };
158
+ const getClientName = (client) => {
159
+ if (typeof client === "number") return `Client #${client}`;
160
+ const parts = [client.firstName, client.lastName].filter(Boolean);
161
+ if (parts.length > 0) return parts.join(" ");
162
+ return client.email || `Client #${client.id}`;
163
+ };
164
+ const displayedSessions = showClosed ? sessions.closed : sessions.active;
165
+ const S = {
166
+ page: { padding: "20px 30px", maxWidth: 1200, margin: "0 auto" },
167
+ container: { display: "grid", gridTemplateColumns: "320px 1fr", gap: 16, minHeight: "calc(100vh - 300px)" },
168
+ sidebar: { borderRight: "1px solid var(--theme-elevation-200)" },
169
+ sessionItem: { display: "block", width: "100%", padding: "10px 14px", border: "none", background: "none", cursor: "pointer", textAlign: "left", borderBottom: "1px solid var(--theme-elevation-100)", fontSize: 13 },
170
+ sessionActive: { background: "var(--theme-elevation-50)" },
171
+ chatPanel: { display: "flex", flexDirection: "column" },
172
+ messagesArea: { flex: 1, overflowY: "auto", padding: "12px 0" },
173
+ bubble: { maxWidth: "70%", padding: "8px 12px", borderRadius: 10, marginBottom: 8, fontSize: 14 },
174
+ bubbleAgent: { background: "#dbeafe", color: "#1e3a5f", marginLeft: "auto" },
175
+ bubbleClient: { background: "var(--theme-elevation-100)", color: "var(--theme-text)" },
176
+ bubbleSystem: { margin: "4px auto", padding: "4px 12px", fontSize: 11, color: "#6b7280", textAlign: "center" },
177
+ composer: { borderTop: "1px solid var(--theme-elevation-200)", padding: "8px 0" },
178
+ composerInput: { flex: 1, padding: "8px 12px", borderRadius: 8, border: "1px solid var(--theme-elevation-200)", fontSize: 13, background: "var(--theme-elevation-0)", color: "var(--theme-text)" },
179
+ sendBtn: { padding: "8px 16px", borderRadius: 8, background: "#2563eb", color: "#fff", border: "none", fontWeight: 600, cursor: "pointer", fontSize: 13 },
180
+ tabsRow: { display: "flex", gap: 4, padding: "8px 14px", borderBottom: "1px solid var(--theme-elevation-200)" },
181
+ tab: { padding: "4px 10px", borderRadius: 6, border: "none", background: "none", cursor: "pointer", fontSize: 12, color: "var(--theme-elevation-500)" },
182
+ tabActive: { background: "var(--theme-elevation-100)", fontWeight: 700, color: "var(--theme-text)" }
183
+ };
184
+ return /* @__PURE__ */ jsxs("div", { style: S.page, children: [
185
+ /* @__PURE__ */ jsxs("div", { style: { marginBottom: 16 }, children: [
186
+ /* @__PURE__ */ jsx("h1", { style: { fontSize: 22, fontWeight: 700, margin: 0, color: "var(--theme-text)" }, children: t("chat.title") }),
187
+ /* @__PURE__ */ jsx("p", { style: { color: "var(--theme-elevation-500)", fontSize: 13, margin: "4px 0 0" }, children: sessions.active.length !== 1 ? t("chat.sessionCountPlural", { count: String(sessions.active.length) }) : t("chat.sessionCount", { count: String(sessions.active.length) }) })
188
+ ] }),
189
+ /* @__PURE__ */ jsxs("div", { style: S.container, children: [
190
+ /* @__PURE__ */ jsxs("div", { style: S.sidebar, children: [
191
+ /* @__PURE__ */ jsxs("div", { style: S.tabsRow, children: [
192
+ /* @__PURE__ */ jsxs("button", { onClick: () => setShowClosed(false), style: { ...S.tab, ...!showClosed ? S.tabActive : {} }, children: [
193
+ t("chat.tabs.active"),
194
+ " (",
195
+ sessions.active.length,
196
+ ")"
197
+ ] }),
198
+ /* @__PURE__ */ jsxs("button", { onClick: () => setShowClosed(true), style: { ...S.tab, ...showClosed ? S.tabActive : {} }, children: [
199
+ t("chat.tabs.closed"),
200
+ " (",
201
+ sessions.closed.length,
202
+ ")"
203
+ ] })
204
+ ] }),
205
+ /* @__PURE__ */ jsx("div", { children: loading ? /* @__PURE__ */ jsx("div", { style: { padding: 20, textAlign: "center", color: "#94a3b8" }, children: t("common.loading") }) : displayedSessions.length === 0 ? /* @__PURE__ */ jsx("div", { style: { padding: 20, textAlign: "center", color: "#94a3b8" }, children: showClosed ? t("chat.noSessionClosed") : t("chat.noSessionActive") }) : displayedSessions.map((s2) => /* @__PURE__ */ jsxs("button", { onClick: () => setSelectedSession(s2.session), style: { ...S.sessionItem, ...selectedSession === s2.session ? S.sessionActive : {} }, children: [
206
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", justifyContent: "space-between" }, children: [
207
+ /* @__PURE__ */ jsx("span", { style: { fontWeight: 600 }, children: getClientName(s2.client) }),
208
+ s2.unreadCount > 0 && /* @__PURE__ */ jsx("span", { style: { padding: "1px 6px", borderRadius: 10, background: "#dc2626", color: "#fff", fontSize: 10, fontWeight: 700 }, children: s2.unreadCount })
209
+ ] }),
210
+ /* @__PURE__ */ jsx("div", { style: { fontSize: 12, color: "var(--theme-elevation-500)", marginTop: 2, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, children: s2.lastMessage }),
211
+ /* @__PURE__ */ jsxs("div", { style: { fontSize: 11, color: "var(--theme-elevation-400)", marginTop: 2 }, children: [
212
+ new Date(s2.lastMessageAt).toLocaleString("fr-FR", { day: "2-digit", month: "2-digit", hour: "2-digit", minute: "2-digit" }),
213
+ " -- ",
214
+ s2.messageCount,
215
+ " ",
216
+ t("chat.msg")
217
+ ] })
218
+ ] }, s2.session)) })
219
+ ] }),
220
+ /* @__PURE__ */ jsx("div", { style: S.chatPanel, children: !selectedSession ? /* @__PURE__ */ jsx("div", { style: { flex: 1, display: "flex", alignItems: "center", justifyContent: "center", color: "#94a3b8", fontSize: 14 }, children: t("chat.selectSession") }) : /* @__PURE__ */ jsxs(Fragment, { children: [
221
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", justifyContent: "space-between", alignItems: "center", padding: "8px 14px", borderBottom: "1px solid var(--theme-elevation-200)" }, children: [
222
+ /* @__PURE__ */ jsx("span", { style: { fontFamily: "monospace", fontSize: 12, color: "var(--theme-elevation-500)" }, children: selectedSession }),
223
+ /* @__PURE__ */ jsx("button", { onClick: closeSession, style: { padding: "4px 12px", borderRadius: 6, border: "1px solid #dc2626", background: "none", color: "#dc2626", fontSize: 12, cursor: "pointer" }, children: t("chat.closeChat") })
224
+ ] }),
225
+ /* @__PURE__ */ jsxs("div", { style: S.messagesArea, children: [
226
+ messages.map((msg) => /* @__PURE__ */ jsx("div", { style: { display: "flex", flexDirection: msg.senderType === "agent" ? "row-reverse" : "row", padding: "2px 14px" }, children: msg.senderType === "system" ? /* @__PURE__ */ jsx("div", { style: S.bubbleSystem, children: msg.message }) : /* @__PURE__ */ jsxs("div", { style: { ...S.bubble, ...msg.senderType === "agent" ? S.bubbleAgent : S.bubbleClient }, children: [
227
+ /* @__PURE__ */ jsx("div", { style: { fontSize: 11, fontWeight: 600, marginBottom: 2 }, children: msg.senderType === "agent" ? t("chat.you") : t("chat.clientLabel") }),
228
+ /* @__PURE__ */ jsx("div", { children: msg.message }),
229
+ /* @__PURE__ */ jsx("div", { style: { fontSize: 10, color: msg.senderType === "agent" ? "#1e40af" : "var(--theme-elevation-400)", marginTop: 2 }, children: new Date(msg.createdAt).toLocaleTimeString("fr-FR", { hour: "2-digit", minute: "2-digit" }) })
230
+ ] }) }, msg.id)),
231
+ /* @__PURE__ */ jsx("div", { ref: messagesEndRef })
232
+ ] }),
233
+ /* @__PURE__ */ jsxs("form", { onSubmit: sendMessage, style: S.composer, children: [
234
+ cannedResponses.length > 0 && /* @__PURE__ */ jsxs("select", { onChange: (e) => {
235
+ const cr = cannedResponses.find((c) => String(c.id) === e.target.value);
236
+ if (cr) setInput(cr.body);
237
+ e.target.value = "";
238
+ }, style: { padding: "4px 8px", borderRadius: 6, border: "1px solid var(--theme-elevation-200)", fontSize: 11, marginBottom: 6, color: "var(--theme-text)", background: "var(--theme-elevation-0)" }, children: [
239
+ /* @__PURE__ */ jsx("option", { value: "", children: t("chat.quickReply") }),
240
+ cannedResponses.map((cr) => /* @__PURE__ */ jsx("option", { value: String(cr.id), children: cr.title }, cr.id))
241
+ ] }),
242
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 8 }, children: [
243
+ /* @__PURE__ */ jsx("input", { type: "text", value: input, onChange: (e) => setInput(e.target.value), placeholder: t("chat.inputPlaceholder"), maxLength: 2e3, style: S.composerInput, autoFocus: true }),
244
+ /* @__PURE__ */ jsx("button", { type: "submit", disabled: !input.trim() || sending, style: S.sendBtn, children: t("chat.sendButton") })
245
+ ] })
246
+ ] })
247
+ ] }) })
248
+ ] })
249
+ ] });
250
+ };
251
+
252
+ export { ChatViewClient };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+ var templates = require('@payloadcms/next/templates');
7
+ var navigation = require('next/navigation');
8
+ var ErrorBoundary = require('../shared/ErrorBoundary');
9
+ var client = require('./client');
10
+
11
+ const ChatView = ({ initPageResult }) => {
12
+ const { req, visibleEntities } = initPageResult;
13
+ if (!req.user) {
14
+ navigation.redirect("/admin/login");
15
+ }
16
+ return /* @__PURE__ */ jsxRuntime.jsx(
17
+ templates.DefaultTemplate,
18
+ {
19
+ i18n: req.i18n,
20
+ locale: initPageResult.locale,
21
+ params: {},
22
+ payload: req.payload,
23
+ permissions: initPageResult.permissions,
24
+ searchParams: {},
25
+ user: req.user,
26
+ visibleEntities,
27
+ children: /* @__PURE__ */ jsxRuntime.jsx(ErrorBoundary.AdminErrorBoundary, { viewName: "ChatView", children: /* @__PURE__ */ jsxRuntime.jsx(client.ChatViewClient, {}) })
28
+ }
29
+ );
30
+ };
31
+ var ChatView_default = ChatView;
32
+
33
+ exports.ChatView = ChatView;
34
+ exports.default = ChatView_default;
@@ -0,0 +1,29 @@
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import { DefaultTemplate } from '@payloadcms/next/templates';
3
+ import { redirect } from 'next/navigation';
4
+ import { AdminErrorBoundary } from '../shared/ErrorBoundary';
5
+ import { ChatViewClient } from './client';
6
+
7
+ const ChatView = ({ initPageResult }) => {
8
+ const { req, visibleEntities } = initPageResult;
9
+ if (!req.user) {
10
+ redirect("/admin/login");
11
+ }
12
+ return /* @__PURE__ */ jsx(
13
+ DefaultTemplate,
14
+ {
15
+ i18n: req.i18n,
16
+ locale: initPageResult.locale,
17
+ params: {},
18
+ payload: req.payload,
19
+ permissions: initPageResult.permissions,
20
+ searchParams: {},
21
+ user: req.user,
22
+ visibleEntities,
23
+ children: /* @__PURE__ */ jsx(AdminErrorBoundary, { viewName: "ChatView", children: /* @__PURE__ */ jsx(ChatViewClient, {}) })
24
+ }
25
+ );
26
+ };
27
+ var ChatView_default = ChatView;
28
+
29
+ export { ChatView, ChatView_default as default };