@scalio-cloud/ui 0.6.2 → 0.8.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,336 @@
1
+ "use strict";
2
+ // Support desk in the apps (ADR 0022): tickets go to the central Scalio support desk in Compass, the app only
3
+ // relays them. The backend routes come from scalio-connect (`scalio_connect.support.web`); supportApi(api) maps
4
+ // them for an axios-like client. SupportDesk is the body of SupportDrawer when it gets a `client`:
5
+ // "Neue Anfrage" (category, subject, description, diagnostics) and "Meine Tickets" (list, detail, reply, rating).
6
+ // SupportConnection is the settings card "Support-Verbindung" (portal address + one-time code).
7
+ const React = require("react");
8
+ const h = React.createElement;
9
+ const { labelsFor } = require("./labels");
10
+
11
+ const CATEGORIES = ["question", "bug", "wish", "license", "billing"];
12
+ const DONE = ["resolved", "closed"];
13
+
14
+ function cx() {
15
+ return Array.prototype.slice.call(arguments).filter(Boolean).join(" ");
16
+ }
17
+
18
+ // api: axios instance with the app's base URL (/api). Every method resolves to the response body.
19
+ function supportApi(api) {
20
+ const data = (p) => p.then((r) => r.data);
21
+ const enc = encodeURIComponent;
22
+ return {
23
+ create: (ticket) => data(api.post("/support/tickets", ticket)),
24
+ list: () => data(api.get("/support/tickets")),
25
+ get: (id) => data(api.get(`/support/tickets/${enc(id)}`)),
26
+ reply: (id, body) => data(api.post(`/support/tickets/${enc(id)}/messages`, { body })),
27
+ rate: (id, score) => data(api.post(`/support/tickets/${enc(id)}/rating`, { score })),
28
+ connection: () => data(api.get("/support/connection")),
29
+ connect: (portal_url, code) => data(api.post("/support/connection", { portal_url, code })),
30
+ check: () => data(api.post("/support/connection/check")),
31
+ disconnect: () => data(api.delete("/support/connection")),
32
+ };
33
+ }
34
+
35
+ function errorText(err) {
36
+ const d = err && err.response && err.response.data && err.response.data.detail;
37
+ if (typeof d === "string") return d;
38
+ if (d && typeof d.message === "string") return d.message;
39
+ return (err && err.message) || String(err);
40
+ }
41
+
42
+ function fmtDate(value, lang) {
43
+ if (!value) return "";
44
+ const d = new Date(value);
45
+ if (isNaN(d.getTime())) return String(value);
46
+ return d.toLocaleString(lang === "en" ? "en-GB" : "de-CH", { dateStyle: "medium", timeStyle: "short" });
47
+ }
48
+
49
+ function statusText(L, s) {
50
+ return (L.supportStatus && L.supportStatus[s]) || s;
51
+ }
52
+
53
+ function NewTicket({ client, page, L, onSent }) {
54
+ const [category, setCategory] = React.useState("question");
55
+ const [subject, setSubject] = React.useState("");
56
+ const [description, setDescription] = React.useState("");
57
+ const [diagnostics, setDiagnostics] = React.useState(true);
58
+ const [busy, setBusy] = React.useState(false);
59
+ const [error, setError] = React.useState("");
60
+ const submit = async (e) => {
61
+ e.preventDefault();
62
+ setBusy(true);
63
+ setError("");
64
+ try {
65
+ const browser = diagnostics && typeof navigator !== "undefined" ? String(navigator.userAgent || "").slice(0, 300) : "";
66
+ const res = await client.create({ category, subject, description, url: page || "", diagnostics, browser });
67
+ setSubject("");
68
+ setDescription("");
69
+ setCategory("question");
70
+ onSent(res && res.number ? L.supportSentAs.replace("{n}", res.number) : L.supportQueued);
71
+ } catch (err) {
72
+ setError(errorText(err));
73
+ } finally {
74
+ setBusy(false);
75
+ }
76
+ };
77
+ return h(
78
+ "form",
79
+ { className: "scalio-support-form", onSubmit: submit },
80
+ h("label", { className: "scalio-field-label", htmlFor: "support-category" }, L.supportCategory),
81
+ h(
82
+ "select",
83
+ { id: "support-category", className: "scalio-input", value: category, onChange: (e) => setCategory(e.target.value), "data-testid": "ticket-category-select" },
84
+ CATEGORIES.map((c) => h("option", { key: c, value: c }, L.supportCategories[c]))
85
+ ),
86
+ h("label", { className: "scalio-field-label", htmlFor: "support-subject" }, L.subject),
87
+ h("input", {
88
+ id: "support-subject", className: "scalio-input", value: subject, required: true, minLength: 3, maxLength: 200,
89
+ onChange: (e) => setSubject(e.target.value), "data-testid": "ticket-subject-input",
90
+ }),
91
+ h("label", { className: "scalio-field-label", htmlFor: "support-description" }, L.description),
92
+ h("textarea", {
93
+ id: "support-description", className: "scalio-input scalio-textarea", rows: 5, value: description, required: true,
94
+ minLength: 3, maxLength: 5000, onChange: (e) => setDescription(e.target.value), "data-testid": "ticket-description-input",
95
+ }),
96
+ h(
97
+ "label",
98
+ { className: "scalio-support-check", "data-testid": "ticket-diagnostics-toggle" },
99
+ h("input", { type: "checkbox", checked: diagnostics, onChange: (e) => setDiagnostics(e.target.checked) }),
100
+ h("span", null, L.supportDiagnostics)
101
+ ),
102
+ error ? h("div", { className: "scalio-notice is-error", role: "alert", "data-testid": "support-error" }, error) : null,
103
+ h("button", { type: "submit", className: "scalio-btn-primary", disabled: busy, "data-testid": "ticket-submit-btn" }, busy ? L.sending : L.send)
104
+ );
105
+ }
106
+
107
+ function Waiting({ item, L }) {
108
+ return h(
109
+ "div",
110
+ { className: "scalio-support-item is-waiting" },
111
+ item.subject ? h("div", { className: "scalio-support-item-title" }, item.subject) : null,
112
+ h("div", { className: "scalio-support-meta" }, item.state === "failed" ? L.supportNotSent : L.supportWaiting)
113
+ );
114
+ }
115
+
116
+ function MyTickets({ client, L, lang, onOpen }) {
117
+ const [data, setData] = React.useState(null);
118
+ React.useEffect(() => {
119
+ let alive = true;
120
+ client.list().then((d) => alive && setData(d)).catch(() => alive && setData({ tickets: [], queued: [], reachable: false }));
121
+ return () => { alive = false; };
122
+ }, [client]);
123
+ if (!data) return h("div", { className: "scalio-support-meta" }, L.loading);
124
+ const tickets = data.tickets || [];
125
+ const queued = data.queued || [];
126
+ return h(
127
+ "div",
128
+ { className: "scalio-support-list", "data-testid": "support-my-tickets" },
129
+ data.connected === false ? h("div", { className: "scalio-support-meta" }, L.supportNotConnected) : null,
130
+ data.reachable === false ? h("div", { className: "scalio-notice is-warn" }, L.supportUnreachable) : null,
131
+ queued.map((q) => h(Waiting, { key: q.id, item: q, L })),
132
+ tickets.map((t) =>
133
+ h(
134
+ "button",
135
+ { key: t.id, type: "button", className: "scalio-support-item", onClick: () => onOpen(t.id), "data-testid": `support-ticket-${t.id}` },
136
+ h(
137
+ "div",
138
+ { className: "scalio-support-item-head" },
139
+ h("span", { className: "scalio-support-item-title" }, t.title),
140
+ t.number ? h("span", { className: "scalio-support-number" }, t.number) : null
141
+ ),
142
+ h("div", { className: "scalio-support-meta" }, `${statusText(L, t.status)} · ${fmtDate(t.last_activity_at || t.created_at, lang)}`)
143
+ )
144
+ ),
145
+ !tickets.length && !queued.length ? h("div", { className: "scalio-support-empty" }, L.supportNone) : null
146
+ );
147
+ }
148
+
149
+ function TicketDetail({ client, id, L, lang, onBack }) {
150
+ const [tk, setTk] = React.useState(null);
151
+ const [reply, setReply] = React.useState("");
152
+ const [busy, setBusy] = React.useState(false);
153
+ const [error, setError] = React.useState("");
154
+ const [note, setNote] = React.useState("");
155
+ const load = React.useCallback(() => client.get(id).then(setTk).catch((err) => { setError(errorText(err)); setTk(false); }), [client, id]);
156
+ React.useEffect(() => { load(); }, [load]);
157
+ const send = async (fn, ok) => {
158
+ setBusy(true);
159
+ setError("");
160
+ try {
161
+ await fn();
162
+ setReply("");
163
+ setNote(ok);
164
+ await load();
165
+ } catch (err) {
166
+ setError(errorText(err));
167
+ } finally {
168
+ setBusy(false);
169
+ }
170
+ };
171
+ const back = h("button", { type: "button", className: "scalio-support-back", onClick: onBack, "data-testid": "support-back" }, `‹ ${L.supportMine}`);
172
+ if (tk === null) return h("div", null, back, h("div", { className: "scalio-support-meta" }, L.loading));
173
+ if (tk === false) return h("div", null, back, h("div", { className: "scalio-notice is-error", role: "alert" }, error));
174
+ return h(
175
+ "div",
176
+ { className: "scalio-support-detail", "data-testid": "support-ticket-detail" },
177
+ back,
178
+ h("div", { className: "scalio-support-number" }, `${tk.number || ""} · ${statusText(L, tk.status)}`),
179
+ h("h4", { className: "scalio-support-item-title" }, tk.title),
180
+ tk.description ? h("p", { className: "scalio-support-msg" }, tk.description) : null,
181
+ (tk.messages || []).map((m) =>
182
+ h(
183
+ "div",
184
+ { key: m.id, className: cx("scalio-support-msg", m.from_support && "is-support") },
185
+ h("div", { className: "scalio-support-meta" }, `${m.from_support ? L.supportFromSupport : m.author_name || ""} · ${fmtDate(m.created_at, lang)}`),
186
+ h("div", null, m.body)
187
+ )
188
+ ),
189
+ (tk.queued || []).map((q) => h(Waiting, { key: q.id, item: q, L })),
190
+ note ? h("div", { className: "scalio-notice is-ok", "data-testid": "support-note" }, note) : null,
191
+ error ? h("div", { className: "scalio-notice is-error", role: "alert", "data-testid": "support-error" }, error) : null,
192
+ tk.status !== "closed"
193
+ ? h(
194
+ "div",
195
+ { className: "scalio-support-form" },
196
+ h("textarea", {
197
+ className: "scalio-input scalio-textarea", rows: 3, value: reply, placeholder: L.supportReplyPlaceholder,
198
+ "aria-label": L.supportReply, onChange: (e) => setReply(e.target.value), "data-testid": "ticket-reply-input",
199
+ }),
200
+ h(
201
+ "button",
202
+ { type: "button", className: "scalio-btn-primary", disabled: busy || !reply.trim(), "data-testid": "ticket-reply-btn",
203
+ onClick: () => send(() => client.reply(id, reply.trim()), L.supportReplySent) },
204
+ L.supportReply
205
+ )
206
+ )
207
+ : null,
208
+ DONE.includes(tk.status) && !tk.csat_score
209
+ ? h(
210
+ "div",
211
+ { className: "scalio-support-rating", "data-testid": "ticket-rating" },
212
+ h("div", { className: "scalio-support-meta" }, L.supportRate),
213
+ h(
214
+ "div",
215
+ { className: "scalio-support-stars" },
216
+ [1, 2, 3, 4, 5].map((n) =>
217
+ h("button", { key: n, type: "button", disabled: busy, "aria-label": String(n), "data-testid": `ticket-rate-${n}`,
218
+ onClick: () => send(() => client.rate(id, n), L.supportThanks) }, "★")
219
+ )
220
+ )
221
+ )
222
+ : null
223
+ );
224
+ }
225
+
226
+ function SupportDesk({ client, page, lang, labels }) {
227
+ const L = labelsFor(lang, labels);
228
+ const [tab, setTab] = React.useState("new");
229
+ const [ticket, setTicket] = React.useState(null);
230
+ const [note, setNote] = React.useState("");
231
+ const tabBtn = (key, label) =>
232
+ h("button", {
233
+ type: "button", role: "tab", "aria-selected": tab === key, className: cx("scalio-support-tab", tab === key && "is-active"),
234
+ onClick: () => { setTab(key); setTicket(null); setNote(""); }, "data-testid": `support-tab-${key}`,
235
+ }, label);
236
+ return h(
237
+ React.Fragment,
238
+ null,
239
+ h("div", { className: "scalio-support-tabs", role: "tablist" }, tabBtn("new", L.supportNew), tabBtn("mine", L.supportMine)),
240
+ note ? h("div", { className: "scalio-notice is-ok", "data-testid": "support-note" }, note) : null,
241
+ tab === "new" ? h(NewTicket, { client, page, L, onSent: (text) => { setNote(text); setTab("mine"); } }) : null,
242
+ tab === "mine" && !ticket ? h(MyTickets, { client, L, lang, onOpen: (id) => { setNote(""); setTicket(id); } }) : null,
243
+ tab === "mine" && ticket ? h(TicketDetail, { client, id: ticket, L, lang, onBack: () => setTicket(null) }) : null
244
+ );
245
+ }
246
+
247
+ const CONN_TONE = { active: "is-ok", pending: "is-warn", blocked: "is-error", unpaired: "is-neutral" };
248
+
249
+ // Settings card. client: supportApi(api). Shown to admins only; the backend answers 403 for others.
250
+ function SupportConnection({ client, lang, labels, onChange }) {
251
+ const L = labelsFor(lang, labels);
252
+ const [st, setSt] = React.useState(null);
253
+ const [portal, setPortal] = React.useState("");
254
+ const [code, setCode] = React.useState("");
255
+ const [busy, setBusy] = React.useState(false);
256
+ const [error, setError] = React.useState("");
257
+ const [note, setNote] = React.useState("");
258
+ React.useEffect(() => {
259
+ let alive = true;
260
+ client.connection().then((d) => alive && setSt(d)).catch((err) => alive && (setError(errorText(err)), setSt(false)));
261
+ return () => { alive = false; };
262
+ }, [client]);
263
+ const run = async (fn, ok) => {
264
+ setBusy(true);
265
+ setError("");
266
+ setNote("");
267
+ try {
268
+ const d = await fn();
269
+ setSt(d);
270
+ setCode("");
271
+ setNote(ok);
272
+ if (onChange) onChange(d);
273
+ } catch (err) {
274
+ setError(errorText(err));
275
+ } finally {
276
+ setBusy(false);
277
+ }
278
+ };
279
+ if (st === null) return h("div", { className: "scalio-support-meta" }, L.loading);
280
+ if (st === false) return h("div", { className: "scalio-notice is-error", role: "alert" }, error);
281
+ const connected = st.status !== "unpaired";
282
+ const outbox = st.outbox || {};
283
+ return h(
284
+ "div",
285
+ { className: "scalio-support-conn", "data-testid": "support-connection" },
286
+ h(
287
+ "div",
288
+ { className: "scalio-support-conn-head" },
289
+ h("span", { className: cx("scalio-badge", CONN_TONE[st.status] || "is-neutral"), "data-testid": "support-connection-status" },
290
+ (L.supportConnStatus && L.supportConnStatus[st.status]) || st.status),
291
+ st.status === "active" && st.detail ? h("span", null, st.detail) : null
292
+ ),
293
+ h("p", { className: "scalio-support-meta" }, connected ? L.supportConnHelpConnected : L.supportConnHelp),
294
+ st.status === "pending" ? h("div", { className: "scalio-notice is-warn" }, L.supportConnPending) : null,
295
+ st.status === "blocked" ? h("div", { className: "scalio-notice is-error" }, L.supportConnBlocked) : null,
296
+ connected && st.key_ok === false ? h("div", { className: "scalio-notice is-error" }, L.supportConnKeyLost) : null,
297
+ connected
298
+ ? h(
299
+ "dl",
300
+ { className: "scalio-support-conn-facts" },
301
+ h("dt", null, L.supportPortal), h("dd", null, st.portal_url),
302
+ h("dt", null, L.supportInstance), h("dd", null, st.instance_id),
303
+ st.paired_at ? h(React.Fragment, null, h("dt", null, L.supportSince), h("dd", null, fmtDate(st.paired_at, lang))) : null
304
+ )
305
+ : null,
306
+ outbox.pending || outbox.failed
307
+ ? h("div", { className: "scalio-support-meta", "data-testid": "support-outbox" },
308
+ L.supportOutbox.replace("{pending}", outbox.pending || 0).replace("{failed}", outbox.failed || 0))
309
+ : null,
310
+ note ? h("div", { className: "scalio-notice is-ok", "data-testid": "support-note" }, note) : null,
311
+ error ? h("div", { className: "scalio-notice is-error", role: "alert", "data-testid": "support-error" }, error) : null,
312
+ connected
313
+ ? h(
314
+ "div",
315
+ { className: "scalio-support-conn-actions" },
316
+ h("button", { type: "button", className: "scalio-btn", disabled: busy, "data-testid": "support-connection-check",
317
+ onClick: () => run(() => client.check(), L.supportChecked) }, L.supportCheck),
318
+ h("button", { type: "button", className: "scalio-btn scalio-btn-danger", disabled: busy, "data-testid": "support-connection-remove",
319
+ onClick: () => (typeof window === "undefined" || window.confirm(L.supportRemoveConfirm)) && run(() => client.disconnect(), L.supportRemoved) },
320
+ L.supportRemove)
321
+ )
322
+ : h(
323
+ "form",
324
+ { className: "scalio-support-form", onSubmit: (e) => { e.preventDefault(); run(() => client.connect(portal.trim(), code.trim()), L.supportConnected); } },
325
+ h("label", { className: "scalio-field-label", htmlFor: "support-portal" }, L.supportPortal),
326
+ h("input", { id: "support-portal", className: "scalio-input", placeholder: "https://portal.scaliocloud.com", value: portal, required: true,
327
+ onChange: (e) => setPortal(e.target.value), "data-testid": "support-portal-input" }),
328
+ h("label", { className: "scalio-field-label", htmlFor: "support-code" }, L.supportCode),
329
+ h("input", { id: "support-code", className: "scalio-input", placeholder: "spc_…", value: code, required: true,
330
+ onChange: (e) => setCode(e.target.value), "data-testid": "support-code-input" }),
331
+ h("button", { type: "submit", className: "scalio-btn-primary", disabled: busy, "data-testid": "support-connect-btn" }, L.supportConnect)
332
+ )
333
+ );
334
+ }
335
+
336
+ module.exports = { supportApi, SupportDesk, SupportConnection, SUPPORT_CATEGORIES: CATEGORIES };