@dashx/react 0.3.0 → 0.3.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/dist/cjs/hooks/index.d.ts +2 -0
- package/dist/cjs/hooks/index.js +1 -1
- package/dist/cjs/hooks/index.js.map +3 -3
- package/dist/cjs/hooks/use-in-app-chat-notifications.d.ts +27 -0
- package/dist/cjs/hooks/use-in-app-chat-notifications.js +2 -0
- package/dist/cjs/hooks/use-in-app-chat-notifications.js.map +7 -0
- package/dist/cjs/hooks/use-in-app-chat.d.ts +4 -1
- package/dist/cjs/hooks/use-in-app-chat.js +1 -1
- package/dist/cjs/hooks/use-in-app-chat.js.map +3 -3
- package/dist/cjs/widgets/InAppChat.d.ts +5 -2
- package/dist/cjs/widgets/InAppChat.js +1 -1
- package/dist/cjs/widgets/InAppChat.js.map +3 -3
- package/dist/cjs/widgets/InAppChatButton.d.ts +1 -1
- package/dist/cjs/widgets/InAppChatButton.js +1 -1
- package/dist/cjs/widgets/InAppChatButton.js.map +3 -3
- package/dist/esm/hooks/index.d.ts +2 -0
- package/dist/esm/hooks/index.js +1 -1
- package/dist/esm/hooks/index.js.map +2 -2
- package/dist/esm/hooks/use-in-app-chat-notifications.d.ts +27 -0
- package/dist/esm/hooks/use-in-app-chat-notifications.js +2 -0
- package/dist/esm/hooks/use-in-app-chat-notifications.js.map +7 -0
- package/dist/esm/hooks/use-in-app-chat.d.ts +4 -1
- package/dist/esm/hooks/use-in-app-chat.js +1 -1
- package/dist/esm/hooks/use-in-app-chat.js.map +3 -3
- package/dist/esm/widgets/InAppChat.d.ts +5 -2
- package/dist/esm/widgets/InAppChat.js +1 -1
- package/dist/esm/widgets/InAppChat.js.map +3 -3
- package/dist/esm/widgets/InAppChatButton.d.ts +1 -1
- package/dist/esm/widgets/InAppChatButton.js +1 -1
- package/dist/esm/widgets/InAppChatButton.js.map +3 -3
- package/package.json +2 -2
- package/src/hooks/index.ts +2 -0
- package/src/hooks/use-in-app-chat-notifications.ts +117 -0
- package/src/hooks/use-in-app-chat.ts +31 -4
- package/src/widgets/InAppChat.tsx +33 -5
- package/src/widgets/InAppChatButton.tsx +72 -28
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useCallback, useRef, useState } from 'react';
|
|
2
2
|
import { MessageCircle } from 'lucide-react';
|
|
3
3
|
|
|
4
4
|
import { Button, Flex, Popover, Theme, type ThemeProps } from '../components';
|
|
5
|
-
import InAppChat,
|
|
5
|
+
import { InAppChat, type InAppChatWrapperProps } from './InAppChat.js';
|
|
6
|
+
import useInAppChat from '../hooks/use-in-app-chat.js';
|
|
7
|
+
import useInAppChatNotifications from '../hooks/use-in-app-chat-notifications.js';
|
|
6
8
|
|
|
7
9
|
type LauncherPosition = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
|
|
8
10
|
|
|
@@ -24,35 +26,77 @@ const POSITION_CLASS: Record<LauncherPosition, string> = {
|
|
|
24
26
|
const InAppChatButton = ({
|
|
25
27
|
identityId,
|
|
26
28
|
idempotencyKey,
|
|
29
|
+
initialMessage,
|
|
30
|
+
notifications = true,
|
|
27
31
|
position = 'bottom-right',
|
|
28
32
|
theme,
|
|
29
33
|
...props
|
|
30
|
-
}: InAppChatButtonProps) =>
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
);
|
|
34
|
+
}: InAppChatButtonProps) => {
|
|
35
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
36
|
+
|
|
37
|
+
// Latch: once the launcher has been opened, keep the chat hook active so its
|
|
38
|
+
// channel subscription persists while the panel is closed — that's what lets
|
|
39
|
+
// us notify on replies that land while the panel is shut. Before the first
|
|
40
|
+
// open the hook is idle, so no conversation is created on page load.
|
|
41
|
+
const hasOpenedRef = useRef(false);
|
|
42
|
+
if (isOpen) hasOpenedRef.current = true;
|
|
43
|
+
const enabled = hasOpenedRef.current;
|
|
44
|
+
|
|
45
|
+
const open = useCallback(() => setIsOpen(true), []);
|
|
46
|
+
|
|
47
|
+
const { handleInboundMessage, requestPermissionOnOpen } = useInAppChatNotifications({
|
|
48
|
+
isOpen,
|
|
49
|
+
enabled: notifications !== false,
|
|
50
|
+
options: typeof notifications === 'object' ? notifications : undefined,
|
|
51
|
+
onActivate: open,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Request notification permission inside the open gesture (synchronously), so
|
|
55
|
+
// the prompt runs within the browser's transient user activation — requesting
|
|
56
|
+
// from an effect after the click unwinds is ignored on some browsers.
|
|
57
|
+
const handleOpenChange = useCallback(
|
|
58
|
+
(nextOpen: boolean) => {
|
|
59
|
+
if (nextOpen) requestPermissionOnOpen();
|
|
60
|
+
setIsOpen(nextOpen);
|
|
61
|
+
},
|
|
62
|
+
[requestPermissionOnOpen],
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const chat = useInAppChat({
|
|
66
|
+
identityId,
|
|
67
|
+
idempotencyKey,
|
|
68
|
+
initialMessage,
|
|
69
|
+
enabled,
|
|
70
|
+
onInboundMessage: handleInboundMessage,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<Theme {...theme}>
|
|
75
|
+
<Flex className={`fixed z-50 ${POSITION_CLASS[position]}`}>
|
|
76
|
+
<Popover.Root open={isOpen} onOpenChange={handleOpenChange}>
|
|
77
|
+
<Popover.Trigger>
|
|
78
|
+
<Button shape="square" roundness="full" variant="fill" size="large">
|
|
79
|
+
<MessageCircle />
|
|
80
|
+
</Button>
|
|
81
|
+
</Popover.Trigger>
|
|
82
|
+
{/* The chat hook lives at the launcher level (above the popover) so its
|
|
83
|
+
subscription survives the panel closing — required to notify on
|
|
84
|
+
replies that arrive while the panel is shut. It activates lazily on
|
|
85
|
+
first open, so no empty conversation is created on page load. */}
|
|
86
|
+
<Popover.Content spacing="large" width="350px" height="450px">
|
|
87
|
+
<InAppChat
|
|
88
|
+
{...chat}
|
|
89
|
+
borderless
|
|
90
|
+
withChatHeader
|
|
91
|
+
withPopoverClose
|
|
92
|
+
{...props}
|
|
93
|
+
/>
|
|
94
|
+
</Popover.Content>
|
|
95
|
+
</Popover.Root>
|
|
96
|
+
</Flex>
|
|
97
|
+
</Theme>
|
|
98
|
+
);
|
|
99
|
+
};
|
|
56
100
|
|
|
57
101
|
export type { InAppChatButtonProps };
|
|
58
102
|
|