@dashx/react 0.4.0 → 0.4.1
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/CHANGELOG.md +15 -0
- package/dist/cjs/hooks/use-in-app-chat-notifications.d.ts +1 -1
- package/dist/cjs/hooks/use-in-app-chat-notifications.js +1 -1
- package/dist/cjs/hooks/use-in-app-chat-notifications.js.map +2 -2
- package/dist/esm/hooks/use-in-app-chat-notifications.d.ts +1 -1
- package/dist/esm/hooks/use-in-app-chat-notifications.js +1 -1
- package/dist/esm/hooks/use-in-app-chat-notifications.js.map +2 -2
- package/package.json +1 -1
- package/src/hooks/use-in-app-chat-notifications.ts +5 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `@dashx/react` are documented in this file. Format loosely follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), versions follow [SemVer](https://semver.org/).
|
|
4
4
|
|
|
5
|
+
## [0.4.1] — 2026-08-05
|
|
6
|
+
|
|
7
|
+
### Changed
|
|
8
|
+
|
|
9
|
+
- **`useInAppChatNotifications`'s `onActivate` now receives the message that raised the notification.**
|
|
10
|
+
|
|
11
|
+
```diff
|
|
12
|
+
- onActivate?: () => void
|
|
13
|
+
+ onActivate?: (message: InAppChatMessageData) => void
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Notifications are tagged per conversation, so a surface watching more than one thread could raise several at once — but the old zero-argument callback gave the handler no way to tell them apart, and it closed over whichever message arrived last. Clicking an older notification opened the wrong conversation.
|
|
17
|
+
|
|
18
|
+
**Backward compatible.** A `() => void` handler is still assignable and still behaves as before; take the parameter only if you need to route by `message.conversationId`. `InAppChatMessageData` is imported from `@dashx/browser`, same as the existing `renderBody` option on this hook.
|
|
19
|
+
|
|
5
20
|
## [0.4.0] — 2026-07-29
|
|
6
21
|
|
|
7
22
|
### Changed
|
|
@@ -9,7 +9,7 @@ type UseInAppChatNotificationsProps = {
|
|
|
9
9
|
isOpen: boolean;
|
|
10
10
|
enabled?: boolean;
|
|
11
11
|
options?: InAppChatNotificationOptions;
|
|
12
|
-
onActivate?: () => void;
|
|
12
|
+
onActivate?: (message: InAppChatMessageData) => void;
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
15
|
* Foreground browser notifications for incoming In-App Chat messages. Returns a
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var y=Object.create;var p=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty;var g=(t,e)=>{for(var r in e)p(t,r,{get:e[r],enumerable:!0})},h=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of O(e))!b.call(t,n)&&n!==r&&p(t,n,{get:()=>e[n],enumerable:!(s=N(e,n))||s.enumerable});return t};var R=(t,e,r)=>(r=t!=null?y(P(t)):{},h(e||!t||!t.__esModule?p(r,"default",{value:t,enumerable:!0}):r,t)),q=t=>h(p({},"__esModule",{value:!0}),t);var M={};g(M,{default:()=>D});module.exports=q(M);var o=require("react"),l=R(require("./use-dashx-provider.js"));const v=t=>typeof t=="string"?t:t?.text??t?.body??"",x=({isOpen:t,enabled:e=!0,options:r,onActivate:s})=>{const n=(0,l.default)(),u=(0,o.useRef)(t);u.current=t;const c=(0,o.useRef)(e);c.current=e;const a=(0,o.useRef)(r);a.current=r;const f=(0,o.useRef)(s);f.current=s;const C=()=>!(!u.current||typeof document>"u"||document.hidden||typeof document.hasFocus=="function"&&!document.hasFocus()),m=(0,o.useCallback)(i=>{if(!c.current||(i.aiRole??"").toUpperCase()==="USER"||C())return;const d=a.current?.renderBody?.(i)??v(i.renderedContent);d&&n.showInAppChatNotification?.({title:a.current?.title??"New message",body:d,icon:a.current?.icon,tag:i.conversationId?`in_app_chat:${i.conversationId}`:void 0,onClick:()=>f.current?.(i)})},[n]),I=(0,o.useCallback)(()=>n.requestNotificationPermission?.()??Promise.resolve("denied"),[n]),A=(0,o.useCallback)(()=>{c.current&&(a.current?.requestPermissionOn??"firstOpen")==="firstOpen"&&n.requestNotificationPermission?.().catch(()=>{})},[n]);return{handleInboundMessage:m,requestPermission:I,requestPermissionOnOpen:A}};var D=x;
|
|
2
2
|
//# sourceMappingURL=use-in-app-chat-notifications.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/use-in-app-chat-notifications.ts"],
|
|
4
|
-
"sourcesContent": ["import { useCallback, useRef } from 'react';\nimport type { InAppChatMessageData } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\n\ntype InAppChatNotificationOptions = {\n // Notification title. Defaults to 'New message'.\n title?: string;\n // Icon URL shown in the notification.\n icon?: string;\n // Custom body text from the inbound message; defaults to the message text.\n renderBody?: (message: InAppChatMessageData) => string;\n // When to request notification permission. 'firstOpen' (the default for the\n // launcher) requests it the first time the visitor opens chat \u2014 a real user\n // gesture, which browsers require for a non-penalized prompt. 'manual' never\n // auto-requests (use for always-visible surfaces, where the embedder drives\n // the prompt via the returned `requestPermission`).\n requestPermissionOn?: 'firstOpen' | 'manual';\n};\n\ntype UseInAppChatNotificationsProps = {\n // Whether the chat surface is currently open/visible. Combined with tab\n // visibility + window focus to decide if the visitor is actively looking at\n // chat (in which case we don't notify).\n isOpen: boolean;\n // `false` disables notifications entirely.\n enabled?: boolean;\n options?: InAppChatNotificationOptions;\n // Invoked when the visitor clicks a notification \u2014 focus is handled by the\n // SDK; this opens the chat panel.\n onActivate?: () => void;\n};\n\nconst textFromContent = (content: any): string => {\n if (typeof content === 'string') return content;\n return content?.text ?? content?.body ?? '';\n};\n\n/**\n * Foreground browser notifications for incoming In-App Chat messages. Returns a\n * `handleInboundMessage` to pass as `useInAppChat`'s `onInboundMessage`; it\n * fires a notification only for agent/AI replies that arrive while the chat\n * isn't focused (panel closed, or tab hidden/unfocused) and only once the\n * visitor has granted permission.\n */\nconst useInAppChatNotifications = ({\n isOpen,\n enabled = true,\n options,\n onActivate,\n}: UseInAppChatNotificationsProps) => {\n const dashX = useDashXProvider();\n\n // Latest values for the message handler, so it stays referentially stable\n // (it's wired into the chat hook's subscription).\n const isOpenRef = useRef(isOpen);\n isOpenRef.current = isOpen;\n const enabledRef = useRef(enabled);\n enabledRef.current = enabled;\n const optionsRef = useRef(options);\n optionsRef.current = options;\n const onActivateRef = useRef(onActivate);\n onActivateRef.current = onActivate;\n\n const isChatFocused = (): boolean => {\n if (!isOpenRef.current) return false;\n if (typeof document === 'undefined') return false;\n if (document.hidden) return false;\n if (typeof document.hasFocus === 'function' && !document.hasFocus()) return false;\n return true;\n };\n\n const handleInboundMessage = useCallback(\n (message: InAppChatMessageData) => {\n if (!enabledRef.current) return;\n // Agent/AI replies only \u2014 never the visitor's own echoed message.\n if ((message.aiRole ?? '').toUpperCase() === 'USER') return;\n // Don't interrupt a visitor who's actively looking at the chat.\n if (isChatFocused()) return;\n\n const body = optionsRef.current?.renderBody?.(message) ?? textFromContent(message.renderedContent);\n if (!body) return;\n\n dashX.showInAppChatNotification?.({\n title: optionsRef.current?.title ?? 'New message',\n body,\n icon: optionsRef.current?.icon,\n tag: message.conversationId ? `in_app_chat:${message.conversationId}` : undefined,\n onClick: () => onActivateRef.current?.(),\n });\n },\n [dashX],\n );\n\n const requestPermission = useCallback(\n () => dashX.requestNotificationPermission?.() ?? Promise.resolve('denied' as const),\n [dashX],\n );\n\n // Call this SYNCHRONOUSLY from the surface's open event (a real user gesture)\n // so the permission prompt lands inside the browser's transient activation\n // window. Requesting from an effect after the click has unwound is\n // ignored/rejected by some browsers. Honors `requestPermissionOn` ('manual'\n // skips it), and is safe to call on every open (no-ops once permission is\n // decided \u2014 see Client.requestNotificationPermission).\n const requestPermissionOnOpen = useCallback(() => {\n if (!enabledRef.current) return;\n if ((optionsRef.current?.requestPermissionOn ?? 'firstOpen') !== 'firstOpen') return;\n dashX.requestNotificationPermission?.().catch(() => {});\n }, [dashX]);\n\n return { handleInboundMessage, requestPermission, requestPermissionOnOpen };\n};\n\nexport type { InAppChatNotificationOptions, UseInAppChatNotificationsProps };\n\nexport default useInAppChatNotifications;\n"],
|
|
5
|
-
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAoC,iBAGpCC,EAA6B,
|
|
4
|
+
"sourcesContent": ["import { useCallback, useRef } from 'react';\nimport type { InAppChatMessageData } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\n\ntype InAppChatNotificationOptions = {\n // Notification title. Defaults to 'New message'.\n title?: string;\n // Icon URL shown in the notification.\n icon?: string;\n // Custom body text from the inbound message; defaults to the message text.\n renderBody?: (message: InAppChatMessageData) => string;\n // When to request notification permission. 'firstOpen' (the default for the\n // launcher) requests it the first time the visitor opens chat \u2014 a real user\n // gesture, which browsers require for a non-penalized prompt. 'manual' never\n // auto-requests (use for always-visible surfaces, where the embedder drives\n // the prompt via the returned `requestPermission`).\n requestPermissionOn?: 'firstOpen' | 'manual';\n};\n\ntype UseInAppChatNotificationsProps = {\n // Whether the chat surface is currently open/visible. Combined with tab\n // visibility + window focus to decide if the visitor is actively looking at\n // chat (in which case we don't notify).\n isOpen: boolean;\n // `false` disables notifications entirely.\n enabled?: boolean;\n options?: InAppChatNotificationOptions;\n // Invoked when the visitor clicks a notification \u2014 focus is handled by the\n // SDK; this opens the chat panel. Receives the message that raised THAT\n // notification, so a surface watching several conversations can open the right\n // one: without it, every notification resolves to whichever arrived last.\n onActivate?: (message: InAppChatMessageData) => void;\n};\n\nconst textFromContent = (content: any): string => {\n if (typeof content === 'string') return content;\n return content?.text ?? content?.body ?? '';\n};\n\n/**\n * Foreground browser notifications for incoming In-App Chat messages. Returns a\n * `handleInboundMessage` to pass as `useInAppChat`'s `onInboundMessage`; it\n * fires a notification only for agent/AI replies that arrive while the chat\n * isn't focused (panel closed, or tab hidden/unfocused) and only once the\n * visitor has granted permission.\n */\nconst useInAppChatNotifications = ({\n isOpen,\n enabled = true,\n options,\n onActivate,\n}: UseInAppChatNotificationsProps) => {\n const dashX = useDashXProvider();\n\n // Latest values for the message handler, so it stays referentially stable\n // (it's wired into the chat hook's subscription).\n const isOpenRef = useRef(isOpen);\n isOpenRef.current = isOpen;\n const enabledRef = useRef(enabled);\n enabledRef.current = enabled;\n const optionsRef = useRef(options);\n optionsRef.current = options;\n const onActivateRef = useRef(onActivate);\n onActivateRef.current = onActivate;\n\n const isChatFocused = (): boolean => {\n if (!isOpenRef.current) return false;\n if (typeof document === 'undefined') return false;\n if (document.hidden) return false;\n if (typeof document.hasFocus === 'function' && !document.hasFocus()) return false;\n return true;\n };\n\n const handleInboundMessage = useCallback(\n (message: InAppChatMessageData) => {\n if (!enabledRef.current) return;\n // Agent/AI replies only \u2014 never the visitor's own echoed message.\n if ((message.aiRole ?? '').toUpperCase() === 'USER') return;\n // Don't interrupt a visitor who's actively looking at the chat.\n if (isChatFocused()) return;\n\n const body = optionsRef.current?.renderBody?.(message) ?? textFromContent(message.renderedContent);\n if (!body) return;\n\n dashX.showInAppChatNotification?.({\n title: optionsRef.current?.title ?? 'New message',\n body,\n icon: optionsRef.current?.icon,\n tag: message.conversationId ? `in_app_chat:${message.conversationId}` : undefined,\n onClick: () => onActivateRef.current?.(message),\n });\n },\n [dashX],\n );\n\n const requestPermission = useCallback(\n () => dashX.requestNotificationPermission?.() ?? Promise.resolve('denied' as const),\n [dashX],\n );\n\n // Call this SYNCHRONOUSLY from the surface's open event (a real user gesture)\n // so the permission prompt lands inside the browser's transient activation\n // window. Requesting from an effect after the click has unwound is\n // ignored/rejected by some browsers. Honors `requestPermissionOn` ('manual'\n // skips it), and is safe to call on every open (no-ops once permission is\n // decided \u2014 see Client.requestNotificationPermission).\n const requestPermissionOnOpen = useCallback(() => {\n if (!enabledRef.current) return;\n if ((optionsRef.current?.requestPermissionOn ?? 'firstOpen') !== 'firstOpen') return;\n dashX.requestNotificationPermission?.().catch(() => {});\n }, [dashX]);\n\n return { handleInboundMessage, requestPermission, requestPermissionOnOpen };\n};\n\nexport type { InAppChatNotificationOptions, UseInAppChatNotificationsProps };\n\nexport default useInAppChatNotifications;\n"],
|
|
5
|
+
"mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAoC,iBAGpCC,EAA6B,sCAgC7B,MAAMC,EAAmBC,GACnB,OAAOA,GAAY,SAAiBA,EACjCA,GAAS,MAAQA,GAAS,MAAQ,GAUrCC,EAA4B,CAAC,CACjC,OAAAC,EACA,QAAAC,EAAU,GACV,QAAAC,EACA,WAAAC,CACF,IAAsC,CACpC,MAAMC,KAAQ,EAAAC,SAAiB,EAIzBC,KAAY,UAAON,CAAM,EAC/BM,EAAU,QAAUN,EACpB,MAAMO,KAAa,UAAON,CAAO,EACjCM,EAAW,QAAUN,EACrB,MAAMO,KAAa,UAAON,CAAO,EACjCM,EAAW,QAAUN,EACrB,MAAMO,KAAgB,UAAON,CAAU,EACvCM,EAAc,QAAUN,EAExB,MAAMO,EAAgB,IAChB,GAACJ,EAAU,SACX,OAAO,SAAa,KACpB,SAAS,QACT,OAAO,SAAS,UAAa,YAAc,CAAC,SAAS,SAAS,GAI9DK,KAAuB,eAC1BC,GAAkC,CAKjC,GAJI,CAACL,EAAW,UAEXK,EAAQ,QAAU,IAAI,YAAY,IAAM,QAEzCF,EAAc,EAAG,OAErB,MAAMG,EAAOL,EAAW,SAAS,aAAaI,CAAO,GAAKf,EAAgBe,EAAQ,eAAe,EAC5FC,GAELT,EAAM,4BAA4B,CAChC,MAAOI,EAAW,SAAS,OAAS,cACpC,KAAAK,EACA,KAAML,EAAW,SAAS,KAC1B,IAAKI,EAAQ,eAAiB,eAAeA,EAAQ,cAAc,GAAK,OACxE,QAAS,IAAMH,EAAc,UAAUG,CAAO,CAChD,CAAC,CACH,EACA,CAACR,CAAK,CACR,EAEMU,KAAoB,eACxB,IAAMV,EAAM,gCAAgC,GAAK,QAAQ,QAAQ,QAAiB,EAClF,CAACA,CAAK,CACR,EAQMW,KAA0B,eAAY,IAAM,CAC3CR,EAAW,UACXC,EAAW,SAAS,qBAAuB,eAAiB,aACjEJ,EAAM,gCAAgC,EAAE,MAAM,IAAM,CAAC,CAAC,CACxD,EAAG,CAACA,CAAK,CAAC,EAEV,MAAO,CAAE,qBAAAO,EAAsB,kBAAAG,EAAmB,wBAAAC,CAAwB,CAC5E,EAIA,IAAOtB,EAAQM",
|
|
6
6
|
"names": ["use_in_app_chat_notifications_exports", "__export", "use_in_app_chat_notifications_default", "__toCommonJS", "import_react", "import_use_dashx_provider", "textFromContent", "content", "useInAppChatNotifications", "isOpen", "enabled", "options", "onActivate", "dashX", "useDashXProvider", "isOpenRef", "enabledRef", "optionsRef", "onActivateRef", "isChatFocused", "handleInboundMessage", "message", "body", "requestPermission", "requestPermissionOnOpen"]
|
|
7
7
|
}
|
|
@@ -9,7 +9,7 @@ type UseInAppChatNotificationsProps = {
|
|
|
9
9
|
isOpen: boolean;
|
|
10
10
|
enabled?: boolean;
|
|
11
11
|
options?: InAppChatNotificationOptions;
|
|
12
|
-
onActivate?: () => void;
|
|
12
|
+
onActivate?: (message: InAppChatMessageData) => void;
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
15
|
* Foreground browser notifications for incoming In-App Chat messages. Returns a
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useCallback as s,useRef as o}from"react";import I from"./use-dashx-provider.js";const
|
|
1
|
+
import{useCallback as s,useRef as o}from"react";import I from"./use-dashx-provider.js";const A=t=>typeof t=="string"?t:t?.text??t?.body??"",y=({isOpen:t,enabled:a=!0,options:p,onActivate:c})=>{const e=I(),u=o(t);u.current=t;const i=o(a);i.current=a;const r=o(p);r.current=p;const f=o(c);f.current=c;const h=()=>!(!u.current||typeof document>"u"||document.hidden||typeof document.hasFocus=="function"&&!document.hasFocus()),l=s(n=>{if(!i.current||(n.aiRole??"").toUpperCase()==="USER"||h())return;const d=r.current?.renderBody?.(n)??A(n.renderedContent);d&&e.showInAppChatNotification?.({title:r.current?.title??"New message",body:d,icon:r.current?.icon,tag:n.conversationId?`in_app_chat:${n.conversationId}`:void 0,onClick:()=>f.current?.(n)})},[e]),C=s(()=>e.requestNotificationPermission?.()??Promise.resolve("denied"),[e]),m=s(()=>{i.current&&(r.current?.requestPermissionOn??"firstOpen")==="firstOpen"&&e.requestNotificationPermission?.().catch(()=>{})},[e]);return{handleInboundMessage:l,requestPermission:C,requestPermissionOnOpen:m}};var P=y;export{P as default};
|
|
2
2
|
//# sourceMappingURL=use-in-app-chat-notifications.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/use-in-app-chat-notifications.ts"],
|
|
4
|
-
"sourcesContent": ["import { useCallback, useRef } from 'react';\nimport type { InAppChatMessageData } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\n\ntype InAppChatNotificationOptions = {\n // Notification title. Defaults to 'New message'.\n title?: string;\n // Icon URL shown in the notification.\n icon?: string;\n // Custom body text from the inbound message; defaults to the message text.\n renderBody?: (message: InAppChatMessageData) => string;\n // When to request notification permission. 'firstOpen' (the default for the\n // launcher) requests it the first time the visitor opens chat \u2014 a real user\n // gesture, which browsers require for a non-penalized prompt. 'manual' never\n // auto-requests (use for always-visible surfaces, where the embedder drives\n // the prompt via the returned `requestPermission`).\n requestPermissionOn?: 'firstOpen' | 'manual';\n};\n\ntype UseInAppChatNotificationsProps = {\n // Whether the chat surface is currently open/visible. Combined with tab\n // visibility + window focus to decide if the visitor is actively looking at\n // chat (in which case we don't notify).\n isOpen: boolean;\n // `false` disables notifications entirely.\n enabled?: boolean;\n options?: InAppChatNotificationOptions;\n // Invoked when the visitor clicks a notification \u2014 focus is handled by the\n // SDK; this opens the chat panel.\n onActivate?: () => void;\n};\n\nconst textFromContent = (content: any): string => {\n if (typeof content === 'string') return content;\n return content?.text ?? content?.body ?? '';\n};\n\n/**\n * Foreground browser notifications for incoming In-App Chat messages. Returns a\n * `handleInboundMessage` to pass as `useInAppChat`'s `onInboundMessage`; it\n * fires a notification only for agent/AI replies that arrive while the chat\n * isn't focused (panel closed, or tab hidden/unfocused) and only once the\n * visitor has granted permission.\n */\nconst useInAppChatNotifications = ({\n isOpen,\n enabled = true,\n options,\n onActivate,\n}: UseInAppChatNotificationsProps) => {\n const dashX = useDashXProvider();\n\n // Latest values for the message handler, so it stays referentially stable\n // (it's wired into the chat hook's subscription).\n const isOpenRef = useRef(isOpen);\n isOpenRef.current = isOpen;\n const enabledRef = useRef(enabled);\n enabledRef.current = enabled;\n const optionsRef = useRef(options);\n optionsRef.current = options;\n const onActivateRef = useRef(onActivate);\n onActivateRef.current = onActivate;\n\n const isChatFocused = (): boolean => {\n if (!isOpenRef.current) return false;\n if (typeof document === 'undefined') return false;\n if (document.hidden) return false;\n if (typeof document.hasFocus === 'function' && !document.hasFocus()) return false;\n return true;\n };\n\n const handleInboundMessage = useCallback(\n (message: InAppChatMessageData) => {\n if (!enabledRef.current) return;\n // Agent/AI replies only \u2014 never the visitor's own echoed message.\n if ((message.aiRole ?? '').toUpperCase() === 'USER') return;\n // Don't interrupt a visitor who's actively looking at the chat.\n if (isChatFocused()) return;\n\n const body = optionsRef.current?.renderBody?.(message) ?? textFromContent(message.renderedContent);\n if (!body) return;\n\n dashX.showInAppChatNotification?.({\n title: optionsRef.current?.title ?? 'New message',\n body,\n icon: optionsRef.current?.icon,\n tag: message.conversationId ? `in_app_chat:${message.conversationId}` : undefined,\n onClick: () => onActivateRef.current?.(),\n });\n },\n [dashX],\n );\n\n const requestPermission = useCallback(\n () => dashX.requestNotificationPermission?.() ?? Promise.resolve('denied' as const),\n [dashX],\n );\n\n // Call this SYNCHRONOUSLY from the surface's open event (a real user gesture)\n // so the permission prompt lands inside the browser's transient activation\n // window. Requesting from an effect after the click has unwound is\n // ignored/rejected by some browsers. Honors `requestPermissionOn` ('manual'\n // skips it), and is safe to call on every open (no-ops once permission is\n // decided \u2014 see Client.requestNotificationPermission).\n const requestPermissionOnOpen = useCallback(() => {\n if (!enabledRef.current) return;\n if ((optionsRef.current?.requestPermissionOn ?? 'firstOpen') !== 'firstOpen') return;\n dashX.requestNotificationPermission?.().catch(() => {});\n }, [dashX]);\n\n return { handleInboundMessage, requestPermission, requestPermissionOnOpen };\n};\n\nexport type { InAppChatNotificationOptions, UseInAppChatNotificationsProps };\n\nexport default useInAppChatNotifications;\n"],
|
|
5
|
-
"mappings": "AAAA,OAAS,eAAAA,EAAa,UAAAC,MAAc,QAGpC,OAAOC,MAAsB,
|
|
4
|
+
"sourcesContent": ["import { useCallback, useRef } from 'react';\nimport type { InAppChatMessageData } from '@dashx/browser';\n\nimport useDashXProvider from './use-dashx-provider.js';\n\ntype InAppChatNotificationOptions = {\n // Notification title. Defaults to 'New message'.\n title?: string;\n // Icon URL shown in the notification.\n icon?: string;\n // Custom body text from the inbound message; defaults to the message text.\n renderBody?: (message: InAppChatMessageData) => string;\n // When to request notification permission. 'firstOpen' (the default for the\n // launcher) requests it the first time the visitor opens chat \u2014 a real user\n // gesture, which browsers require for a non-penalized prompt. 'manual' never\n // auto-requests (use for always-visible surfaces, where the embedder drives\n // the prompt via the returned `requestPermission`).\n requestPermissionOn?: 'firstOpen' | 'manual';\n};\n\ntype UseInAppChatNotificationsProps = {\n // Whether the chat surface is currently open/visible. Combined with tab\n // visibility + window focus to decide if the visitor is actively looking at\n // chat (in which case we don't notify).\n isOpen: boolean;\n // `false` disables notifications entirely.\n enabled?: boolean;\n options?: InAppChatNotificationOptions;\n // Invoked when the visitor clicks a notification \u2014 focus is handled by the\n // SDK; this opens the chat panel. Receives the message that raised THAT\n // notification, so a surface watching several conversations can open the right\n // one: without it, every notification resolves to whichever arrived last.\n onActivate?: (message: InAppChatMessageData) => void;\n};\n\nconst textFromContent = (content: any): string => {\n if (typeof content === 'string') return content;\n return content?.text ?? content?.body ?? '';\n};\n\n/**\n * Foreground browser notifications for incoming In-App Chat messages. Returns a\n * `handleInboundMessage` to pass as `useInAppChat`'s `onInboundMessage`; it\n * fires a notification only for agent/AI replies that arrive while the chat\n * isn't focused (panel closed, or tab hidden/unfocused) and only once the\n * visitor has granted permission.\n */\nconst useInAppChatNotifications = ({\n isOpen,\n enabled = true,\n options,\n onActivate,\n}: UseInAppChatNotificationsProps) => {\n const dashX = useDashXProvider();\n\n // Latest values for the message handler, so it stays referentially stable\n // (it's wired into the chat hook's subscription).\n const isOpenRef = useRef(isOpen);\n isOpenRef.current = isOpen;\n const enabledRef = useRef(enabled);\n enabledRef.current = enabled;\n const optionsRef = useRef(options);\n optionsRef.current = options;\n const onActivateRef = useRef(onActivate);\n onActivateRef.current = onActivate;\n\n const isChatFocused = (): boolean => {\n if (!isOpenRef.current) return false;\n if (typeof document === 'undefined') return false;\n if (document.hidden) return false;\n if (typeof document.hasFocus === 'function' && !document.hasFocus()) return false;\n return true;\n };\n\n const handleInboundMessage = useCallback(\n (message: InAppChatMessageData) => {\n if (!enabledRef.current) return;\n // Agent/AI replies only \u2014 never the visitor's own echoed message.\n if ((message.aiRole ?? '').toUpperCase() === 'USER') return;\n // Don't interrupt a visitor who's actively looking at the chat.\n if (isChatFocused()) return;\n\n const body = optionsRef.current?.renderBody?.(message) ?? textFromContent(message.renderedContent);\n if (!body) return;\n\n dashX.showInAppChatNotification?.({\n title: optionsRef.current?.title ?? 'New message',\n body,\n icon: optionsRef.current?.icon,\n tag: message.conversationId ? `in_app_chat:${message.conversationId}` : undefined,\n onClick: () => onActivateRef.current?.(message),\n });\n },\n [dashX],\n );\n\n const requestPermission = useCallback(\n () => dashX.requestNotificationPermission?.() ?? Promise.resolve('denied' as const),\n [dashX],\n );\n\n // Call this SYNCHRONOUSLY from the surface's open event (a real user gesture)\n // so the permission prompt lands inside the browser's transient activation\n // window. Requesting from an effect after the click has unwound is\n // ignored/rejected by some browsers. Honors `requestPermissionOn` ('manual'\n // skips it), and is safe to call on every open (no-ops once permission is\n // decided \u2014 see Client.requestNotificationPermission).\n const requestPermissionOnOpen = useCallback(() => {\n if (!enabledRef.current) return;\n if ((optionsRef.current?.requestPermissionOn ?? 'firstOpen') !== 'firstOpen') return;\n dashX.requestNotificationPermission?.().catch(() => {});\n }, [dashX]);\n\n return { handleInboundMessage, requestPermission, requestPermissionOnOpen };\n};\n\nexport type { InAppChatNotificationOptions, UseInAppChatNotificationsProps };\n\nexport default useInAppChatNotifications;\n"],
|
|
5
|
+
"mappings": "AAAA,OAAS,eAAAA,EAAa,UAAAC,MAAc,QAGpC,OAAOC,MAAsB,0BAgC7B,MAAMC,EAAmBC,GACnB,OAAOA,GAAY,SAAiBA,EACjCA,GAAS,MAAQA,GAAS,MAAQ,GAUrCC,EAA4B,CAAC,CACjC,OAAAC,EACA,QAAAC,EAAU,GACV,QAAAC,EACA,WAAAC,CACF,IAAsC,CACpC,MAAMC,EAAQR,EAAiB,EAIzBS,EAAYV,EAAOK,CAAM,EAC/BK,EAAU,QAAUL,EACpB,MAAMM,EAAaX,EAAOM,CAAO,EACjCK,EAAW,QAAUL,EACrB,MAAMM,EAAaZ,EAAOO,CAAO,EACjCK,EAAW,QAAUL,EACrB,MAAMM,EAAgBb,EAAOQ,CAAU,EACvCK,EAAc,QAAUL,EAExB,MAAMM,EAAgB,IAChB,GAACJ,EAAU,SACX,OAAO,SAAa,KACpB,SAAS,QACT,OAAO,SAAS,UAAa,YAAc,CAAC,SAAS,SAAS,GAI9DK,EAAuBhB,EAC1BiB,GAAkC,CAKjC,GAJI,CAACL,EAAW,UAEXK,EAAQ,QAAU,IAAI,YAAY,IAAM,QAEzCF,EAAc,EAAG,OAErB,MAAMG,EAAOL,EAAW,SAAS,aAAaI,CAAO,GAAKd,EAAgBc,EAAQ,eAAe,EAC5FC,GAELR,EAAM,4BAA4B,CAChC,MAAOG,EAAW,SAAS,OAAS,cACpC,KAAAK,EACA,KAAML,EAAW,SAAS,KAC1B,IAAKI,EAAQ,eAAiB,eAAeA,EAAQ,cAAc,GAAK,OACxE,QAAS,IAAMH,EAAc,UAAUG,CAAO,CAChD,CAAC,CACH,EACA,CAACP,CAAK,CACR,EAEMS,EAAoBnB,EACxB,IAAMU,EAAM,gCAAgC,GAAK,QAAQ,QAAQ,QAAiB,EAClF,CAACA,CAAK,CACR,EAQMU,EAA0BpB,EAAY,IAAM,CAC3CY,EAAW,UACXC,EAAW,SAAS,qBAAuB,eAAiB,aACjEH,EAAM,gCAAgC,EAAE,MAAM,IAAM,CAAC,CAAC,CACxD,EAAG,CAACA,CAAK,CAAC,EAEV,MAAO,CAAE,qBAAAM,EAAsB,kBAAAG,EAAmB,wBAAAC,CAAwB,CAC5E,EAIA,IAAOC,EAAQhB",
|
|
6
6
|
"names": ["useCallback", "useRef", "useDashXProvider", "textFromContent", "content", "useInAppChatNotifications", "isOpen", "enabled", "options", "onActivate", "dashX", "isOpenRef", "enabledRef", "optionsRef", "onActivateRef", "isChatFocused", "handleInboundMessage", "message", "body", "requestPermission", "requestPermissionOnOpen", "use_in_app_chat_notifications_default"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -27,8 +27,10 @@ type UseInAppChatNotificationsProps = {
|
|
|
27
27
|
enabled?: boolean;
|
|
28
28
|
options?: InAppChatNotificationOptions;
|
|
29
29
|
// Invoked when the visitor clicks a notification — focus is handled by the
|
|
30
|
-
// SDK; this opens the chat panel.
|
|
31
|
-
|
|
30
|
+
// SDK; this opens the chat panel. Receives the message that raised THAT
|
|
31
|
+
// notification, so a surface watching several conversations can open the right
|
|
32
|
+
// one: without it, every notification resolves to whichever arrived last.
|
|
33
|
+
onActivate?: (message: InAppChatMessageData) => void;
|
|
32
34
|
};
|
|
33
35
|
|
|
34
36
|
const textFromContent = (content: any): string => {
|
|
@@ -86,7 +88,7 @@ const useInAppChatNotifications = ({
|
|
|
86
88
|
body,
|
|
87
89
|
icon: optionsRef.current?.icon,
|
|
88
90
|
tag: message.conversationId ? `in_app_chat:${message.conversationId}` : undefined,
|
|
89
|
-
onClick: () => onActivateRef.current?.(),
|
|
91
|
+
onClick: () => onActivateRef.current?.(message),
|
|
90
92
|
});
|
|
91
93
|
},
|
|
92
94
|
[dashX],
|