@armdesk/connect-sdk 1.0.0 → 1.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.
@@ -1,14 +1,40 @@
1
1
  /** The embed protocol shared by the SDK and the embed page (apps/web components/connect-embed). */
2
2
  export const PROTOCOL_SOURCE = "armdesk-connect";
3
- export const VIEW_PATTERN = /^(tickets|new|ticket:[A-Za-z0-9_-]{1,64})$/;
3
+ /**
4
+ * Views the embed understands. 1.0: `tickets`, `new`, `ticket:<id>`. 1.1 (messenger): `home`, `messages`, `help`,
5
+ * `chat:<conversationId>`, `article:<id>`. Ids are 1-64 URL-safe characters.
6
+ */
7
+ export const VIEW_PATTERN = /^(tickets|new|home|messages|help|(ticket|chat|article):[A-Za-z0-9_-]{1,64})$/;
4
8
  export const LOCALES = ["en", "es", "ar"];
5
9
  export const MAX_TOKEN_LENGTH = 8192;
10
+ /** Highest unread count the host accepts from the frame (anything above shows as "99+"). */
11
+ export const MAX_UNREAD = 9999;
12
+ /**
13
+ * Message types.
14
+ * host -> frame: `init {token, theme, features, host?}`, `token {token}`, `navigate {view}`, `theme {theme}`,
15
+ * `open {view?}` (the messenger panel became visible; optionally switch view).
16
+ * frame -> host: `ready`, `request-token`, `resize {height}`, `event {name, data}`, `error {code, message}`,
17
+ * `unread {count}` (launcher badge), `close` (the user closed the messenger from inside the frame).
18
+ */
19
+ export const HOST_MESSAGES = ["init", "token", "navigate", "theme", "open"];
20
+ export const FRAME_MESSAGES = ["ready", "request-token", "resize", "event", "error", "unread", "close"];
6
21
  export function isProtocolMessage(data) {
7
22
  if (typeof data !== "object" || data === null)
8
23
  return false;
9
24
  const d = data;
10
25
  return d.source === PROTOCOL_SOURCE && typeof d.type === "string";
11
26
  }
27
+ /** True for a view string the embed accepts. */
28
+ export function isView(v) {
29
+ return typeof v === "string" && VIEW_PATTERN.test(v);
30
+ }
31
+ /** A sane unread count from an untrusted payload: an integer in 0..MAX_UNREAD, else null. */
32
+ export function readUnread(payload) {
33
+ const n = Number(payload === null || payload === void 0 ? void 0 : payload.count);
34
+ if (!Number.isFinite(n) || n < 0)
35
+ return null;
36
+ return Math.min(MAX_UNREAD, Math.floor(n));
37
+ }
12
38
  /** The origin of a desk URL: https, or http for localhost only. Throws for anything else. */
13
39
  export function deskOrigin(baseUrl) {
14
40
  let url;
@@ -1,6 +1,6 @@
1
1
  import { type ReactNode } from "react";
2
2
  import { type ArmDeskClient } from "./client.js";
3
- import type { AuthOption, ClientMountOptions, ConnectHandle, ConnectTheme, CreatedTicket, CreateTicketInput, ListTicketsParams, MountOptions, TicketDetail, TicketList } from "./types.js";
3
+ import type { AuthOption, ClientMountOptions, ConnectHandle, ConnectTheme, ConnectView, CreatedTicket, CreateTicketInput, ListTicketsParams, MessengerController, MessengerEventMap, MessengerOptions, MountOptions, TicketDetail, TicketList } from "./types.js";
4
4
  export interface ArmDeskProviderProps {
5
5
  orgKey: string;
6
6
  auth: AuthOption;
@@ -58,4 +58,32 @@ export declare function ArmDeskNewTicket({ onCreated, className }: {
58
58
  onCreated?: (t: CreatedTicket) => void;
59
59
  className?: string;
60
60
  }): import("react/jsx-runtime").JSX.Element;
61
- export type { ConnectHandle, ConnectTheme, MountOptions, ArmDeskClient };
61
+ export type ArmDeskMessengerProps = Omit<MessengerOptions, "open" | "container" | "onEvent" | "onError"> & {
62
+ /** Controlled open state. Omit it to let the user (and useMessenger) drive the panel. */
63
+ open?: boolean;
64
+ onOpen?: (e: MessengerEventMap["open"]) => void;
65
+ onClose?: () => void;
66
+ onUnread?: (count: number) => void;
67
+ onEvent?: MessengerOptions["onEvent"];
68
+ onError?: MessengerOptions["onError"];
69
+ /** Bring your own client instead of the provider's. */
70
+ client?: ArmDeskClient;
71
+ };
72
+ /**
73
+ * `<ArmDeskMessenger position="bottom-right" />` inside <ArmDeskProvider>: the floating bubble and panel. It renders
74
+ * nothing in place (the bubble is fixed to the viewport) and is removed on unmount.
75
+ */
76
+ export declare function ArmDeskMessenger(props: ArmDeskMessengerProps): null;
77
+ export interface UseMessenger {
78
+ open: (view?: ConnectView) => void;
79
+ close: () => void;
80
+ toggle: () => void;
81
+ isOpen: boolean;
82
+ unread: number;
83
+ /** False until an <ArmDeskMessenger /> is mounted. */
84
+ ready: boolean;
85
+ controller: MessengerController | null;
86
+ }
87
+ /** Drive the page's <ArmDeskMessenger /> from anywhere: `const { open, unread } = useMessenger()`. */
88
+ export declare function useMessenger(): UseMessenger;
89
+ export type { ConnectHandle, ConnectTheme, MountOptions, ArmDeskClient, MessengerController, MessengerOptions };
package/dist/esm/react.js CHANGED
@@ -197,3 +197,115 @@ export function ArmDeskNewTicket({ onCreated, className }) {
197
197
  };
198
198
  return (_jsxs("form", { className: className, style: style, onSubmit: submit, children: [_jsx("h3", { children: "New ticket" }), _jsxs("label", { children: ["Subject ", _jsx("input", { value: subject, onChange: (e) => setSubject(e.target.value), required: true, minLength: 5, maxLength: 255, style: { width: "100%" } })] }), _jsxs("label", { children: ["Description ", _jsx("textarea", { value: description, onChange: (e) => setDescription(e.target.value), required: true, minLength: 10, maxLength: 20000, rows: 6, style: { width: "100%" } })] }), _jsx("button", { type: "submit", disabled: loading, children: "Submit ticket" }), error ? _jsx("p", { role: "alert", children: error.message }) : null] }));
199
199
  }
200
+ // ---- messenger --------------------------------------------------------------
201
+ /** The messenger <ArmDeskMessenger /> created most recently (one per page is the norm); useMessenger() reads it. */
202
+ let activeMessenger = null;
203
+ const messengerSubs = new Set();
204
+ const announce = () => messengerSubs.forEach((f) => f());
205
+ /**
206
+ * `<ArmDeskMessenger position="bottom-right" />` inside <ArmDeskProvider>: the floating bubble and panel. It renders
207
+ * nothing in place (the bubble is fixed to the viewport) and is removed on unmount.
208
+ */
209
+ export function ArmDeskMessenger(props) {
210
+ var _a, _b;
211
+ const provided = useContext(Ctx);
212
+ const client = (_a = props.client) !== null && _a !== void 0 ? _a : provided;
213
+ if (!client)
214
+ throw new Error("<ArmDeskMessenger> needs <ArmDeskProvider> (or a client prop)");
215
+ const latest = useRef(props);
216
+ latest.current = props;
217
+ const ctl = useRef(null);
218
+ const shape = JSON.stringify([props.position, props.offset, props.label, props.ariaLabel, props.iconUrl, props.lazy, props.zIndex, props.locale, props.features]);
219
+ useEffect(() => {
220
+ const p = latest.current;
221
+ const m = client.messenger({
222
+ position: p.position,
223
+ offset: p.offset,
224
+ label: p.label,
225
+ ariaLabel: p.ariaLabel,
226
+ iconUrl: p.iconUrl,
227
+ view: p.view,
228
+ lazy: p.lazy,
229
+ zIndex: p.zIndex,
230
+ theme: p.theme,
231
+ features: p.features,
232
+ locale: p.locale,
233
+ open: p.open,
234
+ onEvent: (e) => { var _a, _b; return (_b = (_a = latest.current).onEvent) === null || _b === void 0 ? void 0 : _b.call(_a, e); },
235
+ onError: (e) => { var _a, _b; return (_b = (_a = latest.current).onError) === null || _b === void 0 ? void 0 : _b.call(_a, e); },
236
+ });
237
+ const offs = [
238
+ m.on("open", (d) => {
239
+ var _a, _b;
240
+ (_b = (_a = latest.current).onOpen) === null || _b === void 0 ? void 0 : _b.call(_a, d);
241
+ announce();
242
+ }),
243
+ m.on("close", () => {
244
+ var _a, _b;
245
+ (_b = (_a = latest.current).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
246
+ announce();
247
+ }),
248
+ m.on("unread", (n) => {
249
+ var _a, _b;
250
+ (_b = (_a = latest.current).onUnread) === null || _b === void 0 ? void 0 : _b.call(_a, n);
251
+ announce();
252
+ }),
253
+ ];
254
+ ctl.current = m;
255
+ activeMessenger = m;
256
+ announce();
257
+ return () => {
258
+ offs.forEach((f) => f());
259
+ m.destroy();
260
+ ctl.current = null;
261
+ if (activeMessenger === m)
262
+ activeMessenger = null;
263
+ announce();
264
+ };
265
+ }, [client, shape]);
266
+ useEffect(() => {
267
+ if (props.open === undefined || !ctl.current)
268
+ return;
269
+ if (props.open)
270
+ ctl.current.open();
271
+ else
272
+ ctl.current.close();
273
+ }, [props.open]);
274
+ useEffect(() => {
275
+ var _a, _b;
276
+ if (props.view && ((_a = ctl.current) === null || _a === void 0 ? void 0 : _a.isOpen()))
277
+ ctl.current.open(props.view);
278
+ else if (props.view)
279
+ (_b = ctl.current) === null || _b === void 0 ? void 0 : _b.navigate(props.view);
280
+ }, [props.view]);
281
+ const themeKey = JSON.stringify((_b = props.theme) !== null && _b !== void 0 ? _b : null);
282
+ useEffect(() => {
283
+ var _a;
284
+ if (props.theme)
285
+ (_a = ctl.current) === null || _a === void 0 ? void 0 : _a.setTheme(props.theme);
286
+ // eslint-disable-next-line react-hooks/exhaustive-deps
287
+ }, [themeKey]);
288
+ return null;
289
+ }
290
+ /** Drive the page's <ArmDeskMessenger /> from anywhere: `const { open, unread } = useMessenger()`. */
291
+ export function useMessenger() {
292
+ var _a, _b;
293
+ const [, force] = useState(0);
294
+ useEffect(() => {
295
+ const f = () => force((n) => n + 1);
296
+ messengerSubs.add(f);
297
+ return () => {
298
+ messengerSubs.delete(f);
299
+ };
300
+ }, []);
301
+ const m = activeMessenger;
302
+ return {
303
+ open: useCallback((view) => activeMessenger === null || activeMessenger === void 0 ? void 0 : activeMessenger.open(view), []),
304
+ close: useCallback(() => activeMessenger === null || activeMessenger === void 0 ? void 0 : activeMessenger.close(), []),
305
+ toggle: useCallback(() => activeMessenger === null || activeMessenger === void 0 ? void 0 : activeMessenger.toggle(), []),
306
+ isOpen: (_a = m === null || m === void 0 ? void 0 : m.isOpen()) !== null && _a !== void 0 ? _a : false,
307
+ unread: (_b = m === null || m === void 0 ? void 0 : m.unread()) !== null && _b !== void 0 ? _b : 0,
308
+ ready: m !== null,
309
+ controller: m,
310
+ };
311
+ }
@@ -1,5 +1,5 @@
1
1
  // `@armdesk/connect-sdk/elements`: importing it registers <armdesk-tickets>, <armdesk-new-ticket>,
2
- // <armdesk-ticket ticket-id> and <armdesk-launcher>.
2
+ // <armdesk-ticket ticket-id>, <armdesk-messenger> and <armdesk-launcher> (the messenger, opening at `tickets`).
3
3
  import { defineElements } from "./elements.js";
4
4
  defineElements();
5
5
  export { defineElements, ELEMENT_TAGS } from "./elements.js";
@@ -24,10 +24,19 @@ export interface ConnectFeatures {
24
24
  orgTickets?: boolean;
25
25
  replies?: boolean;
26
26
  closeTicket?: boolean;
27
+ /** 1.1: live chat in the messenger. */
28
+ chat?: boolean;
29
+ /** 1.1: the Help tab (article search). */
30
+ helpCenter?: boolean;
31
+ /** 1.1: AI answers in Help, grounded in your docs and the shared knowledge base. */
32
+ aiAnswers?: boolean;
27
33
  }
28
34
  export type ConnectLocale = "en" | "es" | "ar";
29
- /** `tickets` (the list), `new` (the form) or `ticket:<id>` (one ticket). */
30
- export type ConnectView = "tickets" | "new" | `ticket:${string}`;
35
+ /**
36
+ * `tickets` (the list), `new` (the form), `ticket:<id>` (one ticket); 1.1 messenger views: `home`, `messages`,
37
+ * `help`, `chat:<conversationId>`, `article:<id>`.
38
+ */
39
+ export type ConnectView = "tickets" | "new" | "home" | "messages" | "help" | `ticket:${string}` | `chat:${string}` | `article:${string}`;
31
40
  export interface ConnectEvent {
32
41
  name: "ticket.created" | "ticket.replied" | "navigate" | (string & {});
33
42
  data?: unknown;
@@ -51,9 +60,19 @@ export interface MountOptions {
51
60
  height?: "auto" | number;
52
61
  onEvent?: (event: ConnectEvent) => void;
53
62
  onError?: (error: ConnectError) => void;
63
+ /** 1.1: the frame reports how many messages are unread (launcher badge). */
64
+ onUnread?: (count: number) => void;
65
+ /** 1.1: the user closed the messenger from inside the frame. */
66
+ onClose?: () => void;
67
+ /** 1.1: tells the frame it is hosted in a messenger panel (it then shows its own close control). */
68
+ host?: "messenger";
69
+ /** 1.1: iframe title (accessibility). Default "Support tickets". */
70
+ title?: string;
54
71
  }
55
72
  export interface ConnectHandle {
56
73
  navigate(view: ConnectView): void;
74
+ /** 1.1: the panel became visible; optionally switch view. Queued until the frame is ready. */
75
+ open(view?: ConnectView): void;
57
76
  setTheme(theme: ConnectTheme): void;
58
77
  /** Fetch a fresh token through getToken and hand it to the embed. */
59
78
  refreshToken(): Promise<void>;
@@ -295,4 +314,62 @@ export interface ClientMountOptions {
295
314
  height?: "auto" | number;
296
315
  onEvent?: (event: ConnectEvent) => void;
297
316
  onError?: (error: ConnectError) => void;
317
+ onUnread?: (count: number) => void;
318
+ onClose?: () => void;
319
+ host?: "messenger";
320
+ title?: string;
321
+ }
322
+ export type MessengerPosition = "bottom-right" | "bottom-left";
323
+ export interface MessengerOptions {
324
+ /** Corner of the bubble and panel. Default "bottom-right" ("right" / "left" are accepted too). */
325
+ position?: MessengerPosition | "right" | "left";
326
+ /** Distance from the viewport edges in px: one number, or { x, y }. Default 20. */
327
+ offset?: number | {
328
+ x?: number;
329
+ y?: number;
330
+ };
331
+ /** Text shown next to the icon (the bubble becomes a pill). Without it the bubble is round. */
332
+ label?: string;
333
+ /** Accessible name of the bubble and panel. Default "Support". */
334
+ ariaLabel?: string;
335
+ /** An https URL of a custom bubble icon (an image ~28 px). Default: a chat glyph. */
336
+ iconUrl?: string;
337
+ /** The view the panel opens at. Default "home". */
338
+ view?: ConnectView;
339
+ /** Open as soon as it is created. */
340
+ open?: boolean;
341
+ /** Load the iframe only on first open (no unread badge until then). Default false: preloads so the badge works. */
342
+ lazy?: boolean;
343
+ /** Overrides the organisation's theme; `primaryColor` colours the bubble. */
344
+ theme?: ConnectTheme;
345
+ features?: ConnectFeatures;
346
+ locale?: ConnectLocale;
347
+ /** CSS z-index of the bubble and panel. Default 2147483000. */
348
+ zIndex?: number;
349
+ /** Where to attach the messenger. Default document.body. */
350
+ container?: HTMLElement | ShadowRoot;
351
+ onEvent?: (event: ConnectEvent) => void;
352
+ onError?: (error: ConnectError) => void;
353
+ }
354
+ export type MessengerEventMap = {
355
+ unread: number;
356
+ open: {
357
+ view: ConnectView | null;
358
+ };
359
+ close: void;
360
+ };
361
+ export interface MessengerController {
362
+ /** Opens the panel (optionally at a view). */
363
+ open(view?: ConnectView): void;
364
+ close(): void;
365
+ toggle(): void;
366
+ /** Switch the panel's view without opening it. */
367
+ navigate(view: ConnectView): void;
368
+ setTheme(theme: ConnectTheme): void;
369
+ isOpen(): boolean;
370
+ /** The last unread count reported by the frame. */
371
+ unread(): number;
372
+ on<K extends keyof MessengerEventMap>(event: K, cb: (payload: MessengerEventMap[K]) => void): Unsubscribe;
373
+ /** Removes the bubble, the panel and every listener. Idempotent. */
374
+ destroy(): void;
298
375
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armdesk/connect-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "ARM Desk Connect: embed ITSM tickets in another product. Headless client, web components, React, and a server helper.",
5
5
  "keywords": [
6
6
  "armdesk",